-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
185 lines (148 loc) · 5.24 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ObjectId = undefined;
exports.default = MongoBless;
var _mongodb = require('mongodb');
var _mongodb2 = _interopRequireDefault(_mongodb);
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
var debug = require('debug')('mongobless');
var models = [];
var DB = void 0;
var Model = new Function();
var isFunction = function isFunction(obj) {
return typeof obj === 'function';
};
function extend(destination) {
var sources = Array.prototype.slice.call(arguments, 1);
var _loop = function _loop(i) {
var source = sources[i];
Object.keys(source).forEach(function (property) {
Object.defineProperty(destination, property, Object.getOwnPropertyDescriptor(source, property));
});
};
for (var i in sources) {
_loop(i);
}
return destination;
}
Model.bless = function (obj) {
if (!obj) return;
obj.__proto__ = this.prototype;
obj.constructor = this;
return obj;
};
Model.findAll = function () {
var _this = this,
_collection2;
var blessAll = function blessAll(data) {
return data.map(function (obj) {
return _this.bless(obj);
});
};
for (var _len = arguments.length, params = Array(_len), _key = 0; _key < _len; _key++) {
params[_key] = arguments[_key];
}
var fn = params[params.length - 1];
if (isFunction(fn)) {
var _collection;
var cb = function cb(err, res) {
if (err) return fn(err);
fn(null, blessAll(res));
};
(_collection = this.collection).find.apply(_collection, _toConsumableArray(params.slice(0, -1))).toArray(cb);
return this;
}
return (_collection2 = this.collection).find.apply(_collection2, params).toArray().then(blessAll);
};
Model.findOne = function () {
var _this2 = this,
_collection4;
var blessOne = function blessOne(obj) {
return _this2.bless(obj);
};
for (var _len2 = arguments.length, params = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
params[_key2] = arguments[_key2];
}
var fn = params[params.length - 1];
if (isFunction(fn)) {
var _collection3;
var cb = function cb(err, res) {
if (err) return fn(err);
fn(null, blessOne(res));
};
(_collection3 = this.collection).findOne.apply(_collection3, _toConsumableArray(params.slice(0, -1)).concat([cb]));
return this;
}
return (_collection4 = this.collection).findOne.apply(_collection4, params).then(blessOne);
};
Object.defineProperty(Model, 'collection', {
enumerable: true,
configurable: false,
get: function get() {
return this.db.collection(this.collectionName);
}
});
Model.connect = function (db) {
this.db = db;
};
Model.extendableProperties = ['connect', 'collection', 'findOne', 'findAll'];
function MongoBless() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return function (constructor) {
if (options.collection) {
if (constructor.isPersistentRoot) throw new Error('Cannot overload collection\'s name for class \'' + constructor.name + '\'');
models.push(constructor);
constructor.collectionName = options.collection;
constructor.isPersistentRoot = true;
_lodash2.default.each(Model.extendableProperties, function (prop) {
Object.defineProperty(constructor, prop, Object.getOwnPropertyDescriptor(Model, prop));
});
if (!constructor.bless) {
Object.defineProperty(constructor, 'bless', Object.getOwnPropertyDescriptor(Model, 'bless'));
}
} else {
constructor.isPersistentRoot = false;
}
};
}
MongoBless.close = function (cb) {
if (cb) return DB.close(cb);
return DB.close();
};
MongoBless.bless = function (obj) {
return Model.bless.bind(this)(obj);
};
MongoBless.connect = function (opt, cb) {
var options = _lodash2.default.extend({ host: '127.0.0.1', port: 27017, auto_reconnect: true, poolSize: 10, w: 1, strict: true, native_parser: true, verbose: true }, opt);
var mongoserver = void 0;
if (!options.replicaSet) mongoserver = new _mongodb2.default.Server(options.host, options.port, options);else {
var replicaServers = _lodash2.default.map(options.replicaSet.servers, function (server) {
return new _mongodb2.default.Server(server.host, server.port, server.options);
});
mongoserver = new _mongodb2.default.ReplSet(replicaServers, _lodash2.default.extend({}, options.replicaSet.options, { rs_name: options.replicaSet.name }));
}
var dbconnector = new _mongodb2.default.Db(options.database, mongoserver, options);
var initModels = function initModels(db) {
DB = db;
for (var i in models) {
models[i].connect(db);
}
debug("mongobless is ready for your requests ...");
return db;
};
if (cb) {
dbconnector.open(function (err, db) {
if (err) return cb(err);
initModels(db);
cb(null, db);
});
} else {
return dbconnector.open().then(initModels);
}
};
var ObjectId = exports.ObjectId = _mongodb2.default.ObjectID;