postgrejs
Version:
Professional PostgreSQL client NodeJS
45 lines (44 loc) • 1.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArrayJsonbType = exports.JsonbType = void 0;
const constants_js_1 = require("../constants.js");
const buffer_reader_js_1 = require("../protocol/buffer-reader.js");
exports.JsonbType = {
name: 'jsonb',
oid: constants_js_1.DataTypeOIDs.jsonb,
jsType: 'string',
parseBinary(v, options) {
const buf = new buffer_reader_js_1.BufferReader(v);
if (buf.readUInt8() !== 1)
throw new Error('Unexpected Jsonb version value in header');
const fetchAsString = options.fetchAsString &&
options.fetchAsString.includes(constants_js_1.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(constants_js_1.DataTypeOIDs.json);
if (fetchAsString)
return v;
return v ? JSON.parse(v) : null;
},
isType(v) {
return v && typeof v === 'object';
},
};
exports.ArrayJsonbType = {
...exports.JsonbType,
name: '_jsonb',
oid: constants_js_1.DataTypeOIDs._jsonb,
elementsOID: constants_js_1.DataTypeOIDs.jsonb,
};