Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for uuid data type #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lib/pg_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ const int8recv = function (buf) {
return buf.readBigInt64BE(0)
}

const uuidsend = function (buf, uuid, offset) {
if (!uuid) {
return Buffer.alloc(16) // Return empty buffer
}
const hexStr = uuid.replace(/-/g, '')
if (uuid.length != 36 || hexStr.length != 32) throw new Error(`Invalid UUID string: ${uuid}`)
return buf.put(Buffer.from(hexStr, 'hex'))
}

const uuidrecv = function (buffer, offset) {
if (buffer.length != 16) throw new Error(`Invalid buffer length for uuid: ${buffer.length}`)
if (buffer.equals(Buffer.alloc(16))) return null // If buffer is all zeros, return null
const str = buffer.toString('hex')
return `${str.slice(0, 8)}-${str.slice(8, 12)}-${str.slice(12, 16)}-${str.slice(16, 20)}-${str.slice(20)}`
}

// text
const textsend = function (buf, value) {
const tbuf = Buffer.from(value, 'utf-8')
Expand Down Expand Up @@ -208,6 +224,7 @@ const types = {
int2: { oid: 21, send: int2send, recv: int2recv },
int4: { oid: 23, send: int4send, recv: int4recv },
int8: { oid: 20, send: int8send, recv: int8recv },
uuid: { oid: 2950, send: uuidsend, recv: uuidrecv },
text: { oid: 25, send: textsend, recv: textrecv },
varchar: { oid: 1043, send: varcharsend, recv: varcharrecv },
json: { oid: 114, send: json_send, recv: json_recv },
Expand All @@ -220,6 +237,7 @@ const types = {
_int2: { oid: 1005, send: array_send.bind(null, 'int2'), recv: array_recv },
_int4: { oid: 1007, send: array_send.bind(null, 'int4'), recv: array_recv },
_int8: { oid: 1016, send: array_send.bind(null, 'int8'), recv: array_recv },
_uuid: { oid: 2951, send: array_send.bind(null, 'uuid'), recv: array_recv },
_text: { oid: 1009, send: array_send.bind(null, 'text'), recv: array_recv },
_varchar: { oid: 1015, send: array_send.bind(null, 'varchar'), recv: array_recv },
_json: { oid: 199, send: array_send.bind(null, 'json'), recv: array_recv },
Expand Down
Loading