Node.js SequelizeJS ORM model utilities
npm install sequelize-models-postgissh
$ npm install --save sequelize-models
MySQL
$ npm install --save mysql2
PostgreSQL
$ npm install --save pg
$ npm install --save pg-hstore
`
$3
* Auto load of Sequelize models from database schema.
* Auto load models associations from database schema.
* Simplified and meaningful model files syntax.
* One place models and associations definitions.
* MySQL and PSQL support for now (support for MSSQL as soon as possible).
$3
Config and get schema
`js
const SequelizeModels = require("sequelize-models");
var seqModels = new SequelizeModels({
// Database connection options
connection : {
host : "127.0.0.1",
dialect : "mysql",
username : "root",
schema : "sequelize_test",
password : ""
},
// Models loading options
models : {
autoLoad : true,
path : "/models"
},
// Sequelize options passed directly to Sequelize constructor
sequelizeOptions : {
define : {
freezeTableName : true,
underscored : true
}
}
});
seqModels.getSchema().then( schema => {
// schema.models and schema.db available here
})
.catch( err => {
// throwing error out of the promise
setTimeout( () => { throw err });
});
`
Model Definition , file models/User.js
`js
module.exports = {
// Following http://docs.sequelizejs.com/en/latest/docs/models-definition/
tableName : "user",
attributes : {
name : {
type : "string"
},
last_name : {
type : "string"
},
born_date : {
type : "date"
}
},
// Associations -> http://docs.sequelizejs.com/en/latest/docs/scopes/#associations
associations : [{
type : "belongsTo",
target : "Profile",
options : {
foreignKey : "profile_id"
}
}],
validate : {},
indexes : []
};
`
$3
Feel free to submit a PR or create an issue for any bug fixes or feature requests, just remember if you add new features or fix a bug, please provide the respective tests for the case.
$3
`bash
$ npm install -g gulp && gulp docs
`
$3
You need edit test/mysql/config.js and test/psql/config.js with your own databases connection params, before run the steps below which are assuming that you will create a database with the name sequelize_test on each database.
`bash
$ npm install gulp -g && npm install
Create and start development docker databases (needs docker & docker-compose installed)
$ npm run db:up
create mysql config files
$ gulp config-mysql
test data for mysql
$ ./node_modules/sequelize-cli/bin/sequelize db:migrate
create psql config files
$ gulp config-psql
test data for postgres
$ ./node_modules/sequelize-cli/bin/sequelize db:migrate
run test
$ gulp test
``