@nerdware/ddb-single-table
Version:
A schema-based DynamoDB modeling tool, high-level API, and type-generator built to supercharge single-table designs!⚡
66 lines • 4.64 kB
TypeScript
import { BaseSchema } from "./BaseSchema.js";
import type { TableKeysSchemaType, ModelSchemaType, TableKeysSchemaMetadata, MergeModelAndTableKeysSchema } from "./types/index.js";
import type { TableKeysAndIndexes } from "../Table/types/index.js";
/**
* This class and its `BaseSchema` parent currently only serve to organize schema-related types,
* validation methods, etc., but may be used to create schema instances in the future. This is
* currently not the case, as schema attributes would need to be nested under an instance property
* (e.g. `this.attributes`), which would require a lot of refactoring. If/when this is implemented,
* schema instances would also be given "metadata" props like "name", "version", "schemaType", etc.
*/
export declare class TableKeysSchema extends BaseSchema {
/**
* This function validates the provided `tableKeysSchema`, and if valid, returns a
* {@link TableKeysAndIndexes} object specifying the `tableHashKey`, `tableRangeKey`,
* and a map of any included `indexes`.
*
* This function performs the following validation checks:
*
* 1. Ensure all key/index attributes specify `isHashKey`, `isRangeKey`, or `index`.
* 2. Ensure exactly 1 table hash key, and 1 table range key are specified.
* 3. Ensure all key/index attribute `type`s are "string", "number", or "Buffer" (S/N/B in the DDB API).
* 4. Ensure all key/index attributes are `required`.
* 5. Ensure there are no duplicate index names.
*
* @param tableKeysSchema - The schema to validate.
* @param name - The `name` specified in the {@link SchemaMetadata|schema's metadata}.
* @returns A {@link TableKeysAndIndexes} object.
* @throws {SchemaValidationError} if the provided TableKeysSchema is invalid.
*/
static readonly validate: <const S extends TableKeysSchemaType>(tableKeysSchema: S, { name: schemaName }?: Pick<TableKeysSchemaMetadata, "name">) => TableKeysAndIndexes;
/**
* This function returns a ModelSchema with attributes and attribute-configs merged in from the
* provided TableKeysSchema, thereby preventing the need to repeat key/index attribute configs in
* every ModelSchema. Note that when using this "Partial ModelSchema" approach, the schema object
* provided to the `ItemTypeFromSchema` generic must be the "complete" ModelSchema returned from
* this function to ensure correct item typing.
*
* For ModelSchema in which it's desirable to include key/index attributes (e.g., to define a
* Model-specific `"alias"`), please note the table below that oulines which attribute-configs
* may be included and/or customized.
*
* | Key/Index Attribute Config | Can Include in ModelSchema | Can Customize in ModelSchema |
* | :------------------------- | :------------------------: | :--------------------------: |
* | `alias` | ✅ | ✅ |
* | `default` | ✅ | ✅ |
* | `transformValue` | ✅ | ✅ |
* | `validate` | ✅ | ✅ |
* | `type` | ✅ | ❌ |
* | `required` | ✅ | ❌ |
* | `isHashKey` | ❌ | ❌ |
* | `isRangeKey` | ❌ | ❌ |
* | `index` | ❌ | ❌ |
*
* > Note: `schema` and `oneOf` are not included in the table above, as they are not valid for
* key/index attributes which must be of type "string", "number", or "Buffer".
*
* @param tableKeysSchema - The TableKeysSchema to merge into the ModelSchema.
* @param modelSchema - The ModelSchema to merge the TableKeysSchema into.
* @throws {SchemaValidationError} if the provided ModelSchema contains invalid key/index attribute configs.
*/
static readonly getMergedModelSchema: <const TableKeysSchema extends TableKeysSchemaType, const ModelSchema extends ModelSchemaType<TableKeysSchema>>({ tableKeysSchema, modelSchema, }: {
tableKeysSchema: TableKeysSchema;
modelSchema: ModelSchema;
}) => MergeModelAndTableKeysSchema<TableKeysSchema, ModelSchema>;
}
//# sourceMappingURL=TableKeysSchema.d.ts.map