Permalink
Browse files

implemented findAndRemove and findAndModify

  • Loading branch information...
1 parent 10fad2e commit a12e125c5e9daf8c249af7ffb721e71400845a02 @Raynos committed Jan 7, 2013
Showing with 140 additions and 0 deletions.
  1. +13 −0 findAndModify.js
  2. +13 −0 findAndRemove.js
  3. +59 −0 test/findAndModify.js
  4. +53 −0 test/findAndRemove.js
  5. +2 −0 test/index.js
View
@@ -0,0 +1,13 @@
+var expand = require("reducers/expand")
+var take = require("reducers/take")
+var callback = require("callback-reduce")
+
+module.exports = findAndModify
+
+function findAndModify(collection, query, sort, doc, options) {
+ return take(expand(collection, function (collection) {
+ return callback.call(collection
+ , collection.findAndModify, query, sort || []
+ , doc || {}, options || {})
+ }), 1)
+}
View
@@ -0,0 +1,13 @@
+var expand = require("reducers/expand")
+var take = require("reducers/take")
+var callback = require("callback-reduce")
+
+module.exports = findAndRemove
+
+function findAndRemove(collection, query, sort, options) {
+ return take(expand(collection, function (collection) {
+ return callback.call(collection
+ , collection.findAndRemove, query, sort || []
+ , options || {})
+ }), 1)
+}
View
@@ -0,0 +1,59 @@
+var test = require("tape")
+var fold = require("reducers/fold")
+var uuid = require("node-uuid")
+
+var findAndModify = require("../findAndModify")
+var findOne = require("../findOne")
+var insert = require("../insert")
+var close = require("../close")
+var Collection = require("./util/collection")
+
+var collection = Collection()
+var value = uuid()
+
+test("insert document", function (assert) {
+ var result = insert(collection, {
+ foo: value
+ })
+
+ fold(result, function (result) {
+ assert.equal(result.foo, value)
+ assert.ok(result._id)
+ assert.end()
+ })
+})
+
+test("find document", function (assert) {
+ var doc = findAndModify(collection, {
+ foo: value
+ }, [["_id", -1]], {
+ $set: {
+ count: 1
+ }
+ }, {
+ new: true
+ })
+
+ fold(doc, function (doc) {
+ assert.equal(doc.count, 1)
+ assert.end()
+ })
+})
+
+test("was modified", function (assert) {
+ var doc = findOne(collection, {
+ foo: value
+ })
+
+ fold(doc, function (doc) {
+ assert.equal(doc.count, 1)
+ assert.end()
+ })
+})
+
+test("close", function (assert) {
+ close(collection, function (err) {
+ assert.ifError(err)
+ assert.end()
+ })
+})
View
@@ -0,0 +1,53 @@
+var test = require("tape")
+var fold = require("reducers/fold")
+var uuid = require("node-uuid")
+
+var findAndRemove = require("../findAndRemove")
+var findOne = require("../findOne")
+var insert = require("../insert")
+var close = require("../close")
+var Collection = require("./util/collection")
+
+var collection = Collection()
+var value = uuid()
+
+test("insert document", function (assert) {
+ var result = insert(collection, {
+ foo: value
+ })
+
+ fold(result, function (result) {
+ assert.equal(result.foo, value)
+ assert.ok(result._id)
+ assert.end()
+ })
+})
+
+test("find and remove document", function (assert) {
+ var doc = findAndRemove(collection, {
+ foo: value
+ }, [["_id", -1]])
+
+ fold(doc, function (doc) {
+ assert.equal(doc.foo, value)
+ assert.end()
+ })
+})
+
+test("was removed", function (assert) {
+ var doc = findOne(collection, {
+ foo: value
+ })
+
+ fold(doc, function (doc) {
+ assert.equal(doc, null)
+ assert.end()
+ })
+})
+
+test("close", function (assert) {
+ close(collection, function (err) {
+ assert.ifError(err)
+ assert.end()
+ })
+})
View
@@ -6,3 +6,5 @@ require("./update")
require("./findOne")
require("./remove")
require("./mapReduce")
+require("./findAndModify")
+require("./findAndRemove")

0 comments on commit a12e125

Please sign in to comment.