import { type AttributeValue } from '@aws-sdk/client-dynamodb' import { Attribute } from '../attribute' import { type DynamoAttributeTypes } from '../dynamo-attribute-types' import { type IAttributeType } from '../interfaces' import { type AttributeMetadata } from '../metadata/attribute' import { type ITable, type Table } from '../table' import { type Schema } from './schema' export class AttributeType> implements IAttributeType { public type: DynamoAttributeTypes private __attribute: Attribute constructor( protected record: Table, protected propertyName: string, protected metadata?: Metadata, ) {} get attribute(): Attribute { if (this.__attribute == null) { this.__attribute = new Attribute(this.propertyName, this, this.metadata) } return this.__attribute } protected get table(): ITable { return this.record.constructor as ITable } protected get schema(): Schema { return this.table.schema } decorate(): void { this.schema.addAttribute(this.attribute) } toDynamo(value: Value, attribute: Attribute): AttributeValue { return { [this.type]: value } as any // TODO: should not have to use an as any here } fromDynamo(attributeValue: AttributeValue, attribute: Attribute): Value | null { return (attributeValue as any)[this.type] } }