-
Notifications
You must be signed in to change notification settings - Fork 7
the other way #5
Comments
This library works great with e.g. Bookshelf and Knex. Here's a fully-functional example that goes both ways: var pg = require("pg"),
Range = require("pg-range").Range;
require("pg-range").install(pg);
var knex = require('knex')({
client: 'pg',
connection: {
'host': 'localhost',
},
});
var bookshelf = require('bookshelf')(knex);
var Event = bookshelf.Model.extend({
tableName: 'events',
})
knex.schema.dropTableIfExists('events')
.then(function () {
return knex.schema.raw('CREATE TABLE events (id serial, during tsrange)');
})
.then(function () {
return new Event({ during: Range(new Date(Date.now() - 1), new Date()) })
.save();
})
.then(function () {
return Event.fetchAll();
})
.then(function (models) {
range = models.at(0).get('during');
console.log(range); // [Sat Jul 30 2016 11:36:58 GMT-0400 (EDT),Sat Jul 30 2016 11:36:58 GMT-0400 (EDT)]
console.log(range instanceof Range); // true
console.log(range.end.getFullYear()); // 2016
})
.catch(console.log); So you'll have to be more specific! ;) Which frameworks does this module not work with? Seems like it's a problem with those frameworks. |
Ah! Is your concern with the wording, perhaps? You still need to call |
There are lots of libraries out there that implement their own custom query formatting, and they won't understand syntax such as |
I'm trying to use it with pg-promise: var Range = require("pg-range").Range;
var format = require('pg-promise').as.format;
var s = format('INSERT INTO table VALUES ($1)', [Range(1, 3)]);
console.log(s); and getting the following:
I'm not sure this is the correct range presentation though. Or am I wrong? |
Ah, you wrote your own query formatter! Of course this library is incompatible with a custom query formatter! It only claims to be compatible with node-postgres's query formatter. ;) To answer your question: no, that's not quite the right format. That's the string format, which is almost the same as the Postgres format, except in a few important edge cases, like empty ranges. To get the proper string format, you'll need to call So in order for pg-promise to be compatible with this library, pg-promise needs to check for the presence of a |
I see now. In So, I've just tried this: 'use strict';
var pgRange = require("pg-range");
var format = require('pg-promise').as.format;
function range() {
var args = arguments;
return {
_rawDBType: true,
formatDBType: function () {
var r = pgRange.Range.apply(null, args);
return r.toPostgres();
}
};
}
var s = format('INSERT INTO table VALUES ($1)', [range(1, 3)]);
console.log(s); but I got:
Wat do you think should be the right way to implement the custom type for |
I think you pasted the wrong error? I get this error using exactly your code:
Anyway, the way to resolve that is simple! @@ -9,7 +9,7 @@
_rawDBType: true,
formatDBType: function () {
var r = pgRange.Range.apply(null, args);
- return r.toPostgres();
+ return r.toPostgres(require('pg-promise').as.value);
}
};
} I see two ways we can proceed. One is entirely on your end: you can check for |
Closing due to inactivity. Feel free to reopen if you find some time! |
It is worth noting that the other way won't work with any of the frameworks built on top of
node-postgres
, while the initial approach, by using thepg
object directly will work with everything ;)I even would recommend to simplify it by removing
the other
approach altogether, to avoid confusion ;)The text was updated successfully, but these errors were encountered: