Enrich your models
⚠️ This page is relevant only if you installed Forest Admin directly on a database (SQL/Mongodb). If you installed in a Rails/Django/Laravel app, you manage your models like you normally would.
Last updated
Was this helpful?
⚠️ This page is relevant only if you installed Forest Admin directly on a database (SQL/Mongodb). If you installed in a Rails/Django/Laravel app, you manage your models like you normally would.
Last updated
Was this helpful?
Please be sure of your agent type and version and pick the right documentation accordingly.
This is the documentation of the forest-express-sequelize
and forest-express-mongoose
Node.js agents that will soon reach end-of-support.
forest-express-sequelize
v9 and forest-express-mongoose
v9 are replaced by v1.
Please check your agent type and version and read on or switch to the right documentation.
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 a companies
table:
Fields within that model should match your table's fields as shown in next section.
New relationships may be added there:
Any new field must be added manually within the corresponding model of your /models
folder.
Fields are declared as follows:
Lumber introspects your data structure recursively, so nested fields (object in object) are detected any level deep. Your sub-documents (array of nested fields) are detected as well.
The following model...
...will result in the following interface:
Validation allows you to keep control over your data's quality and integrity.
Depending on your database type, your models will have been generated in Sequelize (for SQL databases) or Mongoose (for Mongo databases).
In Sequelize, you add validation using the validate
property:
The 2 validators above will have the following effect on your email field:
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:
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 a update_count
field every time a record is updated:
To add a beforeSave
hook in Sequelize, use the following syntax:
Every time the order is updated, the updateCount field will be incremented by 1:
You can learn more about relationships on this .
You can learn more about relationships on this .
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 .
An exhaustive list of DataTypes can be found in .
You can see how that snippet fits into your code in the above.
An exhaustive list of SchemaTypes can be found in .
You can see how that snippet fits into your code in the above.
For a better user experience, you can .
Conflicting data types will result in the generation of a type field.
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 .
For an exhaustive list of available validators, check out the .
A better yet solution would be to rely on an external library called which provides many :
For further details on validators in Mongoose, check out the .
The exhaustive list of available hooks in Sequelize are available .
As mentioned in