Skip to content

Instantly share code, notes, and snippets.

@jhyland87
Created November 1, 2016 05:24
Show Gist options
  • Save jhyland87/29fb23cb5e80accb4b0f415ed6568f84 to your computer and use it in GitHub Desktop.
Save jhyland87/29fb23cb5e80accb4b0f415ed6568f84 to your computer and use it in GitHub Desktop.
Example of a MongooseJS model with methods used to manage a single or multiple documents
module.exports = Mongoose => {
const Schema = Mongoose.Schema
const Types = Mongoose.Types
const AssetSchema = new Schema()
// INSTANCE METHODS ---------------------------------------------------
/**
* Delete an asset
*
* @function AssetModel#delete
* @name AssetModel#delete
* @instance
* @returns {Promise} Returns a promise
*
* @example // Query for an asset, then delete it
* AssetModel.getAsset( '11111111111111111' )
* .then( assetDoc => assetDoc.delete )
* .then( data => console.log( 'Asset successfully deleted' ) )
* .catch( error => console.error( `Error: ${error}` ) )
*/
AssetSchema.methods.delete = function ( ){
}
// STATIC METHODS -----------------------------------------------------
/**
* Create one or multiple Assets. If an object is provided, then that will be interpreted as the document data for
* a single asset document, and the newly created document will be returned in the promise. If an array of objects
* is provided, then that will be interpreted as the document data for multiple asset documents, and the value
* given to the promise (on success) will be an Object of Objects, with the new document ObjectIds as the keys, and
* the document data as the value.
*
* @function AssetModel.create
* @alias AssetModel.createAsset, AssetModel.createAssets
* @param {(array|object)} data An object of document settings, or an array of objects to create multiple documents
* @returns {Promise} Returns a Bluebird promise
*
* @example // Create a single asset document
* AssetModel.createAsset( { key1: 'val1', key2: 'val2'} )
* .then( data => {
* // data is an OBJECT with the newly created ODM document data
* })
*
* @example // Create multiple asset documents
* AssetModel.createAssets( [ { key1: 'val1', key2: 'val2'}, { key1: 'val1', key2: 'val2'} ] )
* .then( data => {
* // data is an OBJECT of OBJECTS, with the new documents ObjectId as the keys, and the ODM document as
* // the values. Example:
* // {
* // 111111111111111111111111: {
* // _id: '111111111111111111111111'
* // },
* // 222222222222222222222222: {
* // _id: '222222222222222222222222'
* // }
* // }
* })
*/
AssetSchema.statics.create =
AssetSchema.statics.createAssets =
AssetSchema.statics.createAsset = function( data ) {
}
// --------------------------------------------------------------------
/**
* Retrieve a specific asset or assets by the ObjectID(s)
*
* @function AssetModel.get
* @alias AssetModel.getAsset AssetModel.getAssets
* @param {(string|array)} assetId A single document ID as a string, or multiple document IDs in an array
* @returns {Promise} Promise returned
*
* @example // Retrieve a single asset document
* AssetModel.getAsset( '111111111111111111111111' )
* .then( data => {
* // data is an OBJECT with the newly created ODM document data
* })
*
* @example // Create multiple asset documents
* AssetModel.getAssets( [ '111111111111111111111111', '222222222222222222222222' ] )
* .then( data => {
* // data is an OBJECT of OBJECTS, with the documents ObjectId as the keys, and the ODM document as
* // the values. Example:
* // {
* // 111111111111111111111111: {
* // _id: '111111111111111111111111'
* // },
* // 222222222222222222222222: {
* // _id: '222222222222222222222222'
* // }
* // }
* })
*/
AssetSchema.statics.get =
AssetSchema.statics.getAsset =
AssetSchema.statics.getAssets = function( assetId ){
}
// --------------------------------------------------------------------
/**
* Find one or more assets using the document values (not the same as `get`, which gets the documents by the known
* ObjectId value)
*
* @function AssetModel.findAsset
* @alias AssetModel.findAssets
* @param {Object} criteria The search/query data to search the 'assets' collection with
* @returns {Promise} Promise returned
*
* @example // Create multiple asset documents
* AssetModel.getAssets( { status: 'locked' } )
* .then( data => {
* // data is an OBJECT of OBJECTS, with the documents ObjectId as the keys, and the ODM document as
* // the values. Example:
* // {
* // 111111111111111111111111: {
* // _id: '111111111111111111111111'
* // },
* // 222222222222222222222222: {
* // _id: '222222222222222222222222'
* // }
* // }
* })
*/
AssetSchema.statics.findAsset =
AssetSchema.statics.findAssets = function( criteria ){
}
// --------------------------------------------------------------------
/**
* Delete a single Asset document, or multiple Asset documents by the Asset document ObjectId(s)
*
* @function AssetModel.delete
* @name AssetModel.deleteAsset AssetModel.deleteAssets
* @param {(string|array)} assetId Single asset ID as a string, or multiple asset IDs in an array
* @returns {Promise} Promise returned
*
* @example // Delete a single asset document
* AssetModel.deleteAsset( '111111111111111111111111' )
* .then( data => {
* // Not quite sure what content should be provided here.. last document revision, perhaps?
* })
*
* @example // Delete multiple asset documents
* AssetModel.deleteAssets( [ '111111111111111111111111', '222222222222222222222222' ] )
* .then( data => {
* // data is an OBJECT of OBJECTS, with the new documents ObjectId as the keys, and the ODM document as
* // the values. Example:
* // {
* // 111111111111111111111111: {
* // _id: '111111111111111111111111' // Not sure what else a delete should return..
* // },
* // 222222222222222222222222: {
* // _id: '222222222222222222222222'
* // }
* // }
* })
*/
AssetSchema.statics.delete =
AssetSchema.statics.deleteAsset =
AssetSchema.statics.deleteAssets = function( assetId ){
}
// --------------------------------------------------------------------
return Mongoose.model( ModelName, AssetSchema )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment