UNPKG

@grandlinex/bundle-postgresql

Version:
47 lines (46 loc) 1.24 kB
export function convertSpecialFields(meta, value, params) { if (meta.dataType === 'json') { params.push(JSON.stringify(value)); } else { params.push(value); } } export function objToTable(config, entity, update) { const clone = entity; const keysOrigal = Object.keys(entity); const keys = []; const params = []; const values = []; let pCount = 1; keysOrigal.forEach((key) => { const meta = config.meta.get(key); if (!meta) { throw new Error('No col meta'); } if (meta.primaryKey && update) { return; } convertSpecialFields(meta, clone[key], params); if (update) { values.push(`${String(key)}=$${pCount}`); } else { values.push(`$${pCount}`); } pCount += 1; keys.push(key); }); if (values.length === params.length && params.length === keys.length) { return [keys, values, params]; } throw new Error('Invalid output length'); } export function rowToObj(config, row) { return row; } export function tableToObj(config, table) { return table.map((row) => { return rowToObj(config, row); }); }