1.0.3 • Published 8 years ago
sequelauto v1.0.3
Sequelauto
Automatically generate records on database for Sequelize's model.
Usage
Let's say you have the following model:
const Sequelize = require('sequelize');
const sequelize = new Sequelize('sqlite://');
module.exports = User = sequelize.define('user', {
name: {
type: Sequelize.STRING(100),
allowNull: false
},
birth: Sequelize.DATE,
height: {
type: Sequelize.DECIMAL,
allowNull: false
}
});It would be a pain in the ass to create dummy entries on the database manually. That's when sequelauto comes in handy:
const User = require('./models');
const sequelauto = require('sequelauto');
sequelauto.create(User).then(user => {
// "user" is a sequelize model instance!
console.log(user.dataValues); // {id: 1, name: "bjda2sghat", "birth": null, "height": 1.3}
});You can also create many instances at once:
sequelauto.createMany(User, 10).then(users => {
// "users" is an array of all created users
console.log(users.map(u => u.dataValues)); // [{id: 1, name: "bjda2sghat", "birth": null, "height": 1.3}, ...]
});There is also a possibility to define your own values:
sequelauto.create(User, { name: 'John Doe', height: 5.8 }).then(user => {
console.log(user.dataValues); // [{id: 1, name: "John Doe", "height": 5.8, "birth": null}, ...]
});By default nullable fields remain null.
Supported field types
STRINGCHARTEXTTINYINTSMALLINTMEDIUMINTINTEGERBIGINTFLOATDOUBLEDECIMALREALBOOLEANENUMDATEDATEONLYTIMEUUIDJSONJSONBBLOB
In order to avoid mistakes, by default it runs only on SQLite.
Here is how to disable this feature:
const sequelauto = require('sequelauto');
sequelauto.configure('RUN_ONLY_ON_SQLITE', false);