import { DynamoDB } from 'aws-sdk'; import { create } from './create'; import { read } from './read'; import { update } from './update'; import { find } from './find'; import { findAll, __findAll } from './findAll'; import { truncate } from './truncate'; (Symbol).asyncIterator = Symbol.for('asyncIterator'); export type Timestamp = number; export interface IHasId { /** * ID managed by consumer */ id: string; } export interface IHasVersion { /** * Monotomically increasing counter managed by store */ version: number; } export interface IHasMetadata { /** * Unix timestamp of record creation managed by store. * N.B. The store will always silently override this value if specified to support use cases which read an item, modify it and attempt to * write it back. */ created: Timestamp; /** * Unix timestamp of last record modification managed by store. * N.B. The store will always silently override this value if specified to support use cases which read an item, modify it and attempt to * write it back. */ modified: Timestamp; } export interface IConfiguration { client: DynamoDB.DocumentClient; tableName: string; } export interface ICruft { create(item: T & { version: 0 }): Promise; read(item: IHasId): Promise; update(item: T & IHasId & IHasVersion): Promise; find(fields: { [key: string]: string | number | boolean }): Promise; __findAll(fields: { [key: string]: string | number | boolean }, options?: { limit?: number }): Promise>; findAll(fields: { [key: string]: string | number | boolean }): AsyncIterableIterator; truncate(item: IHasId & IHasVersion): Promise; } export default ({ endpoint = process.env.AWS_DYNAMODB_ENDPOINT, region = process.env.AWS_REGION, tableName }): ICruft => { // hack - endpoint isn't a valid property according to the typings const client = new DynamoDB.DocumentClient(<{ endpoint: string }>{ apiVersion: '2012-08-10', endpoint, region }); return { create: create({ client, tableName }), read: read({ client, tableName }), update: update({ client, tableName }), __findAll: __findAll({ client, tableName }), findAll: findAll({ client, tableName }), find: find({ client, tableName }), truncate: truncate({ client, tableName }) }; };