Permalink
Browse files

Add database option support to DB.remove & DB.get

  • Loading branch information...
1 parent 2d00886 commit e6917f25ed67c82bea718ae238b2303604deaabc @wezm committed Apr 4, 2011
Showing with 39 additions and 13 deletions.
  1. +10 −6 src/db.coffee
  2. +29 −7 test/db_test.coffee
View
@@ -269,14 +269,16 @@ class DB
remove: (key, args...) ->
switch args.length
- when 1 then callback = args[0]
+ when 1
+ options = {}
+ callback = args[0]
when 2
- database = args[0]
+ options = args[0]
callback = args[1]
else
throw new Error("Invalid number of arguments (#{args.length}) to remove");
- @restClient.delete key, database, (error) ->
+ @restClient.delete key, options.database, (error) ->
callback error
exists: (key, args...) ->
@@ -294,14 +296,16 @@ class DB
# key, [database], callback
get: (key, args...) ->
switch args.length
- when 1 then callback = args[0]
+ when 1
+ options = {}
+ callback = args[0]
when 2
- database = args[0]
+ options = args[0]
callback = args[1]
else
throw new Error("Invalid number of arguments (#{args.length}) to get");
- @restClient.get key, database, (error, value) ->
+ @restClient.get key, options.database, (error, value) ->
callback error, value
setBulk: (records, args...) ->
View
@@ -321,36 +321,58 @@ module.exports =
'removes records that exist': (test) ->
test.expect 2
- db.set 'test', 'old',testDb, ->
- db.remove 'test', testDb, (error) ->
+ db.set 'test', 'old', ->
+ db.remove 'test', (error) ->
test.ifError error
- db.get 'test', testDb, (error, value) ->
+ db.get 'test', (error, value) ->
test.equal value, null
test.done()
'returns an error if the record doesn\'t exist': (test) ->
test.expect 2
- db.remove 'test', testDb, (error) ->
+ db.remove 'test', (error) ->
test.ok error?
test.equal error.message, "Record not found"
test.done()
+ 'allows the database to be specified': (test) ->
+ test.expect 2
+ options = {database: 'test2.kct'}
+
+ # Set in both dbs, remove from one
+ db.set 'test', 'old', ->
+ db.set 'test', 'old', options, ->
+ db.remove 'test', options, (error) ->
+ test.ifError error
+ db.get 'test', options, (error, value) ->
+ test.equal value, null
+ test.done()
+
get: testCase
setUp: dbClear
'returns null for non-existent key': (test) ->
test.expect 1
- db.get 'test', testDb, (error, value) ->
+ db.get 'test', (error, value) ->
test.ok value == null
test.done()
'retrieves an existing key': (test) ->
test.expect 1
- db.set 'test', "Test\tValue", testDb, (error) ->
- db.get 'test', testDb, (error, value) ->
+ db.set 'test', "Test\tValue", (error) ->
+ db.get 'test', (error, value) ->
test.equal value, "Test\tValue"
test.done()
+ 'allows the database to be specified': (test) ->
+ test.expect 2
+ db.set 'test', 'other db', (error, output) ->
+ test.ifError error
+
+ db.get 'test', {database: 'test2.kct'}, (error, value) ->
+ test.ok value == null
+ test.done()
+
exists: testCase
setUp: dbClear

0 comments on commit e6917f2

Please sign in to comment.