Skip to content

Commit

Permalink
refactor: manager / utils
Browse files Browse the repository at this point in the history
  • Loading branch information
boycce committed Apr 14, 2022
1 parent 4be7a00 commit fafd7cc
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 51 deletions.
62 changes: 30 additions & 32 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,71 +12,69 @@ 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
if (uri) manager.catch(err => manager.error(err))
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') {
Expand Down
2 changes: 1 addition & 1 deletion lib/model-crud.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
38 changes: 20 additions & 18 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<fields.length; i+=1) {
const key = fields[i]
delete shallowCopy[key]
}
return target
return shallowCopy
},

parseData: function(obj) {
Expand Down Expand Up @@ -228,21 +227,24 @@ module.exports = {
}
},

pluck: function(obj, keys) {
let target = {}
for (let prop in obj) {
let match
if (!Object.prototype.hasOwnProperty.call(obj, prop)) continue
for (let key of keys) {
if (this.isString(key) && key == prop) match = true
else if (this.isRegex(key) && prop.match(key)) match = true
pick: function(obj, keys) {
// Similiar to underscore.pick
// @param {string[] | regex[]} keys
if (!this.isObject(obj) && !this.isFunction(obj)) return {}
keys = this.toArray(keys)
let res = {}
for (let key of keys) {
if (this.isString(key) && obj.hasOwnProperty(key)) res[key] = obj[key]
if (this.isRegex(key)) {
for (let key2 in obj) {
if (obj.hasOwnProperty(key2) && key2.match(key)) res[key2] = obj[key2]
}
}
if (match) target[prop] = obj[prop]
}
return target
return res
},

removeUndefined: (variable) => {
removeUndefined: function(variable) {
// takes an array or object
if (Array.isArray(variable)) {
for (let i=variable.length; i--;) {
Expand Down

0 comments on commit fafd7cc

Please sign in to comment.