UNPKG

lemon-core

Version:
193 lines (192 loc) 5.48 kB
import { GeneralItem, Incrementable } from 'lemon-model'; import AWS from 'aws-sdk'; export declare type KEY_TYPE = 'number' | 'string'; export interface DynamoOption { tableName: string; idName: string; sortName?: string; idType?: KEY_TYPE; sortType?: KEY_TYPE; } /** * type `Updatable`: parameter for updateItem * - update field * - 'setIndex': array of [index, value] - replace elements in list field * - 'removeIndex': array of indices - remove elements from list field */ export interface Updatable { [key: string]: GeneralItem['key'] | { setIndex: [number, string | number][]; } | { removeIndex: number[]; }; } /** * class: `DynamoService` * - basic CRUD service for AWS DynamoDB. */ export declare class DynamoService<T extends GeneralItem> { protected options: DynamoOption; constructor(options: DynamoOption); /** * say hello of identity. */ hello: () => string; /** * simple instance maker. * @param region (default as `ap-northeast-2`) */ static instance(region?: string): { dynamo: AWS.DynamoDB; dynamostr: AWS.DynamoDBStreams; dynamodoc: AWS.DynamoDB.DocumentClient; }; /** * export to test.. */ static normalize: (data: any) => any; /** * prepare `CreateTable` payload. * * @param ReadCapacityUnits * @param WriteCapacityUnits * @param StreamEnabled */ prepareCreateTable(ReadCapacityUnits?: number, WriteCapacityUnits?: number, StreamEnabled?: boolean): { TableName: string; KeySchema: { AttributeName: string; KeyType: string; }[]; AttributeDefinitions: { AttributeName: string; AttributeType: any; }[]; ProvisionedThroughput: { ReadCapacityUnits: number; WriteCapacityUnits: number; }; StreamSpecification: { StreamEnabled: boolean; StreamViewType: string; }; }; /** * prepare `DeleteTable` payload. */ prepareDeleteTable(): { TableName: string; }; /** * prepare `SaveItem` payload. * * @param id partition-key * @param item */ prepareSaveItem(id: string, item: T): { TableName: string; Item: any; }; /** * prepare `Key` by id + sort key. * * @param id partition-key * @param sort sort-key */ prepareItemKey(id: string, sort: any): { TableName: string; Key: { [x: string]: string; }; }; /** * prepare `UpdateItem` payload. * * @param id partition-key * @param sort sort-key * @param $update update set * @param $increment increment set. */ prepareUpdateItem(id: string, sort: any, $update: Updatable, $increment?: Incrementable): any; /** * create-table * * @param ReadCapacityUnits * @param WriteCapacityUnits */ createTable(ReadCapacityUnits?: number, WriteCapacityUnits?: number): Promise<import("aws-sdk/lib/request").PromiseResult<AWS.DynamoDB.CreateTableOutput, AWS.AWSError>>; /** * delete-table * */ deleteTable(): Promise<import("aws-sdk/lib/request").PromiseResult<AWS.DynamoDB.DeleteTableOutput, AWS.AWSError>>; /** * read-item * - read whole data of item. * * @param id * @param sort */ readItem(id: string, sort?: string | number): Promise<T>; /** * save-item * - save whole data with param (use update if partial save) * * **WARN** overwrited if exists. * * @param id * @param item */ saveItem(id: string, item: T): Promise<T>; /** * delete-item * - destroy whole data of item. * * @param id * @param sort */ deleteItem(id: string, sort?: string | number): Promise<T>; /** * update-item (or increment-item) * - update or create if not exists. * * @param id * @param sort * @param updates * @param increments */ updateItem(id: string, sort: string | number, updates: Updatable, increments?: Incrementable): Promise<T>; } /** **************************************************************************************************************** * Dummy Dynamo Service ** ****************************************************************************************************************/ /** * class: `DummyDynamoService` * - service in-memory dummy data */ export declare class DummyDynamoService<T extends GeneralItem> extends DynamoService<T> { constructor(dataFile: string, options: DynamoOption); private buffer; load(data: T[]): void; /** * say hello() */ hello: () => string; /** * ONLY FOR DUMMY * - send list of data. * * @param page page number starts from 1 * @param limit limit of count. */ listItems(page?: number, limit?: number): Promise<{ page: number; limit: number; total: number; list: T[]; }>; readItem(id: string, sort?: string | number): Promise<T>; saveItem(id: string, item: T): Promise<T>; deleteItem(id: string, sort?: string | number): Promise<T>; updateItem(id: string, sort: string | number, updates: T, increments?: Incrementable): Promise<T>; }