Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 164 lines (125 sloc) 4.685 kb
9011be1 @meritt Adding non-json param aliases.
authored
1 mongodb = require 'mongodb'
2 {Db, Server} = mongodb
4c2b6e5 @meritt Initial commit
authored
3
4 ensureObjectId = (id) ->
9011be1 @meritt Adding non-json param aliases.
authored
5 if typeof id is 'string' then new mongodb.ObjectID id else id
4c2b6e5 @meritt Initial commit
authored
6
d736f46 @meritt Added abilities to configure sort/limit/skip parameters for find.
authored
7 isFunction = (obj) ->
8 toString.call(obj) is '[object Function]'
9
8044ee8 @meritt Added the ability find documents by the array of _id
authored
10 isObject = (obj) ->
11 obj is Object obj
12
4c2b6e5 @meritt Initial commit
authored
13 class EasyMongo
6b76a7c @meritt Added ability to re-use db and collection instance.
authored
14 db: null
15
064f348 @meritt Rethink auto closing connection with mongo. At now by default connection...
authored
16 collection: {}
6b76a7c @meritt Added ability to re-use db and collection instance.
authored
17
1383e1a @meritt Fix constructor spelling
authored
18 constructor: (@options) ->
4c2b6e5 @meritt Initial commit
authored
19 @options.host = '127.0.0.1' unless @options.host?
20 @options.port = 27017 unless @options.port?
21
22 getInstance: (table, after) ->
23 throw new Error 'The database name must be configured (options.db)' unless @options.db?
24
498287d @meritt Checking params before connecting to mongo
authored
25 if @db isnt null and @db.state and @db.state is 'connected'
54c81d1 @meritt Ensure @db in state connected when trying reuse it.
authored
26 @getCollection table, after
6b76a7c @meritt Added ability to re-use db and collection instance.
authored
27 else
064f348 @meritt Rethink auto closing connection with mongo. At now by default connection...
authored
28 instance = new Db @options.db, new Server @options.host, @options.port, auto_reconnect: true
4c2b6e5 @meritt Initial commit
authored
29
6b76a7c @meritt Added ability to re-use db and collection instance.
authored
30 instance.open (error, db) =>
31 console.log 'Error with connection to MongoDB server: ' + error if error
4c2b6e5 @meritt Initial commit
authored
32
6b76a7c @meritt Added ability to re-use db and collection instance.
authored
33 @db = db
54c81d1 @meritt Ensure @db in state connected when trying reuse it.
authored
34 @getCollection table, after
6b76a7c @meritt Added ability to re-use db and collection instance.
authored
35
54c81d1 @meritt Ensure @db in state connected when trying reuse it.
authored
36 getCollection: (table, after) ->
064f348 @meritt Rethink auto closing connection with mongo. At now by default connection...
authored
37 if @collection[table]?
38 after @collection[table]
6b76a7c @meritt Added ability to re-use db and collection instance.
authored
39 else
54c81d1 @meritt Ensure @db in state connected when trying reuse it.
authored
40 @db.collection table, (error, collection) =>
4c2b6e5 @meritt Initial commit
authored
41 console.log 'Error with fetching collection: ' + error if error
42
064f348 @meritt Rethink auto closing connection with mongo. At now by default connection...
authored
43 @collection[table] = collection
54c81d1 @meritt Ensure @db in state connected when trying reuse it.
authored
44 after collection
4c2b6e5 @meritt Initial commit
authored
45
46 findById: (table, id, after = ->) ->
498287d @meritt Checking params before connecting to mongo
authored
47 try
48 params = _id: ensureObjectId id
49 catch exception
50 console.log 'Error with preparing params for findById: ' + exception
51 return after false
e34b492 @meritt If creation ObjectID failed, callback will be call anyway.
authored
52
498287d @meritt Checking params before connecting to mongo
authored
53 @getInstance table, (collection) =>
89936d4 @meritt Fix error when losing scope
authored
54 collection.find(params).toArray (error, results) =>
1a38c50 @meritt Close connection and return error into callback when getting error
authored
55 if error
56 console.log 'Error with fetching document by id: ' + error
57 return after false
4c2b6e5 @meritt Initial commit
authored
58
740986f @meritt Fix error when document not found
authored
59 after if results and results.length is 1 then results[0] else false
4c2b6e5 @meritt Initial commit
authored
60
b7937ed @meritt Added removeById method
authored
61 removeById: (table, id, after = ->) ->
62 try
63 params = _id: ensureObjectId id
64 catch exception
65 console.log 'Error with preparing params for removeById: ' + exception
66 return after false
67
68 @getInstance table, (collection) =>
69 collection.findAndRemove params, (error, results) =>
70 if error
71 console.log 'Error with removing document by id: ' + error
72 return after false
73
bc8299d @meritt Fix
authored
74 after results
b7937ed @meritt Added removeById method
authored
75
d736f46 @meritt Added abilities to configure sort/limit/skip parameters for find.
authored
76 find: (table, params, options, after) ->
c22714c @meritt Added count function
authored
77 [params, options, after] = @_normalizeArguments params, options, after
d736f46 @meritt Added abilities to configure sort/limit/skip parameters for find.
authored
78
498287d @meritt Checking params before connecting to mongo
authored
79 try
80 if params?._id?
81 if isObject(params._id) and params._id.$in?
82 params._id.$in = params._id.$in.map (value) -> ensureObjectId value
83 else
84 params._id = ensureObjectId params._id
85 catch exception
86 console.log 'Error with preparing params for find: ' + exception
87 return after []
4c2b6e5 @meritt Initial commit
authored
88
498287d @meritt Checking params before connecting to mongo
authored
89 @getInstance table, (collection) =>
c8b52f8 @meritt More unique errors
authored
90 cursor = collection.find params
d736f46 @meritt Added abilities to configure sort/limit/skip parameters for find.
authored
91
92 cursor.sort options.sort if options.sort
93 cursor.limit options.limit if options.limit
94 cursor.skip options.skip if options.skip
95
6b76a7c @meritt Added ability to re-use db and collection instance.
authored
96 cursor.toArray (error, results) =>
1a38c50 @meritt Close connection and return error into callback when getting error
authored
97 if error
98 console.log 'Error with fetching documents: ' + error
99 return after []
4c2b6e5 @meritt Initial commit
authored
100
101 after results
102
6b76a7c @meritt Added ability to re-use db and collection instance.
authored
103 count: (table, params, after) ->
104 if isFunction params
105 after = params
106 params = null
107
67deab1 @meritt Ensure after always set
authored
108 after = (->) if after is null
109
54c81d1 @meritt Ensure @db in state connected when trying reuse it.
authored
110 @getInstance table, (collection) =>
6b76a7c @meritt Added ability to re-use db and collection instance.
authored
111 collection.count params, (error, results) =>
1a38c50 @meritt Close connection and return error into callback when getting error
authored
112 if error
113 console.log 'Error with fetching counts: ' + error
eab5880 @meritt Fix error message. Fix return statements. Add try/catch for save.
authored
114 return after false
6b76a7c @meritt Added ability to re-use db and collection instance.
authored
115
c22714c @meritt Added count function
authored
116 after parseInt results, 10
117
4c2b6e5 @meritt Initial commit
authored
118 save: (table, params, after = ->) ->
498287d @meritt Checking params before connecting to mongo
authored
119 try
120 params._id = ensureObjectId params._id if params._id?
121 catch exception
122 console.log 'Error with preparing params for save: ' + exception
123 return after false
4c2b6e5 @meritt Initial commit
authored
124
498287d @meritt Checking params before connecting to mongo
authored
125 @getInstance table, (collection) =>
6b76a7c @meritt Added ability to re-use db and collection instance.
authored
126 collection.save params, safe: true, (error, results) =>
1a38c50 @meritt Close connection and return error into callback when getting error
authored
127 if error
128 console.log 'Error with saving data: ' + error
eab5880 @meritt Fix error message. Fix return statements. Add try/catch for save.
authored
129 return after false
4c2b6e5 @meritt Initial commit
authored
130
28a0463 @meritt Fix getting document when it updated
authored
131 after if results is 1 then params else results
4c2b6e5 @meritt Initial commit
authored
132
6b76a7c @meritt Added ability to re-use db and collection instance.
authored
133 close: ->
134 if @db isnt null
064f348 @meritt Rethink auto closing connection with mongo. At now by default connection...
authored
135 @collection = {} if @collection isnt {}
6b76a7c @meritt Added ability to re-use db and collection instance.
authored
136 @db.close()
137 @db = null
9f6bc92 @meritt Unset @collection at now independent
authored
138
6b76a7c @meritt Added ability to re-use db and collection instance.
authored
139 @
140
9011be1 @meritt Adding non-json param aliases.
authored
141 Long: (number) -> new mongodb.Long number
d726e93 @meritt Fix creating ObjectId fields.
authored
142 ObjectID: (hex) -> ensureObjectId hex
9011be1 @meritt Adding non-json param aliases.
authored
143 Timestamp: () -> new mongodb.Timestamp()
144 DBRef: (collection, id) -> new mongodb.DBRef collection, id
145 Binary: (buffer) -> new mongodb.Binary buffer
146 Symbol: (string) -> new mongodb.Symbol string
147 Double: (number) -> new mongodb.Double number
148
c22714c @meritt Added count function
authored
149 _normalizeArguments: (params, options, after) ->
150 if isFunction params
151 after = params
152 params = null
153 options = {}
154
155 if isFunction options
156 after = options
157 options = {}
158
159 after = (->) if not after
160 options = {} if not options
161
162 [params, options, after]
163
4c2b6e5 @meritt Initial commit
authored
164 module.exports = EasyMongo
Something went wrong with that request. Please try again.