dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
167 lines (166 loc) • 6.88 kB
JavaScript
import { ifThenElse } from '../../utils/ifThenElse.js';
import { overwrite } from '../../utils/overwrite.js';
import { light } from '../utils/light.js';
import { RecordSchema } from './schema.js';
/**
* Define a new record attribute
* Note that record keys and elements have constraints. They must be:
* - Required (required: AtLeastOnce)
* - Displayed (hidden: false)
* - Not key (key: false)
* - Not renamed (savedAs: undefined)
* - Not defaulted (defaults: undefined)
*
* @param keys Keys (With constraints)
* @param elements Attribute (With constraints)
* @param props _(optional)_ Record Props
*/
export const record = (keys, elements, props = {}) => new RecordSchema_(light(keys), light(elements), props);
/**
* Record attribute interface
*/
export class RecordSchema_ extends RecordSchema {
/**
* 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 RecordSchema_(this.keys, this.elements, 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 RecordSchema_(this.keys, this.elements, overwrite(this.props, { hidden: nextHidden }));
}
/**
* Tag attribute as a primary key attribute or linked to a primary attribute
*/
key(nextKey = true) {
return new RecordSchema_(this.keys, this.elements, overwrite(this.props, { key: nextKey, required: 'always' }));
}
/**
* Rename attribute before save commands
*/
savedAs(nextSavedAs) {
return new RecordSchema_(this.keys, this.elements, overwrite(this.props, { savedAs: nextSavedAs }));
}
/**
* Tag record as partial
*/
partial(nextPartial = true) {
return new RecordSchema_(this.keys, this.elements, overwrite(this.props, { partial: nextPartial }));
}
/**
* Provide a default value for attribute in Primary Key computing
*
* @param nextKeyDefault `keyAttributeInput | (() => keyAttributeInput)`
*/
keyDefault(nextKeyDefault) {
return new RecordSchema_(this.keys, this.elements, overwrite(this.props, { keyDefault: nextKeyDefault }));
}
/**
* Provide a default value for attribute in PUT commands
*
* @param nextPutDefault `putAttributeInput | (() => putAttributeInput)`
*/
putDefault(nextPutDefault) {
return new RecordSchema_(this.keys, this.elements, overwrite(this.props, { putDefault: nextPutDefault }));
}
/**
* Provide a default value for attribute in UPDATE commands
*
* @param nextUpdateDefault `updateAttributeInput | (() => updateAttributeInput)`
*/
updateDefault(nextUpdateDefault) {
return new RecordSchema_(this.keys, this.elements, 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 RecordSchema_(this.keys, this.elements, overwrite(this.props, { keyDefault: nextDefault })), new RecordSchema_(this.keys, this.elements, 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 RecordSchema_(this.keys, this.elements, overwrite(this.props, { keyLink: nextKeyLink }));
}
/**
* Provide a **linked** default value for attribute in PUT commands
*
* @param nextPutLink `putAttributeInput | ((putItemInput) => putAttributeInput)`
*/
putLink(nextPutLink) {
return new RecordSchema_(this.keys, this.elements, overwrite(this.props, { putLink: nextPutLink }));
}
/**
* Provide a **linked** default value for attribute in UPDATE commands
*
* @param nextUpdateLink `unknown | ((updateItemInput) => updateAttributeInput)`
*/
updateLink(nextUpdateLink) {
return new RecordSchema_(this.keys, this.elements, 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 RecordSchema_(this.keys, this.elements, overwrite(this.props, { keyLink: nextLink })), new RecordSchema_(this.keys, this.elements, overwrite(this.props, { putLink: nextLink })));
}
/**
* Provide a custom validator for attribute in Primary Key computing
*
* @param nextKeyValidator `(keyAttributeInput) => boolean | string`
*/
keyValidate(nextKeyValidator) {
return new RecordSchema_(this.keys, this.elements, overwrite(this.props, { keyValidator: nextKeyValidator }));
}
/**
* Provide a custom validator for attribute in PUT commands
*
* @param nextPutValidator `(putAttributeInput) => boolean | string`
*/
putValidate(nextPutValidator) {
return new RecordSchema_(this.keys, this.elements, overwrite(this.props, { putValidator: nextPutValidator }));
}
/**
* Provide a custom validator for attribute in UPDATE commands
*
* @param nextUpdateValidator `(updateAttributeInput) => boolean | string`
*/
updateValidate(nextUpdateValidator) {
return new RecordSchema_(this.keys, this.elements, 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 RecordSchema_(this.keys, this.elements, overwrite(this.props, { keyValidator: nextValidator })), new RecordSchema_(this.keys, this.elements, overwrite(this.props, { putValidator: nextValidator })));
}
clone(nextProps = {}) {
return new RecordSchema_(this.keys, this.elements, overwrite(this.props, nextProps));
}
build(Action) {
return new Action(this);
}
}