build立查询来获取js中的聊天对话消息

比方说,我有以下描述的模型架构,以添加我的帆应用程序中的2个用户之间的一些聊天function。 例如,如果我有用户A发送消息给用户B,则用户B将回复给用户A,用户A将始终在两个用户之间创build2个对话。 所以我的问题是我如何查询两个对话,从A和B获得消息。我尝试了这样的事情,但也许是一个简单的逻辑。

// User Model attributes: { username: { type: 'string', required: true, unique: true, }, conversations_sender: { collection: conversation, via: 'sender' }, conversations_recipient: { collection: conversation, via: 'recipient' } } // Conversation model attributes: { sender: { model: user }, recipient: { model: user }, messages: { collection: 'message', via: 'conversation' } } // Message model attributes: { text: { type: 'string' }, conversation: { model: 'conversation' }, } // Conversation Controller get: function(req, res) { var params = { or : [ { sender: req.param('sender'), recipient: req.param('recipient') }, { sender: req.param('recipient'), recipient: req.param('sender') } ] } Conversation.find(params) ... } 

你应该重新思考你的模式,看看这个链接有一个好的数据库devise为您的需要:

http://www.9lessons.info/2013/05/message-conversation-database-design.html

你应该可以通过这样的2个用户id获取所有的消息:

  Conversation.findAll({sender: ..., receiver: ...}) 

此外,你将需要一个时间戳的消息,在未来,你会想要sorting他们不知何故,也使好的“读昨天”function