UNPKG

dynamodb-toolbox

Version:

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

185 lines (184 loc) 7.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.BooleanSchema_ = exports.boolean = void 0; const ifThenElse_js_1 = require("../../utils/ifThenElse.js"); const overwrite_js_1 = require("../../utils/overwrite.js"); const writable_js_1 = require("../../utils/writable.js"); const schema_js_1 = require("./schema.js"); /** * Define a new schema of boolean type * * @param props _(optional)_ Schema Props */ const boolean = (props = {}) => new BooleanSchema_(props); exports.boolean = boolean; /** * Boolean attribute (warm) */ class BooleanSchema_ extends schema_js_1.BooleanSchema { /** * 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(nextRequired = 'atLeastOnce') { return new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { required: nextRequired })); } /** * Shorthand for `required('never')` */ optional() { return this.required('never'); } /** * Hide attribute after fetch commands and formatting */ hidden(nextHidden = true) { return new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { hidden: nextHidden })); } /** * Tag attribute as a primary key attribute or linked to a primary attribute */ key(nextKey = true) { return new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { key: nextKey, required: 'always' })); } /** * Rename attribute before save commands */ savedAs(nextSavedAs) { return new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { savedAs: nextSavedAs })); } /** * 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(...nextEnum) { return new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { enum: (0, writable_js_1.writable)(nextEnum) })); } /** * Shorthand for `enum(constantValue).default(constantValue)` * * @param constantValue Constant value * @example * string().const('foo') */ const(constant) { return (0, ifThenElse_js_1.ifThenElse)(this.props.key, new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { enum: [constant], keyDefault: constant })), new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { enum: [constant], putDefault: constant }))); } /** * Transform the attribute value in PUT commands OR Primary Key computing if attribute is tagged as key */ transform(transform) { return new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { transform })); } /** * Provide a default value for attribute in Primary Key computing * * @param nextKeyDefault `keyAttributeInput | (() => keyAttributeInput)` */ keyDefault(nextKeyDefault) { return new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { keyDefault: nextKeyDefault })); } /** * Provide a default value for attribute in PUT commands * * @param nextPutDefault `putAttributeInput | (() => putAttributeInput)` */ putDefault(nextPutDefault) { return new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { putDefault: nextPutDefault })); } /** * Provide a default value for attribute in UPDATE commands * * @param nextUpdateDefault `updateAttributeInput | (() => updateAttributeInput)` */ updateDefault(nextUpdateDefault) { return new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { updateDefault: nextUpdateDefault })); } /** * 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) { return (0, ifThenElse_js_1.ifThenElse)(this.props.key, new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { keyDefault: nextDefault })), new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { putDefault: nextDefault }))); } /** * Provide a **linked** default value for attribute in Primary Key computing * * @param nextKeyLink `keyAttributeInput | ((keyInput) => keyAttributeInput)` */ keyLink(nextKeyLink) { return new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { keyLink: nextKeyLink })); } /** * Provide a **linked** default value for attribute in PUT commands * * @param nextPutLink `putAttributeInput | ((putItemInput) => putAttributeInput)` */ putLink(nextPutLink) { return new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { putLink: nextPutLink })); } /** * Provide a **linked** default value for attribute in UPDATE commands * * @param nextUpdateLink `unknown | ((updateItemInput) => updateAttributeInput)` */ updateLink(nextUpdateLink) { return new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { updateLink: nextUpdateLink })); } /** * 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(nextLink) { return (0, ifThenElse_js_1.ifThenElse)(this.props.key, new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { keyLink: nextLink })), new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { putLink: nextLink }))); } /** * Provide a custom validator for attribute in Primary Key computing * * @param nextKeyValidator `(keyAttributeInput) => boolean | string` */ keyValidate(nextKeyValidator) { return new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { keyValidator: nextKeyValidator })); } /** * Provide a custom validator for attribute in PUT commands * * @param nextPutValidator `(putAttributeInput) => boolean | string` */ putValidate(nextPutValidator) { return new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { putValidator: nextPutValidator })); } /** * Provide a custom validator for attribute in UPDATE commands * * @param nextUpdateValidator `(updateAttributeInput) => boolean | string` */ updateValidate(nextUpdateValidator) { return new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { updateValidator: nextUpdateValidator })); } /** * 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) { return (0, ifThenElse_js_1.ifThenElse)(this.props.key, new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { keyValidator: nextValidator })), new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, { putValidator: nextValidator }))); } clone(nextProps = {}) { return new BooleanSchema_((0, overwrite_js_1.overwrite)(this.props, nextProps)); } build(Action) { return new Action(this); } } exports.BooleanSchema_ = BooleanSchema_;