Repository navigation
feat(postgres): add INTERVAL support - #10441
gabegorelick wants to merge 1 commit into
Conversation
|
|
||
| INTERVAL.prototype.key = INTERVAL.key = 'INTERVAL'; | ||
| INTERVAL.prototype.validate = function validate() { | ||
| return true; |
There was a problem hiding this comment.
I've not found a good way to validate intervals.
See https://stackoverflow.com/questions/38692972/how-to-validate-a-moment-js-duration for an example of this not working.
Unfortunately, due to the way moment.duration works this means values like 1 day are silently converted to 0 intervals. But we can probably check if all components of the duration are empty, and then check if the string is a 0 interval.
There was a problem hiding this comment.
Actually, is validate called by anything? I was halfway through updating this to validate ISO and postgres interval strings when I realized nothing seems to call these functions. I assumed these were called before sending sending data to the DB, but it appears that's not the case?
Or are these only useful for users to reference in their custom validators?
| } | ||
|
|
||
| if (this.sequelize.config.intervalStyle) { | ||
| query += `SET intervalstyle TO ${this.sequelize.config.intervalStyle};`; |
There was a problem hiding this comment.
Postgres returns intervals as strings. But we need ISO format to be able to accurately parse them.
See https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-INTERVAL-OUTPUT
This is similar to what Rails does: https://github.com/rails/rails/pull/16919/files#diff-cc31fc1dd1e78db64638200f710b2f59R726
I'm not happy with the need for an intervalStyle setting, but it's the only way to not break backwards compatibility for any consumers currently using intervals like https://www.npmjs.com/package/sequelize-interval-postgres.
Ideally we would detect the format and parse dynamically. But I've yet to see a parser that can do this.
Question: should we just always set intervalstyle to iso_8601 and not even allow it to be configured? That seems to be what the Rails PR does.
Codecov Report
@@ Coverage Diff @@
## master #10441 +/- ##
===========================================
+ Coverage 74.54% 96.3% +21.75%
===========================================
Files 85 92 +7
Lines 8186 9840 +1654
===========================================
+ Hits 6102 9476 +3374
+ Misses 2084 364 -1720
Continue to review full report at Codecov.
|
|
|
||
| INTERVAL.prototype._sanitize = function _sanitize(value) { | ||
| if (typeof value === 'string') { | ||
| return moment.duration(value); |
There was a problem hiding this comment.
moment.duration seemed like the best thing to sanitize to. Alternatively, we could normalize to strings, but then we'd potentially need to decide on a format (ISO, SQL, PG). Or we could normalize to number of milliseconds (or seconds).
node-pg returns postgres-interval objects by default. I think returning moment.duration objects is a good way to abstract that.
| Sequelize.DATE // DATETIME for mysql / sqlite, TIMESTAMP WITH TIME ZONE for postgres | ||
| Sequelize.DATE(6) // DATETIME(6) for mysql 5.6.4+. Fractional seconds support with up to 6 digits of precision | ||
| Sequelize.DATEONLY // DATE without time. | ||
| Sequelize.INTERVAL // Duration of time. PostgreSQL only. |
There was a problem hiding this comment.
Other DBs have a notion of an interval or duration type. E.g. Oracle has INTERVAL YEAR [(year_precision)] TO MONTH and INTERVAL DAY [(day_precision)] TO SECOND [(fractional_seconds)]. This should be generalizable to that.
Note that many DBs, including PG, take a fields option that modifies the interval. For example, INTERVAL YEAR TO MONTH. I think supporting that should be out of scope. Specifying precision should be enough for most use cases.
edaa59e to
a314571
Compare
| inherits(INTERVAL, BaseTypes.INTERVAL); | ||
|
|
||
| INTERVAL.parse = function parse(value) { | ||
| return moment.duration(value); |
There was a problem hiding this comment.
Curious, why not use parse from postgres-interval? https://github.com/bendrucker/postgres-interval/blob/f58ec4bb28d67fe1f9c9a1ad4ab8066c26de24bc/index.js#L113
|
Hey there :) First of all: Thanks a bunch for your contribution to Sequelize! Much appreciated! A couple of months ago, we have switched from master to main branch as our primary development branch and hence this PR is now outdated :( If you still think this change is making sense, please consider recreating the PR against ✌️ |
|
There's still a lot of design decisions that need to be made with this. It also may make sense to wait for https://tc39.es/proposal-temporal/docs/duration.html to stabilize and use that instead of moment durations. |
Pull Request check-list
npm run testornpm run test-DIALECTpass with this change (including linting)?Description of change
Add
INTERVALtype for PostgreSQL. Intervals are represented asmoment.durationobjects.Usage:
Closes #4900