Search code examples
javascriptnode.jssails.jsmodelsrecord

Getting old records from Sails Models


Im actually trying to build up a complete chat with Sails.js websockets but im facing some troubles.

I succeed to send and get new messages when a new client connect to the chat but i would new client to get the older messages (like the 20 last messages sent in the chat).

Message.js (Model)

module.exports = {

  attributes: {
    name    : { type: 'string' },
    message : { type: 'string' }
  }
};

MessageController.js (Controller)

module.exports = {
    new: function(req, res) {
        var data = {
            name: req.param('name'),
            message: req.param('message'),
        };      

        Message.create(data).exec(function created(err, message){
            Message.publishCreate({id: message.id, name: message.name, message: message.message});
        });

    },

    subscribe: function(req, res) {     
        Message.watch(req);
    }
};

I had an idea about using the "find" function on my Model but not really conclusive.

I hope im not missing something big about Sails possibilities !

Need your help :) Thanks !


Solution

  • Message.find({sort: 'createdAt ASC'}).exec(function(err, results) {
      //results is an array of messages
    });
    

    http://sailsjs.org/documentation/concepts/models-and-orm/query-language