0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-11 02:12:21 -05:00

Replaced nql-map-key-values with @nexes/nql (#11896)

closes #11008

- Updated @nexes/nql to 0.4.0

  This version exports the mapKeyValues utility function

- Replaced nql-map-key-values with @nexes/nql util fn

  Usage was found using `rg nql-map-key-values` and replaced globally.

- Deleted nql-map-key-values module in shared 

  Now that this module isn't referenced anywhere else, we can remove it,
  relying solely on the util exported by @nexes/nql
This commit is contained in:
Fabien 'egg' O'Carroll 2020-06-10 19:17:25 +02:00 committed by GitHub
parent 82cf095600
commit bfc03eb120
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 15 additions and 101 deletions

View file

@ -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'

View file

@ -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');

View file

@ -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');

View file

@ -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');

View file

@ -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');

View file

@ -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}
```

View file

@ -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);

View file

@ -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"
}
}

View file

@ -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",

View file

@ -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"