From 619ba46ffed8d1b118ce8bbee9e443980b46ad5c Mon Sep 17 00:00:00 2001 From: Nikhil Benesch Date: Thu, 3 Apr 2014 10:21:10 -0400 Subject: [PATCH] pass `prepareValue` hook to `toPostgres` Pass `toPostgres` type-coercers a reference to the `prepareValue` function to ease constructing literals composed of other Postgres types. --- lib/utils.js | 2 +- test/unit/utils-tests.js | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/utils.js b/lib/utils.js index 3522c10..2c20a77 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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); } diff --git a/test/unit/utils-tests.js b/test/unit/utils-tests.js index ecf3447..116dfdd 100644 --- a/test/unit/utils-tests.js +++ b/test/unit/utils-tests.js @@ -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 = {