-
Notifications
You must be signed in to change notification settings - Fork 4
/
direct.coffee
126 lines (98 loc) · 3.66 KB
/
direct.coffee
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
import Future from 'fibers/future'
export class DirectCollection
connections = {}
constructor: (@name, @_makeNewID, @_databaseUrl) ->
unless @_makeNewID
@_makeNewID = -> Random.id()
findToArray: (selector, options) =>
options = {} unless options
cursor = @_getCollection().find(selector, options)
blocking(cursor, cursor.toArray)()
findEach: (selector, options, eachCallback) =>
if _.isFunction options
eachCallback = options
options = {}
options = {} unless options
cursor = @_getCollection().find(selector, options)
nextObject = blocking(cursor, cursor.next or cursor.nextObject)
while document = nextObject()
eachCallback document
return
count: (selector, options) =>
options = {} unless options
collection = @_getCollection()
blocking(collection, collection.count)(selector, options)
findOne: (selector, options) =>
options = {} unless options
collection = @_getCollection()
blocking(collection, collection.findOne)(selector, options)
insert: (document) =>
unless '_id' of document
document = EJSON.clone document
document._id = @_makeNewID()
collection = @_getCollection()
blocking(collection, collection.insert)(document, w: 1)
return document._id
update: (selector, modifier, options) =>
options = {} unless options
options.w ?= 1
collection = @_getCollection()
result = blocking(collection, collection.update)(selector, modifier, options)
result = result.result if _.isObject(result) and result.result
return result if options._returnObject or not _.isObject result
result.n
remove: (selector, options) =>
options = {} unless options
options.w ?= 1
collection = @_getCollection()
result = blocking(collection, collection.remove)(selector, options)
result = result.result if _.isObject(result) and result.result
return result if options._returnObject or not _.isObject result
result.n
renameCollection: (newName, options) =>
options = {} unless options
collection = @_getCollection()
blocking(collection, collection.rename)(newName, options)
findAndModify: (selector, sort, document, options) =>
options = {} unless options
options.w ?= 1
collection = @_getCollection()
blocking(collection, collection.findAndModify)(selector, sort, document, options)
createIndex: (fieldOrSpec, options) =>
options = {} unless options
options.w ?= 1
collection = @_getCollection()
blocking(collection, collection.createIndex)(fieldOrSpec, options)
dropIndex: (indexName, options) =>
options = {} unless options
options.w ?= 1
collection = @_getCollection()
blocking(collection, collection.dropIndex)(indexName, options)
@_getConnection: (databaseUrl) ->
if databaseUrl?
if not connections[databaseUrl]
connections[databaseUrl] = new MongoInternals.RemoteCollectionDriver databaseUrl, {}
connections[databaseUrl]
else
MongoInternals.defaultRemoteCollectionDriver()
_getCollection: =>
mongo = @constructor._getConnection(@_databaseUrl).mongo
if mongo.rawCollection
mongo.rawCollection @name
else
# _getCollection is for Meteor < 1.0.4.
mongo._getCollection @name
@_getDb: (databaseUrl) ->
mongo = @_getConnection(databaseUrl).mongo
# _withDb is for Meteor < 1.0.4.
if mongo._withDb
future = new Future()
@_getConnection(databaseUrl).mongo._withDb (db) ->
future.return db
future.wait()
else
mongo.db
@command: (selector, options, databaseUrl) ->
options = {} unless options
db = @_getDb databaseUrl
blocking(db, db.command)(selector, options)