Skip to content

Commit

Permalink
What insert and upsert sould return
Browse files Browse the repository at this point in the history
- `insert` return (cid or array of cids) of inserted element
- `upsert` return true if updating and cid if inserting
  • Loading branch information
btwael committed Sep 2, 2014
1 parent 6d17b0c commit 94347ba
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
19 changes: 17 additions & 2 deletions lib/collection.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions src/collection.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,34 @@ class Collection
# Add data
insert: (element) ->
if element instanceof Array
result = []
for elem in element
date = (new Date).toJSON()
@header.lcid++
elem['cid'] = @header.lcid
elem['$created'] = date
elem['$updated'] = date
@items.push elem
result.push @header.lcid
else
date = (new Date).toJSON()
@header.lcid++
element['cid'] = @header.lcid
element['$created'] = date
element['$updated'] = date
@items.push element
result = @header.lcid
@save() if @autosave
return @items
return result

upsert: (element, key, value) ->
check = @where("(@" + key + " == '" + value + "')")
if check.length > 0
this.update check[0].cid, element
@update check[0].cid, element
return true
else
this.insert element
return @items
@insert element
return @header.lcid

# Retrieving data functions
get: (cid) -> _.findWhere(@items, {'cid': cid})
Expand Down

0 comments on commit 94347ba

Please sign in to comment.