我使用sqlight在节点模块中创建数据模型:
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
}));
});
这很好用,但如果我想访问应用程序的另一部分中的模型,比如我想在控制器中创建用户,该怎么办呢?我应该在哪里存储它们以及这样做的好方法?
答案 0 :(得分:1)
组织具有Sequelize
模型的应用程序的常用方法是创建一个models
包,其中index.js
内部处理关联并读取包目录中的模型。例如:
另见@ Farm的回答: