Models
Models are synonymous to database tables. They provide the core functionality
for setting, getting, validating, casting, saving, updating and deleting data.
All models inherit the base Model
class.
Model config
Models are configured through these static properties:
Property | Type | Default | Description |
---|---|---|---|
Model.table | string (required) | none | Configures the model's table-name. NOTE: this config can be omitted if the model is not used for performing any database operations (i.e. fetching, saving, deleting etc) |
Model.schema | string | none | Configures the model's schema-name |
Model.fields | object | none | Configures the model's fields. See the fields guide for more info |
Model.virtuals | object | none | Configures the model's virtual fields. See the virtuals guide for more info |
Model.options | object | none | Configures the model's default query and plugin options (for some plugins). See customizing queries per model for more info |
Model.Query | Query | Query | The Query class that the model uses to perform database operations. This allows customizing queries per model. |
Model.Field | Field | Field | The Field class that the model uses to create field instances. Also allows customizing fields per model. |
Setting data
Assuming this model:
You can set data on the model instance in any of the following ways:
Setting data via virtuals is also supported:
See the
Model
class for more info
Getting data
Following the example in Setting data, you can get data set on the instance via:
info
Since async
virtual getters are intrinsically supported, the methods that get
virtual field data always return a Promise
. However, you can stil use the
sync variants to ignore async virtual data.
Saving, fetching and deleting data
For all Model
instances, you can save, retrieve or delete
data in the database with these methods:
See
Model.prototype.save
,Model.prototype.insert
,Model.prototype.update
,Model.prototype.fetch
andModel.prototype.delete
for more info
All the methods update the instance with the latest data from the database, so
after an update you do not need to re-fetch the row (this can be disabled with
the Query.prototype.returning
query option though).
The Model.prototype.update
, Model.prototype.fetch
and
Model.prototype.delete
methods require a primary or unique field to be set on
the model in order to find the row in the database. See the
primary and unique fields guide
for more info.
All the methods also have static variants that instead enable working with multiple records:
See
Model.save
,Model.insert
,Model.update
,Model.fetch
andModel.delete
for more info
info
Static methods work on multiple rows while the instance methods only work on a single row!
note
Instance methods will automatically throw an error if the record is not found in the database (for fetch, delete and update operations).
In addition, you can configure generated
methods
with the methods
field config option:
note
These methods also throw an error if the record is not found in the database since they are intended to work with single records.
Customizing queries per model
You can set default query options per model via the Model.options
setter.
For example, if your users table has some system users that should not be
fetched/updated/deleted, you can add a default Query.prototype.where
option:
info
These options will also be inherited when the model is inherited.
Read more on setting query options
You could then have a SystemUser
model for interacting only with system users:
For more fine-grained control, you can also override the Query
class and add
custom query options:
note
Query options should return this
to allow chaining.
Model registry
Knorm keeps an internal registry of models that is automatically updated when
models are created and configured. In any of Knorm's classes, the model registry
can be accessed via the models
object:
When accessing other models from within instance and class methods, it's
recommended to use the models
instance or class property, rather than
accessing them via Node's require
function. This allows runnning queries
within transactions
without having to make any further code changes.
Note that for models to be automatically added to the registry, they must be
loaded (i.e. require
d) and configured. If a model is not
configured, you can add it to the registry via Knorm.prototype.addModel
:
Since models are only automatically added when they are require
d, it's
recommended to load all the models syncronously when starting up a node app.
For example: