Models
Last updated
Was this helpful?
Last updated
Was this helpful?
Attention!
Do not update or delete models in the /packages/novel/models
directory. These files are covered by warranty. Updating them may cause unforeseen problems.
Make sure you have a quick look on how Models work with Objection.js below
When Novel development starts, model files are generated based on the migration files present in your app directory.
You can start with these tutorials
During development, whenever migration changes. These are rebuilt and linted.
Make sure you commit and push these files to your repository.
There are tables and models that come out of the box with Novel API. Below is a representation of the whole default schema.
Here are the most important models you should be familiar with
Models that are generated by Novel come with a couple of files that serve specific purposes.
These files live in your /app/models
directory as well as packages/novel/models.
index.ts
File that you can build your specific extensions from.
This is not overwritten during startup.
✅
base.ts
Generated based on your migration file.
Contains typescript definitions and Objection specific properties like relationships and id and table identifiers. This is overwritten every time the server starts.
schema.json
Generated based on your migration file and represents your migration file as JSON schema.
Can directly be inferred via typescript. This is overwritten every time the server starts.
zod.ts
Generated based on your migration file and represents your migration file as a Zod definition.
Can directly be inferred via typescript. This is overwritten every time the server starts.
From the model files discussed above, a couple of conventions are applied so it works well with Novel.
Each model class includes a class property of their columns and relationships.
Table names are defined as plural.
IDs can be both a single key or a composite one.
Zod definitions are exposed in the model itself access via the .z
getter.
JSON Schema validation is built in during insert and update.
Each model exposes it's relationship via Objection's .relationMappings
getter.
Types are inferred from both the model's properties and zod inferred definition.
You can extend an existing model by using ESM/Typescript's extends
feature.
This will let you inherit all the base model's properties and methods and apply your own from a different file.
You can start by extending like below
And you can use this new model in your app like below
Attention!
Do not update or delete models in the /packages/novel/models
directory. These files are covered by warranty. Updating them may cause unforeseen problems.
Novel Models have access to the following helpers provided by Objection as well as useful lodash ones below.
Take a look at Objection's Model implementation for the helpers available by default.
pick(paths)
omit(paths)
at(paths)
entries()
keys()
Objection has built-in validation via JSON schema. It is applied automatically using Objection's jsonSchema
getter.
In-depth explanation on how validation is performed is available below.
During generation, a JSON schema file is generated along with the base model. This schema is also fed into fastify's AJV schema registry.
Zod is available for the model through the .z
getter. You can use it like below
Types are also available via the model's directory. eg, novel/models/accounts/zod
.
You are able to reference a schema in your routes by using this shorthand when defining your route schema in Fastify
2024-12-20 - Initial Documentation
Provided by lodash via . Creates an object composed of the picked object
properties.
Provided by lodash via . The opposite of ; this method creates an object composed of the own and inherited enumerable property paths of object
that are not omitted.
This method is considerably slower than .
Provided by lodash via . Creates an array of values corresponding to paths
of object
.
Provided by lodash via . Creates an array of own enumerable string keyed-value pairs for object
which can be consumed by . If object
is a map or set, its entries are returned.
Provided by lodash via . Creates an array of the own enumerable property names of object
.
Note: Non-object values are coerced to objects. See the for more details.