Adds a nextId
function to a dynamo-table instance to
generate incrementing numbers using DynamoDB's ADD
command in the
UpdateItem
action
var dynamoTable = require('dynamo-table'),
dynamoTableId = require('dynamo-table-id')
// Will use us-east-1 and credentials from process.env unless otherwise specified
var table = dynamoTable('orders', {key: ['customerId', 'orderId']})
table = dynamoTableId(table, {idTable: 'last-ids', key: 'orders'})
// By default the first value will be 1
table.nextId(function(err, id) {
if (err) throw err
console.log(id)
// 1
})
// Will return a collection of values if a length is provided
table.nextId(3, function(err, ids) {
if (err) throw err
console.log(ids)
// [2, 3, 4]
})
// Can also set a value to start incrementing from
table.setLastId(23, function(err) {
if (err) throw err
table.nextId(function(err, id) {
if (err) throw err
console.log(id)
// 24
})
})
Wraps the given dynamo-table object with the methods given below.
Current options are:
idTable
(defaults tolast-ids
) - the name of a table that must already exist with a string hash keykey
(defaults to the table name) - the hash key value used to hold the value itemattr
(defaults tolastId
) - the attribute name of the incrementing value
If length
is not specified, this will generate a new numerical value and return it in the callback -
otherwise an array of numbers of the given length is generated and returned (even if length is 0 or 1).
Sets the current value of the last value - ie, the next generated value will be an increment above this value.
With npm do:
npm install dynamo-table-id