plv8
plv8 copied to clipboard
How to handle bytea
Hello,
I try to understand how i can handle bytea in plv8. I want to create the same object internally as used by plv8:
create or replace test_buffer(n int) returns bytea as
$$
const obj = {"0":60,"1":78,"2":85,"3":76,"4":76,"5":62};
const res = plv8.execute(`select decode as "bytea" from decode(encode('<NULL>', 'hex'), 'hex')`);
const byte = res[0].bytea;
plv8.elog(NOTICE, 'Created object:');
plv8.elog(NOTICE, JSON.stringify(Object.entries(obj)));
plv8.elog(NOTICE, JSON.stringify(obj));
plv8.elog(NOTICE, Object.getPrototypeOf(obj));
plv8.elog(NOTICE, '"bytea" from query:');
plv8.elog(NOTICE, JSON.stringify(Object.entries(byte)));
plv8.elog(NOTICE, JSON.stringify(byte));
const e = Object.getPrototypeOf(byte);
plv8.elog(NOTICE, 'still running');
try {
plv8.elog(NOTICE, Object.getPrototypeOf(byte));
plv8.elog(NOTICE, 'not running here');
try {
} catch(err) {
plv8.elog(NOTICE, err);
}
if (n === 0) {
return '<NULL>';
}
if (n === 1) {
return byte;
}
if (n === 2) {
return obj;
}
$$ language plv8;
When i get the bytea returned by postgres and try to log the Object.getPrototypeOf(byte)
i get an error: 'TypeError: this is not a typed array.' When i try to construct an object with the same structure and entries and return it the result is totally different from the expected result.
Executing test_buffer with n = 0 and n = 1 i get the desired result of \x3c4e554c4c3e, but with n = 2 i get
\x060000200100008001000000010000000100000001000000010000000a000010080000100800001008000010080000100800001030313233343500002000000000803c002000000000804e0020000000008055002000000000804c002000000000804c002000000000803e00.
I tried also the functions suggested in https://github.com/plv8/plv8/issues/189 where i get from
select create_array(5);
create_array
\x0500004006000090080000100800001008000010080000101800000000800000180000000080000018000000008000001800000000800000180000000080
and from
select get_array_length(create_array(5));
get_array_length
62
This is also related to my other question about typed arrays i posted on stackoverflow:
https://stackoverflow.com/questions/59466409/plv8-manipulating-typed-arrays-not-possible
Thank you