-
Notifications
You must be signed in to change notification settings - Fork 7
F. Models
In this section, you will learn how to create and configure an OnixJS Model.
Models represent entities within your system, these will be instantiated by the framework once these are injected for the very first time, further injections will inject a singleton instance.
If those models are never injected, the framework would never create an instance, therefore the assigned data sources won't be ever instantiated, executed, nor connected.
import { Model, IModel, Property } from '@onixjs/core';
import { MongooseDatasource } from './mongoose.datasource';
@Model({ datasource: MongooseDatasource })
export class MyModel implements IModel {
// User Email
@Property({
type: String,
required: true,
unique: true,
dropDups: true
}) text: String;
}
The first thing you might notice already is that by using the @Model
decorator you are able to install any data source you already created and by the moment this model is instantiated the data source will be also be instantiated and connected.
Remember that a model is a Singleton Injectable which will be instantiated the very first time this model is injected.
The @Property
decorator will allow you to pass any configuration that the ORM accepts for your a model, in this case mongoose allows the following configurations:
{
"type": String,
"required": true,
"unique": true,
"dropDups": true
}
But please always consider that configuration is not related to OnixJS but to the ORM configurations, at the end if you remember your data source configuration will provide a schema in order to configure some ORMS.
The configuration schema defined above will end in something like:
{
"text": {
"type": String,
"required": true,
"unique": true,
"dropDups": true
}
}