4.0.2 • Published 3 years ago

dynamodb-fast-access v4.0.2

Weekly downloads
16
License
MIT
Repository
github
Last release
3 years ago

dynamodb-fast-access

Default CRUD operations for managing AWS DynamoDB table items in an easy-to-extend structure.

Build Status Npm version GitHub license Coverage Status Conventional Commits semantic-release

Quick example

import * as DDFA from 'dynamodb-fast-access';

DDFA.DatabaseConfig.init({
    maxRetries: 9,
    tables: [{
        tableAlias: 'Products',
        tableName: 'Products_test',
        partitionKeyName: 'id',
        partitionKeyType: 'string'
    }]
});

class ProductsDB extends DDFA.DBMutable<any, any, any>('Products') {
    // You inherit a lot of basic functions
    // You can write further DB queries here
}

ProductsDB.create({ id: '123456abc', name: 'gloves', color: 'red' });
ProductsDB.getById('123456abc');
ProductsDB.deleteById('123456abc');
// And a lot more...

Usage

  1. Configure the library by invoking the init function with the database configuration parameters. You can optionally provide your own configured AWS DynamoDB DocumentClient instance, otherwise it will be created.

  2. (optional, recommended) Create the models for your database objects:

    • RawModel: that describes the object that is stored in the database.
    • Model: that describes the object that is stored in the database extended by additional derived attributes.
    • UpdateModel: that describes the attributes of the object that is stored in the database that are mutable.
  3. (optional, recommended) Create the extender function that accepts RawModel type object array and returns Model type object array. This will extend your the raw database object upon request. (The default extender function does not modify the database objects.)

  4. (optional) Create the deleter function to perform some task before the object is being deleted. This function is invoked before an item is deleted. (The default deleter function does not do anything.)

  5. Create the database class by extending one of the provided base classes that provide a lot of basic functions to access the database. You can write your own database functions here as well.

Database configuration parameters

AttributeRequiredTypeDescription
maxRetriestruenumberMaximum number of retries for batch write with an exponential backoff algorithm as it is recommended in the DynamoDB API reference here.
tablestruearrayThe array of DynamoDB table configurations.
tables[].tableAliastruestringThe alias of the DynamoDB table. Table configuration is referenced by this alias attribute when its base class is created.
tables[].tableNametruestringThe name of the DynamoDB table.
tables[].partitionKeyNametruestringThe name of the partition key of the DynamoDB table.
tables[].partitionKeyTypetrueenumThe type of the partition key of the DynamoDB table. Possible values: 'string', 'number'.
tables[].sortKeyNamefalsestringThe name of the sort key of the DynamoDB table.
tables[].sortKeyTypefalseenumThe type of the sort key of the DynamoDB table. Possible values: 'string', 'number'.
tables[].sortKeySeparatorfalsestringIn case of composite key, the item id (required by some functions) is the partition key concatenated with the sort key with this separator.
tables[].indicesfalsearrayThe array of DynamoDB table index configurations.
tables[].indices[].nametruestringThe name of the DynamoDB table index.
tables[].indices[].partitionKeyNametruestringThe name of the partition key of the DynamoDB table index.
tables[].indices[].partitionKeyTypetruestringThe type of the partition key of the DynamoDB table index. Possible values: 'string', 'number'.
tables[].indices[].sortKeyNamefalsestringThe name of the sort key of the DynamoDB table index.
tables[].indices[].sortKeyTypefalsestringThe type of the sort key of the DynamoDB table index. Possible values: 'string', 'number'.
tables[].indices[].sortKeySeparatorfalsestringIn case of composite key, the item id (required by some functions) is the partition key concatenated with the sort key with this separator.

Example:

{
    maxRetries: 9,
    tables: [{
        tableAlias: 'Products',
        tableName: 'Products_test',
        partitionKeyName: 'id',
        partitionKeyType: 'string',
        sortKeyName: 'timestamp',
        sortKeyType: 'number',
        sortKeySeparator: '$'
    }]
}

Provided function descriptions

FunctionDescription
getTableAliasReturns the configured alias of the DynamoDB table.
getTableNameReturns the configured name of the DynamoDB table.
getPartitionKeyNameReturns the configured name of the partition key of the DynamoDB table.
getPartitionKeyTypeReturns the configured type of the partition key of the DynamoDB table.
getSortKeyNameReturns the configured name of the sort key of the DynamoDB table.
getSortKeyTypeReturns the configured type of the sort key of the DynamoDB table.
getSortKeySeparatorReturns the string that separates the partition key and sort key.
createCreates an entry in the DynamoDB table and returns the extended entry.
createRawCreates an entry in the DynamoDB table and returns the raw entry.
createBatchCreates entries in the DynamoDB table and returns the extended entries.
createBatchRawCreates entries in the DynamoDB table and returns the raw entries.
getByIdReturns the extended entry from the DynamoDB table by its id. Throws 'Resource does not exist.' error if entry with the provided id does not exist.
getByIdRawReturns the raw entry from the DynamoDB table by its id. Throws 'Resource does not exist.' error if entry with the provided id does not exist.
getByIdsReturns the extended entries from the DynamoDB table by their ids using AWS DynamoDB batchGet.
getByIdsRawReturns the raw entries from the DynamoDB table by their ids using AWS DynamoDB batchGet.
scanFilteredReturns the extended entries from the DynamoDB table using AWS DynamoDB scan recursively.
scanFilteredRawReturns the raw entries from the DynamoDB table using AWS DynamoDB scan recursively.
deleteByIdDeletes the entry from the DynamoDB table by its id. If the base class was initiated with a deleter function, then it is called before the item is deleted. Returns the id of the deleted entry if the delete is successful (also shadows DynamoDB 'ResourceNotFoundException' and considers the delete successful) and throws DynamoDB exceptions otherwise.
deleteByIdsDeletes entries from the DynamoDB table by their ids using AWS DynamoDB batchWrite. If the base class was initiated with a deleter function, then it is called before the item is deleted. Returns the ids of the deleted entries if deletes are successful and throws DynamoDB exceptions otherwise.
deleteScanFilteredDeletes entries from the DynamoDB table by filter attributes (an attribute to match a specific value or an array to contain a specific item) using AWS DynamoDB scan and AWS DynamoDB batchWrite recursively. If the base class was initiated with a deleter function, then it is called before the item is deleted.
updateByIdUpdates the provided attributes of an entry of a DynamoDB table by id.
updateByIdWithDeleteUpdates and deletes the provided attributes of an entry of a DynamoDB table by id.
queryReturns the extended entries from the DynamoDB table that has the provided partition key using AWS DynamoDB query.
queryRawReturns the raw entries from the DynamoDB table that has the provided partition key using AWS DynamoDB query.
queryRecursePerforms a recursive query and returns the extended entries from the DynamoDB table that has the provided partition key using AWS DynamoDB query.
queryRecurseRawPerforms a recursive query and returns the raw entries from the DynamoDB table that has the provided partition key using AWS DynamoDB query.
queryBeginsWithReturns the extended entries from the DynamoDB table that has the provided partition key and whose sort key begins with the provided value using AWS DynamoDB query.
queryBeginsWithRawReturns the raw entries from the DynamoDB table that has the provided partition key and whose sort key begins with the provided value using AWS DynamoDB query.
queryBeginsWithRecursePerforms a recursive query and returns the extended entries from the DynamoDB table that has the provided partition key and whose sort key begins with the provided value using AWS DynamoDB query.
queryBeginsWithRecurseRawPerforms a recursive query and returns the raw entries from the DynamoDB table that has the provided partition key and whose sort key begins with the provided value using AWS DynamoDB query.
combineKeysCombines the provided DynamoDB Key object attributes by concatenating the partition key and the optional sort key by the configured separator.

Base class functions

FunctionDBDBCompositeDBMutableDBCompositeMutable
getTableAlias
getTableName
getPartitionKeyName
getPartitionKeyType
getSortKeyName
getSortKeyType
getSortKeySeparator
create
createRaw
createBatch
createBatchRaw
getById
getByIdRaw
getByIds
getByIdsRaw
scanFiltered
scanFilteredRaw
deleteById
deleteByIds
deleteScanFiltered
updateById
updateByIdWithDelete
query
queryRaw
queryRecurse
queryRecurseRaw
queryBeginsWith
queryBeginsWithRaw
queryBeginsWithRecurse
queryBeginsWithRecurseRaw
combineKeys
4.0.2

3 years ago

4.0.1

3 years ago

4.0.0

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.0

3 years ago

2.1.2

3 years ago

2.1.1

4 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago