- Simplified data modeling and mapping to DynamoDB types
- Advanced chainable apis for query and scan operations
- Data validation
- Autogenerating UUIDs
- Global Secondary Indexes
- Local Secondary Indexes
- Parallel Scans
npm install vogels
First, you need to configure the AWS SDK with your credentials.
var vogels = ;vogelsAWSconfig;
You can also directly pass in your access key id and secret
var vogels = ;vogelsAWSconfig;
Define a Model
Models are defined through the toplevel define method.
var Account = vogels;
Models can also be defined with hash and range keys.
var BlogPost = vogels;
Vogels provides the following schema types:
UUIDs can be declared for any attributes, including hash and range keys. By Default, the uuid will be automatically generated when attempting to create the model in DynamoDB.
var Tweet = vogels;
After you've defined your model you can configure the table name to use. By default, the table name used will be the lowercased and pluralized version of the name you provided when defining the model.
You can also pass in a custom instance of the aws-sdk DynamoDB client
var dynamodb = ;Account;// or globally use custom DynamoDB instance// all defined models will now use this drivervogels;
Saving Models to DynamoDB
With your models defined, we can start saving them to DynamoDB.
You can also first instantiate a model and then save it.
var acc = email: 'firstname.lastname@example.org' name: 'Test Example';acc;
Saving models that require range and hashkeys are identical to ones with only hashkeys.
When updating a model the hash and range key attributes must be given, all other attributes are optional
// update the name of the email@example.com accountAccount;
Model.update accepts options to pass to DynamoDB when making the updateItem request
Account;// Only update the account if the current age of the account is 21Account;// setting an attribute to null will delete the attribute from DynamoDBAccount;
You can also pass what action to perform when updating a given attribute Use $add to increment or decrement numbers and add values to sets
$del will remove values from a given set
You delete items in DynamoDB using the hashkey of model If your model uses both a hash and range key, than both need to be provided
Account;// Destroy model using hash and range keyBlogPost;BlogPost;
Model.destroy accepts options to pass to DynamoDB when making the deleteItem request
Loading models from DynamoDB
The simpliest way to get an item from DynamoDB is by hashkey.
Perform the same get request, but this time peform a consistent read.
Model.get accepts any options that DynamoDB getItem request supports. For
Get a model using hash and range key.
// load up blog post written by Werner, titled DynamoDB Keeps Getting Better and cheaperBlogPost;
Model.get also supports passing an object which contains hash and range key
attributes to load up a model
For models that use hash and range keys Vogels provides a flexible and chainable query api
// query for blog posts by firstname.lastname@example.orgBlogPost;// same as above, but load all resultsBlogPost;// only load the first 5 posts by wernerBlogPost;// query for posts by werner where the tile begins with 'Expanding'BlogPost;// return only the count of documents that begin with the title ExpandingBlogPost;// only return title and content attributes of 10 blog posts// that begin with the title ExpandingBlogPost;// sorting by title ascendingBlogPost// sorting by title descendingBlogPost// All query options are chainableBlogPost;
Vogels supports all the possible KeyConditions that DynamoDB currently supports.
BlogPost;// less than equalsBlogPost;// less thanBlogPost;// greater thanBlogPost;// greater than equalsBlogPost;BlogPost;BlogPost;
Query Filters allow you to further filter results on non-key attributes.
See the queryFilter.js example for more examples of using query filters
First, define a model with a global secondary index.
var GameScore = vogels;
Now we can query against the global index
When can also configure the attributes projected into the index. By default all attributes will be projected when no Projection pramater is present
var GameScore = vogels;
Filter items against the configured rangekey for the global index.
Local Secondary Indexes
First, define a model using a local secondary index
var BlogPost = vogels;
Now we can query for blog posts using the secondary index
Could also query for published posts, but this time return oldest first
Finally lets load all published posts sorted by publish date
Learn more about secondary indexes
Vogels provides a flexible and chainable api for scanning over all your items This api is very similar to the query api.
// scan all accounts, returning the first page or resultsAccount;// scan all accounts, this time loading all results// note this will potentially make several calls to DynamoDB// in order to load all resultsAccount;// Load 20 accountsAccount;// Load All accounts, 20 at a time per requestAccount;// Load accounts which match a filter// only return email and created attributes// and return back the consumed capacity the request tookAccountattributes'email''created';// Returns number of matching accounts, rather than the matching accounts themselvesAccount;// Start scan using start keyAccount
Vogels supports all the possible Scan Filters that DynamoDB currently supports.
// equalsAccount;// not equalsAccount;// less than equalsAccount;// less thanAccount;// greater than equalsAccount;// greater thanAccount;// name attribute doesn't existAccount;// name attribute existsAccount;// containsAccount;// not containsAccount;// inAccount;// begins withAccount;// betweenAccount;// multiple filtersAccount;
Parallel scans increase the throughput of your table scans. The parallel scan operation is identical to the scan api. The only difference is you must provide the total number of segments
Caution you can easily consume all your provisioned throughput with this api
var totalSegments = 8;Accountattributes'age';// Load All accountsAccount
More info on Parallel Scans
Batch Get Items
Model.getItems allows you to load multiple models with a single request to DynamoDB.
DynamoDB limits the number of items you can get to 100 or 1MB of data for a single request. Vogels automatically handles splitting up into multiple requests to load all items.
Account;// For models with range keys you must pass in objects of hash and range key attributesvar postKey1 = email : 'email@example.com' title : 'Hello World!';var postKey2 = email : 'firstname.lastname@example.org' title : 'Another Post';BlogPost;
Model.getItems accepts options which will be passed to DynamoDB when making the batchGetItem request
// Get both accounts, using a consistent readAccount;
vogels supports a basic streaming api in addition to the callback
var stream = Account;stream;stream;var querystream = BlogPost;querystream;querystream;
Dynamic Table Names
vogels supports dynamic table names, useful for storing time series data.
var Event = vogels;
var vogels = ;var Account = vogels;Account;
See the examples for more working sample code.
- Batch Write Items
- Streaming api support for all operations
- DDL operations (update throughput)
- Full intergration test suite
(The MIT License)
Copyright (c) 2014 Ryan Fitzgerald
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.