Models
As Strapi is a headless Content Management System (CMS), creating a data structure for the content is one of the most important aspects of using the software. Models define a representation of the data structure.
There are 2 different types of models in Strapi:
- content-types, which can be collection types or single types, depending on how many entries they manage,
- and components that are data structures re-usable in multiple content-types.
If you are just starting out, it is convenient to generate some models with the Content-type Builder directly in the admin panel. The user interface takes over a lot of validation tasks and showcases all the options available to create the content's data structure. The generated model mappings can then be reviewed at the code level using this documentation.
Model creationβ
Content-types and components models are created and stored differently.
Content-typesβ
Content-types in Strapi can be created:
- with the Content-type Builder in the admin panel,
- or with Strapi's interactive CLI
strapi generate
command.
The content-types use the following files:
schema.json
for the model's schema definition. (generated automatically, when creating content-type with either method)lifecycles.js
for lifecycle hooks. This file must be created manually.
These models files are stored in ./src/api/[api-name]/content-types/[content-type-name]/
, and any JavaScript or JSON file found in these folders will be loaded as a content-type's model (see project structure).
In TypeScript-enabled projects, schema typings can be generated using the ts:generate-types
command (e.g., npm run strapi ts:generate-types
or yarn strapi ts:generate-types
).
Componentsβ
Component models can't be created with CLI tools. Use the Content-type Builder or create them manually.
Components models are stored in the ./src/components
folder. Every component has to be inside a subfolder, named after the category the component belongs to (see project structure).
Model schemaβ
The schema.json
file of a model consists of:
- settings, such as the kind of content-type the model represents or the table name in which the data should be stored,
- information, mostly used to display the model in the admin panel and access it through the REST and GraphQL APIs,
- attributes, which describe the data structure of the model,
- and options used to defined specific behaviors on the model.
Model settingsβ
General settings for the model can be configured with the following parameters:
Parameter | Type | Description |
---|---|---|
collectionName | String | Database table name in which the data should be stored |
kind Optional,only for content-types | String | Defines if the content-type is:
|
// ./src/api/[api-name]/content-types/restaurant/schema.json
{
"kind": "collectionType",
"collectionName": "Restaurants_v1",
}
Model informationβ
The info
key in the model's schema describes information used to display the model in the admin panel and access it through the Content API. It includes the following parameters:
Parameter | Type | Description |
---|---|---|
displayName | String | Default name to use in the admin panel |
singularName | String | Singular form of the content-type name.Used to generate the API routes and databases/tables collection.Should be kebab-case. |
pluralName | String | Plural form of the content-type name.Used to generate the API routes and databases/tables collection.Should be kebab-case. |
description | String | Description of the model |
"info": {
"displayName": "Restaurant",
"singularName": "restaurant",
"pluralName": "restaurants",
"description": ""
},