@nerdware/ddb-single-table
Version:
A schema-based DynamoDB modeling tool, high-level API, and type-generator built to supercharge single-table designs!⚡
68 lines • 3.36 kB
TypeScript
import { BaseSchema } from "./BaseSchema.js";
import type { TableKeysAndIndexes } from "../Table/index.js";
import type { ModelSchemaType, ModelSchemaMetadata, ModelSchemaEntries } from "./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 ModelSchema extends BaseSchema {
static readonly DEFAULT_OPTIONS: {
readonly allowUnknownAttributes: false;
readonly autoAddTimestamps: false;
};
static readonly TIMESTAMP_ATTRIBUTES: {
readonly createdAt: {
readonly type: "Date";
readonly required: true;
readonly default: () => Date;
readonly validate: (value: any) => value is string | Date;
};
readonly updatedAt: {
readonly transformValue: {
readonly toDB: () => Date;
};
readonly type: "Date";
readonly required: true;
readonly default: () => Date;
readonly validate: (value: any) => value is string | Date;
};
};
/**
* This function validates the provided `modelSchema`, and if valid, returns an
* object specifying the Model's alias maps:
* - `attributesToAliasesMap`
* - `aliasesToAttributesMap`
*
* This function performs the following validation checks:
*
* 1. Ensure ModelSchema does not specify key-attribute configs which are only valid in the
* TableKeysSchema (e.g. "isHashKey", "isRangeKey", "index")
* 2. Ensure all "alias" values are unique
*
* @param modelSchema - The schema to validate.
* @param name - A name to identify the schema in any error messages.
* @returns An object specifying the Model's alias maps.
* @throws {SchemaValidationError} if the provided ModelSchema is invalid.
*/
static readonly validate: (modelSchema: ModelSchemaType, { name: schemaName }: Pick<ModelSchemaMetadata, "name">) => {
attributesToAliasesMap: Record<string, string>;
aliasesToAttributesMap: Record<string, string>;
};
/**
* This function provides sorted ModelSchema entries for IO-Actions. After passing the provided
* `modelSchema` into Object.entries, the resulting entries array is sorted as follows:
*
* 1. The `tableHashKey` is sorted to the front.
* 2. The `tableRangeKey`, if present, goes after the `tableHashKey`.
* 3. Index PKs, if any, go after the `tableRangeKey`.
* 4. For all other attributes, the order in the provided `modelSchema` is preserved.
*
* @param modelSchema - The ModelSchema object to use to obtain sorted entries.
* @returns An array of sorted ModelSchema entries.
*/
static readonly getSortedSchemaEntries: (modelSchema: ModelSchemaType, { tableHashKey, tableRangeKey, indexes }: TableKeysAndIndexes) => ModelSchemaEntries;
}
//# sourceMappingURL=ModelSchema.d.ts.map