Skip to content
Browse files

added count/insert/remove/update

  • Loading branch information...
1 parent 711113b commit 37ca003cd708f8389e1334e3630745012a0813c0 Karl Seguin committed Jan 23, 2012
Showing with 88 additions and 1 deletion.
  1. +1 −1 readme.md
  2. +19 −0 spec/countSpec.coffee
  3. +12 −0 spec/insertSpec.coffee
  4. +22 −0 spec/removeSpec.coffee
  5. +18 −0 spec/updateSpec.coffee
  6. +16 −0 src/mongolight.coffee
View
2 readme.md
@@ -1,5 +1,5 @@
# THIS IS NOT DONE
-This is about 10% complete. No package nor the /lib/*.js files will be available until it is
+This is about 50% complete. No package nor the /lib/*.js files will be available until it is
# MongoLight for node.js
A lightweight MongoDB mapper for node.js
View
19 spec/countSpec.coffee
@@ -0,0 +1,19 @@
+helper = require('./helper')
+mongolight = helper.mongolight
+User = helper.user
+
+describe 'count', ->
+
+ it "counts all documents", ->
+ helper.withinDb ->
+ User.count (err, count) ->
+ expect(err).toBeNull();
+ expect(count).toEqual(2)
+ helper.done()
+
+ it "counts the specified documents", ->
+ helper.withinDb ->
+ User.count {name: 'vladimi'}, (err, count) ->
+ expect(err).toBeNull()
+ expect(count).toEqual(0)
+ helper.done()
View
12 spec/insertSpec.coffee
@@ -0,0 +1,12 @@
+helper = require('./helper')
+mongolight = helper.mongolight
+User = helper.user
+
+describe 'insert', ->
+
+ it "inserts a document", ->
+ helper.withinDb ->
+ User.insert {test: '123'}, (err, document) ->
+ User.count {test: '123'}, (err, count) ->
+ expect(count).toEqual(1)
+ helper.done()
View
22 spec/removeSpec.coffee
@@ -0,0 +1,22 @@
+helper = require('./helper')
+mongolight = helper.mongolight
+User = helper.user
+
+describe 'remove', ->
+
+ it "removes the specified documents", ->
+ helper.withinDb ->
+ User.remove {name: 'leto'}, (err, x) ->
+ expect(x).toBeUndefined()
+ expect(err).toBeUndefined()
+ User.find {}, (err, users) ->
+ expect(users.length).toEqual(1)
+ expect(users[0].name).toEqual('ghanima')
+ helper.done()
+
+ it "returns the number of deleted records", ->
+ helper.withinDb ->
+ User.remove {}, {safe: true}, (err, x) ->
+ expect(err).toBeNull()
+ expect(x).toEqual(2)
+ helper.done()
View
18 spec/updateSpec.coffee
@@ -0,0 +1,18 @@
+helper = require('./helper')
+mongolight = helper.mongolight
+User = helper.user
+
+describe 'update', ->
+
+ it "updates the document", ->
+ helper.withinDb ->
+ User.update {name: 'leto'}, {$set: {name: 'leto 2'}}, (err, x) ->
+ User.count {name: 'leto 2'}, (err, count) ->
+ expect(count).toEqual(1)
+ helper.done()
+
+ it "returns the number of updated records", ->
+ helper.withinDb ->
+ User.update {}, {$set: {family: 'atreides'}}, {multi: true, safe: true}, (err, count) ->
+ expect(count).toEqual(2)
+ helper.done()
View
16 src/mongolight.coffee
@@ -50,12 +50,28 @@ class MongoLight
Helper.handleSingleLiteral(err, literal, options, entity, callback)
@remove: (selector, options, callback) ->
+ [options, callback] = Helper.optionalOptions(options, callback)
+ this.collection (err, collection) ->
+ return callback(err, null) if err?
+ collection.remove(selector, options, callback)
@insert: (document, options, callback) ->
+ [options, callback] = Helper.optionalOptions(options, callback)
+ this.collection (err, collection) ->
+ return callback(err, null) if err?
+ collection.insert(document, options, callback)
@update: (selector, document, options, callback) ->
+ [options, callback] = Helper.optionalOptions(options, callback)
+ this.collection (err, collection) ->
+ return callback(err, null) if err?
+ collection.update(selector, document, options, callback)
@count: (selector, callback) ->
+ [selector, callback] = Helper.optionalOptions(selector, callback)
+ this.collection (err, collection) ->
+ return callback(err, null) if err?
+ collection.count(selector, callback)
@collection: (callback) ->
this._mongolight.db.collection this._mongolight.collectionName, callback

0 comments on commit 37ca003

Please sign in to comment.
Something went wrong with that request. Please try again.