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.
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": ""
},
Model attributesβ
The data structure of a model consists of a list of attributes. Each attribute has a type
parameter, which describes its nature and defines the attribute as a simple piece of data or a more complex structure used by Strapi.
Many types of attributes are available:
- scalar types (e.g. strings, dates, numbers, booleans, etc.),
- Strapi-specific types, such as:
media
for files uploaded through the Media libraryrelation
to describe a relation between content-typescustomField
to describe custom fields and their specific keyscomponent
to define a component (i.e. a data structure usable in multiple content-types)dynamiczone
to define a dynamic zone (i.e. a flexible space based on a list of components)- and the
locale
andlocalizations
types, only used by the Internationalization (i18n) plugin
The type
parameter of an attribute should be one of the following values:
Type categories | Available types |
---|---|
String types |
|
Date types |
|
Number types |
|
Other generic types |
|
Special types unique to Strapi | |
Internationalization (i18n)-related types Can only be used if the i18n is enabled on the content-type |
|
Validationsβ
Basic validations can be applied to attributes using the following parameters:
Parameter | Type | Description | Default |
---|---|---|---|
required | Boolean | If true , adds a required validator for this property | false |
max | Integer | Checks if the value is greater than or equal to the given maximum | - |
min | Integer | Checks if the value is less than or equal to the given minimum | - |
minLength | Integer | Minimum number of characters for a field input value | - |
maxLength | Integer | Maximum number of characters for a field input value | - |
private | Boolean | If true , the attribute will be removed from the server response.π‘ This is useful to hide sensitive data. | false |
configurable | Boolean | If false , the attribute isn't configurable from the Content-type Builder plugin. | true |
{
// ...
"attributes": {
"title": {
"type": "string",
"minLength": 3,
"maxLength": 99,
"unique": true
},
"description": {
"default": "My description",
"type": "text",
"required": true
},
"slug": {
"type": "uid",
"targetField": "title"
}
// ...
}
}