Skip to content

Instantly share code, notes, and snippets.

@michelalbers
Last active August 29, 2015 14:07
Show Gist options
  • Save michelalbers/6e3851d503ab371dba69 to your computer and use it in GitHub Desktop.
Save michelalbers/6e3851d503ab371dba69 to your computer and use it in GitHub Desktop.
// Generated by CoffeeScript 1.7.1
var Model, bcrypt, lodash, mongoose;
mongoose = require("mongoose");
bcrypt = require("bcrypt");
lodash = require("lodash");
Model = (function(mongo, crypt, _) {
var mongooseSchema;
mongooseSchema = {};
return {
/**
* Add a mongoose relation to a mongooseSchema
* @param {String} propertyname under which the relation will be stored
* @param {Boolean} is this a required property?
* @param {Boolean} is this a toMany relation?
*/
addRelation: function(propertyName, to, required, toMany) {
if ((toMany != null) === true) {
mongooseSchema[propertyName] = [
{
type: mongo.Schema.ObjectId,
ref: to,
required: required != null
}
];
} else {
mongooseSchema[propertyName] = {
type: mongo.Schema.ObjectId,
ref: to,
required: required != null
};
}
},
/**
* Sets a property to the mongooseSchema
* @param {String} propertyname under which the relation will be stored
* @param {Object} Configuration for this property
*/
setProperty: function(propertyName, config) {
mongooseSchema[propertyName] = config;
},
/**
* Sets an object of properties to the mongooseSchema
* @param {Object} Configuration for some properties
*/
setProperties: function(propertiesConfig) {
_.assign(mongooseSchema, propertiesConfig);
},
/**
* Renders the mongoose.Schema
* @return {Object} the mongoose.Schema instance
*/
instanciateSchema: function() {
var mongooseSchemaObj;
mongooseSchemaObj = new mongo.Schema(mongooseSchema);
return mongooseSchemaObj;
},
/**
* Compiles the mongoose model
* @param {String} Model name
* @param {Object} mongoose.Schema to be used (optional)
* @return {Object} mongoose.Model
*/
compileModel: function(name, ms) {
if (ms != null) {
return mongo.model(name, ms);
} else {
return mongo.model(name, this.instanciateSchema(mongooseSchema));
}
},
utilityFunctions: {
/**
* Converts a string into lower case
* @param {String} String to be converted
*/
toLower: function(v) {
if (v.toLowerCase) {
return v.toLowerCase();
} else {
return v;
}
}
}
};
})(mongoose, bcrypt, lodash);
module.exports = Model;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment