-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
67 lines (58 loc) · 1.55 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
var Promise = require('bluebird')
var DefaultStore = require('./mongodb')
var Query = require('./query')
var ObjectId = require('mongodb').ObjectID
var util = require('./util')
var toString = require('to-string')
var Minq = module.exports = function Minq (store) {
if (typeof store !== 'object') {
throw new TypeError('Missing required parameter: store')
}
var self = this
this.store = store
this.ready = Promise.resolve(this.store.ready)
.then(this._initialize.bind(this))
.then(function (store) {
return self
})
// override Query.ObjectId
Query.ObjectId = Minq.ObjectId
}
Minq.connect = function (connectionString, options) {
var minq = new Minq(DefaultStore.connect(connectionString, options))
return minq.ready
}
var proto = Minq.prototype
proto.disconnect = function () {
return this.store.disconnect && Promise.resolve(this.store.disconnect()) || Promise.resolve()
}
proto._initialize = function () {
var self = this
return this.store.getCollectionNames().then(function (names) {
names.forEach(function (name) {
Object.defineProperty(self, name, {
enumerable: true,
get: function () {
return self.from(name)
}
})
})
})
}
proto.from = function (collection) {
var query = new Query(this.store)
query.from(collection)
return query
}
// (any) => String|ObjectId
Minq.ObjectId = function (id) {
if (id instanceof ObjectId) {
return id
}
var strId = toString(id)
if (util.isObjectId(strId)) {
return ObjectId(strId)
}
return strId
}
Minq.Query = Query