createModel
The createModel function creates a new Entity model (a content type) with its list of fields. It is the JavaScript/TypeScript counterpart of the POST /model/entity REST endpoint.
Example:
// Using the previously created clientconst { id } = await client.createModel({ name: 'Article', fieldList: [ { name: 'title', type: 'short-text', validations: [{ name: 'required' }, { name: 'max-length', customArgs: { length: 120 } }] }, { name: 'body', type: 'long-text' }, { name: 'tags', type: 'short-text', isArray: true }, { name: 'writtenBy', type: 'relation', relatedModelId: authorId }, { name: 'size', type: 'enum', relatedModelId: sizeEnumId }, ],});console.log(id);Interface
export interface ApiClient { createModel: (params: CreateEntityParams) => Promise<SaveModelResponse>; // ... other methods}
export interface CreateEntityParams { name: string; fieldList: FieldSpec[];}
export interface FieldSpec { id?: string; // omit when creating — the server assigns one name: string; type: FieldType; // 'short-text' | 'long-text' | 'number' | 'date' | 'date-time' // | 'media' | 'boolean' | 'color' | 'relation' | 'enum' relatedModelId?: string; // required for 'relation' / 'enum' isArray?: boolean; // true → the field stores a list (default false) validations?: Validation[];}
export interface Validation { name: string; // 'required' | 'unique' | 'min-length' | 'max-length' | 'media-type' customArgs?: any;}
export interface SaveModelResponse { id: string;}Parameters
| Name | Description |
|---|---|
name | The model name. Must satisfy the name rules and be unique across the project’s models. |
fieldList | The fields of the entity, in order. At least one is required. See the field types and validations reference. |
Output
The function returns a Promise<SaveModelResponse> resolving to an object with the id of the new model. Use it as the
relatedModelId of a relation field on another model, or as the contentType when creating content of this type with
createContent.
On failure it throws a typed ApiClientError (e.g. VALIDATION_ERROR, NOT_FOUND).