DynamoDB processor operates a process by simple JSON expression.
- If it have failed to set child objects to Map Type field, auto trying to update with initial fields again. futhermore, If it have failed by the conflict, auto trying the updating process at first once more.
- Node.js 12 or later
- AWS SDK for JavaScript v3
Click here for the version that supports AWS SDK v2
$ npm install -save @aws-sdk/client-dynamodb @aws-sdk/lib-dynamodb @aws-sdk/util-dynamodb
$ npm install -save dynamo-processor
const DynamoProcessor = require('dynamo-processor')
const dp = new DynamoProcessor({ region: 'ap-northeast-1' });
new DynamoProcessor(options)
- options
<Object>
DynamoDBClientConfig as well- wrapFunc
<Boolean>
If this is true, proc method returns a Function that wraps the Promise in case that promise evaluation need lazy. (default is false)
- wrapFunc
proc
method is to analyze an item and to process the item by the actionget
,put
,update
,delete
,batchWrite
,batchDelete
methods as shortcut for each action
dp.proc({
table: 'users',
action: 'get', // optional
key: {
id: 1
}
})
.then((item) => {
console.log(item); // { name: 'Taro', id: 1, age: 16 }
});
dp.get('users', { id: 1 });
dp.proc({
table: 'users',
action: 'get', // optional
keys: [
{ id: 1 },
{ id: 2 }
]
})
.then((items) => {
console.log(items[0]); // { id: 1, ... }
console.log(items[1]); // { id: 2, ... }
});
dp.proc({
table: 'users',
action: 'put', // optional
item: {
id: 2,
name: 'Michael',
age: 25,
address: {
prefecture: 'Osaka'
}
}
})
.then((item) => {
console.log(item);
// { name: 'Michael',
// id: 2,
// address: { prefecture: 'Osaka' },
// age: 25 }
});
dp.put('users', {
id: 2,
name: 'Michael',
age: 25,
address: {
prefecture: 'Osaka'
}
});
dp.proc({
table: 'users',
action: 'put', // optional
items: [
{ id: 2, name: 'Michael' },
{ id: 3, name: 'Cindy' }
]
})
.then(unprocessedItems => {
console.log(unprocessedItems); // undefined shows all success
});
dp.batchWrite('users', [
{ id: 2, name: 'Michael' },
{ id: 3, name: 'Cindy' }
]);
The space in a key is instead of the separator (.
) between parent and child
because a space is rarely used for a variable name.
dp.proc({
table: 'users',
action: 'update', // optional
key: {
id: 3
},
set: {
name: 'Taro',
age: 14,
'address prefecture': 'Tokyo'
}
})
.then((item) => {
console.log(item);
// { name: 'Taro',
// id: 3,
// address: { prefecture: 'Tokyo' },
// age: 14 }
});
dp.proc({
table: 'users',
action: 'update', // optional
key: {
id: 3
},
add: {
age: 1
}
})
.then((item) => {
console.log(item);
// { name: 'Taro',
// id: 3,
// address: { prefecture: 'Tokyo' },
// age: 15 } age was incremented
});
pushset
is adding to NumberSet or StringSet or BinarySet.
dp.proc({
table: 'users',
action: 'update', // optional
key: {
id: 4
},
pushset: {
cards: 30
}
})
.then((item) => {
console.log(item);
});
dp.proc({
table: 'users',
action: 'update', // optional
key: {
id: 3
},
remove: [
'age',
'address prefecture'
]
})
.then((item) => {
console.log(item);
// { name: 'Taro',
// id: 3,
// address: {} } age and address.prefecture was removed
});
delete
is removing from NumberSet or StringSet or BinarySet.
dp.proc({
table: 'users',
action: 'update', // optional
key: {
id: 4
},
delete: {
cards: 20
}
})
.then((item) => {
console.log(item);
});
dp.update('users', {
id: 4
},
{
set: { name: 'foo' }
}
})
.then((item) => {
console.log(item);
});
dp.proc({
table: 'users',
action: 'delete',
key: {
id: 1
}
})
.then((item) => {
console.log(item); // null
});
dp.delete('users', { id: 1 });
dp.proc({
table: 'users',
action: 'delete',
keys: [
{ id: 2 },
{ id: 3 }
]
})
.then(unprocessedItemKeys => {
console.log(unprocessedItemKeys); // undefined shows all success
});
dp.batchDelete('users',[
{ id: 2 },
{ id: 3 }
])
Promise.all(
dp.proc({
table: 'users',
action: 'get', // optional
keys: [
{ id: 1 },
{ id: 2 }
]
}, { useBatch: false })
)
.then((items) => {
console.log(items[0]); // { id: 1, ... }
console.log(items[1]); // { id: 2, ... }
});
Promise.all(
dp.proc({
table: 'users',
action: 'put', // optional
items: [
{ id: 1, val: 'foo' },
{ id: 2, val: 'bar' }
]
}, { useBatch: false })
)
.then((items) => {
console.log(items[0]); // { id: 1, ... }
console.log(items[1]); // { id: 2, ... }
});
Promise.all(
dp.proc({
table: 'users',
action: 'delete',
keys: [
{ id: 1 },
{ id: 2 }
]
}, { useBatch: false })
)
.then(results => {
console.log(results[0]); // null
console.log(results[1]); // null
});
dp.createTable('producthistories', {
productId: 'S', // HASH key
version: 'N' // RANGE key
},
{ // options
readCU: 20, // default 5
writeCU: 3 // default 5
})
.then(() => {
console.log('Succeeded to create table')
});
If first argument is object, it passes through as raw params. In short, dp.createTable(params)
equals dynamodb.createTable(params).promise()
.
dp.deleteTable('producthistories')
.then(() => {
console.log('Delete to create table')
});