You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support for time INTERVAL data type in postgres #4900
I have times > 24 hours, which just broke upon moving to postrgres, so I think I need to implement an INTERVAL data type in data-types.js.
Reacted by Dmitry Erastov, Cody Veal, Soubhik Mondal, Mario Casciaro, Jan Marthedal Rasmussen, Abel Osorio, KillWolfVlad, gunar, Patrick Sturm, Rodrigo Campassi and 7 more
/**
* A time interval column
* @property INTERVAL
*/
var INTERVAL = function() {
if (!(this instanceof INTERVAL)) return new INTERVAL();
ABSTRACT.apply(this, arguments);
};
util.inherits(INTERVAL, ABSTRACT);
INTERVAL.prototype.key = INTERVAL.key = 'INTERVAL';
INTERVAL.prototype.toSql = function() {
return 'INTERVAL';
};
and the entry in module.exports.
Unless I get told otherwise I'll submit that shortly. I cant fully test yet as I have a weird bug on constraints and block inserts (it goes away when I bring the block size down so I need to have a proper look at what is being said to postrgres)
While other dialects use strings to identify their types (e.g. INTEGER, GEOMETRY etc), postgres uses oids, which are numbers. The oid for interval is 1186, and interval[] is 1187. Most oids are static, but those for user created types / types you have to manually enable, fx geometry and hstore are dynamic.
(You can check for an oid using `select typname, oid, typarray from pg_type where typtype = 'b')
ok, thanks,
I am about to return to the server and so will have another punt, I need to to implement a sequence manager, something that does this kind of thing (all totally untested).
sequenceManage:function(offset){
var self=this;
var query = "UPDATE "+this.tableName+
" SET "+this.sequenceName+" = "+this.sequenceName+"+"+offset+
" WHERE "+this.sequenceName +" >= "+this.get(this.sequenceName);
_.each(this.seqIndexes,function(seqIndex){
query+=" AND "+seqIndex+"="+self.get(seqIndex);
});
return this.sequelize.query(query);
},
and obviously give your def a sequenceName (name of field that holds the sequence) and sequencIndex[], list of fields to sequence across.
If there's a thing to do that pray tell, but I will go back now and attempt to return to using INTERVAL
I need guidance.
I am trying to bend an INTERVAL into a timey thing that returns a string of the form H:m:s
I made this parse method, but to no avail, interval is a "H:m:s" string already, but I am getting an object from my query .
any clues on where I am supposed to stop an INTERVAL field from returning an object consisting of hours,minutes and seconds.,
and is this what we really want anyway ?. The object form might be what we should have. What I myself want is just an H:m:s string where H can be > 24. is that fair ?.
I don't have any strong feelings about the returned type for interval, since you are the one implementing it, feel free to write it to suit your needs.
Adding a .parse method should be all that's required - Is that method being called at all? Perhaps you can create a pull request with what you have so far, that would make it easier for me to offer some guidance
OK, I let this slip as I went off back into the client. I have some other stuff now, namely I want to do things like select "name",earth_distance (ll_to_earth(88.9761,24.621281) , ll_to_earth("lon","lat")) from "Stops"; so that would be a cool thing to have.
I did some work previously on epilogue so it could do aggregates like max and min, and you can append $lt etc so you can do afield$lt=, so I got near to this stuff, at least using the interface.
I'll have another look first and then post an issue. If someone has done any gis stuff and I just cant see it please say.
If you'd like to help out, please provide feedback on #10441. There are still some open questions that need to be answered about the design of that approach.
I think so too. I'd prefer to map the interval data type to Temporal.Duration (rather than our own representation), as they're both ISO 8601 compatible
I think so too. I'd prefer to map the interval data type to Temporal.Duration (rather than our own representation), as they're both ISO 8601 compatible
On that, I have been using a custom data type in the meantime that others may find helpful, it uses Luxon's duration under the hood. I haven't tested it exhaustively, but I haven't run into issue so far.
import{DataTypes}from"@sequelize/core";importtype{DurationLikeObject}from"luxon";import{Duration}from"luxon";exportclassDurationDataTypeextendsDataTypes.ABSTRACT<Duration>{toSql(){return"interval";}validate(value: unknown): value is Duration{returnvalueinstanceofDuration;}areValuesEqual(value: Duration|null|undefined,originalValue: Duration|null|undefined): boolean{returnvalue==null||originalValue==null
? value===originalValue
: value.equals(originalValue);}escape(value: Duration): string{conststringified=value.toISO();if(stringified==null){thrownewError("Could not serialize Duration to ISO string");}returnstringified;}toBindableValue(value: Duration): string{returnthis.escape(value);}parseDatabaseValue(value: unknown): unknown{try{if(typeofvalue==="string"){returnDuration.fromISO(value);}if(typeofvalue==="number"){returnDuration.fromMillis(value);}if(typeofvalue==="object"){returnDuration.fromObject(valueasDurationLikeObject);}thrownewError("Could not parse Duration from database value");}catch(error){constmessage=errorinstanceofError ? error.message : "Unknown error";thrownewError(`Could not parse Duration from database value: ${message}`);}}}
Activity
[-]Support for time INTERVAL data type in postgress[/-][+]Support for time INTERVAL data type in postgres[/+]mark-lester commentedon Nov 21, 2015
I added the obvious
and the entry in module.exports.
Unless I get told otherwise I'll submit that shortly. I cant fully test yet as I have a weird bug on constraints and block inserts (it goes away when I bring the block size down so I need to have a proper look at what is being said to postrgres)
mickhansen commentedon Nov 21, 2015
The newer version uses
ABSTRACT.inherts(cb)afaik, but @janmeier is more knowledgeable here.janmeier commentedon Nov 24, 2015
Have a look at https://github.com/sequelize/sequelize/blob/master/lib/dialects/postgres/data-types.js for an example of how to extend the abstract datatype.
You'll need to add an entry in https://github.com/sequelize/sequelize/blob/master/lib/data-types.js as well, like with hstore and json, otherwise the type won't be accessible
janmeier commentedon Dec 4, 2015
ping @mark-lester :)
mark-lester commentedon Dec 4, 2015
Hey. where did I get to ?, I think I gave up, yes, I aborted and went for strings, because ...,
https://groups.google.com/forum/#!topic/sequelize/IBoT8wf72tg
I am just in the middle of critical phase in today's cooking, let me remember why I bottled out, ..., brb
janmeier commentedon Dec 4, 2015
While other dialects use strings to identify their types (e.g. INTEGER, GEOMETRY etc), postgres uses oids, which are numbers. The oid for interval is 1186, and interval[] is 1187. Most oids are static, but those for user created types / types you have to manually enable, fx geometry and hstore are dynamic.
(You can check for an oid using `select typname, oid, typarray from pg_type where typtype = 'b')
mark-lester commentedon Dec 4, 2015
ok, thanks,
I am about to return to the server and so will have another punt, I need to to implement a sequence manager, something that does this kind of thing (all totally untested).
and this
and obviously give your def a sequenceName (name of field that holds the sequence) and sequencIndex[], list of fields to sequence across.
If there's a thing to do that pray tell, but I will go back now and attempt to return to using INTERVAL
mark-lester commentedon Dec 4, 2015
I need guidance.
I am trying to bend an INTERVAL into a timey thing that returns a string of the form H:m:s
I made this parse method, but to no avail, interval is a "H:m:s" string already, but I am getting an object from my query .
any clues on where I am supposed to stop an INTERVAL field from returning an object consisting of hours,minutes and seconds.,
and is this what we really want anyway ?. The object form might be what we should have. What I myself want is just an H:m:s string where H can be > 24. is that fair ?.
janmeier commentedon Dec 7, 2015
I don't have any strong feelings about the returned type for interval, since you are the one implementing it, feel free to write it to suit your needs.
Adding a
.parsemethod should be all that's required - Is that method being called at all? Perhaps you can create a pull request with what you have so far, that would make it easier for me to offer some guidancemark-lester commentedon Dec 18, 2015
OK, I let this slip as I went off back into the client. I have some other stuff now, namely I want to do things like select "name",earth_distance (ll_to_earth(88.9761,24.621281) , ll_to_earth("lon","lat")) from "Stops"; so that would be a cool thing to have.
I did some work previously on epilogue so it could do aggregates like max and min, and you can append $lt etc so you can do afield$lt=, so I got near to this stuff, at least using the interface.
I'll have another look first and then post an issue. If someone has done any gis stuff and I just cant see it please say.
19 remaining items
abelosorio commentedon Feb 13, 2019
gunar commentedon Feb 14, 2019
Thank you for your work @abelosorio @gabegorelick . Excited about getting this merged.
abelosorio commentedon Feb 14, 2019
Yeah... so do I. Finally closing a 3-year issue 💪
gabegorelick commentedon Feb 14, 2019
If you'd like to help out, please provide feedback on #10441. There are still some open questions that need to be answered about the design of that approach.
stale commentedon Jul 23, 2019
gabegorelick commentedon Jul 24, 2019
Still an issue. I will rebase #10441 when I get a chance.
jbdevelop commentedon Jan 28, 2021
I was trying to insert data...
I got it just like that
https://stackoverflow.com/questions/43634819/how-to-use-interval-in-sequelizejs
sfroskos commentedon Aug 31, 2021
Are PostgreSQL intervals now supported in sequelize? Is there any guidance on how to do create a model and query it using intervals?
jthoward64 commentedon Jun 14, 2023
Just for anyone who comes looking, I think this issue is essentially blocked behind the new temporal support (#14295)
ephys commentedon Jun 14, 2023
I think so too. I'd prefer to map the interval data type to
Temporal.Duration(rather than our own representation), as they're both ISO 8601 compatiblejthoward64 commentedon Jun 14, 2023
On that, I have been using a custom data type in the meantime that others may find helpful, it uses Luxon's duration under the hood. I haven't tested it exhaustively, but I haven't run into issue so far.