-
Notifications
You must be signed in to change notification settings - Fork 119
Common API methods
Alexey Gordeyev edited this page Jan 8, 2017
·
6 revisions
- create
- all, run
- find
- findOrCreate
- findOne
- findById
- update
- updateOrCreate, upsert
- count
- remove
- destroy
- destroyAll
var post = new Post();
Save model (of course async)
Post.create(function(err){
// your code here
});
// same as new Post({userId: user.id});
user.posts.build
// same as Post.create({userId: user.id}, function(err){
// your code here
// });
// use promise
Post.create({userId: user.id}).then(function(created){
// your code here
});
user.posts.create(function(err){
// your code here
});
Get all instances
// all published posts
var Query = Post.all();
Query.where('published', true).desc('date');
Query.run({}, function(err, post){
// your code here
});
// all posts
Post.all(function(err, posts){
// your code here
});
// all posts by user
Post.all({where: {userId: 2}, order: 'id', limit: 10, skip: 20}, function(err, posts){
// your code here
});
// the same as prev
user.posts(function(err, posts){
// your code here
})
// use promise
Post.all({where:{userId: 2}, order: 'id', limit: 10, skip: 20}).then(function (founds) {
// your code here
});
Find instances
// all posts
Post.find(function(err, posts){
// your code here
});
// all posts by user
var Query = Post.find();
Query.where('userId', 2);
Query.order('id', 'ASC');
Query.skip(20).limit(10);
Query.run({},function(err, posts){
// your code here
});
// the same as prev
Post.find({where: {userId: user.id}, order: 'id', limit: 10, skip: 20}, function(err, posts){
// your code here
});
Find if exists or create instance.
// find user by email
User.findOrCreate({
email : 'example@example.com'
}, {
name : 'Gocha',
age : 31
}, function(err, user){
// your code here
});
Get one latest instance {where: {published: true}, order: 'date DESC'}
Post.findOne({where: {published: true}, order: 'date DESC'}, function(err, post){
// your code here
});
// or
var Query = Post.findOne();
Query.where('published',true).desc('date');
Query.run({}, function(err, post){
// your code here
});
Find instance by id
User.findById(1, function(err, user){
// your code here
})
Update if exists or create instance
Post.updateOrCreate({
id: 100
}, {
title: 'Riga',
tag: 'city'
}, function(err, post){
// your code here
});
// or
User.updateOrCreate({
email: 'example@example.com'
}, {
name: 'Alex',
age: 43
}, function(err, user){
// your code here
});
Update if exists instance
User.update({
where : {
email: 'example@example.com'
}
}, {
active: 0
}, function(err, user){
// your code here
});
// or
Post.update({
id: {
inq: [100, 101, 102]
}
}, {
tag: 'city'
}, function(err, post){
// your code here
});
Count instances
// count posts by user
Post.count({where: {userId: user.id}}, function(err, count){
// your code here
});
Remove instances.
// remove all unpublished posts
Post.remove({where: {published: false}},function(err){
// your code here
});
Destroy instance
User.findById(22, function(err, user) {
user.destroy(function(err){
// your code here
});
});
// or
User.destroyById(22, function(err) {
// your code here
});
Destroy all instances
User.destroyAll(function(err){
// your code here
});
Post.scope('active', { published : true });
Post.active(function(err, posts){
// your code here
});
User.prototype.getNameAndAge = function () {
return this.name + ', ' + this.age;
};