Replaced usage of mongo util with nql-map-key-values

no-issue

Removes the shared module in favour of an "external" one
This commit is contained in:
Fabien O'Carroll 2019-08-12 13:09:28 +08:00
parent ef4fd4b8ef
commit 8c5f1d0ef0
4 changed files with 42 additions and 104 deletions

View File

@ -16,27 +16,21 @@ const Promise = require('bluebird'),
],
unsafeAttrs = ['author_id', 'status', 'authors'];
const mongo = require('../v2/utils/serializers/input/utils/mongo.js');
const mapNQLKeyValues = require('nql-map-key-values');
/*
* Replaces references of "page" in filters
* with the correct column "type"
*/
function replacePageWithType(mongoJSON) {
return mongo.mapKeysAndValues(mongoJSON, {
key: {
from: 'page',
to: 'type'
},
values: [{
from: false,
to: 'post'
}, {
from: true,
to: 'page'
}]
});
}
const replacePageWithType = mapNQLKeyValues({
key: {
from: 'page',
to: 'type'
},
values: [{
from: false,
to: 'post'
}, {
from: true,
to: 'page'
}]
});
function convertTypeToPage(model) {
// Respect include param

View File

@ -1,29 +1,23 @@
const _ = require('lodash');
const mapNQLKeyValues = require('nql-map-key-values');
const debug = require('ghost-ignition').debug('api:v2:utils:serializers:input:pages');
const converters = require('../../../../../lib/mobiledoc/converters');
const url = require('./utils/url');
const localUtils = require('../../index');
const mongo = require('./utils/mongo');
/*
* Replaces references of "page" in filters
* with the correct column "type"
*/
function replacePageWithType(mongoJSON) {
return mongo.mapKeysAndValues(mongoJSON, {
key: {
from: 'page',
to: 'type'
},
values: [{
from: false,
to: 'post'
}, {
from: true,
to: 'page'
}]
});
}
const replacePageWithType = mapNQLKeyValues({
key: {
from: 'page',
to: 'type'
},
values: [{
from: false,
to: 'post'
}, {
from: true,
to: 'page'
}]
});
function removeMobiledocFormat(frame) {
if (frame.options.formats && frame.options.formats.includes('mobiledoc')) {

View File

@ -1,30 +1,24 @@
const _ = require('lodash');
const mapNQLKeyValues = require('nql-map-key-values');
const debug = require('ghost-ignition').debug('api:v2:utils:serializers:input:posts');
const url = require('./utils/url');
const localUtils = require('../../index');
const labs = require('../../../../../services/labs');
const converters = require('../../../../../lib/mobiledoc/converters');
const mongo = require('./utils/mongo');
/*
* Replaces references of "page" in filters
* with the correct column "type"
*/
function replacePageWithType(mongoJSON) {
return mongo.mapKeysAndValues(mongoJSON, {
key: {
from: 'page',
to: 'type'
},
values: [{
from: false,
to: 'post'
}, {
from: true,
to: 'page'
}]
});
}
const replacePageWithType = mapNQLKeyValues({
key: {
from: 'page',
to: 'type'
},
values: [{
from: false,
to: 'post'
}, {
from: true,
to: 'page'
}]
});
function removeMobiledocFormat(frame) {
if (frame.options.formats && frame.options.formats.includes('mobiledoc')) {

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.mapKeysAndValues = mapKeysAndValues;