Skip to content

Commit 619ba46

Browse files
committed
pass prepareValue hook to toPostgres
Pass `toPostgres` type-coercers a reference to the `prepareValue` function to ease constructing literals composed of other Postgres types.
1 parent 6ced524 commit 619ba46

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

lib/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ function prepareObject(val, seen) {
6868
}
6969
seen.push(val);
7070

71-
return prepareValue(val.toPostgres(), seen);
71+
return prepareValue(val.toPostgres(prepareValue), seen);
7272
}
7373
return JSON.stringify(val);
7474
}

test/unit/utils-tests.js

+12
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@ test('prepareValue: objects with complex toPostgres prepared properly', function
141141
assert.strictEqual(out, '{"1","2"}');
142142
});
143143

144+
test('prepareValue: objects with toPostgres receive prepareValue', function() {
145+
var customRange = {
146+
lower: { toPostgres: function() { return 5; } },
147+
upper: { toPostgres: function() { return 10; } },
148+
toPostgres: function(prepare) {
149+
return "[" + prepare(this.lower) + "," + prepare(this.upper) + "]";
150+
}
151+
};
152+
var out = utils.prepareValue(customRange);
153+
assert.strictEqual(out, "[5,10]");
154+
});
155+
144156
test('prepareValue: objects with circular toPostgres rejected', function() {
145157
var buf = new Buffer("zomgcustom!");
146158
var customType = {

0 commit comments

Comments
 (0)