From fafd7cc011c2ede39bff8795819f1629b2f1e0ea Mon Sep 17 00:00:00 2001 From: Ricky Boyce Date: Fri, 15 Apr 2022 10:19:51 +1200 Subject: [PATCH] refactor: manager / utils --- lib/index.js | 62 +++++++++++++++++++++++------------------------ lib/model-crud.js | 2 +- lib/util.js | 38 +++++++++++++++-------------- 3 files changed, 51 insertions(+), 51 deletions(-) diff --git a/lib/index.js b/lib/index.js index 036d528..43db0bb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -12,59 +12,51 @@ module.exports = function(uri, opts, fn) { * @param {object} opts * @return monk manager */ + let monasteryOpts = [ + 'defaultObjects', 'imagePlugin', 'limit', 'nullObjects', 'timestamps', 'useMilliseconds' + ] + if (!opts) opts = {} if (util.isDefined(opts.defaultFields)) { var depreciationWarningDefaultField = true opts.timestamps = opts.defaultFields + delete opts.defaultFields + } + if (!util.isDefined(opts.timestamps)) { + opts.timestamps = true } - let defaultObjects = opts.defaultObjects - let imagePlugin = opts.imagePlugin - let limit = opts.limit - let nullObjects = opts.nullObjects - let timestamps = util.isDefined(opts.timestamps)? opts.timestamps : true - let useMilliseconds = opts.useMilliseconds - delete opts.defaultFields - delete opts.defaultObjects - delete opts.imagePlugin - delete opts.limit - delete opts.nullObjects - delete opts.timestamps - delete opts.useMilliseconds - // Monk Manager instance or manager mock + // Monk manager instance or manager mock // Monk manager instances have manager._db defined which is the raw mongodb connection if (typeof uri === 'object') var manager = uri - else if (uri) manager = monk(uri, { useUnifiedTopology: true, ...opts }, fn) + else if (uri) manager = monk(uri, { useUnifiedTopology: true, ...util.omit(opts, monasteryOpts) }, fn) else manager = { id: monk.id } // Add monastery properties - manager.error = debug('monastery:error*') - manager.warn = debug('monastery:warn') - manager.info = debug('monastery:info') - manager.model = require('./model') - manager.models = models - manager.defaultObjects = defaultObjects - manager.imagePlugin = imagePlugin + manager.arrayWithSchema = arrayWithSchema + manager.beforeModel = [] manager.imagePluginFile = require('../plugins/images') manager.isId = util.isId.bind(util) - manager.limit = limit - manager.nullObjects = nullObjects + manager.model = require('./model') + manager.models = models manager.parseData = util.parseData.bind(util) - manager.timestamps = timestamps - manager.useMilliseconds = useMilliseconds - manager.beforeModel = [] - manager.arrayWithSchema = manager.arraySchema = (array, schema) => { - array.schema = schema; return array + manager.warn = debug('monastery:warn') + manager.error = debug('monastery:error*') + manager.info = debug('monastery:info') + + // Add opts onto manager + for (let key of monasteryOpts) { + manager[key] = monasteryOpts[key] } // Depreciation warnings if (depreciationWarningDefaultField) { - manager.error('manager.defaultFields has been depreciated in favour of manager.timestamps') + manager.error('opts.defaultFields has been depreciated in favour of opts.timestamps') } // Initiate any plugins if (manager.imagePlugin) { - manager.imagePluginFile.setup(manager, util.isObject(imagePlugin)? imagePlugin : {}) + manager.imagePluginFile.setup(manager, util.isObject(manager.imagePlugin)? manager.imagePlugin : {}) } // Catch mongodb connectivity errors @@ -72,11 +64,17 @@ module.exports = function(uri, opts, fn) { return manager } -function models(path) { +let arrayWithSchema = function(array, schema) { + array.schema = schema + return array +} + +let models = function(path) { /** * Setup model definitions from a folder location * @param {string} pathname * @return {object} - e.g. { user: , article: , .. } + * @this Manager */ let models = {} if (!path || typeof path !== 'string') { diff --git a/lib/model-crud.js b/lib/model-crud.js index cb5e167..c353da4 100644 --- a/lib/model-crud.js +++ b/lib/model-crud.js @@ -181,8 +181,8 @@ module.exports = { opts = await this._queryObject(opts, 'update') let data = opts.data let response = null - let operators = util.pluck(opts, [/^\$/]) let custom = ['blacklist', 'data', 'query', 'respond', 'skipValidation', 'validateUndefined'] + let operators = util.pick(opts, [/^\$/]) // Validate if (util.isDefined(data)) data = await this.validate(opts.data, { ...opts }) diff --git a/lib/util.js b/lib/util.js index 7923e4f..ec7b41f 100644 --- a/lib/util.js +++ b/lib/util.js @@ -140,14 +140,13 @@ module.exports = { return typeof value === 'undefined' }, - omit: function(obj, keys) { - let target = {} - for (let i in obj) { - if (keys.indexOf(i) >= 0) continue - if (!Object.prototype.hasOwnProperty.call(obj, i)) continue - target[i] = obj[i] + omit: function(obj, fields) { + const shallowCopy = Object.assign({}, obj) + for (let i=0; i { + removeUndefined: function(variable) { // takes an array or object if (Array.isArray(variable)) { for (let i=variable.length; i--;) {