-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for time INTERVAL data type in postgres #4900
Comments
I added the obvious
and the entry in module.exports. |
The newer version uses |
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 |
ping @mark-lester :) |
Hey. where did I get to ?, I think I gave up, yes, I aborted and went for strings, because ..., |
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,
and this
and obviously give your def a sequenceName (name of field that holds the sequence) and sequencIndex[], list of fields to sequence across. |
I need guidance.
any clues on where I am supposed to stop an INTERVAL field from returning an object consisting of hours,minutes and seconds., |
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 |
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. |
My team is interested in the INTERVAL type as well. |
So unsupported until further notice? Would like this as well. |
Another vote here. Please support the INTERVAL type. |
+1 Support for INTERVAL would be very helpful for my team as well. |
another interval support request |
This comment was marked as spam.
This comment was marked as spam.
another interval support request |
Hi everyone! I really need this feature. I've been waiting for this a long time (I didn't have the time to work on it) and today I decided to spend some time trying to add it. I wrote it using @mark-lester's suggestions and @janmeier's comments. It's very simple. It does not perform any parsing PostgreSQL <-> Sequelize. Values are read and returned as strings. Example table:
Example record creation:
Example query:
This is pretty much all I need so far. Let me know if it is good enough for everyone and I can continue working on my PR in order to having this ready to merge ASAP. Thanks in advance. cc @sergioshev |
ping @mickhansen @janmeier Sorry for bothering you, I'm sure you have plenty of work going on, but I really need this feature and I need basic directions to keep working on this. My PR contains the basic functionally, but if you are OK with this approach I'll add more tests and all it needs to be merged. Thanks for your time! |
Temporary solution 👉 https://www.npmjs.com/package/sequelize-interval-postgres |
My team is interested in the INTERVAL type as well. |
See rails/rails#16919 for INTERVAL support in Rails. Lots of interesting stuff there. Some question for any potential solution:
@abelosorio Seems like your solution just passes the results directly to/from |
I've attempted a solution here: #10441 Caveats:
|
Hi @gabegorelick! Great to see someone else worried about this issue. My solution passes the value direct to PostgreSQL, yes. When it reads it, it returns an object. This is because the value is parsed by postgres-interval. This object is completely compatible to the Let me know if I can help further. It's been a while since the last time I worked on this issue, but I'm glad you are now. |
Thanks @abelosorio. I've updated #10441 to support PgInterval objects. They'll be converted to moment.duration objects, which are then converted to ISO strings before being sent to PG. By the way, since postgres-interval doesn't support non-default intervalstyles, what happens when you use your plugin with other intervalstyles? Does postgres-interval get confused? I've worked around this by setting intervalstyle to |
Hmmm I don't know for sure, but I guess `postgres-interval` will throw some
error.
…On Wed, Feb 13, 2019 at 3:04 PM Gabe Gorelick ***@***.***> wrote:
Thanks @abelosorio <https://github.com/abelosorio>. I've updated #10411
<#10411> to support
PgInterval objects. They'll be converted to moment.duration objects, which
are then converted to ISO strings before being sent to PG.
By the way, since postgres-interval doesn't support non-default
intervalstyles <bendrucker/postgres-interval#9>,
what happens when you use your plugin with other intervalstyles? Does
postgres-interval get confused?
I've worked around this by setting intervalstyle to iso_8601 when we grab
a connection. But I wish there was a better solution.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#4900 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ALzzT1RQR3f3fTYH1jRqNicQd7Vn5qzfks5vNFOwgaJpZM4GmtxO>
.
|
Thank you for your work @abelosorio @gabegorelick . Excited about getting this merged. |
Yeah... so do I. Finally closing a 3-year issue 💪 |
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. |
This comment was marked as outdated.
This comment was marked as outdated.
Still an issue. I will rebase #10441 when I get a chance. |
I was trying to insert data... I got it just like that
https://stackoverflow.com/questions/43634819/how-to-use-interval-in-sequelizejs |
Are PostgreSQL intervals now supported in sequelize? Is there any guidance on how to do create a model and query it using intervals? |
Just for anyone who comes looking, I think this issue is essentially blocked behind the new temporal support (#14295) |
I think so too. I'd prefer to map the interval data type to |
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";
import type { DurationLikeObject } from "luxon";
import { Duration } from "luxon";
export class DurationDataType extends DataTypes.ABSTRACT<Duration> {
toSql() {
return "interval";
}
validate(value: unknown): value is Duration {
return value instanceof Duration;
}
areValuesEqual(
value: Duration | null | undefined,
originalValue: Duration | null | undefined
): boolean {
return value == null || originalValue == null
? value === originalValue
: value.equals(originalValue);
}
escape(value: Duration): string {
const stringified = value.toISO();
if (stringified == null) {
throw new Error("Could not serialize Duration to ISO string");
}
return stringified;
}
toBindableValue(value: Duration): string {
return this.escape(value);
}
parseDatabaseValue(value: unknown): unknown {
try {
if (typeof value === "string") {
return Duration.fromISO(value);
}
if (typeof value === "number") {
return Duration.fromMillis(value);
}
if (typeof value === "object") {
return Duration.fromObject(value as DurationLikeObject);
}
throw new Error("Could not parse Duration from database value");
} catch (error) {
const message = error instanceof Error ? error.message : "Unknown error";
throw new Error(
`Could not parse Duration from database value: ${message}`
);
}
}
} |
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.
The text was updated successfully, but these errors were encountered: