postgrejs
Version:
Professional PostgreSQL client NodeJS
42 lines (41 loc) • 1.37 kB
JavaScript
import { DataTypeOIDs } from '../constants.js';
import { BufferReader } from '../protocol/buffer-reader.js';
export const JsonbType = {
name: 'jsonb',
oid: DataTypeOIDs.jsonb,
jsType: 'string',
parseBinary(v, options) {
const buf = new BufferReader(v);
if (buf.readUInt8() !== 1)
throw new Error('Unexpected Jsonb version value in header');
const fetchAsString = options.fetchAsString &&
options.fetchAsString.includes(DataTypeOIDs.json);
const content = buf.readLString(buf.length - buf.offset);
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 '\x0001' + v;
},
parseText(v, options) {
const fetchAsString = options.fetchAsString &&
options.fetchAsString.includes(DataTypeOIDs.json);
if (fetchAsString)
return v;
return v ? JSON.parse(v) : null;
},
isType(v) {
return v && typeof v === 'object';
},
};
export const ArrayJsonbType = {
...JsonbType,
name: '_jsonb',
oid: DataTypeOIDs._jsonb,
elementsOID: DataTypeOIDs.jsonb,
};