Model

This guide introduces the concept of model used in Teo server implementation. To understand how to define a model to suit your business requirements, read our guide article Data modeling. To get a list of model APIs, check our API documentation.

A model is mapped into a table or collection in the underlying database, with each of its fields mapped to a table column. This is similar to the ORM concepts. Besides, a model has a list of auto handlers which is equivalent to HTTP endpoints in traditional HTTP frameworks. Developers can declare custom request handlers under the model.

Declare a model

A model is declared in Teo schema like this:

/// @name Platform User
///
/// Users on our platform.
model User {
  @id
  id: Int
  @unique
  email: String
  name: String
  @default(18)
  age: Int
}

Model content

A model has a name, optionally a documented readable name, optionally a documented description, a list of fields, optionally a list of custom handlers, optionally a list of model level decorators, a primary index, and optionally a list of indexes and unique indexes.

Fields

Model fields fall into 3 main categories:

Primary index

A model must have one primary index. It can be a on single scalar field, or a compound index of some scalar fields.

A single field primary index

An @id decorator can be placed on a scalar number or string field.

model User {
  @id
  id: Int
  @unique
  email: String
}

A compound primary index

Place the @id decorator on the model level to create a compound primary index.

@id([.id, .email])
model User {
  id: Int
  email: String
}

Handlers

Handlers are custom HTTP API endpoints. Except auto handlers synthesized by Teo for models to perform basic CRUD and aggregation, developers must provide the implementation with the server API.

Auto-synthesized handlers

Teo synthesizes common CRUD and aggregate handlers for models. See Builtin auto-synthesized handlers for a list of auto-synthesized handlers.

Custom handlers

A custom handler can be placed within the model block.

model User {
  @id
  id: Int
  name: String
 
  declare handler myCustomCreate(input: MyCustomInput): MyCustomOutput
}

See handler CONCEPT to learn more about handlers.