UNPKG

@nova-odm/auto-marshaller

Version:

A data marshaller that converts JavaScript types into Amazon DynamoDB AttributeValues

118 lines (117 loc) 4.53 kB
import { AttributeValue } from "@aws-sdk/client-dynamodb"; import { BinarySet, BinaryValue } from "./BinarySet"; import { NumberValue } from "./NumberValue"; import { NumberValueSet } from "./NumberValueSet"; export declare const EmptyHandlingStrategies: { omit: string; nullify: string; leave: string; }; /** * The behavior the marshaller should exhibit when it encounters "empty" * data that would be rejected as invalid by DynamoDB, such as 0-length * buffers or the string `''`. * * Possible values: * * `omit` - Remove the empty value from the marshalled output (i.e., marshall * this value to `undefined` rather than to an {AttributeValue}). * * * `nullify` - Convert the value from its detected to data type to `null`. * This allows marshalled data to preserve a sigil of emptiness in a way * compatible with DynamoDB. * * This option will also cause empty strings and buffers to be dropped from * string and binary sets, respectively. * * * `leave` - Do not alter the value. */ export type EmptyHandlingStrategy = keyof typeof EmptyHandlingStrategies; export declare const InvalidHandlingStrategies: { /** * Remove any invalid values from the serialized output. */ omit: string; /** * Throw an error when an unserializable value is encountered. */ throw: string; }; /** * The behavior the marshaller should exhibit when it encounters data that * cannot be marshalled to a DynamoDB AttributeValue, such as a Symbol or * Function object. * * Possible values: * * `omit` - Remove any invalid values from the serialized output. * * * `throw` - Throw an error when an unserializable value is encountered. */ export type InvalidHandlingStrategy = keyof typeof InvalidHandlingStrategies; export type UnmarshalledAttributeValue = string | number | NumberValue | BinaryValue | Set<string> | Set<number> | NumberValueSet | BinarySet | null | boolean | UnmarshalledListAttributeValue | UnmarshalledMapAttributeValue; export interface UnmarshalledListAttributeValue extends Array<UnmarshalledAttributeValue> { } export interface UnmarshalledMapAttributeValue { [key: string]: UnmarshalledAttributeValue; } export interface MarshallingOptions { /** * The behavior the marshaller should exhibit when it encounters "empty" * data that would be rejected as invalid by DynamoDB, such as 0-length * buffers or the string `''`. */ onEmpty?: EmptyHandlingStrategy; /** * The behavior the marshaller should exhibit when it encounters data that * cannot be marshalled to a DynamoDB AttributeValue, such as a Symbol or * Function object. */ onInvalid?: InvalidHandlingStrategy; /** * Whether numbers should be unmarshalled to a special object type that can * preserve values that would lose precision if converted to JavaScript's * native number type. */ unwrapNumbers?: boolean; } /** * A class that will convert arbitrary JavaScript data types to their most * logical in the DynamoDB schema. */ export declare class Marshaller { private readonly onEmpty; private readonly onInvalid; private readonly unwrapNumbers; constructor({ onEmpty, onInvalid, unwrapNumbers }?: MarshallingOptions); /** * Convert a JavaScript object with string keys and arbitrary values into an * object with string keys and DynamoDB AttributeValue objects as values. */ marshallItem(item: { [key: string]: any; }): Record<string, AttributeValue>; /** * Convert a JavaScript value into a DynamoDB AttributeValue or `undefined`. * * @throws Error if the value cannot be converted to a DynamoDB type and the * marshaller has been configured to throw on invalid input. */ marshallValue(value: any): AttributeValue | undefined; /** * Convert a DynamoDB operation result (an object with string keys and * AttributeValue values) to an object with string keys and native * JavaScript values. */ unmarshallItem(item: Record<string, AttributeValue>): UnmarshalledMapAttributeValue; /** * Convert a DynamoDB AttributeValue into a native JavaScript value. */ unmarshallValue(item: AttributeValue): UnmarshalledAttributeValue; private marshallComplexType; private marshallBinaryValue; private marshallList; private marshallMap; private marshallObject; private marshallSet; private collectSet; private handleEmptyString; }