dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
52 lines (51 loc) • 2.02 kB
JavaScript
import { resetLinks } from '../../schema/utils/resetLinks.js';
import { overwrite } from '../../utils/overwrite.js';
import { lightObj } from '../utils/light.js';
import { ItemSchema } from './schema.js';
/**
* Define a new item schema
*
* @param attributes Dictionary of attributes
*/
export const item = (attributes) => new ItemSchema_(lightObj(attributes));
export class ItemSchema_ extends ItemSchema {
/**
* Reject additional (undeclared) attributes when parsing input values
*/
strict(nextStrict = true) {
return new ItemSchema_(this.attributes, overwrite(this.props, { strict: nextStrict }));
}
pick(...attributeNames) {
const nextAttributes = {};
for (const attributeName of attributeNames) {
if (!(attributeName in this.attributes)) {
continue;
}
nextAttributes[attributeName] = resetLinks(this.attributes[attributeName]);
}
return new ItemSchema_(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 ItemSchema_(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 ItemSchema_(nextAttributes, this.props);
}
build(Action) {
return new Action(this);
}
}