Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added connection/collection insert

  • Loading branch information...
commit c664e0d4c06c71cd929c176d718c8c8484d9a252 1 parent b3082d5
@assaf assaf authored
View
1  .travis.yml
@@ -1,4 +1,3 @@
language: node_js
node_js:
- - 0.4
- 0.6
View
30 lib/poutine/collection.coffee
@@ -186,6 +186,36 @@ class Collection
return new Scope(this, selector)
+ # -- Insert/update/remove --
+
+ # Inserts document(s) into the database.
+ #
+ # If the document(s) do not have an ID, sets the ID before insertion. This method does not block, with a callback it
+ # simply passes the document(s) to the callback. You can use the callback if you're inserting with `safe: true` or
+ # want to wait for after-save hooks.
+ #
+ # When called with a single document, passes it to the callback. WHen called with an array, inserts all the documents
+ # and passes that array to the callback.
+ #
+ # Example:
+ # posts = connect().find("posts")
+ # posts.insert title: "New and exciting", (error, post)->
+ # console.log "Inserted #{post._id}"
+ insert: (document, options, callback)->
+ if !callback && typeof options == "function"
+ [options, callback] = [null, options]
+ @_connect (error, collection, database)=>
+ return callback error if error
+ if callback
+ collection.insert document, options, (error, results)->
+ if Array.isArray(document)
+ callback error, results
+ else
+ callback error, results[0]
+ else
+ collection.insert document, options
+
+
# -- Implementation details --
# Passes error, collection and database to callback.
View
17 lib/poutine/database.coffee
@@ -180,5 +180,22 @@ class Database extends EventEmitter
@collection(name).distinct key, selector, callback
+ # -- Insert/update/remove --
+
+ # Inserts document(s) into the database.
+ #
+ # If the document(s) do not have an ID, sets the ID before insertion. This method does not block, with a callback it
+ # simply passes the document(s) to the callback. You can use the callback if you're inserting with `safe: true` or
+ # want to wait for after-save hooks.
+ #
+ # When called with a single document, passes it to the callback. WHen called with an array, inserts all the documents
+ # and passes that array to the callback.
+ #
+ # Example:
+ # connect().insert "posts", title: "New and exciting"
+ insert: (name, object, options, callback)->
+ @collection(name).insert object, options, callback
+
+
exports.Configuration = Configuration
exports.Database = Database
View
101 spec/collection/insert_spec.coffee
@@ -0,0 +1,101 @@
+{ assert, vows, connect, setup } = require("../helpers")
+
+
+vows.describe("Collection insert").addBatch
+
+ # -- collection().insert --
+
+ "insert":
+ topic: ->
+ setup =>
+ @callback null, connect().collection("posts")
+
+ "document only":
+ topic: (collection)->
+ result = collection.insert(title: "Insert 2.1")
+ return result || "nothing"
+ "should return null": (result)->
+ assert.equal result, "nothing"
+ "new document":
+ topic: (result, collection)->
+ collection.find(title: "Insert 2.1").one @callback
+ "should exist in database": (object)->
+ assert object
+
+ "document and options":
+ topic: (collection)->
+ result = collection.insert({ title: "Insert 2.2" }, { safe: true })
+ return result || "nothing"
+ "should return null": (result)->
+ assert.equal result, "nothing"
+ "new document":
+ topic: (result, collection)->
+ collection.find(title: "Insert 2.2").one @callback
+ "should exist in database": (object)->
+ assert object
+
+ "document, options and callback":
+ topic: (collection)->
+ collection.insert { title: "Insert 2.3" }, { safe: true }, @callback
+ "should pass document to callback": (post)->
+ assert post
+ assert.equal post.title, "Insert 2.3"
+ "should set document ID": (post)->
+ assert post._id
+ "new document":
+ topic: (post, collection)->
+ collection.find post._id, @callback
+ "should exist in database": (post)->
+ assert post
+ assert.equal post.title, "Insert 2.3"
+
+ "document and callback":
+ topic: (collection)->
+ collection.insert title: "Insert 2.4", @callback
+ "should pass document to callback": (post)->
+ assert post
+ assert.equal post.title, "Insert 2.4"
+ "should set document ID": (post)->
+ assert post._id
+ "new document":
+ topic: (post, collection)->
+ collection.find post._id, @callback
+ "should exist in database": (post)->
+ assert post
+ assert.equal post.title, "Insert 2.4"
+
+ "multiple documents, no callback":
+ topic: (collection)->
+ result = collection.insert([{ title: "Insert 2.5", category: "foo" }, { title: "Insert 2.5", category: "bar" }])
+ return result || "nothing"
+ "should return null": (result)->
+ assert.equal result, "nothing"
+ "new documents":
+ topic: (result, collection)->
+ collection.find(title: "Insert 2.5").all @callback
+ "should all exist in database": (posts)->
+ assert.lengthOf posts, 2
+ categories = (post.category for post in posts)
+ assert.deepEqual categories, ["foo", "bar"]
+
+ "multiple documents and callback":
+ topic: (collection)->
+ collection.insert [{ title: "Insert 2.6", category: "foo" }, { title: "Insert 2.6", category: "bar" }], @callback
+ "should pass all document to callback": (posts)->
+ assert.lengthOf posts, 2
+ for post in posts
+ assert.equal post.title, "Insert 2.6"
+ "should set document ID": (posts)->
+ for post in posts
+ assert post._id
+ "new documents":
+ topic: (posts, collection)->
+ ids = (post._id for post in posts)
+ collection.find ids, @callback
+ "should exist in database": (posts)->
+ assert.lengthOf posts, 2
+ categories = (post.category for post in posts)
+ assert.deepEqual categories, ["foo", "bar"]
+
+.export(module)
+
View
99 spec/connection/insert_spec.coffee
@@ -0,0 +1,99 @@
+{ assert, vows, connect, setup } = require("../helpers")
+
+
+vows.describe("Connection insert").addBatch
+
+ # -- connect().insert --
+
+ "insert":
+ topic: ->
+ setup @callback
+
+ "document only":
+ topic: ->
+ result = connect().insert("posts", title: "Insert 1.1")
+ return result || "nothing"
+ "should return null": (result)->
+ assert.equal result, "nothing"
+ "new document":
+ topic: ->
+ connect().find("posts", title: "Insert 1.1").one @callback
+ "should exist in database": (object)->
+ assert object
+
+ "document and options":
+ topic: ->
+ result = connect().insert("posts", { title: "Insert 1.2" }, { safe: true })
+ return result || "nothing"
+ "should return null": (result)->
+ assert.equal result, "nothing"
+ "new document":
+ topic: ->
+ connect().find("posts", title: "Insert 1.2").one @callback
+ "should exist in database": (object)->
+ assert object
+
+ "document, options and callback":
+ topic: ->
+ connect().insert "posts", { title: "Insert 1.3" }, { safe: true }, @callback
+ "should pass document to callback": (post)->
+ assert post
+ assert.equal post.title, "Insert 1.3"
+ "should set document ID": (post)->
+ assert post._id
+ "new document":
+ topic: (post)->
+ connect().find "posts", post._id, @callback
+ "should exist in database": (post)->
+ assert post
+ assert.equal post.title, "Insert 1.3"
+
+ "document and callback":
+ topic: ->
+ connect().insert "posts", title: "Insert 1.4", @callback
+ "should pass document to callback": (post)->
+ assert post
+ assert.equal post.title, "Insert 1.4"
+ "should set document ID": (post)->
+ assert post._id
+ "new document":
+ topic: (post)->
+ connect().find "posts", post._id, @callback
+ "should exist in database": (post)->
+ assert post
+ assert.equal post.title, "Insert 1.4"
+
+ "multiple documents, no callback":
+ topic: ->
+ result = connect().insert("posts", [{ title: "Insert 1.5", category: "foo" }, { title: "Insert 1.5", category: "bar" }])
+ return result || "nothing"
+ "should return null": (result)->
+ assert.equal result, "nothing"
+ "new documents":
+ topic: ->
+ connect().find("posts", title: "Insert 1.5").all @callback
+ "should all exist in database": (posts)->
+ assert.lengthOf posts, 2
+ categories = (post.category for post in posts)
+ assert.deepEqual categories, ["foo", "bar"]
+
+ "multiple documents and callback":
+ topic: ->
+ connect().insert "posts", [{ title: "Insert 1.6", category: "foo" }, { title: "Insert 1.6", category: "bar" }], @callback
+ "should pass all document to callback": (posts)->
+ assert.lengthOf posts, 2
+ for post in posts
+ assert.equal post.title, "Insert 1.6"
+ "should set document ID": (posts)->
+ for post in posts
+ assert post._id
+ "new documents":
+ topic: (posts)->
+ ids = (post._id for post in posts)
+ connect().find "posts", ids, @callback
+ "should exist in database": (posts)->
+ assert.lengthOf posts, 2
+ categories = (post.category for post in posts)
+ assert.deepEqual categories, ["foo", "bar"]
+
+.export(module)
Please sign in to comment.
Something went wrong with that request. Please try again.