Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mhart/dynamo-table-id
Adds a nextId function to a dynamo-table instance to generate incrementing numbers
https://github.com/mhart/dynamo-table-id
Last synced: 11 days ago
JSON representation
Adds a nextId function to a dynamo-table instance to generate incrementing numbers
- Host: GitHub
- URL: https://github.com/mhart/dynamo-table-id
- Owner: mhart
- License: mit
- Created: 2013-07-07T13:08:54.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2014-02-12T03:11:21.000Z (almost 11 years ago)
- Last Synced: 2024-11-29T20:51:20.586Z (28 days ago)
- Language: JavaScript
- Homepage:
- Size: 146 KB
- Stars: 0
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
dynamo-table-id
---------------[![Build Status](https://secure.travis-ci.org/mhart/dynamo-table-id.png?branch=master)](http://travis-ci.org/mhart/dynamo-table-id)
Adds a `nextId` function to a [dynamo-table](https://github.com/mhart/dynamo-table) instance to
generate incrementing numbers using DynamoDB's `ADD` command in the
[`UpdateItem` action](http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html)Example
-------```javascript
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 errconsole.log(id)
// 1
})// Will return a collection of values if a length is provided
table.nextId(3, function(err, ids) {
if (err) throw errconsole.log(ids)
// [2, 3, 4]
})// Can also set a value to start incrementing from
table.setLastId(23, function(err) {
if (err) throw errtable.nextId(function(err, id) {
if (err) throw errconsole.log(id)
// 24
})
})```
API
---### dynamoTableId(table, [options])
Wraps the given [dynamo-table](https://github.com/mhart/dynamo-table) object with the methods given below.
Current options are:
- `idTable` (defaults to `last-ids`) - the name of a table that must already exist with a string hash key
- `key` (defaults to the table name) - the hash key value used to hold the value item
- `attr` (defaults to `lastId`) - the attribute name of the incrementing value### nextId([length], callback)
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).### setLastId(lastId, callback)
Sets the current value of the last value - ie, the next generated value will be an increment above this value.
Installation
------------With [npm](http://npmjs.org/) do:
```
npm install dynamo-table-id
```