Permalink
Browse files

yjh0502 deleteItem additional attributes feature

  • Loading branch information...
1 parent 4cda48c commit 2f6d8833ee24c4132a8684ca515580fb2b1065c2 @rjrodger committed Apr 9, 2011
Showing with 122 additions and 61 deletions.
  1. +2 −1 .gitignore
  2. +18 −8 README.md
  3. +2 −1 lib/simpledb.js
  4. +100 −51 test/simpledb.test.js
View
@@ -1 +1,2 @@
-keys.mine.js
+keys.mine.js
+*~
View
@@ -80,7 +80,7 @@ function was successful then _error_ is _null_.
So, you check if _error_ is _null_ to see if you can continue working:
- sdb.listDomains( functions( error, result, meta ) {
+ sdb.listDomains( function( error, result, meta ) {
if( error ) {
console.log('listDomains failed: '+error.Message )
}
@@ -304,7 +304,9 @@ _$AsArrays_ meta-directive in the _override_ argument. When _true_, all
attribute values are returned as arrays. As
SimpleDb is schemaless, it is not possible to know in advance if an attribute
is multi-valued. In the default case, `{$AsArrays:false}`,
-multiple values are returned as string, with the value list comma-separated.
+multiple values are returned as string, with the value list
+comma-separated. SimpleDB returns multiple values in alphabetical
+order.
sdb.getItem('<domain>','<itemname>',function( error , result, meta ){
console.log("Those are good burgers, Walter: "+JSON.stringify(res))
@@ -328,8 +330,13 @@ _simpledb.SimpleDB_. Or you can set it on a case-by-case basis, using an overrid
* _override_: (optional) SimpleDB attributes to override function defaults
* _callback_: (required) callback function accepting parameters _callback(error, result, metadata)_
-Delete an item from SimpleDB. The _attrs_ argument is an optional
-array of attribute names. If not present, the item is completely
+Delete an item from SimpleDB. The _attrs_ argument is optional, and can be:
+ * an array of attribute names: all matching attributes will be deleted
+ * an object whose properties are attribute names:
+attributes of the item will be deleted if they have the same value as the object properties.
+Values can be either a single string, or an array of string values, in which case all matching attributes are deleted.
+
+If no attributes are specified, the item is completely
removed. If present, only the specified attributes are removed. If all
the attributes of an item are removed, then it will also be completely
deleted.
@@ -338,13 +345,16 @@ deleted.
console.log("Well, Ted, like I said the last time: it won't happen again: "+JSON.stringify(res))
})
- sdb.deleteItem('<domain>','<itemname>',[ '<attr>', ... ]function( error, result, meta ){
+ sdb.deleteItem('<domain>','<itemname>',[ '<attr>', ... ], function( error, result, meta ){
console.log("I felt like destroying something beautiful. "+JSON.stringify(res))
})
- sdb.deleteItem('<domain>','<itemname>',{ '<attr>': [<value1>, ... ], ... }function( error, result, meta ){
- console.log("I don't know what to write about. "+JSON.stringify(res))
- })
+ sdb.deleteItem('<domain>','<itemname>',
+ { '<attr1>': '<value1>', 'attr2': ['<value2>, ... ], ... },
+ function( error, result, meta ){
+ console.log("I don't know what to write about. "+JSON.stringify(res))
+ }
+ )
### select: `sdb.select( query, override, callback )`
View
@@ -448,7 +448,8 @@ exports.SimpleDB = function(opts,logger) {
for( var aI = 1; aI <= attrs.length; aI++ ) {
q['Attribute.'+aI+'.Name'] = attrs[aI-1]
}
- } else {
+ }
+ else {
putattrs(q,attrs,'');
}
View
@@ -7,7 +7,7 @@ var eyes = require('eyes')
var simpledb = require('../lib/simpledb.js')
-var keys = require('./keys.mine.js')
+var keys = require('./keys.js')
module.exports = {
@@ -207,17 +207,17 @@ module.exports = {
;sdb.createDomain('simpledbtest',function(err,res,meta){
- debugres(err,res,meta)
+ debugres(null, err,res,meta)
assert.isNull(err)
;sdb.domainMetadata('simpledbtest',function(err,res,meta){
- debugres(err,res,meta)
+ debugres(null, err,res,meta)
assert.isNull(err)
sdb.handle = againhandle
;sdb.listDomains(function(err,res,meta){
- debugres(err,res,meta)
+ debugres(null, err,res,meta)
assert.isNull(err)
assert.ok( 2 <= meta.trycount )
assert.ok( Array.isArray(res) )
@@ -230,34 +230,37 @@ module.exports = {
{
foo:1,
bar:'BAR',
- woz:['one','two'],
+ woz:['one','two','three','four'],
quote:"'n"
},function(err,res,meta){
- debugres(err,res,meta)
+ debugres(null, err,res,meta)
assert.isNull(err)
;sdb.getItem('simpledbtest','not-an-item',function(err,res,meta){
- debugres(err,res,meta)
+ debugres(null, err,res,meta)
assert.isNull(err)
assert.isNull(res)
;sdb.getItem('simpledbtest','item1',function(err,res,meta){
- debugres(err,res,meta)
+ debugres(null, err,res,meta)
assert.isNull(err)
assert.equal('item1',res.$ItemName)
assert.equal(1,parseInt(res.foo,10))
assert.equal('BAR',res.bar)
- assert.equal('one,two',res.woz)
+ assert.equal('four,one,three,two',res.woz)
assert.equal("'n",res.quote)
;sdb.getItem('simpledbtest','item1',{$AsArrays:true},function(err,res,meta){
- debugres(err,res,meta)
+ debugres(null, err,res,meta)
assert.isNull(err)
assert.equal('item1',res.$ItemName)
assert.equal(1,parseInt(res.foo[0],10))
assert.equal('BAR',res.bar[0])
- assert.equal('one',res.woz[0])
- assert.equal('two',res.woz[1])
+ assert.equal('four',res.woz[0])
+ assert.equal('one',res.woz[1])
+ assert.equal('three',res.woz[2])
+ assert.equal('two',res.woz[3])
+ assert.equal(4,res.woz.length)
assert.equal("'n",res.quote[0])
;sdb.request("GetAttributes",
@@ -267,31 +270,32 @@ module.exports = {
ConsistentRead:'true'
},
function(err,res,meta){
- debugres(err,res,meta)
+ debugres(null, err,res,meta)
assert.isNull(err)
- assert.equal( 5, res.GetAttributesResult.Attribute.length )
+ assert.equal( 7, res.GetAttributesResult.Attribute.length )
+
;sdb.select("not a select expression at all at all",function(err,res,meta){
- debugres(err,res,meta)
+ debugres(null, err,res,meta)
assert.isNotNull(err)
assert.equal( 'InvalidQueryExpression', err.Code )
;sdb.select("select * from simpledbtest where bar = 'BAR'",function(err,res,meta){
- debugres(err,res,meta)
+ debugres(null, err,res,meta)
assert.isNull(err)
assert.ok( 1 == res.length )
assert.equal('item1',res[0].$ItemName)
assert.equal( 'BAR', res[0].bar )
;sdb.select("select * from simpledbtest where bar = '?'",['BAR'],function(err,res,meta){
- debugres(err,res,meta)
+ debugres(null, err,res,meta)
assert.isNull(err)
assert.ok( 1 == res.length )
assert.equal('item1',res[0].$ItemName)
assert.equal( 'BAR', res[0].bar )
;sdb.select("select * from simpledbtest where bar = '?' and quote = '?'",['BAR',"'n"],function(err,res,meta){
- debugres(err,res,meta)
+ debugres(null, err,res,meta)
assert.isNull(err)
assert.ok( 1 == res.length )
assert.equal('item1',res[0].$ItemName)
@@ -301,50 +305,72 @@ module.exports = {
[
{ $ItemName:'b1', batch:'yes', field:'one'},
{ $ItemName:'b2', batch:'yes', field:'two'}
- ],function(err,res,meta){
- debugres(err,res,meta)
+ ],
+ function(err,res,meta){
+ debugres(null, err,res,meta)
assert.isNull(err)
;sdb.select("select * from simpledbtest where batch = 'yes'",function(err,res,meta){
- debugres(err,res,meta)
+ debugres(null, err,res,meta)
assert.isNull(err)
assert.ok( 2 == res.length )
assert.equal('b1',res[0].$ItemName)
assert.equal('one', res[0].field )
assert.equal('b2',res[1].$ItemName)
assert.equal('two', res[1].field )
- ;sdb.deleteItem('simpledbtest','item1', {'woz': ['one']},function(err,res,meta) {
- debugres(err,res,meta)
+
+ // delete individual attr by value but leave item in place
+ ;sdb.deleteItem('simpledbtest','item1', {'woz':'one'},function(err,res,meta) {
+ debugres(null, err,res,meta)
+
+
+ ;sdb.getItem('simpledbtest','item1',function(err,res,meta){
+ debugres(null, err,res,meta)
+ assert.isNull(err)
+ assert.equal('item1',res.$ItemName)
+ assert.equal(1,parseInt(res.foo,10))
+ assert.equal('BAR',res.bar)
+ assert.equal('four,three,two',res.woz)
+ assert.equal("'n",res.quote)
+
+
+ // delete individual attr by values but leave item in place
+ ;sdb.deleteItem('simpledbtest','item1', {'woz': ['two','three']},function(err,res,meta) {
+ debugres(null, err,res,meta)
;sdb.getItem('simpledbtest','item1',function(err,res,meta){
- debugres(err,res,meta)
+ debugres(null, err,res,meta)
assert.isNull(err)
assert.equal('item1',res.$ItemName)
assert.equal(1,parseInt(res.foo,10))
assert.equal('BAR',res.bar)
- assert.equal('two',res.woz)
+ assert.equal('four',res.woz)
assert.equal("'n",res.quote)
- ;sdb.deleteItem('simpledbtest','item1', ['foo', 'bar'],function(err,res,meta) {
- debugres(err,res,meta)
+
+ // delete individual attr by name but leave item in place
+ ;sdb.deleteItem('simpledbtest','item1', ['foo', 'bar'],function(err,res,meta) {
+ debugres(null, err,res,meta)
assert.isNull(err)
;sdb.getItem('simpledbtest','item1',function(err,res,meta){
- debugres(err,res,meta)
- assert.isNull(err)
+ debugres(null,err,res,meta)
+ assert.isNull(err)
assert.equal('item1',res.$ItemName)
- assert.isNull(res.foo)
- assert.isNull(res.bar)
- assert.equal('two',res.woz)
+ assert.isUndefined(res.foo)
+ assert.isUndefined(res.bar)
+ assert.equal('four',res.woz)
assert.equal("'n",res.quote)
+
;sdb.deleteItem('simpledbtest','item1',function(err,res,meta){
- debugres(err,res,meta)
+ debugres(null, err,res,meta)
assert.isNull(err)
+
;sdb.deleteDomain('simpledbtest',function(err,res,meta){
- debugres(err,res,meta)
+ debugres(null, err,res,meta)
assert.isNull(err)
@@ -353,10 +379,39 @@ module.exports = {
//eyes.inspect(sdb)
;sdb.listDomains(function(err,res,meta){
- debugres(err,res,meta)
+ debugres(null, err,res,meta)
assert.isNotNull(err)
- }) }) }) }) }) }) }) }) }) }) }) }) }) }) }) }) }) }) }) }) })
+
+ }) // listDomains
+
+ }) // deleteDomain
+
+ }) // deleteItem
+ }) // getItem
+ }) // deleteItem
+ }) // getItem
+ }) // deleteItem
+ }) // getItem
+ }) // deleteItem
+
+ }) // select
+ }) // batchPutItem
+
+ }) // select
+ }) // select
+ }) // select
+ }) // select
+
+ }) // request
+ }) // getItem
+ }) // getItem
+ }) // getItem
+ }) // putItem
+
+ }) // listDomains
+ }) // domainMetadata
+ }) // createDomain
},
example: function() {
@@ -378,23 +433,17 @@ module.exports = {
}
-function debugres(err,res,meta) {
- /*
- util.debug(
- '\nerr: '+JSON.stringify(err)+
- '\nres: '+JSON.stringify(res)+
- '\nmeta:'+JSON.stringify(meta)
- )
- */
-}
-
-assert.isNull = function(obj) {
- return null == obj;
+function debugres(note, err,res,meta) {
+ if( note ) {
+ util.debug(
+ '\nnote: '+JSON.stringify(err)+
+ '\nerr: '+JSON.stringify(err)+
+ '\nres: '+JSON.stringify(res)+
+ '\nmeta:'+JSON.stringify(meta)
+ )
+ }
}
-assert.isNotNull = function(obj) {
- return null != obj;
-}
if( 'run' == process.argv[2] ) {
for( fname in module.exports ) {

0 comments on commit 2f6d883

Please sign in to comment.