Skip to content

Instantly share code, notes, and snippets.

@jhyland87
Created September 26, 2016 20:36
Show Gist options
  • Save jhyland87/ab3b5682b49b47248eb32cf154ad2a32 to your computer and use it in GitHub Desktop.
Save jhyland87/ab3b5682b49b47248eb32cf154ad2a32 to your computer and use it in GitHub Desktop.
'use strict'
// Used for makeHash
//const crypto = require('crypto')
import _ from 'moar-lodash'
import Promise from 'bluebird'
import Async from 'async'
import Util from 'util'
import Mongoose from 'mongoose'
import * as appRoot from 'app-root-path'
import path from 'path'
module.exports = ( Mongoose, _ ) => {
const mixins = {}
/**
* Check is an item is a Mongoose object ID or not
*
* @param {Mixed} elem Element to check, usually either a string (not populated) or an object (populated)
* @returns {boolean}
*/
mixins.isObjectId = elem => {
return ! _.isEmpty( elem ) && !!Mongoose.Types.ObjectId.isValid( elem.toString() )
}
/**
* Retrieve the Object ID of a specific element. This is mainly used for references. When un-populated, they are just
* ObjectIds, when populated, they are documents with an _id element, which is the object Id. This will return the top
* level ObjectId, or the populated documents _id value, and converted to string; If no ObjectID is found, then
* undefined will be returned
*
* @param {object|string} elem Element to grab the _id from
* @returns {string|undefined} If no _id is found, then undefined, otherwise, the _id in string format is returned
* @todo A param to convert the ID to a string, as opposed to handing over what it is exactly (can be an object)
*/
mixins.getObjectId = elem => {
if( _.isEmpty( elem ) )
return undefined
else if( mixins.isObjectId( elem ) )
return elem.toString()
else if( _.isObject( elem ) && ! _.isUndefined( elem._id ) && mixins.isObjectId( elem._id ) )
return elem._id.toString()
else
return undefined
}
/**
* Similar to the getObjectId, except instead of a single reference, this works on elements that are an array of
* references.
*
* @param {array} elem Document item holding the array of references
* @var {object|string} elem[0] Either a populated document, or an ObjectId
* @returns {array|false|undefined} false if elem is not array; undefined if elem is empty; array of objectIDs if
* elem is an array of ObjectIds or documents; undefined if anything else
* @todo A param to convert the IDs to strings, as opposed to handing over what they are exactly (can be an objects)
*/
mixins.getObjectIds = elem => {
if( ! _.isArray( elem ) )
return false
else if( _.isEmpty( elem ) )
return undefined
else if( mixins.isObjectId( elem[0] ) )
return elem
else if( _.isObject( elem[0] ) && ! _.isUndefined( elem[0]._id ) && mixins.isObjectId( elem[0]._id ) )
return _.map( elem, e => e._id )
else
return undefined
}
/**
* Check if a specific document element reference is populated or not. The reason that _.isObject cant simply be used,
* is because if it isn't populated, the ObjectId value does register as an object. So if the element provided is an
* ObjectId, or it is NOT a JS object, then the return val will be `false`, otherwise, it's `true`
*
* @param {object|string|object} elem Element to grab verify
* @returns {boolean}
*/
mixins.isPopulated = elem => {
return ! mixins.isObjectId( elem ) || ! _.isObject( elem )
}
/**
* Convert a string (file name) to a an appropriate model name
*
* @param {string} fileName Name of file
* @returns {string} Model name to use
* @todo Should strip off any file extensions (even though thats done before it gets here)
*/
mixins.file2Model = fileName => {
return _.chain( fileName ).toLower().upperFirst().value()
}
/**
* Retrieve the type of a specific element. This basically just the _.typeof mixin, just adds the ability to check if
* an element is a Mongoose ObjectId
*
* @param {Mixed} elem Element to determine the type for
* @returns {string} Element type (undefined, null, string, boolean, array, element, date, regexp, object, number,
* function, unknown - and the newly added objectid)
*/
mixins.typeof = elem => {
if( mixins.isObjectId( elem ) )
return 'objectid'
else
return _.typeof( elem )
}
// Add these functions to Lodash as mixins
_.mixin( mixins, { 'chain': false } )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment