A lightweight model wrapper for DynamoDB with syntax that sucks less.
Install Fireball:
npm install fireball-db --save
Fireball, in its simplest form, is a wrapper around the AWS SDK DynamoDB Document Client. The primary purpose of this library is to create model object to reflect a single DynamoDB table. The model object provides many useful additions such as unique ID generation, cleaner parameter syntax and promise based functions.
fireball = require 'fireball-db'
The model object is a wrapper around the DocumentClient that simplify the parameter syntax to most functions and will automatically add the TableName parameter as needed.
User = fireball.model 'SomeDynamoTable'
User = fireball.model 'SomeDynamoTable',
find_by_email: (email) ->
...
User.find_by_email('[email protected]').then (users) ->
...
All functions of the model object return Promises.
User = ...
User.query(
expression: '#field = :value'
names: {'#field': 'name'}
values: {':value': 'fred'}
).then (users) ->
...
User = ...
User.query(
expression: '#field = :value'
names: {'#field': 'name'}
values: {':value': 'fred'}
IndexName: 'secondary_index'
).then (users) ->
...
User = ...
User.scan(
expression: '#field = :value'
names: {'#field': 'name'}
values: {':value': 'fred'}
).then (users) ->
...
User = ...
key = email: '[email protected]'
User.get(key).then (user) ->
...
Wrapper around get that will use the managed identifier key.
User = ...
User.for_id('12312312321').then (user) ->
...
User = ...
User.put(first: 'John', last: 'Doe', email: '[email protected]').then (user) ->
...
User = ...
user_data = first: 'John', last: 'Doe', email: '[email protected]'
condition = expression: '#field = :value', names: {'#field': 'name'}, values: {':value': 'fred'}
User.put(user_data, condition).then (user) ->
...
Wrapper around put that will automatically add an identifier field and ensure uniqueness.
User = ...
User.insert(first: 'John', last: 'Doe', email: '[email protected]').then (user) ->
...
User = ...
key = email: '[email protected]'
User.insert(key, first: 'John', last: 'Doe').then (user) ->
...
User = ...
key = email: '[email protected]'
User.delete(key).then (user) ->
...
User = ...
user = email: '[email protected]', name: 'someone', ...
User.delete(item).then (user) ->
...
User = ...
keys = [
{email: '[email protected]'}
{email: '[email protected]'}
{email: '[email protected]'}
]
User.for_keys(keys).then (users) ->
...
User = ...
ids = [
'12121'
'12122'
'12123'
]
User.for_ids(ids).then (users) ->
...
User = ...
items = [
{first: 'John', last: 'Doe', email: '[email protected]'}
{first: 'Jane', last: 'Doe', email: '[email protected]'}
{first: 'Sally', last: 'Doe', email: '[email protected]'}
]
User.put_all(items).then (users) ->
...
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality.