Sequelize - sequelize.sync()

时间:2016-11-10 11:50:06

标签: node.js sequelize.js

我不明白它是如何运作的sequelize.sync()。 这是一个例子:

进入我的server.js文件:

db.sequelize.sync().then(function() {

app.listen(PORT);
console.log("Express listen on  port: " + PORT);
})

并在我的db.js文件中创建一个新数据库:

//create a sequelize database for export it into server.js

var Sequelize = require('sequelize');
var sequelize = new Sequelize(undefined, undefined, undefined, {
    'dialect': 'sqlite',
    'storage': __dirname + '/data/dev-todo-api.sqlite' // location where you create a new sqlite database 
});

var db = {};

db.todo = sequelize.import(__dirname + "/models/todo.js"); 
db.sequelize = sequelize; //contain a settings of database
db.Sequelize = Sequelize;

module.exports = db;

进入__dirname + "/models/todo.js"我创建了我的表/模型。当我启动db.sequelize.sync()时进入server.js,如何知道我的模型必须存储到数据库中?我从不打电话给db.todo(仅用于创建模特)

非常感谢你 莫里斯

1 个答案:

答案 0 :(得分:-1)

var Sequelize = require('sequelize');
var sequelize = new Sequelize(undefined, undefined, undefined, {
    'dialect': 'sqlite',
    'storage': __dirname + '/data/dev-todo-api.sqlite' // location where you create a new sqlite database 
});

var db = {};

db.todo = sequelize.import(__dirname + "/models/todo.js");

db.sequelize = sequelize; //contain a settings of database
db.Sequelize = Sequelize;

sequelize.sync().done(); // Use here

module.exports = db;