dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
51 lines (50 loc) • 2.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EntityDTO = void 0;
const index_js_1 = require("../../../entity/index.js");
const index_js_2 = require("../../../schema/actions/dto/index.js");
const schema_js_1 = require("../../../schema/item/schema.js");
const index_js_3 = require("../../../table/actions/dto/index.js");
class EntityDTO extends index_js_1.EntityAction {
constructor(entity) {
super(entity);
const constructorShemaDTO = new index_js_2.SchemaDTO(new schema_js_1.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(index_js_3.TableDTO);
}
toJSON() {
return {
entityName: this.entityName,
schema: this.schema.toJSON(),
entityAttribute: this.entity.entityAttribute,
timestamps: this.entity.timestamps,
table: this.table.toJSON()
};
}
}
exports.EntityDTO = EntityDTO;
EntityDTO.actionName = 'dto';