Init's DynamoDB instance for Modli adapter
10K+
This module provides adapter for the DynamoDB datasource for integration with Modli.
npm install modli-dynamodb --save
IMPORTANT: For testing (with linked services) to run correctly you must have docker and DevLab npm install devlab -g installed.
Configure your adapter and model
import { model, adapter, use } from 'modli';
import dynamodb from 'modli-dynamodb';
// Set your configuration
let dynamoConfig = {
region: 'us-east-1', // Your specific AWS Region
endpoint: 'http://localhost:8000', // Optional value to specify an end point
accessKeyId: process.env.AWS_ACCESS_KEY_ID || '123456789',
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY || '123456789'
};
Add an instance of the model where the autoCreate flag is used to determine if helpers.checkCreateTable() should automatically create the table from the model.
Use indexes to define a hash key, range key or global secondary indexes as desired.
The optional index key projectionType can specify a projection type of 'ALL', 'KEYS_ONLY' or 'INCLUDE'. Please note that the use of 'INCLUDE' also requires the index key nonKeyAttributes as an array of keys to include in the projection.
Both projectionType and nonKeyAttributes are optional values and the projection type will default to 'ALL' in their absence.
model.add({
name: 'roles',
version: 1,
autoCreate: true,
indexes: [
{ keytype: 'hash', value: 'id', type: 'N'},
{ keytype: 'secondary', value: 'login', type: 'S', projectionType: 'INCLUDE', nonKeyAttributes: ['age']}
],
schema: {
id: { type: 'string' },
name: { type: 'string' },
age: { type: 'number' }
}
});
Add the adapter with the previously defined config object structure:
adapter.add({
name: 'dynamoAdapter',
source: dynamodb,
config: dynamoConfig
});
You can now use the adapter with the model with:
const testDynamo = use('roles', 'dynamoAdapter');
listGets a list of all active tables
testDynamo.list()
.then(/*...*/)
.catch(/*...*/);
scanPerform a full unfiltered scan of a table with an optional dynamo scan-level filter that doesn't rely on secondary indexes. Optional filters are constructed in a way that correlates to dynamo filter conditionals. In addition, optionally pass a limit and lastKey, which could be used to support pagination.
testDynamo.scan()
.then(/*...*/)
.catch(/*...*/);
testDynamo.scan({email: {eq: '[email protected]'}})
.then(/*...*/)
.catch(/*...*/);
testDynamo.scan({accounts: {contains: 'someuser'}})
.then(/*...*/)
.catch(/*...*/);
testDynamo.scan({firstName: { in: ['Ben', 'Tom']}})
.then(/*...*/)
.catch(/*...*/);
testDynamo.scan({age: { between: [18, 26]}})
.then(/*...*/)
.catch(/*...*/);
testDynamo.scan({accounts: {contains: 'someuser'}, email: {eq: '[email protected]'}})
.then(/*...*/)
.catch(/*...*/);
testDynamo.scan(undefined, {limit: 10})
.then(/*...*/)
.catch(/*...*/);
testDynamo.scan({age: { between: [18, 26]}}, {limit: 25, lastKey: 'somekey'})
.then(/*...*/)
.catch(/*...*/);
createTablePass through method that uses explicit dynamo creation params to create a table
testDynamo.createTable(dynamoParams)
.then(/*...*/)
.catch(/*...*/);
createTableFromModelPerforms a deterministic create based on the specified schema. Will construct the creation query without additional input.
testDynamo.createTableFromModel()
.then(/*...*/)
.catch(/*...*/);
deleteTableDeletes a table by specified object containing hash / value pair
testDynamo.deleteTable({TableName: 'myTable'})
.then(/*...*/)
.catch(/*...*/);
createCreates a new entry in the table specified in the schema
testDynamo.create({HASH:Value, SomeIndex: OtherValue})
.then(/*...*/)
.catch(/*...*/);
readPerforms a deterministic read on a table by hash / value pair OR secondary index / value pair
testDynamo.read({HASH: SomeValue})
.then(/*...*/)
.catch(/*...*/);
testDynamo.read({SOMEINDEX: SomeOtherValue})
.then(/*...*/)
.catch(/*...*/);
readPaginatePerforms a read on a table expecting a global secondary index that accepts a limit and lastKey, which could be used to support pagination.
testDynamo.readPaginate({SOMEINDEX: SomeValue}, {limit: 10})
.then(/*...*/)
.catch(/*...*/);
testDynamo.readPaginate({SOMEINDEX: SomeValue}, {limit: 25, lastKey: 'SomeKey'})
.then(/*...*/)
.catch(/*...*/);
getItemByHashPerforms a read on a table expecting a HASH / Value pair
testDynamo.getItemByHash({SOMEHASH: SomeValue})
.then(/*...*/)
.catch(/*...*/);
getItemByIdPerforms a read on a table expecting a global secondary index
testDynamo.getItemById({SOMEINDEX: SomeValue})
.then(/*...*/)
.catch(/*...*/);
getItemsInArrayCalls batchGetItem using a Hash identifier
testDynamo.getItemsInArray('HASHNAME', [1,2,3,4])
.then(/*...*/)
.catch(/*...*/);
updateUpdates a row in the table by HASH / Value pair and JSON Object specifying new values
testDynamo.update({HASH: 'SomeValue'}, { /* update object */ })
.then(/*...*/)
.catch(/*...*/);
patchPartial updates a row in the table by HASH / Value pair and JSON Object specifying new values
testDynamo.patch({HASH: 'SomeValue'}, { /* partial update object */ })
.then(/*...*/)
.catch(/*...*/);
deleteDeletes a row from the table by HASH / Value pair
testDynamo.delete({HASH: 'SomeValue'})
.then(/*...*/)
.catch(/*...*/);
extendExtends the adapter to allow custom methods
testDynamo.extend('methodName', () => {
/*...*/
})
The system utilizes DevLab for running tasks in containers. The below tasks will
be executed in containers via lab {task-name}:
clean will remove the /node_modules, /build and /coverage directoriesinstall will install dependencieslint will lint all files in /src and /testbuild will transpile ES2015 code in /src to /buildmocha will run all spec files in /test/src recursivelytest will run both lint and covercover will run code coverage on all tests in /test/src recursivelyRunning lab mocha will run the tests.
Modli-DynamoDB is licensed under the MIT license. Please see LICENSE.txt for full details.
Modli-DynamoDB was designed and created at TechnologyAdvice.
Content type
Image
Digest
sha256:a3ebe3ab9…
Size
313.1 MB
Last updated
almost 10 years ago
docker pull modli/dynamodb