UNPKG

dynamodb-toolbox

Version:

Lightweight and type-safe query builder for DynamoDB and TypeScript.

52 lines (51 loc) 1.73 kB
import { DynamoDBToolboxError } from '../errors/index.js'; import { isString } from '../utils/validation/isString.js'; import { $entities, $interceptor, $sentArgs } from './constants.js'; export class Table { constructor({ documentClient, /** * @debt v3 "To rename tableName" */ name, partitionKey, sortKey, indexes = {}, entityAttributeSavedAs = '_et', meta = {} }) { this.getDocumentClient = () => { if (this.documentClient === undefined) { throw new DynamoDBToolboxError('actions.missingDocumentClient', { message: 'You need to set a document client on your table to send a command' }); } return this.documentClient; }; this.documentClient = documentClient; this.tableName = name; this.partitionKey = partitionKey; if (sortKey) { this.sortKey = sortKey; } this.indexes = indexes; this.entityAttributeSavedAs = entityAttributeSavedAs; this[$entities] = []; this.meta = meta; } getName() { if (this.tableName === undefined) { throw new DynamoDBToolboxError('table.missingTableName', { message: 'Please specify a table name in your Table constructor or in your command options.' }); } if (isString(this.tableName)) { return this.tableName; } else { return this.tableName(); } } build(Action) { return new Action(this, this[$entities]); } } export class TableAction { constructor(table, entities = []) { this.table = table; this[$entities] = entities; } }