postgrejs
Version:
Professional PostgreSQL client NodeJS
40 lines (39 loc) • 1.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ArrayUuidType = exports.UuidType = void 0;
const constants_js_1 = require("../constants.js");
const GUID_PATTERN = /^[0-9a-fA-F]{8}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{4}-?[0-9a-fA-F]{12}$/;
exports.UuidType = {
name: 'uuid',
oid: constants_js_1.DataTypeOIDs.uuid,
jsType: 'String',
parseBinary(v) {
return (v.toString('hex', 0, 4) +
'-' +
v.toString('hex', 4, 6) +
'-' +
v.toString('hex', 6, 8) +
'-' +
v.toString('hex', 8, 10) +
'-' +
v.toString('hex', 10, 16));
},
encodeBinary(buf, v) {
if (!GUID_PATTERN.test(v))
throw new Error(`"${v}" is not a valid guid value`);
const b = Buffer.from(v.replace(/-/g, ''), 'hex');
buf.writeBuffer(b);
},
parseText(v) {
return v;
},
isType(v) {
return typeof v === 'string' && GUID_PATTERN.test(v);
},
};
exports.ArrayUuidType = {
...exports.UuidType,
name: '_uuid',
oid: constants_js_1.DataTypeOIDs._uuid,
elementsOID: constants_js_1.DataTypeOIDs.uuid,
};