dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
47 lines (46 loc) • 2.1 kB
JavaScript
import { EntityAction } from '../../../entity/index.js';
import { SchemaDTO } from '../../../schema/actions/dto/index.js';
import { ItemSchema } from '../../../schema/item/schema.js';
import { TableDTO } from '../../../table/actions/dto/index.js';
export class EntityDTO extends EntityAction {
constructor(entity) {
super(entity);
const constructorShemaDTO = new SchemaDTO(new ItemSchema(this.entity.attributes));
const { partitionKey, sortKey } = this.entity.table;
const partitionKeyAttr = Object.entries(constructorShemaDTO.attributes).find(([attrName, attr]) => { var _a; return ((_a = attr.savedAs) !== null && _a !== void 0 ? _a : attrName) === partitionKey.name; });
if (partitionKeyAttr === undefined) {
constructorShemaDTO.attributes[partitionKey.name] = {
type: partitionKey.type,
key: true,
required: 'always',
hidden: true
};
}
if (sortKey !== undefined) {
const sortKeyAttr = Object.entries(constructorShemaDTO.attributes).find(([attrName, attr]) => { var _a; return ((_a = attr.savedAs) !== null && _a !== void 0 ? _a : attrName) === sortKey.name; });
if (sortKeyAttr === undefined) {
constructorShemaDTO.attributes[sortKey.name] = {
type: sortKey.type,
key: true,
required: 'always',
hidden: true
};
}
}
this.entityName = this.entity.entityName;
this.schema = constructorShemaDTO;
this.entityAttribute = this.entity.entityAttribute;
this.timestamps = this.entity.timestamps;
this.table = this.entity.table.build(TableDTO);
}
toJSON() {
return {
entityName: this.entityName,
schema: this.schema.toJSON(),
entityAttribute: this.entity.entityAttribute,
timestamps: this.entity.timestamps,
table: this.table.toJSON()
};
}
}
EntityDTO.actionName = 'dto';