From 2e738af78dee4a32885511baa8b67ae1f3f33635 Mon Sep 17 00:00:00 2001 From: Mike James Date: Wed, 24 May 2017 14:19:24 +1000 Subject: [PATCH 1/7] temporarily change package name --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 77af7fb3cc..baa49f46be 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "keystone", + "name": "@thinkmill/keystone", "version": "4.0.0-beta.5", "description": "Web Application Framework and Admin GUI / Content Management System built on Express.js and Mongoose", "main": "index.js", From 644577e39da09f539a61be814d573c04dc0e865f Mon Sep 17 00:00:00 2001 From: Mike James Date: Wed, 9 Aug 2017 09:44:36 +1000 Subject: [PATCH 2/7] publish forked version for thinkmill/jm --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index baa49f46be..1a291b68a4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@thinkmill/keystone", - "version": "4.0.0-beta.5", + "version": "4.0.0-beta.5-jm-fork-1", "description": "Web Application Framework and Admin GUI / Content Management System built on Express.js and Mongoose", "main": "index.js", "repository": { From ab8225a8505d1ea8f09225fdbde7c6fc6348e3bb Mon Sep 17 00:00:00 2001 From: Mike James Date: Wed, 9 Aug 2017 15:30:53 +1000 Subject: [PATCH 3/7] if value is objectid then query the current list otherwise lookup the docs with the value provided and get its object id to join against --- admin/server/api/list/get.js | 58 ++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/admin/server/api/list/get.js b/admin/server/api/list/get.js index 26acdf6228..1e34fdd071 100644 --- a/admin/server/api/list/get.js +++ b/admin/server/api/list/get.js @@ -24,25 +24,24 @@ module.exports = function (req, res) { catch (e) { } // eslint-disable-line no-empty } let priorQuery; - if (typeof filters === 'object' && !req.query.expandRelationshipFields) { - Object.keys(filters) - .map(path => { - const relationship = req.list.relationshipFields.find(relationship => relationship.path === path); - const RefModel = req.keystone.lists[relationship.options.ref]; - if (RefModel) { - Object.keys(RefModel.fields) - .reduce((acc, fieldName) => { - const field = RefModel.fields[fieldName]; - if (req.list.key === field.options.ref) { - priorQuery = { Model: RefModel, find: toFind({ valueFilters: req.query.filters, filters: field.filters }) }; - } - - }); - } - }); - } - - if (typeof filters === 'object' && req.query.expandRelationshipFields) { + if (!filterValuesAreObjectIds(filters)) { + if (typeof filters === 'object' && !req.query.expandRelationshipFields) { + Object.keys(filters) + .map(path => { + const relationship = req.list.relationshipFields.find(relationship => relationship.path === path); + const RefModel = req.keystone.lists[relationship.options.ref]; + if (RefModel) { + Object.keys(RefModel.fields) + .reduce((acc, fieldName) => { + const field = RefModel.fields[fieldName]; + if (req.list.key === field.options.ref) { + priorQuery = { Model: RefModel, find: toFind({ valueFilters: req.query.filters, filters: field.filters }) }; + } + }); + } + }); + } + } else if (typeof filters === 'object') { assign(where, req.list.addFiltersToQuery(filters)); } if (req.query.search) { @@ -128,3 +127,24 @@ function toFind ({ valueFilters, filters }) { // { key: 'futurism' } }, {}); } + +// eg filters = { site: '6d406d696b656a616d2e6573' }; +// when passed through mongo's ObjectId we get the same value back +// return on first filter in object. +// this limits keystone relationship filter to be on one field only. +function filterValuesAreObjectIds (filters) { + if (typeof (filters) === 'object') { + for (const key in filters) { + if (filters.hasOwnProperty(key)) { + try { + const value = filters[key].value; + const objectId = Mongoose.Types.ObjectId(value); + return value === objectId.toString(); + } catch (e) { + return false; + } + } + } + } + return false; +} From d9421ed9ff6f15255491120d3dc8a414d8907908 Mon Sep 17 00:00:00 2001 From: Mike James Date: Wed, 9 Aug 2017 16:03:22 +1000 Subject: [PATCH 4/7] bump jm fork version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1a291b68a4..52cb751dba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@thinkmill/keystone", - "version": "4.0.0-beta.5-jm-fork-1", + "version": "4.0.0-beta.5-jm-fork-2", "description": "Web Application Framework and Admin GUI / Content Management System built on Express.js and Mongoose", "main": "index.js", "repository": { From 17feabb3c9b6aa3bf380cf15f386830170ad1d13 Mon Sep 17 00:00:00 2001 From: Mike James Date: Thu, 10 Aug 2017 10:39:49 +1000 Subject: [PATCH 5/7] attempt at fixing filtering --- admin/server/api/list/get.js | 1 + 1 file changed, 1 insertion(+) diff --git a/admin/server/api/list/get.js b/admin/server/api/list/get.js index 1e34fdd071..cea69642c1 100644 --- a/admin/server/api/list/get.js +++ b/admin/server/api/list/get.js @@ -138,6 +138,7 @@ function filterValuesAreObjectIds (filters) { if (filters.hasOwnProperty(key)) { try { const value = filters[key].value; + if (Array.isArray(value)) return true; const objectId = Mongoose.Types.ObjectId(value); return value === objectId.toString(); } catch (e) { From e02313094978f42866489cb73bb169cdb750221a Mon Sep 17 00:00:00 2001 From: Mike James Date: Thu, 10 Aug 2017 12:53:26 +1000 Subject: [PATCH 6/7] setting keystone.item on item get data success --- admin/client/App/screens/Item/reducer.js | 1 + admin/server/api/list/get.js | 77 +------------------ .../types/relationship/RelationshipField.js | 2 +- 3 files changed, 5 insertions(+), 75 deletions(-) diff --git a/admin/client/App/screens/Item/reducer.js b/admin/client/App/screens/Item/reducer.js index ce3b4c06dc..7b5c689871 100644 --- a/admin/client/App/screens/Item/reducer.js +++ b/admin/client/App/screens/Item/reducer.js @@ -39,6 +39,7 @@ function item (state = initialState, action) { loading: true, }); case DATA_LOADING_SUCCESS: + Keystone.item = action.data; // Fix keystone filter return assign({}, state, { data: action.data, loading: false, diff --git a/admin/server/api/list/get.js b/admin/server/api/list/get.js index cea69642c1..707ee2c81b 100644 --- a/admin/server/api/list/get.js +++ b/admin/server/api/list/get.js @@ -1,7 +1,7 @@ var async = require('async'); var assign = require('object-assign'); var listToArray = require('list-to-array'); -var Mongoose = require('mongoose'); + module.exports = function (req, res) { var where = {}; var fields = req.query.fields; @@ -23,25 +23,7 @@ module.exports = function (req, res) { try { filters = JSON.parse(req.query.filters); } catch (e) { } // eslint-disable-line no-empty } - let priorQuery; - if (!filterValuesAreObjectIds(filters)) { - if (typeof filters === 'object' && !req.query.expandRelationshipFields) { - Object.keys(filters) - .map(path => { - const relationship = req.list.relationshipFields.find(relationship => relationship.path === path); - const RefModel = req.keystone.lists[relationship.options.ref]; - if (RefModel) { - Object.keys(RefModel.fields) - .reduce((acc, fieldName) => { - const field = RefModel.fields[fieldName]; - if (req.list.key === field.options.ref) { - priorQuery = { Model: RefModel, find: toFind({ valueFilters: req.query.filters, filters: field.filters }) }; - } - }); - } - }); - } - } else if (typeof filters === 'object') { + if (typeof filters === 'object') { assign(where, req.list.addFiltersToQuery(filters)); } if (req.query.search) { @@ -58,33 +40,17 @@ module.exports = function (req, res) { } var sort = req.list.expandSort(req.query.sort); async.waterfall([ - function (next) { - if (!priorQuery) { - return next(null); - } - priorQuery.Model.model.find(priorQuery.find) - .then(results => { - const ids = results.map(r => Mongoose.Types.ObjectId(r.id)); // return id's for $in query - assign(where, Object.keys(req.query.filters) - .reduce((acc, key) => { - if (!req.query.filters[key]) return acc; - return Object.assign({}, { [key]: { $in: ids } }); - }, {})); - next(null); - }); - }, function (next) { if (!includeCount) { return next(null, 0); } - query.find(where); query.count(next); }, function (count, next) { if (!includeResults) { return next(null, count, []); } - query.find(where); + query.find(); query.limit(Number(req.query.limit) || 100); query.skip(Number(req.query.skip) || 0); if (sort.string) { @@ -112,40 +78,3 @@ module.exports = function (req, res) { }); }); }; - -// INPUT: -// valueFilters { domain: { value: 'bobsyouruncle.com``' }} -// OUTPUT: -// { domain: 'bobsyouruncle.com' } -function toFind ({ valueFilters, filters }) { - return Object.keys(valueFilters) - .reduce((acc, k) => { - if (!filters[k]) return acc; - const value = valueFilters[k].value; - const key = filters[k].replace(/:/g, ''); - return Object.assign({}, acc, { [key]: value }); - // { key: 'futurism' } - }, {}); -} - -// eg filters = { site: '6d406d696b656a616d2e6573' }; -// when passed through mongo's ObjectId we get the same value back -// return on first filter in object. -// this limits keystone relationship filter to be on one field only. -function filterValuesAreObjectIds (filters) { - if (typeof (filters) === 'object') { - for (const key in filters) { - if (filters.hasOwnProperty(key)) { - try { - const value = filters[key].value; - if (Array.isArray(value)) return true; - const objectId = Mongoose.Types.ObjectId(value); - return value === objectId.toString(); - } catch (e) { - return false; - } - } - } - } - return false; -} diff --git a/fields/types/relationship/RelationshipField.js b/fields/types/relationship/RelationshipField.js index e757bd8494..635a4ec079 100644 --- a/fields/types/relationship/RelationshipField.js +++ b/fields/types/relationship/RelationshipField.js @@ -68,7 +68,7 @@ module.exports = Field.create({ } // check if filtering by id and item was already saved - if (fieldName === ':_id' && Keystone.item) { + if (fieldName === '_id' && Keystone.item) { filters[key] = Keystone.item.id; return; } From 7a8f0f54f5124179c34a0069271a7b069b03c587 Mon Sep 17 00:00:00 2001 From: Mike James Date: Thu, 10 Aug 2017 12:54:16 +1000 Subject: [PATCH 7/7] jm fork 3 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 52cb751dba..69389443b3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@thinkmill/keystone", - "version": "4.0.0-beta.5-jm-fork-2", + "version": "4.0.0-beta.5-jm-fork-3", "description": "Web Application Framework and Admin GUI / Content Management System built on Express.js and Mongoose", "main": "index.js", "repository": {