6.1.0 • Published 5 years ago

@typemon/dynamodb-service v6.1.0

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
5 years ago

DynamoDB Service - npm-version npm-downloads

DynamoDB with Dependency injection for TypeScript

About

We wanted to use DynamoDB with OOP, dependency injection, and many other features. But I did not find a satisfactory library and eventually I created it. It is a DynamoDB service with very easy to use expressions and empty string handling. I hope this helps.

Features

Installation

$ npm install @typemon/dynamodb-service
              @typemon/dynamodb-expression
              @typemon/di
              aws-sdk
import {
    /* DynamoDB service class */
    DynamoDBService,

    /* Injection tokens */
    TABLE_NAME_TOKEN, OPTIONS_TOKEN,

    /* Types, Interfaces */
    Options, QueryOptions, QueryResult,

    /* Empty string namespace */
    EmptyString
} from '@typemon/dynamodb-service';

Usage

Although you can do direct instantiation, we recommend using injectors.

  • Table name tokens are required.
const injector: Injector = Injector.create([
    Provider.useValue({
        identifier: TABLE_NAME_TOKEN,
        value: 'monster-space-network'
    }),
    Provider.useValue({
        identifier: OPTIONS_TOKEN,
        value: {
            region: 'ap-northeast-2'
        }
    }),
    Provider.use(DynamoDBService)
]);

const dynamodb: DynamoDBService = injector.get(DynamoDBService);

Put

const typemon: Monster = {
    partition: 'monster', // partition key
    name: 'typemon',      // sort key
    description: 'TypeScript Monster',
    packages: [
        'ioc-container',
        'dynamodb-service',
        'dynamodb-expression'
    ]
};

await dynamodb.put(typemon);

Get

const typemon: Monster | null = await dynamodb.get({
    partition: 'monster', // partition key
    name: 'typemon',      // sort key
});

console.log(typemon);
{
    partition: 'monster',
    packages: [
        'ioc-container',
        'dynamodb-service',
        'dynamodb-expression'
    ],
    description: 'TypeScript Monster',
    name: 'typemon'
}

Update

const updateExpressions: ReadonlyArray<UpdateExpression> = [
    listAppend('packages', [
        'serverless',
        'stack'
    ])
];
const typemon: Monster = await dynamodb.update({
    partition: 'monster', // partition key
    name: 'typemon',      // sort key
}, updateExpressions);

console.log(typemon);
{
    packages: [
        'ioc-container',
        'dynamodb-service',
        'dynamodb-expression',
        'serverless',
        'stack'
    ],
    partition: 'monster',
    description: 'TypeScript Monster',
    name: 'typemon'
}

Delete

await dynamodb.delete({
    partition: 'monster', // partition key
    name: 'typemon',      // sort key
});

Query

const keyConditionExpression: Expression = equal('partition', 'monster');
const filterExpression: Expression = equal(size('packages'), 0);
const monsters: QueryResult<Monster> = await dynamodb.query(
    keyConditionExpression,
    filterExpression,
    { limit: 1 }
);

console.log(monsters);
{
    items: [
        {
            partition: 'monster',
            packages: [],
            description: 'Minecraft Monster',
            name: 'minemon'
        }
    ],
    lastEvaluatedKey: { name: 'minemon', partition: 'monster' }
}

Empty String Handling

  • Handles empty strings and identifiers through serialization and deserialization functions.
const item: Item = {
    emptyString: '',
    emptyStringInArray: [''],
    emptyStringInIndex: {
        emptyString: ''
    }
};
const serialized: Item = EmptyString.serialize(item);
const deserialized: Item = EmptyString.deserialize(serialized);

console.log(serialized, deserialized);
{
    emptyString: 'EMPTY_STRING_IDENTIFIER',
    emptyStringInArray: ['EMPTY_STRING_IDENTIFIER'],
    emptyStringInIndex: {
        emptyString: 'EMPTY_STRING_IDENTIFIER'
    }
}
{
    emptyString: '',
    emptyStringInArray: [''],
    emptyStringInIndex: {
        emptyString: ''
    }
}

If you want to disable the handling of empty strings, set the option to false.

Provider.useValue(OPTIONS_TOKEN, {
    region: 'ap-northeast-2',
    useEmptyStringHandling: false // Disable empty string handling.
})