Skip to content

Commit

Permalink
pass prepareValue hook to toPostgres
Browse files Browse the repository at this point in the history
Pass `toPostgres` type-coercers a reference to the `prepareValue`
function to ease constructing literals composed of other Postgres types.
  • Loading branch information
benesch committed Apr 3, 2014
1 parent 6ced524 commit 619ba46
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function prepareObject(val, seen) {
}
seen.push(val);

return prepareValue(val.toPostgres(), seen);
return prepareValue(val.toPostgres(prepareValue), seen);
}
return JSON.stringify(val);
}
Expand Down
12 changes: 12 additions & 0 deletions test/unit/utils-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ test('prepareValue: objects with complex toPostgres prepared properly', function
assert.strictEqual(out, '{"1","2"}');
});

test('prepareValue: objects with toPostgres receive prepareValue', function() {
var customRange = {
lower: { toPostgres: function() { return 5; } },
upper: { toPostgres: function() { return 10; } },
toPostgres: function(prepare) {
return "[" + prepare(this.lower) + "," + prepare(this.upper) + "]";
}
};
var out = utils.prepareValue(customRange);
assert.strictEqual(out, "[5,10]");
});

test('prepareValue: objects with circular toPostgres rejected', function() {
var buf = new Buffer("zomgcustom!");
var customType = {
Expand Down

0 comments on commit 619ba46

Please sign in to comment.