UNPKG

pg-db-libs

Version:

A TypeScript library for calling PostgreSQL functions and procedures with entity mapping

31 lines (30 loc) 1.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PgEntityMapper = void 0; class PgEntityMapper { /** * Maps a database row to an entity by matching keys to the instance properties. * @param row The database row to map. * @param entityInstance An instance of the entity to use for property mapping. * @returns The mapped entity instance. */ static mapToEntity(row, entityInstance) { const entity = Object.assign({}, entityInstance); Object.keys(row).forEach((key) => { if (key in entity) { entity[key] = row[key]; } }); return entity; } /** * Maps a list of rows to entity instances. * @param rows The list of database rows. * @param entityInstance An instance of the entity to use for property mapping. * @returns A list of mapped entity instances. */ static mapToEntities(rows, entityInstance) { return rows.map((row) => this.mapToEntity(row, entityInstance)); } } exports.PgEntityMapper = PgEntityMapper;