diff --git a/core/frontend/services/url/UrlGenerator.js b/core/frontend/services/url/UrlGenerator.js index a5c7b7713a..915efbf5e2 100644 --- a/core/frontend/services/url/UrlGenerator.js +++ b/core/frontend/services/url/UrlGenerator.js @@ -24,8 +24,6 @@ const EXPANSIONS = [{ replacement: 'primary_author.slug' }]; -const mapNQLKeyValues = require('../../../shared/nql-map-key-values'); - /** * The UrlGenerator class is responsible to generate urls based on a router's conditions. * It is the component which sits between routers and resources and connects them together. @@ -49,7 +47,7 @@ class UrlGenerator { this.filter = this.router.getFilter(); this.nql = nql(this.filter, { expansions: EXPANSIONS, - transformer: mapNQLKeyValues({ + transformer: nql.utils.mapKeyValues({ key: { from: 'page', to: 'type' diff --git a/core/server/api/canary/utils/serializers/input/pages.js b/core/server/api/canary/utils/serializers/input/pages.js index f858740cb8..d4fce26deb 100644 --- a/core/server/api/canary/utils/serializers/input/pages.js +++ b/core/server/api/canary/utils/serializers/input/pages.js @@ -1,6 +1,6 @@ const _ = require('lodash'); const debug = require('ghost-ignition').debug('api:canary:utils:serializers:input:pages'); -const mapNQLKeyValues = require('../../../../../../shared/nql-map-key-values'); +const mapNQLKeyValues = require('@nexes/nql').utils.mapKeyValues; const mobiledoc = require('../../../../../lib/mobiledoc'); const url = require('./utils/url'); const localUtils = require('../../index'); diff --git a/core/server/api/canary/utils/serializers/input/posts.js b/core/server/api/canary/utils/serializers/input/posts.js index 2f5f93e167..4a955c5dc6 100644 --- a/core/server/api/canary/utils/serializers/input/posts.js +++ b/core/server/api/canary/utils/serializers/input/posts.js @@ -1,6 +1,6 @@ const _ = require('lodash'); const debug = require('ghost-ignition').debug('api:canary:utils:serializers:input:posts'); -const mapNQLKeyValues = require('../../../../../../shared/nql-map-key-values'); +const mapNQLKeyValues = require('@nexes/nql').utils.mapKeyValues; const url = require('./utils/url'); const localUtils = require('../../index'); const mobiledoc = require('../../../../../lib/mobiledoc'); diff --git a/core/server/api/v2/utils/serializers/input/pages.js b/core/server/api/v2/utils/serializers/input/pages.js index 21bc94296f..df128fd8d2 100644 --- a/core/server/api/v2/utils/serializers/input/pages.js +++ b/core/server/api/v2/utils/serializers/input/pages.js @@ -1,5 +1,5 @@ const _ = require('lodash'); -const mapNQLKeyValues = require('../../../../../../shared/nql-map-key-values'); +const mapNQLKeyValues = require('@nexes/nql').utils.mapKeyValues; const debug = require('ghost-ignition').debug('api:v2:utils:serializers:input:pages'); const mobiledoc = require('../../../../../lib/mobiledoc'); const url = require('./utils/url'); diff --git a/core/server/api/v2/utils/serializers/input/posts.js b/core/server/api/v2/utils/serializers/input/posts.js index 519137cadb..661eae8714 100644 --- a/core/server/api/v2/utils/serializers/input/posts.js +++ b/core/server/api/v2/utils/serializers/input/posts.js @@ -1,5 +1,5 @@ const _ = require('lodash'); -const mapNQLKeyValues = require('../../../../../../shared/nql-map-key-values'); +const mapNQLKeyValues = require('@nexes/nql').utils.mapKeyValues; const debug = require('ghost-ignition').debug('api:v2:utils:serializers:input:posts'); const url = require('./utils/url'); const localUtils = require('../../index'); diff --git a/core/shared/nql-map-key-values/README.md b/core/shared/nql-map-key-values/README.md deleted file mode 100644 index 1bd406117f..0000000000 --- a/core/shared/nql-map-key-values/README.md +++ /dev/null @@ -1,26 +0,0 @@ -# nql-map-key-values - -This utility returns a transformer which can be passed to the `@nexes/nql` library to transform queries - -### Usage - -```js -const nql = require('@nexes/nql'); -const mapKeyValues = require('nql-map-key-values'); - -nql('good:true', { - transformer: mapKeyValues({ - key: { - from: 'good', - to: 'bad' - }, - values: [{ - from: true, - to: false - }, { - from: false, - to: true - }] - }); -}).toJSON(); // => {bad: false} -``` diff --git a/core/shared/nql-map-key-values/index.js b/core/shared/nql-map-key-values/index.js deleted file mode 100644 index 5348c67d22..0000000000 --- a/core/shared/nql-map-key-values/index.js +++ /dev/null @@ -1,44 +0,0 @@ -const _ = require('lodash'); -const nql = require('@nexes/nql'); - -/* - * Returns the replacement value for input, or input if it doesn't exist - */ -function replaceValue(input, valueMappings) { - const replacer = valueMappings.find(({from}) => from === input); - return replacer && replacer.to || input; -} - -function fmap(item, fn) { - return Array.isArray(item) ? item.map(fn) : fn(item); -} - -function mapKeysAndValues(input, mapping) { - return nql.utils.mapQuery(input, function (value, key) { - // Ignore everything that has nothing to do with our mapping - if (key !== mapping.key.from) { - return { - [key]: value - }; - } - - // key: valueA - if (typeof value !== 'object') { - return { - [mapping.key.to]: replaceValue(value, mapping.values) - }; - } - - // key: { "$in": ['valueA', 'valueB'] } - // key: { "$ne": 'valueA' } - return { - [mapping.key.to]: _.reduce(value, (memo, objValue, objKey) => { - return Object.assign(memo, { - [objKey]: fmap(objValue, item => replaceValue(item, mapping.values)) - }); - }, {}) - }; - }); -} - -module.exports = mapping => input => mapKeysAndValues(input, mapping); diff --git a/core/shared/nql-map-key-values/package.json b/core/shared/nql-map-key-values/package.json deleted file mode 100644 index cc454e3e03..0000000000 --- a/core/shared/nql-map-key-values/package.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "name": "nql-map-key-values", - "version": "0.0.0", - "description": "Map keys and values for nql query objects", - "main": "index.js", - "repository": "https://github.com/TryGhost/Ghost/tree/master/core/shared/nql-map-key-values", - "author": "Ghost Foundation", - "license": "MIT", - "private": true, - "dependencies": { - "@nexes/nql": "0.3.0", - "lodash": "4.17.15" - } -} diff --git a/package.json b/package.json index 71a56d4fe4..aaa2a5da60 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "cli": "^1.12.0" }, "dependencies": { - "@nexes/nql": "0.3.0", + "@nexes/nql": "0.4.0", "@sentry/node": "5.17.0", "@tryghost/adapter-manager": "0.1.6", "@tryghost/errors": "0.2.0", diff --git a/yarn.lock b/yarn.lock index 2a09823ee8..05ab3da7a8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -74,10 +74,10 @@ debug "^4.1.0" lodash "^4.17.10" -"@nexes/mongo-utils@0.2.1": - version "0.2.1" - resolved "https://registry.yarnpkg.com/@nexes/mongo-utils/-/mongo-utils-0.2.1.tgz#746d004fc17741b50e945d76e9ab53496ffab15b" - integrity sha512-g/8IpnxldGbog3yTwZg1tD+6zRLbGmYDkotZiDg3O5eLxQxNXZzoP0lmv4DDfPU1Bc8sh9JlSjt+xPrOoXlIXw== +"@nexes/mongo-utils@0.3.0": + version "0.3.0" + resolved "https://registry.yarnpkg.com/@nexes/mongo-utils/-/mongo-utils-0.3.0.tgz#144c56851b3bb773839281b416964a7d2f4982c0" + integrity sha512-diYGCysU0f2+YCwGEjJTDJIM79rGjAdaP7+iFK+cXvnmnVdUOAF/lUCaHLtHkXOBCdan3v9jTGTBfgHPeBhJjA== dependencies: bluebird "^3.5.3" ghost-ignition "^2.9.6" @@ -88,13 +88,13 @@ resolved "https://registry.yarnpkg.com/@nexes/nql-lang/-/nql-lang-0.0.1.tgz#a13c023873f9bc11b9e4e284449c6cfbeccc8011" integrity sha1-oTwCOHP5vBG55OKERJxs++zMgBE= -"@nexes/nql@0.3.0": - version "0.3.0" - resolved "https://registry.yarnpkg.com/@nexes/nql/-/nql-0.3.0.tgz#12a0506b3e112561503091a9aad8cdca9784d80d" - integrity sha512-Y5DxRwoZKyk/HsX4ZDrIjcqeaR9le+x/Sfbw08/GlPiITnO4ukoqua9Cs0LgJ/cuvlo6DDDW4g+z/1y9+U0ZCA== +"@nexes/nql@0.4.0": + version "0.4.0" + resolved "https://registry.yarnpkg.com/@nexes/nql/-/nql-0.4.0.tgz#5ae28f8d339d56812eb8452ead9a5e5996087efd" + integrity sha512-fEsV2aMiPpqBneZosamFLypLAQAp8M5SA5MgeeJKMpHF0dQiHn9ifD556RgaSSY5RgO0gbRGoKypgy0YmyE1KQ== dependencies: "@nexes/mongo-knex" "0.3.0" - "@nexes/mongo-utils" "0.2.1" + "@nexes/mongo-utils" "0.3.0" "@nexes/nql-lang" "0.0.1" mingo "2.2.2"