UNPKG

betterddb

Version:

A definition-based DynamoDB wrapper library that provides a schema-driven and fully typesafe DAL.

165 lines 5.2 kB
import {} from "zod"; import { QueryBuilder } from "./builders/query-builder.js"; import { ScanBuilder } from "./builders/scan-builder.js"; import { UpdateBuilder } from "./builders/update-builder.js"; import { CreateBuilder } from "./builders/create-builder.js"; import { GetBuilder } from "./builders/get-builder.js"; import { DeleteBuilder } from "./builders/delete-builder.js"; import {} from "@aws-sdk/lib-dynamodb"; import { BatchGetBuilder } from "./builders/batch-get-builder.js"; /** * BetterDDB is a definition-based DynamoDB wrapper library. */ export class BetterDDB { schema; tableName; entityType; client; keys; timestamps; counter; constructor(options) { this.schema = options.schema; this.tableName = options.tableName; this.entityType = options.entityType?.toUpperCase(); this.keys = options.keys; this.client = options.client; this.timestamps = options.timestamps ?? false; this.counter = options.counter ?? false; } getCounter() { return this.counter; } getKeys() { return this.keys; } getTableName() { return this.tableName; } getClient() { return this.client; } getSchema() { return this.schema; } getTimestamps() { return this.timestamps; } getEntityType() { return this.entityType; } // Helper: Retrieve the key value from a KeyDefinition. getKeyValue(def, rawKey) { if (typeof def === "string" || typeof def === "number" || typeof def === "symbol") { return String(rawKey[def]); } else { return def.build(rawKey); } } /** * Build the primary key from a raw key object. */ buildKey(rawKey) { const keyObj = {}; // For primary (partition) key: const pkConfig = this.keys.primary; keyObj[pkConfig.name] = typeof pkConfig.definition === "string" || typeof pkConfig.definition === "number" || typeof pkConfig.definition === "symbol" ? String(rawKey[pkConfig.definition]) : pkConfig.definition.build(rawKey); // For sort key, if defined: if (this.keys.sort) { const skConfig = this.keys.sort; keyObj[skConfig.name] = typeof skConfig.definition === "string" || typeof skConfig.definition === "number" || typeof skConfig.definition === "symbol" ? String(rawKey[skConfig.definition]) : skConfig.definition.build(rawKey); } return keyObj; } /** * Build index attributes for each defined GSI. */ buildIndexes(rawItem) { const indexAttributes = {}; if (this.keys.gsis) { for (const gsiName in this.keys.gsis) { const gsiConfig = this.keys.gsis[gsiName]; if (!gsiConfig) continue; // Compute primary index attribute. const primaryConfig = gsiConfig.primary; indexAttributes[primaryConfig.name] = typeof primaryConfig.definition === "string" || typeof primaryConfig.definition === "number" || typeof primaryConfig.definition === "symbol" ? String(rawItem[primaryConfig.definition]) : primaryConfig.definition.build(rawItem); // Compute sort index attribute if provided. if (gsiConfig?.sort) { const sortConfig = gsiConfig.sort; indexAttributes[sortConfig.name] = typeof sortConfig.definition === "string" || typeof sortConfig.definition === "number" || typeof sortConfig.definition === "symbol" ? String(rawItem[sortConfig.definition]) : sortConfig.definition.build(rawItem); } } } return indexAttributes; } /** * Create an item: * - Computes primary key and index attributes, * - Optionally injects timestamps, * - Validates the item and writes it to DynamoDB. */ create(item) { return new CreateBuilder(this, item); } /** * Get an item by its primary key. */ get(rawKey) { return new GetBuilder(this, rawKey); } /** * Get multiple items by their primary keys. */ batchGet(rawKeys) { return new BatchGetBuilder(this, rawKeys); } /** * Update an item. */ update(key) { return new UpdateBuilder(this, key); } /** * Delete an item. */ delete(rawKey) { return new DeleteBuilder(this, rawKey); } /** * Query items. */ query(key) { return new QueryBuilder(this, key); } /** * Scan for items. */ scan() { return new ScanBuilder(this); } } //# sourceMappingURL=betterddb.js.map