Skip to content

Instantly share code, notes, and snippets.

@jhyland87
Last active February 9, 2016 00:21
Show Gist options
  • Save jhyland87/10de6d5bf408d7c5c1ad to your computer and use it in GitHub Desktop.
Save jhyland87/10de6d5bf408d7c5c1ad to your computer and use it in GitHub Desktop.
Example of a mongoose model
'use strict'
module.exports = Mongoose => {
const Schema = Mongoose.Schema
const fooSchema = new Schema({
name: {
type: Schema.Types.String,
trim: true,
select: true,
unique: true,
required: true,
minlength: 4,
maxlength: 30
},
description: Schema.Types.String,
status: {
type: Schema.Types.Boolean,
default: true
},
_groups: [{
type: Schema.Types.ObjectId,
ref: 'Group'
}],
_fields: [{
type: Schema.Types.ObjectId,
ref: 'Field'
}]
}, {
timestamps: {
createdAt: 'createdAt',
updatedAt: 'updatedAt'
}
})
//----------------------------------------
// INSTANCE METHODS
/**
* Verify an attribute value against the corresponding field settings in the assets partition
*
* @param {object|string} options Object containing the attribute/field name and 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)
* @returns {boolean}
*/
fooSchema.methods.verifyAttr = function( options, callback ){
}
/**
* 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
*/
fooSchema.methods.getFieldIdsByName = function( fieldNames ){
}
/**
* Add asset to associated partition
*
* @param {array} attributes Array of objects (Attributes)
* @param {function} callback Callback to execute (As opposed to a Promise)
* @returns {Promise}
*/
fooSchema.methods.createAsset = function ( attributes, callback ) {
}
/**
* Create multiple assets asynchronously
*
* @param {array|number} attributes Either an array of objects (each separate object are the attributes for a
* new asset), or a number of how many new attributes to create (which will
* attempted to be created with no attributes, and may throw errors, depending
* on the partition field settings)
* @param {function} callback Callback to execute, if not handling as a promise
* @returns {Promise} Bluebird promise, or callback gets executed
*/
fooSchema.methods.createAssets = function ( attributes, callback ) {
}
/**
* Delete assets from associated partition, this will allow the deletion of multiple
* assets, without having to create new asset objects
*
* @param {array|number} assetIds Single asset ID or array of asset IDs
* @param {boolean} requireDelete If true, then a failure will be returned when an asset
* is not deleted (for any reason, such as it doesn't exist
* in the first place)
* @return {Promise} On success, an array of asset data is returned, if requireDelete is false,
* and no assets were found/deleted, then null is returned
*/
fooSchema.methods.deleteAssets = function ( assetIds, requireDelete = false ) {
}
//----------------------------------------
// STATIC METHODS
/**
* Retrieve select Partition Field ID's by the field names (STATIC METHOD)
*
* @param {object} partitionId Partition object
* @param {string|array|object} fieldNames String of field names, or array of multiple
* @param {function} callback Callback to execute (As opposed to a Promise)
* @return {object} Object of field IDs and Names, with the field names as the keys
* @todo Should this be a methid instead of a static?
*/
fooSchema.statics.getFieldIdsByName = function( partitionId, fieldNames, callback ) {
}
/**
* Get partition data, with or without the fields populated
*
* @param {string} partitionId Partition ID to retrieve data for
* @return {Promise} Returns a promise with a Mongoose model as the only param
* @todo Add parameters and population for the groups
*/
fooSchema.statics.getPartition = function( partitionId ) {
}
/**
* Retrieve a partition and its associated assets
*
* @param {string} partitionId Partition ID to query for
* @return {Promise} Only parameter in promise is an object with all the partitions
* data and assets
*/
fooSchema.statics.getPartitionAssets = function( partitionId ) {
}
/**
* Create a new Partition, including the partitions fields
*
* @param {object} partitionData Object of the partition data, should contain:
* @var {string} partitionData.name Name of partition
* @var {boolean} partitionData.status Status of the partition (Default: true)
* @var {string} partitionData.description Partition description
* @var {array} partitionData.groups List of permitted group IDs
* @var {array} partitionData.fields Field configs (passed to Field model)
* @return {Promise}
*/
fooSchema.statics.createPartition = function( partitionData ) {
}
/**
* Delete a partition by partition ID
*
* @param {string} partitionId Partition ID to delete
* @param {boolean} requireDelete If true, then a failure will be returned when an asset
* is not deleted (for any reason, such as it doesn't exist
* in the first place)
* @returns {Promise}
* @todo Allow deletion by partition name - Check if partitionId is not numeric, then go by name
*/
fooSchema.statics.deletePartition = function( partitionId, requireDelete = false ) {
}
return Mongoose.model( 'Partition', fooSchema )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment