Skip to content

Commit

Permalink
Add binary datatype support for Postgres BYTEA
Browse files Browse the repository at this point in the history
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
  • Loading branch information
polastre committed Aug 7, 2013
1 parent 0384ead commit 2a0f248
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/utils.js
Expand Up @@ -139,6 +139,11 @@ utils.prepareValue = function(value) {
value = JSON.stringify(value);
}

// Store Buffers as hex strings (for BYTEA)
if (Buffer.isBuffer(value)) {
value = '\\x' + value.toString('hex');
}

return value;
};

Expand Down Expand Up @@ -229,6 +234,10 @@ utils.sqlTypeCast = function(type) {
case 'json':
return 'JSON';

case 'binary':
case 'bytea':
return 'BYTEA';

default:
console.error("Unregistered type given: " + type);
return "TEXT";
Expand Down

0 comments on commit 2a0f248

Please sign in to comment.