From 7e57f562afb3995eb627ed97ece8f03d8d822561 Mon Sep 17 00:00:00 2001 From: Tyler Yang Date: Fri, 26 Jun 2015 16:43:18 +1000 Subject: [PATCH 1/3] Make _embed support partial match --- src/router.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/router.js b/src/router.js index 1472c99f4..49dbc8026 100644 --- a/src/router.js +++ b/src/router.js @@ -159,6 +159,7 @@ module.exports = function (source) { // GET /:resource/:id function show (req, res, next) { var _embed = req.query._embed + var _exact = req.query._exact var id = utils.toNative(req.params.id) var resource = db(req.params.resource) .getById(id) @@ -177,7 +178,12 @@ module.exports = function (source) { && db.object[otherResource]) { var query = {} var prop = pluralize.singular(req.params.resource) + 'Id' - query[prop] = id + if (_exact) { + query[prop] = id + } else { + query[prop] = [id] + } + resource[otherResource] = db(otherResource).where(query) } From 03ea80c0f723ee113aa66974fc0e4bb432b9d790 Mon Sep 17 00:00:00 2001 From: Tyler Yang Date: Mon, 29 Jun 2015 17:20:32 +1000 Subject: [PATCH 2/3] Add batch for list action --- src/router.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/router.js b/src/router.js index 49dbc8026..a61d27527 100644 --- a/src/router.js +++ b/src/router.js @@ -78,6 +78,9 @@ module.exports = function (source) { delete req.query._order delete req.query._limit + //Batch + var _ids = req.query._ids; + if (req.query.q) { // Full-text search @@ -110,11 +113,18 @@ module.exports = function (source) { } } + // Batch + var _dbConnection = db(req.params.resource); + if (!_.isEmpty(_ids)) { + _ids = _.isArray(_ids) ? _ids : [_ids]; + _dbConnection = _dbConnection.where( { id: _ids} ); + } + // Filter if (_(filters).isEmpty()) { - array = db(req.params.resource).value() + array = _dbConnection.value() } else { - var chain = db(req.params.resource).chain() + var chain = _dbConnection.chain() for (var f in filters) { // This syntax allow for deep filtering using lodash (i.e. a.b.c[0]) chain = chain.filter(f, filters[f]) From 27902f8eb1bafaab1364702f347fcef4e74a618a Mon Sep 17 00:00:00 2001 From: Tyler Yang Date: Tue, 30 Jun 2015 10:31:16 +1000 Subject: [PATCH 3/3] add batch --- src/router.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/router.js b/src/router.js index a61d27527..261ca4d7a 100644 --- a/src/router.js +++ b/src/router.js @@ -80,6 +80,7 @@ module.exports = function (source) { //Batch var _ids = req.query._ids; + delete req.query._ids; if (req.query.q) { @@ -115,10 +116,6 @@ module.exports = function (source) { // Batch var _dbConnection = db(req.params.resource); - if (!_.isEmpty(_ids)) { - _ids = _.isArray(_ids) ? _ids : [_ids]; - _dbConnection = _dbConnection.where( { id: _ids} ); - } // Filter if (_(filters).isEmpty()) { @@ -130,9 +127,20 @@ module.exports = function (source) { chain = chain.filter(f, filters[f]) } array = chain.value() + } } + // Batch + if (!_.isEmpty(_ids)) { + _ids = _ids.split(','); + _ids = _.isArray(_ids) ? _ids : [_ids]; + + array = _.filter(array, function(n){ + return _.includes(_ids, n.id.toString()); + }); + } + // Sort if (_sort) { _order = _order || 'ASC' @@ -189,9 +197,9 @@ module.exports = function (source) { var query = {} var prop = pluralize.singular(req.params.resource) + 'Id' if (_exact) { - query[prop] = id + query[prop] = id } else { - query[prop] = [id] + query[prop] = [id] } resource[otherResource] = db(otherResource).where(query)