Database synchronization
When starting a new project you won't have a database structure and using Sequelize you won't need to. Just specify your model structures and let the library do the rest. Currently supported is the creation and deletion of tables:
// Create the tables:
Project.sync()
Task.sync()
// Force the creation!
Project.sync({force: true}) // this will drop the table first and re-create it afterwards
// drop the tables:
Project.drop()
Task.drop()
// event handling:
Project.[sync|drop]().then(() => {
// ok ... everything is nice!
}).catch(error => {
// oooh, did you enter wrong database credentials?
})
Because synchronizing and dropping all of your tables might be a lot of lines to write, you can also let Sequelize do the work for you:
// Sync all models that aren't already in the database
sequelize.sync()
// Force sync all models
sequelize.sync({force: true})
// Drop all tables
sequelize.drop()
// emit handling:
sequelize.[sync|drop]().then(() => {
// woot woot
}).catch(error => {
// whooops
})
Because .sync({ force: true })
is destructive operation, you can use match
option as an additional safety check.match
option tells sequelize to match a regex against the database name before syncing - a safety check for caseswhere force: true
is used in tests but not live code.
// This will run .sync() only if database name ends with '_test'
sequelize.sync({ force: true, match: /_test$/ });