Created
September 22, 2016 17:56
-
-
Save jhyland87/700235ca98026698e5e19daeb8871ceb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module.exports = Mongoose => { | |
const Schema = Mongoose.Schema | |
const modelSchema = new Schema({ | |
name: { | |
type: Schema.Types.String, | |
trim: true | |
}, | |
description: Schema.Types.String | |
}, { | |
timestamps: { | |
createdAt: 'createdAt', | |
updatedAt: 'updatedAt' | |
} | |
}) | |
// MIDDLEWARE --------------------------------------------------------- | |
/** | |
* Since most of the instance methods, static methods and virtual properties rely on the partition fields, | |
* automatically populate the _fields reference to the field model documents for every find-like query | |
*/ | |
_.forEach( [ 'find', 'findOne', 'findOneById', 'findOneAndRemove', 'findOneAndUpdate' ], query => { | |
modelSchema.pre( query, function( next ) { | |
this.populate( '_fields' ) | |
next() | |
}) | |
}) | |
// VIRTUAL PROPERTIES ------------------------------------------------- | |
/** | |
* Retrieve the primary field for said partition | |
* | |
* @returns {object} Returns the entire field, if the primary is true | |
*/ | |
modelSchema.virtual('primaryField').get(function() { | |
const primary = _.find( this._fields, f => !!f.primary ) | |
return ! _.isEmpty( primary ) | |
? primary | |
: undefined | |
}) | |
// INSTANCE METHODS --------------------------------------------------- | |
/** | |
* Verify an assets attribute value against the corresponding field settings in the assets partition | |
* | |
* @param {object|string} options Object containing the attribute/field name & value, or the attribute | |
* name/field ID (Which if provided as a string, value will be | |
* automatically nulled, since it cant be defined anywhere else | |
* @param {function} callback Callback to be executed (if passed) | |
* @var {string} options.attr Name attribute or ID of field | |
* @var {Mixed} options.value Value of attribute (Null if undefined) | |
* @var {string|ObjectId} options.assetId If the attribute value is being validated while updating the asset | |
* (as opposed to creating), then set the asset ID (This is required | |
* for some validations such as unique) | |
* @returns {boolean} | |
*/ | |
modelSchema.methods.verifyAttr = function( options, callback ) { | |
// .... removed | |
} | |
// -------------------------------------------------------------------- | |
/** | |
* Retrieve select Partition Field ID's by the field names (ORM METHOD) | |
* | |
* @param {string|array} fieldNames String of field names, or array of multiple. | |
* @return {object} Object of field IDs and Names, with the field names as the keys | |
*/ | |
modelSchema.methods.getFieldIdsByName = function( fieldNames ) { | |
// .... removed | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment