-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
123 lines (107 loc) · 2.88 KB
/
index.js
File metadata and controls
123 lines (107 loc) · 2.88 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
'use strict';
var _defaults = require('lodash/defaults');
var _isPlainObject = require('lodash/isPlainObject');
var Mongoose = require('mongoose');
var Schema = Mongoose.Schema;
var DEFAULT_CONFIG = {
id: false,
versionKey: false
};
/**
*
* @private
* @param {Object} base_configuration
* @param {Object} [columns]
* @returns {Schema}
*/
function createMongooseSchema(base_configuration, columns) {
var has_columns = _isPlainObject(columns);
var schema_configuration = _defaults(
{},
base_configuration.strict !== 'boolean' && {
strict: has_columns && !!Object.keys(columns).length
},
base_configuration
);
return Schema(
has_columns ?
columns :
{},
schema_configuration
);
}
/**
*
* @param {Application} app
* @param {Function} MongooseSchema
*/
exports.schema = function MongooseSchema(app, MongooseSchema) {
MongooseSchema.schema = 'mongoose';
/**
* @callback MongooseConnectionCreator
*
* @param {Mongoose} mongoose
* @returns {Mongoose|Connection}
*/
/**
* @typedef {Object} MongooseConnectionConfig {@link http://mongoosejs.com/docs/connections.html#options}
*/
/**
*
* @param {MongooseConnectionCreator|MongooseConnectionConfig|string} config
* @returns {Mongoose|Connection}
*/
MongooseSchema.driver = function(config) {
if (typeof config === 'function') {
return config.call(this, Mongoose);
}
return typeof config === 'string' ?
Mongoose.createConnection(config) :
Mongoose.createConnection(config.uri, config.options, config.callback);
};
/**
*
* @returns {Schema}
*/
MongooseSchema.prototype.schema = function() {
return this._schema;
};
/**
*
* @returns {?Model}
*/
MongooseSchema.prototype.model = function() {
return this._model;
};
/**
*
* @param {Object} model_config
*/
MongooseSchema.prototype.initialize = function(model_config) {
var collection = model_config.collection;
var name = model_config.name;
var columns = model_config.columns;
var config = model_config.config;
this._schema = createMongooseSchema(
_defaults(
{ collection: collection },
config,
DEFAULT_CONFIG
),
columns
);
this._model = null;
this.collection = collection;
this.name = name || collection;
this.statics = this._schema.statics = {};
this.methods = this._schema.methods = {};
};
/**
*
* @returns {Model}
*/
MongooseSchema.prototype.compile = function() {
this._model = this._driver.model(this.name, this._schema);
return this._model;
};
};