From a48ff0a3adbf126123d7d0c12852816fd44b1ab0 Mon Sep 17 00:00:00 2001 From: Tim Leslie Date: Mon, 30 Sep 2019 14:23:30 +1000 Subject: [PATCH] Internal refactor to move defintion of modifierConditions closer to where they're used (#1705) --- .changeset/spicy-clouds-invite/changes.json | 1 + .changeset/spicy-clouds-invite/changes.md | 1 + .../adapter-mongoose/lib/adapter-mongoose.js | 47 ---------------- .../adapter-mongoose/lib/tokenizers/simple.js | 54 ++++++++++++++++--- .../adapter-mongoose/tests/simple.test.js | 10 ++-- 5 files changed, 54 insertions(+), 59 deletions(-) create mode 100644 .changeset/spicy-clouds-invite/changes.json create mode 100644 .changeset/spicy-clouds-invite/changes.md diff --git a/.changeset/spicy-clouds-invite/changes.json b/.changeset/spicy-clouds-invite/changes.json new file mode 100644 index 00000000000..4534bc651c0 --- /dev/null +++ b/.changeset/spicy-clouds-invite/changes.json @@ -0,0 +1 @@ +{ "releases": [{ "name": "@keystone-alpha/adapter-mongoose", "type": "patch" }], "dependents": [] } diff --git a/.changeset/spicy-clouds-invite/changes.md b/.changeset/spicy-clouds-invite/changes.md new file mode 100644 index 00000000000..08f40bb2c21 --- /dev/null +++ b/.changeset/spicy-clouds-invite/changes.md @@ -0,0 +1 @@ +Internal refactor to move defintion of modifierConditions closer to where they're used. \ No newline at end of file diff --git a/packages/adapter-mongoose/lib/adapter-mongoose.js b/packages/adapter-mongoose/lib/adapter-mongoose.js index c729cfb2062..f19e1005a16 100644 --- a/packages/adapter-mongoose/lib/adapter-mongoose.js +++ b/packages/adapter-mongoose/lib/adapter-mongoose.js @@ -24,52 +24,6 @@ const slugify = require('@sindresorhus/slugify'); const debugMongoose = () => !!process.env.DEBUG_MONGOOSE; -const modifierConditions = { - // TODO: Implement configurable search fields for lists - $search: value => { - if (!value || (getType(value) === 'String' && !value.trim())) { - return undefined; - } - return { - $match: { - name: new RegExp(`${escapeRegExp(value)}`, 'i'), - }, - }; - }, - - $orderBy: (value, _, listAdapter) => { - const [orderField, orderDirection] = value.split('_'); - - const mongoField = listAdapter.graphQlQueryPathToMongoField(orderField); - - return { - $sort: { - [mongoField]: orderDirection === 'DESC' ? -1 : 1, - }, - }; - }, - - $skip: value => { - if (value < Infinity && value > 0) { - return { - $skip: value, - }; - } - }, - - $first: value => { - if (value < Infinity && value > 0) { - return { - $limit: value, - }; - } - }, - - $count: value => ({ - $count: value, - }), -}; - class MongooseAdapter extends BaseKeystoneAdapter { constructor() { super(...arguments); @@ -168,7 +122,6 @@ class MongooseListAdapter extends BaseListAdapter { // executed for simple query components (eg; 'fulfilled: false' / name: 'a') simple: simpleTokenizer({ getRelatedListAdapterFromQueryPath: getRelatedListAdapterFromQueryPathFactory(this), - modifierConditions, }), // executed for complex query components (eg; items: { ... }) relationship: relationshipTokenizer({ diff --git a/packages/adapter-mongoose/lib/tokenizers/simple.js b/packages/adapter-mongoose/lib/tokenizers/simple.js index 45e5335b896..061a33a80cb 100644 --- a/packages/adapter-mongoose/lib/tokenizers/simple.js +++ b/packages/adapter-mongoose/lib/tokenizers/simple.js @@ -1,10 +1,6 @@ -const { objMerge } = require('@keystone-alpha/utils'); +const { objMerge, getType, escapeRegExp } = require('@keystone-alpha/utils'); -const simpleTokenizer = ({ getRelatedListAdapterFromQueryPath, modifierConditions }) => ( - query, - queryKey, - path -) => { +const simpleTokenizer = ({ getRelatedListAdapterFromQueryPath }) => (query, queryKey, path) => { // NOTE: We slice the last path segment off because we're interested in the // related list, not the field on the related list. ie, if the path is // ['posts', 'comments', 'author', 'name'], @@ -33,4 +29,50 @@ const simpleTokenizer = ({ getRelatedListAdapterFromQueryPath, modifierCondition return {}; }; +const modifierConditions = { + // TODO: Implement configurable search fields for lists + $search: value => { + if (!value || (getType(value) === 'String' && !value.trim())) { + return undefined; + } + return { + $match: { + name: new RegExp(`${escapeRegExp(value)}`, 'i'), + }, + }; + }, + + $orderBy: (value, _, listAdapter) => { + const [orderField, orderDirection] = value.split('_'); + + const mongoField = listAdapter.graphQlQueryPathToMongoField(orderField); + + return { + $sort: { + [mongoField]: orderDirection === 'DESC' ? -1 : 1, + }, + }; + }, + + $skip: value => { + if (value < Infinity && value > 0) { + return { + $skip: value, + }; + } + }, + + $first: value => { + if (value < Infinity && value > 0) { + return { + $limit: value, + }; + } + }, + + $count: value => ({ + $count: value, + }), +}; + module.exports = { simpleTokenizer }; diff --git a/packages/adapter-mongoose/tests/simple.test.js b/packages/adapter-mongoose/tests/simple.test.js index 1662e23cf97..6f3a98bacb5 100644 --- a/packages/adapter-mongoose/tests/simple.test.js +++ b/packages/adapter-mongoose/tests/simple.test.js @@ -18,29 +18,27 @@ describe('Simple tokenizer', () => { test('Falls back to modifier conditions when no simple condition found', () => { const simpleConditions = { notinuse: () => ({ foo: 'bar' }) }; - const modifierConditions = { name: () => ({ zip: 'quux' }) }; const getQueryConditions = jest.fn(() => simpleConditions); const getRelatedListAdapterFromQueryPath = jest.fn(() => ({ fieldAdapters: [{ getQueryConditions }], })); - const simple = simpleTokenizer({ getRelatedListAdapterFromQueryPath, modifierConditions }); + const simple = simpleTokenizer({ getRelatedListAdapterFromQueryPath }); - expect(simple({ name: 'hi' }, 'name', ['name'])).toMatchObject({ - postJoinPipeline: [{ zip: 'quux' }], + expect(simple({ $count: 'hi' }, '$count', ['$count'])).toMatchObject({ + postJoinPipeline: [{ $count: 'hi' }], }); expect(getQueryConditions).toHaveBeenCalledTimes(1); }); test('returns empty array when no matches found', () => { const simpleConditions = { notinuse: () => ({ foo: 'bar' }) }; - const modifierConditions = { idontexist: () => ({ zip: 'quux' }) }; const getQueryConditions = jest.fn(() => simpleConditions); const getRelatedListAdapterFromQueryPath = jest.fn(() => ({ fieldAdapters: [{ getQueryConditions }], })); - const simple = simpleTokenizer({ getRelatedListAdapterFromQueryPath, modifierConditions }); + const simple = simpleTokenizer({ getRelatedListAdapterFromQueryPath }); const result = simple({ name: 'hi' }, 'name', ['name']); expect(result).toMatchObject({});