-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
75 lines (57 loc) · 1.86 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
var getCollection = require("./lib/getCollection")
, collectionMethods = require("./lib/collectionMethods")
, cursorMethods = require("./lib/cursorMethods")
, Replay = require("replaying")
module.exports = collection
function collection(collectionName, database, options) {
var col = Replay(collectionMethods, withCollection)
// With cursors
col.find = find
// Close db
col.close = $close
return col
function withCollection(callback) {
getCollection(collectionName, database, options, callback)
}
function find() {
var args = arguments
, cb = args[args.length - 1]
, replay = Replay(cursorMethods, getCursor)
return replay
function getCursor(callback) {
withCollection(callOnCollection)
function callOnCollection(err, collection) {
if (err) {
if (typeof cb === "function") {
cb(err)
}
return callback(err)
}
col.find = collectionMethod
var cursor = collection.find.apply(collection, args)
callback(null, cursor)
if (typeof cb === "function") {
cb(null, cursor)
}
function collectionMethod() {
return collection.find.apply(collection, arguments)
}
}
}
}
function $close() {
var args = arguments
, cb = args[args.length -1]
withCollection(closeDb)
function closeDb(err, collection) {
if (err) {
if (typeof cb === "function") {
cb(err)
}
return
}
var db = collection.db
db.close.apply(db, args)
}
}
}