Sequelize documentation is horrendous, so here's a handy cheat sheet.
createdb database_name
sequelize init
NOTE: The model name will be singular
NOTE 2: Any foreign keys will be modelNameId
- example: a foreign key to an
author
model will beauthorId
sequelize model:create --name comment --attributes author:string,content:text,favoriteId:integer
sequelize migration:create --name migrationName
sequelize db:migrate
sequelize db:migrate:undo
db.favorite.create({
author: 'Brian',
content: 'A comment'
}).then(function(favorite) {
//code here
});
//returns the instance if exists, otherwise creates it
db.favorite.findOrCreate({
where: {
author: 'Brian'
},
defaults: {
content: 'A comment'
}
}).spread(function(favorite, created) {
//code here
});
//returns only one instance; the first one that matches the where clause
db.favorite.find({
where: {
author: 'Brian'
}
}).then(function(favorite) {
//code here
});
//returns only one instance, and loads all the comments associated with the favorite
db.favorite.find({
where: {
author: 'Brian'
},
include: [db.comment]
}).then(function(favorite) {
// favorite.comments will exist, allowing access to the comments
});
//returns all instances that match the where clause
db.favorite.findAll({
where: {
author: 'Brian'
}
}).then(function(favorite) {
//code here
});
db.favorite.findById(1).then(function(favorite) {
//code here
});
//returns only one instance
db.favorite.findOne().then(function(favorite) {
//code here
});
db.favorite.update({
name: 'Josh'
}, {
where: {
author: 'Brian'
}
}).then(function(favorite){
// do something after update
});
db.favorite.destroy({
where: {
author: 'Brian'
}
}).then(function() {
// do something after destruction
});
Adding associations (using author
and post
models)
author.js
Authors should have many posts
associate: function(models) {
models.author.hasMany(models.post);
}
post.js
A post should belong to an author
associate: function(models) {
models.post.belongsTo(models.author);
}
Adding associations (using post
and tag
models, with a join table called postsTags
)
post.js
A post should belong to many tags
associate: function(models) {
models.post.belongsToMany(models.tag, {through: 'postsTags'});
}
tag.js
A tag should belong to many posts
associate: function(models) {
models.tag.belongsToMany(models.post, {through: 'postsTags'})
}
createPost()
- should create a post when called on a model related to post. Takes attributes as parametersgetPosts()
- should get posts when called on a model related to postaddPost(post)
- should add an existing post when called on a model related to postsetPost([post1, post2])
- should delete all existing associations and add the array of posts when called on a model related to post
This is the default promise called when a query is completed.
This is used to spread an array of values to parameters. This is only used for findOrCreate
, where the callback has two parameters.
This is triggered if something goes wrong (an error).
This is triggered after all other callbacks (including then
, spread
, and catch
). Can be used for cleanup.