Skip to content

Commit

Permalink
Internal refactor to move defintion of modifierConditions closer to w…
Browse files Browse the repository at this point in the history
…here they're used
  • Loading branch information
timleslie committed Sep 30, 2019
1 parent b2c5277 commit 8a57aa8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 59 deletions.
1 change: 1 addition & 0 deletions .changeset/spicy-clouds-invite/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "releases": [{ "name": "@keystone-alpha/adapter-mongoose", "type": "patch" }], "dependents": [] }
1 change: 1 addition & 0 deletions .changeset/spicy-clouds-invite/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Internal refactor to move defintion of modifierConditions closer to where they're used.
47 changes: 0 additions & 47 deletions packages/adapter-mongoose/lib/adapter-mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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({
Expand Down
54 changes: 48 additions & 6 deletions packages/adapter-mongoose/lib/tokenizers/simple.js
Original file line number Diff line number Diff line change
@@ -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'],
Expand Down Expand Up @@ -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 };
10 changes: 4 additions & 6 deletions packages/adapter-mongoose/tests/simple.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
Expand Down

0 comments on commit 8a57aa8

Please sign in to comment.