Skip to content

Commit

Permalink
Checking that ID is set immediately by calling post.
Browse files Browse the repository at this point in the history
  • Loading branch information
assaf committed Nov 28, 2011
1 parent 85fdf4e commit 0407658
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 12 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
@@ -1,11 +1,17 @@
## Version 0.3

Added insert operations on connection and collection. For example:
Added insert operations on connection, collection and model. For example:

user = new User(name: "Assaf")
User.insert user
console.log "Inserted #{user._id}"

posts = connect().find("posts")
posts.insert title: "New and exciting", (error, post)->
posts.insert { title: "New and exciting" }, { safe: true }, (error, post)->
console.log "Inserted #{post._id}"

connect().insert "posts", title: "Directly to connection"

Added support for hooks with callbacks. `afterLoad` becomes a hook. For example:

class User
Expand Down
11 changes: 9 additions & 2 deletions spec/collection/insert_spec.coffee
Expand Up @@ -12,10 +12,13 @@ vows.describe("Collection insert").addBatch

"document only":
topic: (collection)->
result = collection.insert(title: "Insert 2.1")
@post = { title: "Insert 2.1" }
result = collection.insert(@post)
return result || "nothing"
"should return null": (result)->
assert.equal result, "nothing"
"should set document ID": ->
assert @post._id
"new document":
topic: (result, collection)->
collection.find(title: "Insert 2.1").one @callback
Expand Down Expand Up @@ -66,10 +69,14 @@ vows.describe("Collection insert").addBatch

"multiple documents, no callback":
topic: (collection)->
result = collection.insert([{ title: "Insert 2.5", category: "foo" }, { title: "Insert 2.5", category: "bar" }])
@posts = [{ title: "Insert 2.5", category: "foo" }, { title: "Insert 2.5", category: "bar" }]
result = collection.insert(@posts)
return result || "nothing"
"should return null": (result)->
assert.equal result, "nothing"
"should set document ID": ->
for post in @posts
assert post._id
"new documents":
topic: (result, collection)->
collection.find(title: "Insert 2.5").all @callback
Expand Down
11 changes: 9 additions & 2 deletions spec/connection/insert_spec.coffee
Expand Up @@ -17,10 +17,13 @@ vows.describe("Connection insert").addBatch

"document only":
topic: ->
result = connect().insert("posts", title: "Insert 1.1")
@post = { "posts", title: "Insert 1.1" }
result = connect().insert("posts", @post)
return result || "nothing"
"should return null": (result)->
assert.equal result, "nothing"
"should set document ID": ->
assert @post._id
"new document":
topic: ->
connect().find("posts", title: "Insert 1.1").one @callback
Expand Down Expand Up @@ -71,10 +74,14 @@ vows.describe("Connection insert").addBatch

"multiple documents, no callback":
topic: ->
result = connect().insert("posts", [{ title: "Insert 1.5", category: "foo" }, { title: "Insert 1.5", category: "bar" }])
@posts = [{ title: "Insert 1.5", category: "foo" }, { title: "Insert 1.5", category: "bar" }]
result = connect().insert("posts", @posts)
return result || "nothing"
"should return null": (result)->
assert.equal result, "nothing"
"should set document ID": ->
for post in @posts
assert post._id
"new documents":
topic: ->
connect().find("posts", title: "Insert 1.5").all @callback
Expand Down
24 changes: 18 additions & 6 deletions spec/model/insert_spec.coffee
Expand Up @@ -22,10 +22,13 @@ vows.describe("Model insert").addBatch
"single":
"no callback":
topic: ->
result = Post.insert(title: "Insert 3.1")
@post = { title: "Insert 3.1" }
result = Post.insert(@post)
return result || "nothing"
"should return nothing": (result)->
assert.equal result, "nothing"
"should set document ID": ->
assert @post._id
"new document":
topic: ->
Post.find title: "Insert 3.1", @callback
Expand All @@ -50,10 +53,14 @@ vows.describe("Model insert").addBatch
"multiple":
"no callback":
topic: ->
result = Post.insert([{ title: "Insert 3.3", category: "foo" }, { title: "Insert 3.3", category: "bar" }])
@posts = [{ title: "Insert 3.3", category: "foo" }, { title: "Insert 3.3", category: "bar" }]
result = Post.insert(@posts)
return result || "nothing"
"should return nothing": (result)->
assert.equal result, "nothing"
"should set document ID": ->
for post in @posts
assert post._id
"new documents":
topic: ->
Post.find title: "Insert 3.3", @callback
Expand Down Expand Up @@ -89,11 +96,13 @@ vows.describe("Model insert").addBatch
"Model":
"no callback":
topic: ->
post = new Post(title: "Insert 3.5")
result = Post.insert(post)
@post = new Post(title: "Insert 3.5")
result = Post.insert(@post)
return result || "nothing"
"should return nothing": (result)->
assert.equal result, "nothing"
"should set document ID": ->
assert @post._id
"new document":
topic: ->
Post.find title: "Insert 3.5", @callback
Expand All @@ -119,11 +128,14 @@ vows.describe("Model insert").addBatch
"multiple":
"no callback":
topic: ->
posts = [new Post( title: "Insert 3.7", category: "foo" ), new Post( title: "Insert 3.7", category: "bar" )]
result = Post.insert(posts)
@posts = [new Post( title: "Insert 3.7", category: "foo" ), new Post( title: "Insert 3.7", category: "bar" )]
result = Post.insert(@posts)
return result || "nothing"
"should return nothing": (result)->
assert.equal result, "nothing"
"should set document ID": ->
for post in @posts
assert post._id
"new documents":
topic: ->
Post.find title: "Insert 3.7", @callback
Expand Down

0 comments on commit 0407658

Please sign in to comment.