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

bytea support, not 1 : 1 in javascript/native #184

Closed
ghost opened this issue Sep 13, 2012 · 3 comments
Closed

bytea support, not 1 : 1 in javascript/native #184

ghost opened this issue Sep 13, 2012 · 3 comments

Comments

@ghost
Copy link

ghost commented Sep 13, 2012

In native, it kind of works how you would've figure it should work...
It wants a byte array or else your data will get clipped. It'll give you back a Buffer of the literal string byte array you gave it. So you need to convert this back.

In javascript, it'll take anything, it'll treat it as string, that means it'll complain about non escaped text. It'll give you back a string. You'd have to escape this or encode it as hex or base64. Then decode it later when you retrieve it.

@aleksandrmelnyk
Copy link

exactly same issue here. So how to properly save bytea data? In statement with parameters provided data is cut in native mode.

@aleksandrmelnyk
Copy link

In both modes (js and native) works following solution:
fs.readFile(self.c.req.files.attachment.path,'hex', function(err, data){
if (err) {
clbSer(err);
} else {
params.attachment_data = '\x' + data;
params.attachment_name = self.c.req.files.attachment.name;
//attachment_data can be inserted as conn.query('insert into table (byteaColumn) VALUES ($1)', [params.attachment_data]);
clbSer(null, params);
}
});

conn.query('select byteaResult ...', function(err, res){
fs.writeFile(res.rows[0].byteaResult, function(err, res){});
});

polastre added a commit to Innovation-Toolkit/sails-postgresql that referenced this issue Jan 5, 2014
Waterline supports binary datatypes, so map them to BYTEA column
types in Postgres.  Convert buffers to hex strings (as per the
tradition in node-postgres) starting with \x, which will automatically
be converted back into Buffer objects when read from Postgres by
node-postgres.

See
brianc/node-postgres#184
and
parseByteA of https://github.com/brianc/node-postgres/blob/master/lib/types/textParsers.js
@brianc
Copy link
Owner

brianc commented Oct 20, 2014

I think this is fixed, right? node-pg-types has had a byte array parser for quite a while.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants