forked from kissjs/node-mongoskin
-
Notifications
You must be signed in to change notification settings - Fork 4
/
db.js
205 lines (184 loc) · 5.09 KB
/
db.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
var __slice = Array.prototype.slice,
mongodb = require('mongodb'),
events = require('events'),
utils = require('./utils'),
SkinAdmin = require('./admin').SkinAdmin,
SkinCollection = require('./collection').SkinCollection,
SkinGridStore = require('./gridfs').SkinGridStore,
Db = mongodb.Db,
STATE_CLOSE = 0,
STATE_OPENNING = 1,
STATE_OPEN = 2;
var _extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
};
var SkinDb = exports.SkinDb = function(db, username, password) {
this.db = db;
this.username = username;
this.password = password;
this.state = STATE_CLOSE;
this.emitter = new events.EventEmitter();
this.admin = new SkinAdmin(this);
this._collections = {};
this.bson_serializer = db.bson_serializer;
this.ObjectID = mongodb.ObjectID /* 0.9.7-3-2 */ || db.bson_serializer.ObjectID /* <= 0.9.7 */;
};
SkinDb.prototype.toObjectID = SkinDb.prototype.toId = function(hex) {
if(hex instanceof this.ObjectID) {
return hex;
}
return this.ObjectID.createFromHexString(hex);
};
/**
* retrieve native_db
*
* @param fn function(err, native_db)
*
*/
SkinDb.prototype.open = function(fn) {
switch (this.state) {
case STATE_OPEN:
return fn(null, this.db);
case STATE_OPENNING:
// if call 'open' method multi times before opened
return this.emitter.once('open', fn);
case STATE_CLOSE:
default:
var that = this;
var onDbOpen = function(err, db) {
if (!err && db) {
that.state = STATE_OPEN;
that.db = db;
}else {
db = null;
that.state = STATE_CLOSE;
}
that.emitter.emit('open', err, db);
};
this.emitter.once('open', fn);
this.state = STATE_OPENNING;
this.db.open(function(err, db) {
if (db && that.username) {
//do authenticate
db.authenticate(that.username, that.password, function(err) {
onDbOpen(err, db);
});
} else {
onDbOpen(err, db);
}
});
}
};
/**
* Close skinDb
*/
SkinDb.prototype.close = function(callback) {
if (this.state === STATE_CLOSE) {
return callback && callback();
}else if (this.state === STATE_OPEN) {
this.state = STATE_CLOSE;
this.db.close(callback);
}else if (this.state === STATE_OPENNING) {
var that = this;
this.emitter.once('open', function(err, db) {
that.state = STATE_CLOSE;
db.close(callback);
});
}
};
/**
* create or retrieval skin collection
*/
SkinDb.prototype.collection = function(name) {
var collection = this._collections[name];
if (!collection) {
this._collections[name] = collection = new SkinCollection(this, name);
}
return collection;
};
/**
* gridfs
*/
SkinDb.prototype.gridfs = function() {
return this.skinGridStore || (this.skinGridStore = new SkinGridStore(this));
}
/**
* bind additional method to SkinCollection
*
* 1. collectionName
* 2. collectionName, extends1, extends2,... extendsn
* 3. collectionName, SkinCollection
*/
SkinDb.prototype.bind = function() {
var args = __slice.call(arguments),
name = args[0];
if (typeof name !== 'string' || name.length === 0) {
throw new Error('Must provide name parameter for bind.');
}
if (args.length === 1) {
return this.bind(name, this.collection(name));
}
if (args.length === 2 && args[1].constructor === SkinCollection) {
this._collections[name] = args[1];
Object.defineProperty(this, name, {
value: args[1],
writable: false,
enumerable: true
});
// support bind for system.js
var names = name.split('.');
if(names.length > 1){
var prev = this, next;
for(var i =0; i<names.length - 1; i++){
next = prev[names[i]];
if(!next){
next = {};
Object.defineProperty(prev, names[i], {
value : next
, writable : false
, enumerable : true
});
}
prev = next;
}
Object.defineProperty(prev, names[names.length - 1], {
value : args[1]
, writable : false
, enumerable : true
});
}
return args[1];
}
for (var i = 1, len = args.length; i < len; i++) {
if (typeof args[i] != 'object')
throw new Error('the arg' + i + ' should be object, but is ' + args[i]);
}
var coll = this.collection(name);
for (var i = 0, len = args.length; i < len; i++) {
_extend(coll, args[i]);
}
return this.bind(name, coll);
};
var bindSkin = function(name, method) {
return SkinDb.prototype[name] = function() {
var args = arguments.length > 0 ? __slice.call(arguments, 0) : [];
return this.open(function(err, db) {
if (err) {
utils.error(err, args, 'SkinDb.' + name);
} else {
return method.apply(db, args);
}
});
};
};
//bind method of mongodb.Db to SkinDb
for (var name in Db.prototype) {
if(!name || name[0] == '_' || name == 'state') continue;
var method = Db.prototype[name];
if (name !== 'bind' && name !== 'open' && name !== 'collection' && name !== 'admin') {
bindSkin(name, method);
}
}