UNPKG

dynamodb-toolbox

Version:

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

45 lines (44 loc) 1.7 kB
import { resetLinks } from '../../schema/utils/resetLinks.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 { 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); } 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); } 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); } build(Action) { return new Action(this); } }