Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
dynamodb-model
Advanced tools
Readme
A simple and lightweight object mapper for Amazon DynamoDB, influenced by MongoDB and the Mongoose API. This library allows mapping of DynamoDB tables to Javascript objects using schemas while providing a comfortable high-level API for maximum productivity. It also enables a smooth transition to DynamoDB if you already know MongoDB or Mongoose.
npm install dynamodb-model
To run the tests:
npm test
A schema describes attributes, keys, and indexes for a DynamoDB table. A schema instance allows mapping of Javascript objects to DynamoDB items and vice-versa.
var DynamoDBModel = require('dynamodb-model');
var productSchema = new DynamoDBModel.Schema({
productId: {
type: Number,
key: 'hash' // indicates a Hash key
},
sku: String,
inStock: Boolean, // will be stored as a "Y" or "N" string
properties: {}, // will be converted to a JSON string
created: Date // will be converted to a number with Date.getTime
});
Schemas support the following data types native to DynamoDB:
In addition, schemas also support the following data types with some transformations:
Date.getTime
as a number)JSON.stringify
and JSON.parse
), defined using an empty object {}
It is also possible to implement you own mapping if necessary:
var DynamoDBModel = require('dynamodb-model');
var schema = new DynamoDBModel.Schema({
...
customField: {
dynamoDbType: 'S', // the native DynamoDB type, either S, N, B, SS, NS or BS
mapFromDb: function(value) {
/* your implementation */
},
mapToDb: function(value) {
/* your implementation, must return a string */
}
}
});
To specify a Hash or Range key, define a key
attribute on the field.
"hash"
to specify a Hash key"range"
to specify a Range keyLocal secondary indexes are not yet supported.
The specify a default value, use default
to specify a static value or a function returning a value.
var DynamoDBModel = require('dynamodb-model');
var schema = new DynamoDBModel.Schema({
...
active: {
type: Boolean,
default: true
}
});
Default values replace missing attributes when reading items from DynamoDB.
Schemas are independent from the AWS SDK and can be used with any other DynamoDB client. To map an object to a DynamoDB structure manually, use schema.mapToDb
. Likewise, to map a DynamoDB structure to an object, use schema.mapFromDb
.
var DynamoDBModel = require('dynamodb-model');
var schema = new DynamoDBModel.Schema({
id: {
type: Number,
key: 'hash'
},
message: String
});
schema.mapToDb({ id: 1, message: 'some text' });
// returns { id: { N: '1' }, message: { 'S': 'some text' } };
schema.mapFromDb({ id: { N: '1' }, message: { 'S': 'some text' } });
// returns { id: 1, message: 'some text' };
The Model
class provides the high-level API you use to interact with the table, such as reading and writing data. The model class uses the official AWS SDK which already implement most of the best practices, such as automatic retries on a "HTTP 400: Capacity Exceeded" error.
Models also create the table automatically if required. There is no need to validate table existence or the "active" status. Operations performed while the table is not ready are queued until the table becomes active.
var DynamoDBModel = require('dynamodb-model');
var productSchema = new DynamoDBModel.Schema({
productId: {
type: Number,
key: 'hash'
},
sku: String,
inStock: Boolean,
properties: {},
created: Date
});
// create a model using the name of the DynamoDB table and a schema
var productTable = new DynamoDBModel.Model('dynamo-products', productSchema);
// the model provides methods for all DynamoDB operations
// no need to check for table status, we can start using it right away
productTable.putItem(/* ... */);
productTable.getItem(/* ... */);
productTable.updateItem(/* ... */);
productTable.deleteItem(/* ... */);
// but some of them return intermediate objects in order to provide a better API
var query = productTable.query(/* ... */);
query.select(/* ... */).limit(100).exec(callback);
The dynamodb-model
module uses the official AWS SDK module for low-level operations. To properly connect to your DynamoDB table, make sure you configure the AWS SDK first. More details on the official AWS website here.
var AWS = require('aws-sdk');
// setup region and credentials
AWS.config.update({
accessKeyId: /* Your acess key */,
secretAccessKey: /* Your secret key */,
region: 'us-east-1'
});
// specify the API version (optional)
AWS.config.apiVersions = {
dynamodb: '2012-08-10'
};
Additionally, if you need to specify options for the AWS.DynamoDB
constructor, pass them to the Model
constructor like this:
var myTable = new DynamoDBModel.Model(tableName, schema, {
maxRetries: 1000,
sslEnabled: true
});
When creating a model instance, a describeTable
call is immediately performed to check for table existence. If the table does not exist, a createTable
call follows immediately.
If the table is not yet active (table status is not "ACTIVE"
), all operations are queued and will not be executed until the table is ready. When the table becomes active, operations from the queue are executed in sequential order. This means that you can create a model instance and start writing data immediately, even if the table does not exist.
If you wish to wait for the table to become available before performing an action, use the waitForActiveTable
method, which invokes describeTable
repeatedly until the status switches to "ACTIVE"
.
WARNING: the queue is not durable and should not be used in a production environment, since changes will be lost if the application terminates before the table becomes active. Instead, create your table beforehand, or use the waitForActiveTable
method to make sure the table is ready.
This method is not yet implemented.
AWS Documentation for BatchGetItem
This method is not yet implemented.
AWS Documentation for BatchWriteItem
Creates the DynamoDB table represented by the schema and returns the AWS service response.
var myTable = new DynamoDBModel.Model(tableName, schema);
myTable.createTable(function (err, response) {
// table creation started
})
Note: table creation in DynamoDB is asynchronous. The table is not ready until its status property is set to "ACTIVE". If you need to wait for the table to become active, use the waitForActiveTable
method.
AWS Documentation for CreateTable
Deletes a single item in a table by primary key and returns the AWS service response.
var myTable = new DynamoDBModel.Model(tableName, schema);
myTable.deleteItem({ id: 1 }, function (err, response) {
// item removed
})
AWS Documentation for DeleteItem
Note: conditional deletes are not yet implemented.
Removes the table represented by the schema, as well as all items in the table, then returns the AWS service response.
var myTable = new DynamoDBModel.Model(tableName, schema);
myTable.deleteTable(function (err, response) {
// table removal started
})
AWS Documentation for DeleteTable
Returns information about the table represented by the schema, including the current status of the table, when it was created, the primary key schema, and any indexes on the table. The table description is the AWS service response.
var myTable = new DynamoDBModel.Model(tableName, schema);
myTable.describeTable(function (err, response) {
// response contains the table description, see AWS docs for more details
})
Invokes describeTable
repeatedly until the table status is "ACTIVE"
.
describeTable
(optional, default value is 3000)describeTable
(optional)var myTable = new DynamoDBModel.Model(tableName, schema);
var pollingInterval = 5000; // 5 seconds
myTable.waitForActiveTable(pollingInterval, function (err, response) {
// response contains the table description, with an "ACTIVE" status
})
Retrieves a specific item based on its primary key, returning the mapped item as well as the AWS service response.
var myTable = new DynamoDBModel.Model(tableName, schema);
myTable.getItem({ id: 1 }, function (err, item, response) {
// item represents the DynamoDB item mapped to an object using the schema
})
This method is not yet implemented.
AWS Documentation for ListTables
TBD
TBD
TBD
TBD
AWS Documentation for UpdateItem
TBD
AWS Documentation for UpdateTable
TBD
This is the initial release of dynamodb-model
and should not be considered production-ready yet. Some features are also missing (see TODO section).
BatchGetItem
operationBatchWriteItem
operationDeleteItem
operationScan
operationThe MIT License (MIT)
Copyright (c) 2013, Nicolas Mercier
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.
FAQs
Elegant DynamoDB object modeling influenced from MongoDB and the Mongoose API
The npm package dynamodb-model receives a total of 27 weekly downloads. As such, dynamodb-model popularity was classified as not popular.
We found that dynamodb-model demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.