-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
68 lines (66 loc) · 2.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
'use strict';
var mongoose = require('mongoose');
var ParamController = require('./param.controller');
var _ = require('lodash');
var log4js = require('log4js');
log4js.levels.forName('AUDIT',50001);
var logger = process.env.PROD_ENV?log4js.getLogger('swagger-mongoose-crud'):log4js.getLogger('swagger-mongoose-crud-dev');
var params = require('./swagger.params.map');
var uniqueValidator = require('mongoose-unique-validator');
/**
* Constructor function for MongooseModel
* @classdesc Basic mongoose Model sytem
* Uses the definition passed on the by the Input object for creating crud operations
* @constructor
* @inherits ParamController
* @param {Object} schema - Schema for mongoose object.
* @param {String} modelName - Model name to which data needs to be populated.
* @param {Object} options - optional options object. Takes 2 values - logger and collectionName
*/
function MongooseModel(schema,modelName,options) {
this.schema = injectDefaults(schema);
logger = options.logger?options.logger:logger;
schema.plugin(uniqueValidator);
this.model = mongoose.model(modelName, this.schema, options.collectionName);
ParamController.call(this, this.model, modelName,logger);
this.index = this._index.bind(this);
this.create = this._create.bind(this);
this.show = this._show.bind(this);
this.update = this._update.bind(this);
this.destroy = this._destroy.bind(this);
this.rucc = this._rucc.bind(this);
this.count = this._count.bind(this);
this.bulkUpdate = this._bulkUpdate.bind(this);
this.bulkUpload = this._bulkUpload.bind(this);
this.bulkShow = this._bulkShow.bind(this);
this.markAsDeleted = this._markAsDeleted.bind(this);
logger.trace('Initialised Mongoose Model');
}
MongooseModel.prototype = {
constructor: MongooseModel,
model: null,
schema: null,
definition: null,
swagMapper: params.map
};
function injectDefaults(schema){
schema.add( {createdAt : {
type:Date,
default:Date.now
}});
schema.add( {lastUpdated : {
type:Date,
default:Date.now
}});
schema.add( {deleted : {
type:Boolean,
default:false
}});
schema.index({lastUpdated:1});
schema.index({createdAt:1});
schema.pre('save',function(next){this.lastUpdated = new Date();next();});
schema.pre('update',function(next){this.lastUpdated = new Date();next();});
return schema;
}
MongooseModel.prototype = _.create(ParamController.prototype,MongooseModel.prototype);
exports = module.exports = MongooseModel.bind(MongooseModel);