UNPKG

postgrejs

Version:

Professional PostgreSQL client NodeJS

38 lines (37 loc) 1.13 kB
import { DataTypeOIDs } from '../constants.js'; export const JsonType = { name: 'json', oid: DataTypeOIDs.json, jsType: 'string', parseBinary(v, options) { const content = v.toString('utf8'); const fetchAsString = options.fetchAsString && options.fetchAsString.includes(DataTypeOIDs.jsonb); if (fetchAsString) return content; return content ? JSON.parse(content) : undefined; }, encodeText(v) { if (typeof v === 'object' || typeof v === 'bigint') return JSON.stringify(v); if (typeof v === 'boolean') return v ? 'true' : 'false'; return '' + v; }, parseText(v, options) { const fetchAsString = options.fetchAsString && options.fetchAsString.includes(DataTypeOIDs.jsonb); if (fetchAsString) return v; return v ? JSON.parse(v) : null; }, isType(v) { return v && typeof v === 'object'; }, }; export const ArrayJsonType = { ...JsonType, name: '_json', oid: DataTypeOIDs._json, elementsOID: DataTypeOIDs.json, };