UNPKG

massive

Version:

A small query tool for Postgres that embraces json and makes life simpler

45 lines (36 loc) 1.07 kB
'use strict'; const _ = require('lodash'); /** * Transforms a record or records which contain a "body" field into a document * object or objects. Metadata is removed and the "id" field is moved into the * body. * * @module docify * @param {Array|Object} result - A record or list of records to transform. * @return {Array|Object} The transformed record or records. */ exports = module.exports = function (result) { /** * Transforms a single record containing a "body" field into a document object. * * @param {Object} row - A flat object representing a database record. * @return {Object} A document object generated from the row. */ function docify (row) { if (row == null) { return null; } if (row.body) { const returnDoc = _.cloneDeep(row.body); returnDoc.id = row.id; returnDoc.created_at = row.created_at; returnDoc.updated_at = row.updated_at; return returnDoc; } return row; } if (_.isArray(result)) { return result.map(docify); } return docify(result); };