dynamodb-data-mapper-extra

An extended version of @aws/dynamodb-data-mapper


Keywords
dynamodb, dynamodb data mapper, data mapper
License
GPL-3.0
Install
npm install dynamodb-data-mapper-extra@1.0.1

Documentation

DynamoDB Data Mapper Extra

This library provides a DataMapperExtra class that improve the original DataMapper class created by Amazon (@aws/dynamodb-data-mapper).

Supported operations

Operations are inherited from the original DataMapper class, inherited operations documentation is available here

getOrDefault

Retrieves an item from DynamoDB. If no item with the specified key was found, the returned promise will give you the default value.

Method declaration:

getOrDefault<T>(item: T, defaultValue?: any, options?: GetOptions): Promise<T | any | null>;

Example:

const myInstance = await mapper.getOrDefault(Object.assign(
    new MyDomainObject,
    {id: 'foo', createdAt: new Date(946684800000)}
), myDefaultValue);

If not found in DynamoDB, your variable myInstance will be setted with myDefaultValue variable value.

getOrNull

Retrieves an item from DynamoDB. If no item with the specified key was found, the returned promise will give you NULL.

Method declaration:

getOrNull<T>(item: T, options?: GetOptions): Promise<T | null>;

Example:

const myInstance: MyDomainObject = await mapper.getOrNull(Object.assign(
    new MyDomainObject,
    {id: 'foo', createdAt: new Date(946684800000)}
));

the variable myInstance could be null, you can handle your own logic.

if (isNull(myInstance)) {
    // implements your logic
}

exists

Returns a boolean, TRUE is the document exists in DynamoDB, FALSE if not.

Method declaration:

exists<T>(item: T, options?: GetOptions): Promise<boolean>;

Example:

const doExist = await mapper.exists(Object.assign(
    new MyDomainObject,
    {id: 'foo', createdAt: new Date(946684800000)}
));

if (doExist) {
    console.log('MyDomainObject exists.');
} else {
    console.log('MyDomainObject do not exists.');
}

findAll

Returns an array of all items.

Method declaration:

findAll<T>(item: ZeroArgumentsConstructor<T>, options?: ScanOptions | ParallelScanWorkerOptions): Promise<T[]>;

Example:

const items: MyDomainObject[] = await mapper.findAll(MyDomainObject, {});

_fetchOne

Protected method that returns a promise of one element, null, or an exception depending of the configuration.

Method declaration:

protected _fetchOne<T>(iterator: QueryIterator<T>, errorIfNotFound?: boolean): Promise<T | null>;

Example:

export class BaseRepository extends DataMapperExtra{

    constructor(){
        super({
            client: DynamoDbClient,
            tableNamePrefix: TABLE_NAME_PREFIX
        });
    }
}

class PaymentServiceProviderRepository extends BaseRepository{

    findOneByAuthorizationToken(authorizationToken:string, nullIfNotFound:boolean = false): Promise<PaymentServiceProvider|null>{

        const params = {
            subject: 'authorizationToken',
            type: 'Equals',
            object: authorizationToken
        };

        const options = {
            indexName: 'authorizationTokenIndex'
        };

        return this._fetchOne(this.query(PaymentServiceProvider, params, options), !nullIfNotFound);
    }
}

_fetchArray

Protected method that returns array of elements using promise.

Method declaration:

protected _fetchArray<T>(iterator: QueryIterator<T>): Promise<T[]>;

Example:

export class BaseRepository extends DataMapperExtra{

    constructor(){
        super({
            client: DynamoDbClient,
            tableNamePrefix: TABLE_NAME_PREFIX
        });
    }
}

class PaymentServiceProviderRepository extends BaseRepository{
    
    findByIsPublic(isPublic:boolean, projection:Array<string> = PAYMENT_SERVICE_PROVIDER_PROJECTION_LIST): Promise<PaymentServiceProvider[]|null>{

        const options:ScanOptions = {
            projection: projection,
            filter: {
                subject: 'isPublic',
                type: 'Equals',
                object: isPublic
            }
        };

        return this._fetchArray(this.scan(PaymentServiceProvider, options));
    }
}