Declaring a new model
Whenever you have a new table/collection in your database, you will have to create file to declare it. Here is a template example for acompanies table:
- SQL
- Mongoose
When you manually add a new model, you need to configure the permissions for the corresponding collection in the UI (allow record details view, record creation, record edit, etc). By default a new collection is not visible and all permissions are disabled. You can set permissions by going to the Roles settings.
Declaring a new field in a model
Any new field must be added manually within the corresponding model of your/models folder.
- SQL
- Mongoose
Fields are declared as follows:An exhaustive list of DataTypes can be found in Sequelize documentation.You can see how that snippet fits into your code in the model example above.
Managing nested documents in Mongoose
For a better user experience, flatten nested fields. In v2 see the Flattener plugin.

Removing a model
By default all tables/collections in your database are analyzed by Lumber to generate your models. If you want to exclude some of them to prevent them from appearing in your Forest, check out this how-to.Adding validation to your models
Validation allows you to keep control over your data’s quality and integrity.If your existing app already has validation conditions, you may - or may not - want to reproduce the same validation conditions in your admin backend’s models. If so, you’ll have to do it manually, using the below examples.
- SQL
- Mongoose
In Sequelize, you add validation using the The 2 validators above will have the following effect on your email field:.png?fit=max&auto=format&n=7CReagZK6gkpGoy9&q=85&s=fa7197406b487e7372e3c34fadec1831)
.png?fit=max&auto=format&n=7CReagZK6gkpGoy9&q=85&s=fbe66f5ed0f2787c65dd2de1d731207a)
For an exhaustive list of available validators, check out the Sequelize documentation.
validate property:.png?fit=max&auto=format&n=7CReagZK6gkpGoy9&q=85&s=fa7197406b487e7372e3c34fadec1831)
.png?fit=max&auto=format&n=7CReagZK6gkpGoy9&q=85&s=fbe66f5ed0f2787c65dd2de1d731207a)
Adding a default value to your models
You can choose to add a default value for some fields in your models. As a result, the corresponding fields will be prefilled with their default value in the creation form:- SQL
- Mongoose
Adding a hook
Hooks are a powerful mechanism which allow you to automatically trigger an event at specific moments in your records lifecycle. In our case, let’s pretend we want to update aupdate_count field every time a record is updated:
- SQL
- Mongoose
To add a Every time the order is updated, the updateCount field will be incremented by 1:
The exhaustive list of available hooks in Sequelize are available here.
beforeSave hook in Sequelize, use the following syntax:
.png?fit=max&auto=format&n=7CReagZK6gkpGoy9&q=85&s=ae7216861be2c3a11ad26f1f5f37340a)
.png?fit=max&auto=format&n=7CReagZK6gkpGoy9&q=85&s=abc104cc5a987527576ea6bdf0a91b2a)
.png?fit=max&auto=format&n=7CReagZK6gkpGoy9&q=85&s=26a8bcb167bdf46bdac0e1c3809d6ca6)