UNPKG

dynamodb-toolbox

Version:

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

31 lines (30 loc) 1.15 kB
import { DynamoDBToolboxError } from '../errors/index.js'; import { $interceptor, $sentArgs } from './constants.js'; import { buildEntitySchema, doesSchemaValidateTableSchema } from './utils/index.js'; export class Entity { constructor({ name, table, schema, computeKey, entityAttribute = true, timestamps = true, meta = {} }) { this.type = 'entity'; this.entityName = name; this.table = table; this.entityAttribute = entityAttribute; this.timestamps = timestamps; if (computeKey === undefined && !doesSchemaValidateTableSchema(schema, table)) { throw new DynamoDBToolboxError('entity.invalidSchema', { message: `Entity ${name} schema does not follow its table primary key schema` }); } this.attributes = schema.attributes; this.schema = buildEntitySchema({ ...this, schema }); this.schema.check(); this.computeKey = computeKey; this.meta = meta; } build(Action) { return new Action(this); } } export class EntityAction { constructor(entity) { this.entity = entity; } }