https://github.com/bakerface/fluent-dynamo
A fluent interface for Amazon DynamoDB in Node.js
https://github.com/bakerface/fluent-dynamo
Last synced: 7 months ago
JSON representation
A fluent interface for Amazon DynamoDB in Node.js
- Host: GitHub
- URL: https://github.com/bakerface/fluent-dynamo
- Owner: bakerface
- License: mit
- Created: 2015-02-16T15:00:54.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2015-11-24T22:38:33.000Z (over 10 years ago)
- Last Synced: 2025-06-29T21:42:12.270Z (11 months ago)
- Language: JavaScript
- Size: 30.3 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fluent-dynamo
**A fluent interface for Amazon DynamoDB in Node.js**
[](http://badge.fury.io/js/fluent-dynamo)
[](https://travis-ci.org/bakerface/fluent-dynamo)
[](https://codeclimate.com/github/bakerface/fluent-dynamo)
[](https://codeclimate.com/github/bakerface/fluent-dynamo/coverage)
[](https://github.com/bakerface/fluent-dynamo/issues)
[](https://david-dm.org/bakerface/fluent-dynamo)
[](https://david-dm.org/bakerface/fluent-dynamo#info=devDependencies)
[](https://www.npmjs.com/package/fluent-dynamo)
### dynamo.createTable(table)
Creates a table with the specified configuration (see [CreateTable](http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_CreateTable.html)). Below is an example of creating a table with a global secondary index and a local secondary index.
``` javascript
var fluent = require('fluent-dynamo');
var dynamo = fluent()
.withAccessKeyId('YOUR_ACCESS_KEY_ID')
.withRegion('YOUR_REGION')
.withSecretAccessKey('YOUR_SECRET_ACCESS_KEY');
dynamo.createTable('Thread')
.withHashKey('ForumName').asString()
.withRangeKey('Subject').asString()
.withReadCapacity(5)
.withWriteCapacity(5)
.withGlobalSecondaryIndex('PostCountIndex')
.withHashKey('ForumName').asString()
.withRangeKey('PostCount').asNumber()
.withReadCapacity(1)
.withWriteCapacity(1)
.withAllAttributesProjection()
.withLocalSecondaryIndex('LastPostIndex')
.withHashKey('ForumName').asString()
.withRangeKey('LastPostDateTime').asString()
.withKeysOnlyProjection()
.then(function() {
// the table was created
})
.catch(function(reason) {
// an error occurred
});
```
### dynamo.putItem(table)
Creates a new item or replaces an existing item in the table (see [PutItem](http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html)). Below is an example of inserting an item with attribute conditions.
``` javascript
var fluent = require('fluent-dynamo');
var dynamo = fluent()
.withAccessKeyId('YOUR_ACCESS_KEY_ID')
.withRegion('YOUR_REGION')
.withSecretAccessKey('YOUR_SECRET_ACCESS_KEY');
dynamo.putItem('Thread')
.withAttribute('ForumName').asString('Amazon')
.withAttribute('Subject').asString('DynamoDB')
.withAttribute('LastPostDateTime').asString('201303190422')
.withAttribute('PostCount').asNumber(100)
.withCondition('ForumName').isNotEqualToString('Amazon')
.withCondition('Subject').isNotEqualToString('DynamoDB');
.then(function() {
// the item was inserted into the table
})
.catch(function(reason) {
// an error occurred
});
```
### dynamo.deleteItem(table)
Deletes an item in the table (see [DeleteItem](http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteItem.html)). Below is an example of deleting an item with a specific hash key and range key.
``` javascript
var fluent = require('fluent-dynamo');
var dynamo = fluent()
.withAccessKeyId('YOUR_ACCESS_KEY_ID')
.withRegion('YOUR_REGION')
.withSecretAccessKey('YOUR_SECRET_ACCESS_KEY');
dynamo.deleteItem('Thread')
.withHashKey('ForumName').asString('Amazon')
.withRangeKey('Subject').asString('DynamoDB')
.then(function() {
// the item was deleted from the table
})
.catch(function(reason) {
// an error occurred
});
```
### dynamo.query(table)
Searches for items in the table (see [Query](http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html)). Below is an example of querying for an item by a specific hash key.
``` javascript
var fluent = require('fluent-dynamo');
var dynamo = fluent()
.withAccessKeyId('YOUR_ACCESS_KEY_ID')
.withRegion('YOUR_REGION')
.withSecretAccessKey('YOUR_SECRET_ACCESS_KEY');
dynamo.query('Thread')
.withConsistentRead()
.withCondition('ForumName').isEqualToString('Amazon')
.then(function(items) {
// the items were found and are in the following format:
// items = [
// {
// ForumName: 'Amazon',
// Subject: 'DynamoDB',
// PostCount: 100
// },
// {
// ForumName: 'Amazon',
// Subject: 'Elastic Beanstalk',
// PostCount: 50
// }
// ];
})
.catch(function(reason) {
// an error occurred
});
```
### dynamo.deleteTable(table)
Deletes the table (see [DeleteTable](http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteTable.html)). Below is an example of deleting a table by name.
``` javascript
var fluent = require('fluent-dynamo');
var dynamo = fluent()
.withAccessKeyId('YOUR_ACCESS_KEY_ID')
.withRegion('YOUR_REGION')
.withSecretAccessKey('YOUR_SECRET_ACCESS_KEY');
dynamo.deleteTable('Thread')
.then(function() {
// the table was deleted
})
.catch(function(reason) {
// an error occurred
});
```
### dynamo.updateItem(table)
Updates and item in a table (see [UpdateItem](http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateItem.html)). Below is an example of updating an attribute in a table.
``` javascript
var fluent = require('fluent-dynamo');
var dynamo = fluent()
.withAccessKeyId('YOUR_ACCESS_KEY_ID')
.withRegion('YOUR_REGION')
.withSecretAccessKey('YOUR_SECRET_ACCESS_KEY');
dynamo.updateItem('Thread')
.withHashKey('ForumName').asString('Amazon')
.withRangeKey('Subject').asString('DynamoDB')
.withSetExpression('LastPostedBy').asString('alice@example.com')
.withRemoveExpression('Archived')
.withCondition('LastPostedBy').isEqualToString('fred@example.com')
.withAllNewReturnValues();
.then(function() {
// the "LastPostedBy" attribute is now "alice@example.com"
// and the "Archived" attribute is removed
})
.catch(function(reason) {
// an error occurred
});
```