UNPKG

dynamodb-toolbox

Version:

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

185 lines (184 loc) 7.48 kB
import { resetLinks } from '../../schema/utils/resetLinks.js'; import { ifThenElse } from '../../utils/ifThenElse.js'; import { overwrite } from '../../utils/overwrite.js'; import { lightObj } from '../utils/light.js'; import { MapSchema } from './schema.js'; /** * Define a new map attribute * * @param attributes Dictionary of attributes * @param props _(optional)_ Map Props */ export const map = (attributes, props = {}) => new MapSchema_(lightObj(attributes), props); /** * Map schema */ export class MapSchema_ extends MapSchema { /** * Tag schema values 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 MapSchema_(this.attributes, overwrite(this.props, { required: nextRequired })); } /** * Shorthand for `required('never')` */ optional() { return this.required('never'); } /** * Hide schema values after fetch commands and formatting */ hidden(nextHidden = true) { return new MapSchema_(this.attributes, overwrite(this.props, { hidden: nextHidden })); } /** * Tag schema values as a primary key attribute or linked to a primary key attribute */ key(nextKey = true) { return new MapSchema_(this.attributes, overwrite(this.props, { key: nextKey, required: 'always' })); } /** * Rename schema values before save commands */ savedAs(nextSavedAs) { return new MapSchema_(this.attributes, overwrite(this.props, { savedAs: nextSavedAs })); } /** * Provide a default value during Primary Key computing * * @param nextKeyDefault `keyInput | (() => keyInput)` */ keyDefault(nextKeyDefault) { return new MapSchema_(this.attributes, overwrite(this.props, { keyDefault: nextKeyDefault })); } /** * Provide a default value for attribute in PUT commands * * @param nextPutDefault `putAttributeInput | (() => putAttributeInput)` */ putDefault(nextPutDefault) { return new MapSchema_(this.attributes, overwrite(this.props, { putDefault: nextPutDefault })); } /** * Provide a default value for attribute in UPDATE commands * * @param nextUpdateDefault `updateAttributeInput | (() => updateAttributeInput)` */ updateDefault(nextUpdateDefault) { return new MapSchema_(this.attributes, 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 ifThenElse(this.props.key, new MapSchema_(this.attributes, overwrite(this.props, { keyDefault: nextDefault })), new MapSchema_(this.attributes, 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 MapSchema_(this.attributes, overwrite(this.props, { keyLink: nextKeyLink })); } /** * Provide a **linked** default value for attribute in PUT commands * * @param nextPutLink `putAttributeInput | ((putItemInput) => putAttributeInput)` */ putLink(nextPutLink) { return new MapSchema_(this.attributes, overwrite(this.props, { putLink: nextPutLink })); } /** * Provide a **linked** default value for attribute in UPDATE commands * * @param nextUpdateLink `unknown | ((updateItemInput) => updateAttributeInput)` */ updateLink(nextUpdateLink) { return new MapSchema_(this.attributes, 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 ifThenElse(this.props.key, new MapSchema_(this.attributes, overwrite(this.props, { keyLink: nextLink })), new MapSchema_(this.attributes, overwrite(this.props, { putLink: nextLink }))); } /** * Provide a custom validator for attribute in Primary Key computing * * @param nextKeyValidator `(keyAttributeInput) => boolean | string` */ keyValidate(nextKeyValidator) { return new MapSchema_(this.attributes, overwrite(this.props, { keyValidator: nextKeyValidator })); } /** * Provide a custom validator for attribute in PUT commands * * @param nextPutValidator `(putAttributeInput) => boolean | string` */ putValidate(nextPutValidator) { return new MapSchema_(this.attributes, overwrite(this.props, { putValidator: nextPutValidator })); } /** * Provide a custom validator for attribute in UPDATE commands * * @param nextUpdateValidator `(updateAttributeInput) => boolean | string` */ updateValidate(nextUpdateValidator) { return new MapSchema_(this.attributes, 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 ifThenElse(this.props.key, new MapSchema_(this.attributes, overwrite(this.props, { keyValidator: nextValidator })), new MapSchema_(this.attributes, overwrite(this.props, { putValidator: nextValidator }))); } pick(...attributeNames) { const nextAttributes = {}; for (const attributeName of attributeNames) { if (!(attributeName in this.attributes)) { continue; } nextAttributes[attributeName] = resetLinks(this.attributes[attributeName]); } return new MapSchema_(nextAttributes, this.props); } omit(...attributeNames) { const nextAttributes = {}; const attributeNamesSet = new Set(attributeNames); for (const _attributeName of Object.keys(this.attributes)) { if (attributeNamesSet.has(_attributeName)) { continue; } const attributeName = _attributeName; nextAttributes[attributeName] = resetLinks(this.attributes[attributeName]); } return new MapSchema_(nextAttributes, this.props); } and(additionalAttr) { const additionalAttributes = (typeof additionalAttr === 'function' ? additionalAttr(this) : additionalAttr); const nextAttributes = { ...this.attributes }; for (const [attributeName, additionalAttribute] of Object.entries(additionalAttributes)) { nextAttributes[attributeName] = additionalAttribute; } return new MapSchema_(nextAttributes, this.props); } clone(nextProps = {}) { return new MapSchema_(this.attributes, overwrite(this.props, nextProps)); } build(Action) { return new Action(this); } }