Reputation: 15006
I use sqlight to create a datamodel in a node-module:
database.js:
var Sequelize = require('sequelize'),
settings = require('./settings.js').settings,
sequelize = new Sequelize(settings.database),
//Datamodel
User = sequelize.define('user', {
authID: Sequelize.STRING,
name: Sequelize.STRING,
}),
UserAttribute = sequelize.define('userAttributes', {
name: Sequelize.STRING,
value: Sequelize.STRING,
}),
Hail = sequelize.define('hail', {
lat: Sequelize.INTEGER,
lon: Sequelize.INTEGER,
});
UserAttribute.belongsTo(User);
Hail.belongsTo(User, {as: 'driver'});
Hail.belongsTo(User, {as: 'rider'});
sequelize.sync(settings.sync).then(function() {
return User.create({});
}).then(function(driver) {
console.log(driver.get({
plain: true
}));
});
This works fine, but what if I want to access the models in another part of my application, like if I would like to create a user inside a controller. Where should i store them and what would be a good way of doing that?
Upvotes: 1
Views: 273
Reputation: 473833
A common way of organizing apps having Sequelize
models is creating a models
package with index.js
inside handling association and reading the models inside the package directory. Example:
Also see @Farm's answer in this thread:
Upvotes: 1