oguimbal / pg-mem

An in memory postgres DB instance for your unit tests
MIT License
1.95k stars 94 forks source link

Bytea uses utf8 instead of hex #354

Open sangaman opened 1 year ago

sangaman commented 1 year ago

Describe the bug

Pg-mem returns bytea types with utf8 encoding, when it should use hex encoding, as per https://www.postgresql.org/docs/current/datatype-binary.html

I think the offending code is here: https://github.com/oguimbal/pg-mem/blob/master/src/datatypes/datatypes.ts#L218 - it's calling bufToString which returns a utf8 encoded string.

To Reproduce

Query data from a bytea field.

pg-mem version

2.6.13

justdvl commented 3 months ago

If anyone came to this issue, a workaround is to use this helper function to store buffer in a bytea column as a UTF-8 compliant buffer:

const convertBufferToUtf8CompatibleBytea = (buffer: Buffer): Buffer => {
  const base64String = buffer.toString('base64');
  return Buffer.from(base64String, 'utf8');
};

This is a function to convert back:

const convertUtf8CompatibleByteaToBuffer = (
  base64EncodedData: string,
): Buffer => {
  return Buffer.from(base64EncodedData, 'base64');
};