UNPKG

dynamodb-toolbox

Version:

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

233 lines (232 loc) 9 kB
/** * @debt circular "Remove & prevent imports from entity to schema" */ import type { UpdateValueInput } from '../../entity/actions/update/types.js'; import type { Paths, SchemaAction, ValidValue } from '../../schema/index.js'; import type { Transformer } from '../../transformers/index.js'; import type { If, NarrowObject, Overwrite, ValueOrGetter, Writable } from '../../types/index.js'; import type { Always, AtLeastOnce, Never, Schema, SchemaRequiredProp, Validator } from '../types/index.js'; import type { ResolveStringSchema, ResolvedStringSchema } from './resolve.js'; import { StringSchema } from './schema.js'; import type { StringSchemaProps } from './types.js'; type StringSchemer = <PROPS extends Omit<StringSchemaProps, 'enum'> = {}>(props?: NarrowObject<PROPS>) => StringSchema_<PROPS>; /** * Define a new schema of string type * * @param props _(optional)_ Schema Props */ export declare const string: StringSchemer; /** * String attribute (warm) */ export declare class StringSchema_<PROPS extends StringSchemaProps = StringSchemaProps> extends StringSchema<PROPS> { /** * Tag attribute as required. Possible values are: * - `'atLeastOnce'` _(default)_: Required in PUTs, optional in UPDATEs * - `'never'`: Optional in PUTs and UPDATEs * - `'always'`: Required in PUTs and UPDATEs * * @param nextRequired SchemaRequiredProp */ required<NEXT_IS_REQUIRED extends SchemaRequiredProp = AtLeastOnce>(nextRequired?: NEXT_IS_REQUIRED): StringSchema_<Overwrite<PROPS, { required: NEXT_IS_REQUIRED; }>>; /** * Shorthand for `required('never')` */ optional(): StringSchema_<Overwrite<PROPS, { required: Never; }>>; /** * Hide attribute after fetch commands and formatting */ hidden<NEXT_HIDDEN extends boolean = true>(nextHidden?: NEXT_HIDDEN): StringSchema_<Overwrite<PROPS, { hidden: NEXT_HIDDEN; }>>; /** * Tag attribute as a primary key attribute or linked to a primary attribute */ key<NEXT_KEY extends boolean = true>(nextKey?: NEXT_KEY): StringSchema_<Overwrite<PROPS, { key: NEXT_KEY; required: Always; }>>; /** * Rename attribute before save commands */ savedAs<NEXT_SAVED_AS extends string | undefined>(nextSavedAs: NEXT_SAVED_AS): StringSchema_<Overwrite<PROPS, { savedAs: NEXT_SAVED_AS; }>>; /** * Provide a finite list of possible values for attribute * (For typing reasons, enums are only available as attribute methods, not as input props) * * @param enum Possible values * @example * string().enum('foo', 'bar') */ enum<const NEXT_ENUM extends readonly ResolveStringSchema<this>[]>(...nextEnum: NEXT_ENUM): StringSchema_<Overwrite<PROPS, { enum: Writable<NEXT_ENUM>; }>>; /** * Shorthand for `enum(constantValue).default(constantValue)` * * @param constantValue Constant value * @example * string().const('foo') */ const<CONSTANT extends ResolveStringSchema<this>>(constant: CONSTANT): If<PROPS['key'], StringSchema_<Overwrite<PROPS, { enum: [CONSTANT]; keyDefault: unknown; }>>, StringSchema_<Overwrite<PROPS, { enum: [CONSTANT]; putDefault: unknown; }>>>; /** * Transform the attribute value in PUT commands OR Primary Key computing if attribute is tagged as key */ transform<TRANSFORMER extends Transformer<ResolvedStringSchema, ResolveStringSchema<this>>>(transform: TRANSFORMER): StringSchema_<Overwrite<PROPS, { transform: TRANSFORMER; }>>; /** * Provide a default value for attribute in Primary Key computing * * @param nextKeyDefault `keyAttributeInput | (() => keyAttributeInput)` */ keyDefault(nextKeyDefault: ValueOrGetter<ValidValue<this, { mode: 'key'; }>>): StringSchema_<Overwrite<PROPS, { keyDefault: unknown; }>>; /** * Provide a default value for attribute in PUT commands * * @param nextPutDefault `putAttributeInput | (() => putAttributeInput)` */ putDefault(nextPutDefault: ValueOrGetter<ValidValue<this>>): StringSchema_<Overwrite<PROPS, { putDefault: unknown; }>>; /** * Provide a default value for attribute in UPDATE commands * * @param nextUpdateDefault `updateAttributeInput | (() => updateAttributeInput)` */ updateDefault(nextUpdateDefault: ValueOrGetter<UpdateValueInput<this, { filled: true; }>>): StringSchema_<Overwrite<PROPS, { updateDefault: unknown; }>>; /** * Provide a default value for attribute in PUT commands OR Primary Key computing if attribute is tagged as key * * @param nextDefault `key/putAttributeInput | (() => key/putAttributeInput)` */ default(nextDefault: ValueOrGetter<If<PROPS['key'], ValidValue<this, { mode: 'key'; }>, ValidValue<this>>>): If<PROPS['key'], StringSchema_<Overwrite<PROPS, { keyDefault: unknown; }>>, StringSchema_<Overwrite<PROPS, { putDefault: unknown; }>>>; /** * Provide a **linked** default value for attribute in Primary Key computing * * @param nextKeyLink `keyAttributeInput | ((keyInput) => keyAttributeInput)` */ keyLink<SCHEMA extends Schema>(nextKeyLink: (keyInput: ValidValue<SCHEMA, { mode: 'key'; defined: true; }>) => ValidValue<this, { mode: 'key'; }>): StringSchema_<Overwrite<PROPS, { keyLink: unknown; }>>; /** * Provide a **linked** default value for attribute in PUT commands * * @param nextPutLink `putAttributeInput | ((putItemInput) => putAttributeInput)` */ putLink<SCHEMA extends Schema>(nextPutLink: (putItemInput: ValidValue<SCHEMA, { defined: true; }>) => ValidValue<this>): StringSchema_<Overwrite<PROPS, { putLink: unknown; }>>; /** * Provide a **linked** default value for attribute in UPDATE commands * * @param nextUpdateLink `unknown | ((updateItemInput) => updateAttributeInput)` */ updateLink<SCHEMA extends Schema>(nextUpdateLink: (updateItemInput: UpdateValueInput<SCHEMA, { defined: true; extended: false; }, Paths<SCHEMA>>) => UpdateValueInput<this, { filled: true; }>): StringSchema_<Overwrite<PROPS, { updateLink: unknown; }>>; /** * Provide a **linked** default value for attribute in PUT commands OR Primary Key computing if attribute is tagged as key * * @param nextLink `key/putAttributeInput | (() => key/putAttributeInput)` */ link<SCHEMA extends Schema>(nextLink: (keyOrPutItemInput: If<PROPS['key'], ValidValue<SCHEMA, { mode: 'key'; defined: true; }>, ValidValue<SCHEMA, { defined: true; }>>) => If<PROPS['key'], ValidValue<this, { mode: 'key'; }>, ValidValue<this>>): If<PROPS['key'], StringSchema_<Overwrite<PROPS, { keyLink: unknown; }>>, StringSchema_<Overwrite<PROPS, { putLink: unknown; }>>>; /** * Provide a custom validator for attribute in Primary Key computing * * @param nextKeyValidator `(keyAttributeInput) => boolean | string` */ keyValidate(nextKeyValidator: Validator<ValidValue<this, { mode: 'key'; defined: true; }>, this>): StringSchema_<Overwrite<PROPS, { keyValidator: Validator; }>>; /** * Provide a custom validator for attribute in PUT commands * * @param nextPutValidator `(putAttributeInput) => boolean | string` */ putValidate(nextPutValidator: Validator<ValidValue<this, { defined: true; }>, this>): StringSchema_<Overwrite<PROPS, { putValidator: Validator; }>>; /** * Provide a custom validator for attribute in UPDATE commands * * @param nextUpdateValidator `(updateAttributeInput) => boolean | string` */ updateValidate(nextUpdateValidator: Validator<UpdateValueInput<this, { filled: true; }>, this>): StringSchema_<Overwrite<PROPS, { updateValidator: Validator; }>>; /** * Provide a custom validator for attribute in PUT commands OR Primary Key computing if attribute is tagged as key * * @param nextValidator `(key/putAttributeInput) => boolean | string` */ validate(nextValidator: Validator<If<PROPS['key'], ValidValue<this, { mode: 'key'; defined: true; }>, ValidValue<this, { defined: true; }>>, this>): If<PROPS['key'], StringSchema_<Overwrite<PROPS, { keyValidator: Validator; }>>, StringSchema_<Overwrite<PROPS, { putValidator: Validator; }>>>; clone<NEXT_PROPS extends StringSchemaProps = {}>(nextprops?: NarrowObject<NEXT_PROPS>): StringSchema_<Overwrite<PROPS, NEXT_PROPS>>; build<ACTION extends SchemaAction<this> = SchemaAction<this>>(Action: new (schema: this) => ACTION): ACTION; } export {};