/
index.coffee
165 lines (127 loc) · 4.72 KB
/
index.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
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
mongodb = require 'mongodb'
{Db, Server} = mongodb
class EasyMongo
db: null
collection: {}
constructor: (@options) ->
@options.host = '127.0.0.1' unless @options.host?
@options.port = 27017 unless @options.port?
getInstance: (table, after) ->
throw new Error 'The database name must be configured (options.db)' unless @options.db?
if @db isnt null and @db.state and @db.state is 'connected'
@getCollection table, after
else
server = new Server @options.host, @options.port, auto_reconnect: true
instance = new Db @options.db, server, safe: true
instance.open (error, db) =>
console.log "Error with connection to MongoDB server: #{error}" if error
@db = db
@getCollection table, after
getCollection: (table, after) ->
if @collection[table]?
after @collection[table]
else
@db.collection table, (error, collection) =>
console.log "Error with fetching collection: #{error}" if error
@collection[table] = collection
after collection
close: ->
if @db isnt null
@collection = {} if @collection isnt {}
@db.close()
@db = null
find: (table, params, options, after) ->
[params, options, after] = normalizeArguments params, options, after
try
if params?._id?
if isObject(params._id) and params._id.$in?
params._id.$in = params._id.$in.map (value) -> ensureObjectId value
else
params._id = ensureObjectId params._id
catch exception
console.log "Error with preparing params for find: #{exception}"
return after []
@getInstance table, (collection) =>
cursor = collection.find params
cursor.sort options.sort if options.sort
cursor.limit options.limit if options.limit
cursor.skip options.skip if options.skip
cursor.toArray (error, results) =>
if error
console.log "Error with fetching documents: #{error}"
return after []
after results
save: (table, params, after = ->) ->
try
params._id = ensureObjectId params._id if params._id?
catch exception
console.log "Error with preparing params for save: #{exception}"
return after false
@getInstance table, (collection) =>
collection.save params, safe: true, (error, results) =>
if error
console.log "Error with saving data: #{error}"
return after false
after if results is 1 then params else results
count: (table, params, after) ->
if isFunction params
after = params
params = null
after = (->) if after is null
@getInstance table, (collection) =>
collection.count params, (error, results) =>
if error
console.log "Error with fetching counts: #{error}"
return after false
after parseInt results, 10
findById: (table, id, after = ->) ->
try
params = _id: ensureObjectId id
catch exception
console.log "Error with preparing params for findById: #{exception}"
return after false
@getInstance table, (collection) =>
collection.find(params).toArray (error, results) =>
if error
console.log "Error with fetching document by id: #{error}"
return after false
after if results and results.length is 1 then results[0] else false
removeById: (table, id, after = ->) ->
try
params = _id: ensureObjectId id
catch exception
console.log "Error with preparing params for removeById: #{exception}"
return after false
@getInstance table, (collection) =>
collection.findAndRemove params, (error, results) =>
if error
console.log "Error with removing document by id: #{error}"
return after false
after results
Long: (number) -> new mongodb.Long number
ObjectID: (hex) -> ensureObjectId hex
Timestamp: -> new mongodb.Timestamp()
DBRef: (collection, id) -> new mongodb.DBRef collection, id
Binary: (buffer) -> new mongodb.Binary buffer
Symbol: (string) -> new mongodb.Symbol string
MinKey: -> new mongodb.MinKey()
MaxKey: -> new mongodb.MaxKey()
Double: (number) -> new mongodb.Double number
ensureObjectId = (id) ->
if typeof id is 'string' then new mongodb.ObjectID id else id
isFunction = (obj) ->
toString.call(obj) is '[object Function]'
isObject = (obj) ->
obj is Object obj
normalizeArguments = (params, options, after) ->
if isFunction params
after = params
params = null
options = {}
if isFunction options
after = options
options = {}
after = (->) if not after
options = {} if not options
[params, options, after]
module.exports = EasyMongo