dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
34 lines (33 loc) • 1.3 kB
JavaScript
import { EntityAction } from '../../../entity/index.js';
import { Parser } from '../../../schema/actions/parse/index.js';
import { PrimaryKeyParser } from '../../../table/actions/parsePrimaryKey/index.js';
import { $parser } from './constants.js';
export class EntityParser extends EntityAction {
constructor(entity) {
super(entity);
this[$parser] = new Parser(entity.schema);
}
parse(input, options = {}) {
const { fill = true } = options;
const parser = this[$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 PrimaryKeyParser(this.entity.table).parse(keyInput);
return {
parsedItem: parsedItem,
item: { ...item, ...key },
key
};
}
reparse(input, options = {}) {
return this.parse(input, options);
}
}