I am working with Sequelize for the first time and am have structured my application in a similar way as shown in the official Sequelize github.
Each model is setup in its own file (e.g.):
const { DataTypes } = require('sequelize');
// We export a function that defines the model.
// This function will automatically receive as parameter the Sequelize connection object.
module.exports = (sequelize) => {
sequelize.define('user', {
// The following specification of the 'id' attribute could be omitted
// since it is the default.
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
username: {
allowNull: false,
type: DataTypes.STRING,
unique: true,
validate: {
// We require usernames to have length of at least 3, and
// only use letters, numbers and underscores.
is: /^\w{3,}$/
}
},
});
};
Each of the models is then pulled in to the application as so:
const modelDefiners = [
require('./models/user.model'),
require('./models/instrument.model'),
require('./models/orchestra.model'),
// Add more models here...
// require('./models/item'),
];
// We define all models according to their files.
for (const modelDefiner of modelDefiners) {
modelDefiner(sequelize);
}
I'd like to add specific functions to each of the models so that the functions are available elsewhere in my application, however I am not sure how best to do this. There is an example in the Sequelize documentation however that only shows hows to do it if extending the model class. I could try and refactor to match this, but wanted to see if there was a way to do it as per the example setup provided by Sequelize.