lundi 6 avril 2015

Sequelize join and limit

I am trying to form a query with Sequelize to join and select + limit. Let me illustrate the problem:


I have a Many-to-Many relationship: Book - Reader. A reader can have many books and a book can have many readers.


Given the id of a Book, I would like to page through all of its Readers. Here's what I currently have tried, it has some issues. First the paging does not work. Second it still selects all the attributes of Book as well as Reader, when I only need Reader (I'm throwing away all of Book).



/**
* Get all Readers of a Book.
*
* @param {Number} bookId id of the book
* @param {Number} limit number of readers to retrieve
* @param {Number} offset number of readers to skip
* @return {Promise} contains an array of readers
*/
function getBookReaders(bookId, limit, offset) {
return Book.findOne({
id: bookId,
include: [Reader],
limit: 10,
offset: 30
}).then(function(book) {
return book.readers;
});
}


Any suggestions on how to form this query correctly?


Aucun commentaire:

Enregistrer un commentaire