Woodshop
Search…
Re-use a smart field logic
This example shows you how to re-use the logic of your smart field.
Here, we created a fullname field that concatenates first and last name. We'll create our own function and export it for use elsewhere in our admin backend.

Requirements

    An admin backend running on forest-express-sequelize

How it works

Directory: /models

This directory contains the users.js where the model is defined.
/models/users.js
1
module.exports = (sequelize, DataTypes) => {
2
const { Sequelize } = sequelize;
3
4
const Users = sequelize.define('users', {
5
firstName: {
6
type: DataTypes.STRING,
7
},
8
lastName: {
9
type: DataTypes.STRING,
10
},
11
}, {
12
tableName: 'users',
13
timestamps: false,
14
schema: process.env.DATABASE_SCHEMA,
15
});
16
17
return Users;
18
};
Copied!

Directory: /forest

This directory contains the users.js file where the Smart Field fullnameis declared.
1
const { collection } = require('forest-express-sequelize');
2
3
// create our own module
4
const getFullname = (user) => {
5
return `${user.firstName} ${user.lastName}`;
6
};
7
8
9
collection('users', {
10
fields: [{
11
field: 'fullname',
12
type: 'String',
13
get: getFullname,
14
}],
15
});
16
17
// export it to use it somewhere else in our admin backend
18
module.exports = {
19
getFullname,
20
};
Copied!
You can now require this module everywhere in your admin backend:
1
const { getFullname } = require('../forest/user');
Copied!
Last modified 1yr ago