-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
097056d
commit a2676e3
Showing
7 changed files
with
159 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* Module dependencies | ||
*/ | ||
const path = require('path'); | ||
const _ = require('lodash'); | ||
|
||
const AppError = require(path.resolve('./lib/helpers/AppError')); | ||
const tricks = require(path.resolve('./lib/helpers/tricks')); | ||
|
||
/** | ||
* @desc Function to prepare data for mongo save and historisation, all value became [{updatedAT value }] | ||
* @param {object} object - object to prepare | ||
* @param {date} date - date of update | ||
* @return {object} object - object edited | ||
*/ | ||
const prepareSave = (object, date) => { | ||
const result = {}; | ||
const keys = tricks.objectDeepKeys(object); | ||
keys.forEach((k) => { | ||
const value = _.get(object, k); | ||
if (k.charAt(0) === '@') _.set(result, k, value); | ||
else if (!Array.isArray(value) && typeof value !== 'object') _.set(result, k, [{ updatedAt: date, value }]); | ||
}); | ||
return result; | ||
}; | ||
|
||
/** | ||
* @desc Function to transform an object in an array of object to blukwrite object in mongo | ||
* @param {object} object - object to prepare | ||
* @return {[object]} [object] - array of action for insert in mongoose | ||
*/ | ||
const prepareMongoose = (object) => { | ||
const filter = {}; | ||
const arrays = []; | ||
const edits = []; | ||
const keys = tricks.objectDeepKeys(object); | ||
// ini filter base | ||
keys.forEach((k) => { | ||
if (k.charAt(0) === '@') _.set(filter, k, _.get(object, k)); | ||
}); | ||
// set edits | ||
keys.forEach((k) => { | ||
const value = _.get(object, k); | ||
const paths = k.split('.'); | ||
const lastPath = paths[paths.length - 1]; | ||
// if we are abd array or in dend of line | ||
if (lastPath !== 'value' && lastPath !== 'updatedAt' && Array.isArray(value)) { | ||
// set base edition | ||
const edit = { | ||
filter: _.clone(filter), | ||
update: {}, | ||
}; | ||
// if we aren't in end of line, but an intermediate array (mongo dot notation create object if null, not array ...) | ||
if (!value[0].updatedAt) { | ||
edit.filter[k] = { $exists: false }; | ||
edit.update.$set = {}; | ||
edit.update.$set[`${k}`] = []; | ||
edit.upsert = false; | ||
arrays.push(edit); | ||
} else { | ||
// if we are in end of line, setup update of value if different | ||
edit.filter[`${k}.0.value`] = { $ne: value[0].value }; | ||
edit.update.$push = {}; | ||
edit.update.$push[`${k}`] = {}; | ||
edit.update.$push[`${k}`].$each = [value[0]]; | ||
edit.update.$push[`${k}`].$position = -1; | ||
edit.upsert = false; | ||
edits.push(edit); | ||
} | ||
} | ||
}); | ||
|
||
const init = { | ||
filter, | ||
update: { | ||
$set: filter, | ||
}, | ||
upsert: true, | ||
}; | ||
|
||
return [init, arrays, edits]; | ||
}; | ||
|
||
|
||
/** | ||
* @desc saving | ||
*/ | ||
exports.prepare = (json, date) => { | ||
if (Array.isArray(json)) { | ||
return json.map((j) => prepareSave(j, date)); | ||
} if (typeof example === 'object' && typeof data === 'object') { | ||
return prepareSave(json, date); | ||
} | ||
throw new AppError('Saving failed', { code: 'HELPERS_ERROR' }); | ||
}; | ||
|
||
/** | ||
* @desc saving | ||
*/ | ||
exports.save = (json, date) => { | ||
if (Array.isArray(json)) { | ||
return _.flattenDeep(json.map((j) => prepareMongoose(j, date))); | ||
} if (typeof example === 'object' && typeof data === 'object') { | ||
return prepareMongoose(json, date); | ||
} | ||
throw new AppError('Saving failed', { code: 'HELPERS_ERROR' }); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* @desc generate list of all objects keys | ||
* @param {Object} object | ||
* @return {[String]} array of path | ||
*/ | ||
exports.objectDeepKeys = (obj) => Object.keys(obj).filter((key) => obj[key] instanceof Object).map((key) => this.objectDeepKeys(obj[key]).map((k) => `${key}.${k}`)).reduce((x, y) => x.concat(y), Object.keys(obj)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters