Skip to content
Alexey Gordeyev edited this page Jan 8, 2017 · 6 revisions

Back to Overview

Common API methods

Just instantiate model

   var post = new Post();

#create(callback)

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
});

#all(params, callback)

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(params, callback)

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
});

#findOrCreate(params, data, callback)

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
});

#findOne(params, callback)

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
});

#findById(id, callback)

Find instance by id

User.findById(1, function(err, user){
   // your code here
})

#updateOrCreate(params, data, callback)

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(params, data, callback)

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(params, callback)

Count instances

// count posts by user
Post.count({where: {userId: user.id}}, function(err, count){
   // your code here
});

#remove(params, callback)

Remove instances.

// remove all unpublished posts
Post.remove({where: {published: false}},function(err){
   // your code here
});

#destroy(callback)

Destroy instance

User.findById(22, function(err, user) {
    user.destroy(function(err){
       // your code here
    });
});
// or
User.destroyById(22, function(err) {
    // your code here
});

#destroyAll(callback)

Destroy all instances

User.destroyAll(function(err){
   // your code here
});

Define scope

Post.scope('active', { published : true });

Post.active(function(err, posts){
    // your code here
});

Define any Custom Method

User.prototype.getNameAndAge = function () {
    return this.name + ', ' + this.age;
};