sequelize-virtual-model v0.2.1
Sequelize virtual models
Autogenerate Sequelize models without any stored file and after the database creation. This project is for use on a existant database. Can be used to create a new models but haven't any sense existing better options to these works.
How it works: VirtualModel
This lib is based on Sequelize method describeTable
. This method returns an object with exactly table definition (DataTypes, default values...). This create a on demand model from this schema.
You have to know that is most faster a stored file with model definition that this project.
- Set the Sequelize instance variables.
- Create a empty class model that extends from
Sequelize.Model
. - Call to init with correct options to get the model instance.
Create first model
With an example:
// database, user, password, host, dialect, dialectOptions
new VirtualModel('MyDb', 'sa', 'password', 'host', 'dialect', {
options: {
requestTimeout: 30000,
trustServerCertificate: false,
useUTC: false,
dateFirst: 1,
enableArithAbort: true,
}
});
After this you have to set the config options on the init method. This method is async
and returns a promise. You need to use promises or async/await.
let UserModel = await Schema.init('userTable', {
someMethod: function () {
return this.userid;
}, {
hasIdKey: false,
timestamps: false,
// I don't want createdAt
createdAt: false,
// I don't want updatedAt
updatedAt: false
}
});
let results = await UserModel.findAll({
limit: 1
});
Third arg is a config options. This will be explained on next steps. Has a config by default if you only need the model instance.
Prevent to plural names by default
To create a entities with not plural names, the init method required the name of the table and use it to force the entity value name. For example, if your table name is product
, Sequelize model by default returns an model named products
. With this lib, this doesn't happend.
Prevent autogenerate primary key ID
By default, when you creates a model if you don't define a primary key, Sequelize asummes that the column value is ID. If your primary key isn't ID you can prevent this on init
options. Set hasIdKey
to false.
CreateAt and UpdateAt
This project is based for databases that existing after use sequelize. Only change this values if you use this project for new creation databases. Timestamp
values to update and create should be setted to this values on init
options:
{
// reset timestamps: asummes that table already exists
timestamps: false,
// I don't want createdAt
createdAt: false,
// I don't want updatedAt
updatedAt: false
}
Extend model methods
You can add methods to your entity:
let UserModel = await User.init('wgcdoclinhas', {
someMethod: function () {
return this.username;
}
});
let results = await UserModel.findAll({
limit: 1
});
results.map(document => {
console.log(document.someMethod());
});
- Encapsule your methods into a function() {} scope. If you use arrow functions on this action the context is
Sequelize
instance instead of the model. If you don't use this format you can't access to column values on theinstanceMethods
.