import {Binary} from "mongodb" import {Id} from "../../id.js" import {Row} from "../../types.js" import {objectMap} from "../../tools/object-map.js" // strip away the mongo database id's -- we don't use 'em function skimMongoId(row: xRow): xRow { if (row) { const {_id: noop, ...skimmed} = row return skimmed } return undefined } export function valueUp(value: any, key: string): any { return value instanceof Id ? new Binary(Buffer.from(value.binary)) : value } export function valueDown(value: any, key: string): any { return value instanceof Binary ? new Id(value.buffer) : value } // process a row before it's sent to mongo // - transform any id to mongo binary type export function up(row: Partial): any { return objectMap(row, valueUp) } // process a row retrieved from mongo // - transform any binary types into id export function down(data: any): xRow { return (data && typeof data === "object") ? objectMap(skimMongoId(data), valueDown) : data } export function ups(rows: Partial[]): any[] { return rows.map(up) } export function downs(rows: any[]): xRow[] { return rows.map(down) }