UNPKG

dynamodb-toolbox

Version:

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

38 lines (37 loc) 1.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.EntityParser = void 0; const index_js_1 = require("../../../entity/index.js"); const index_js_2 = require("../../../schema/actions/parse/index.js"); const index_js_3 = require("../../../table/actions/parsePrimaryKey/index.js"); const constants_js_1 = require("./constants.js"); class EntityParser extends index_js_1.EntityAction { constructor(entity) { super(entity); this[constants_js_1.$parser] = new index_js_2.Parser(entity.schema); } parse(input, options = {}) { const { fill = true } = options; const parser = this[constants_js_1.$parser].start(input, { ...options, transform: true }); if (fill) { parser.next(); // defaulted parser.next(); // linked } /** * @debt type "we could remove those casts by using named generator yields: const parsedItem = parser.next<"parsed">().value" */ const parsedItem = parser.next().value; const item = parser.next().value; const keyInput = this.entity.computeKey ? this.entity.computeKey(parsedItem) : item; const key = new index_js_3.PrimaryKeyParser(this.entity.table).parse(keyInput); return { parsedItem: parsedItem, item: { ...item, ...key }, key }; } reparse(input, options = {}) { return this.parse(input, options); } } exports.EntityParser = EntityParser;