This guide introduces the best practices on data modeling.
Design model ID
Teo has a flexible schema language for modeling data. So does defining model ID.
Teo supports integer ID, string ID, object id
MongoDB Only, and compound ID. To specify a model's ID, use
To declare an integer ID, specify the field's type to be
In model designing with SQL databases, auto incremental IDs are common. Add an
@autoIncrement for a database generated default value. By the
@readonly could be added if this field is designed not to change
Teo supports string ID. While these IDs could be provided by the frontend, or randomly generated by Teo. When the value is supposed to be generated by the frontend, do not specify a default value.
To declare a UUID ID, use the default decorator with the
$uuid pipeline item:
To declare a CUID ID, use the default decorator with the
$cuid pipeline item:
To declare a slug ID, use the default decorator with the
$slug pipeline item:
Custom string ID strategy
To use a custom random string ID strategy, custom pipeline item is required.
Object ID is mapped to MongoDB's
ObjectIdtype. Thus this is a MongoDB only feature.
An ObjectId is generated by MongoDB internally. And its "column name" is always
Normally declare it with these four decorators:
A model's primary key could be a combination of various fields. Move the decorator up to the model, multiple field name arguments are accepted.
Design model fields
A model represents a table or collection in the underlying database. Teo models are shaped models which means developer need to define fields on a model with field types and other information related to a field. To understand the concepts, see Model, Model field and Model property.
Field or property
A model field represents a normal "table column". While a property with a setter is a shortcut for setting values of fields. A property with a getter represents calculated result.
Use virtual field if some argument is required for input but it doesn't need to be stored in the database.
A cached property is stored in the database. This is useful for properties which involves heavy calculating.
Design model relations
Teo is great for working with model relations. Define model relations with
decorator and developer can get nested mutation and query for free.
Relations without join table
The difference between one-to-one relation and one-to-many relation is whether the field type is array or not.
This example represents a one-to-one-relation:
This example represents a one-to-many or many-to-one relation:
Relations with join table
There isn't an easy and unified way to implement a many-to-many relation without a join table. Teo prefers and supports declaring join table explicitly.