@nerdware/ddb-single-table
Version:
A schema-based DynamoDB modeling tool, high-level API, and type-generator built to supercharge single-table designs!⚡
107 lines (106 loc) • 4.56 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Table = void 0;
const index_js_1 = require("../DdbClientWrapper/index.js");
const Model_js_1 = require("../Model/Model.js");
const TableKeysSchema_js_1 = require("../Schema/TableKeysSchema.js");
const createTable_js_1 = require("./createTable.js");
const ensureTableIsActive_js_1 = require("./ensureTableIsActive.js");
/**
* `Table` provides an easy-to-use API for managing your DynamoDB table and the
* {@link Model|Models} that use it. It is the primary entry point for DDBST.
*/
class Table {
// STATIC PROPERTIES:
static DEFAULT_MARSHALLING_CONFIGS = index_js_1.DdbClientWrapper.DEFAULT_MARSHALLING_CONFIGS;
// INSTANCE PROPERTIES:
tableName;
tableKeysSchema;
tableHashKey;
tableRangeKey;
indexes;
/** A wrapper-class around the DynamoDB client instance which greatly simplifies DDB operations. */
ddb;
logger;
isTableActive;
constructor({ tableName, tableKeysSchema, ddbClient, marshallingConfigs, logger = console.info, }) {
// Validate the TableKeysSchema and obtain the table's keys+indexes
const { tableHashKey, tableRangeKey, indexes } = TableKeysSchema_js_1.TableKeysSchema.validate(tableKeysSchema);
this.tableName = tableName;
this.tableKeysSchema = tableKeysSchema;
this.isTableActive = false;
this.logger = logger;
this.tableHashKey = tableHashKey;
this.tableRangeKey = tableRangeKey;
this.indexes = indexes;
// Attach proc exit handler which calls destroy method
process.on("exit", () => ddbClient.destroy());
// DDB client wrapper
this.ddb = new index_js_1.DdbClientWrapper({ ddbClient, tableName, marshallingConfigs });
}
// INSTANCE METHODS:
createTable = createTable_js_1.createTable;
ensureTableIsActive = ensureTableIsActive_js_1.ensureTableIsActive;
/** A `DescribeTable` wrapper for Table instances which call the method with their `tableName`. */
describeTable = async () => {
return await this.ddb.describeTable({ TableName: this.tableName });
};
/**
* Returns a validated ModelSchema with the TableKeysSchema merged in. Use this method to create a
* ModelSchema which can be provided to the `ItemTypeFromSchema` generic type-gen util to produce
* a complete Model-item type, even if the ModelSchema does not specify the table's keys.
*/
getModelSchema = (modelSchema) => {
return TableKeysSchema_js_1.TableKeysSchema.getMergedModelSchema({
tableKeysSchema: this.tableKeysSchema,
modelSchema,
});
};
/**
* Returns a new Model instance. This method simply offers a more concise alternative to invoking
* the bare Model constructor, i.e., the two examples below are equivalent:
* ```ts
* // A Table instance for both examples:
* const table = new Table({ ... table constructor args ... });
*
* // Example 1 using this method:
* const fooModel_1 = table.createModel(
* "FooModel",
* fooModelSchema,
* {
* // Optional ModelSchemaOptions
* allowUnknownAttributes: true
* // Using the createModel method, there's no need to provide
* // Model properties which are provided by the table, like
* // `"tableName"`, `"tableHashKey"`, `"tableRangeKey"`, etc.
* }
* );
*
* // Example 2 does the same as above, only with the bare Model constructor:
* const fooModel_2 = new Model(
* "FooModel",
* fooModelSchema,
* {
* // Same ModelSchemaOptions as above
* allowUnknownAttributes: true,
* ...table
* // When using the bare Model constructor, table properties like
* // `"tableName"`, `"tableHashKey"`, `"tableRangeKey"`, etc., must
* // be explicitly provided. An easy way to do this is to simply
* // spread a table instance object as shown here.
* }
* );
* ```
*/
createModel = (modelName, modelSchema, modelSchemaOptions = {}) => {
return new Model_js_1.Model(modelName, this.getModelSchema(modelSchema), {
...modelSchemaOptions,
ddb: this.ddb,
tableName: this.tableName,
tableHashKey: this.tableHashKey,
...(this.tableRangeKey && { tableRangeKey: this.tableRangeKey }),
...(this.indexes && { indexes: this.indexes }),
});
};
}
exports.Table = Table;