var express = require('express'); var router = express.Router(); var _ = require('underscore'); var paginate = require('../util/paginate'); var HashMap = require('hashmap').HashMap; function find(db, pager, appendwhere, callback) { var books = new HashMap(); var qparams = new Array(); var query = 'SELECT books.id as id,title,series_index,name as series_name,series.id AS series_id'+ ' FROM books' + ' LEFT OUTER JOIN books_series_link ON books.id = books_series_link.book' + ' LEFT OUTER JOIN series ON series.id = books_series_link.series'; if (appendwhere) { query = appendwhere(query, qparams); } query+= ' LIMIT ? OFFSET ?'; qparams.push(pager.perpage + 1); qparams.push(pager.offset); // console.log(query); // console.log(qparams); db.each(query, qparams, function (err, book) { if (books.count() < pager.perpage) { books.set(''+book.id, book); } else pager.hasNext = true; /* more than perpage */ }, function(err) { if (err) console.log(err); /* query books authors */ var qAuthors = 'SELECT books_authors_link.book as book,authors.id as id,name FROM authors, books_authors_link WHERE books_authors_link.author = authors.id AND books_authors_link.book IN ('; for (var i=0;i