Repository navigation
Description
Problem
TimescaleDB is an incredible postgreSQL extension that allows you to work with time-series data in native SQL. It's amazing for IOT heavy projects and there's plenty of other use cases.
It's possible to use TimescaleDB and prisma together with some manual work and using raw queries.
-
This was already possible with prisma1 as stated here : TimeScaleDB connector prisma1#1672 (comment)
-
This has been made easier with the implementation of composite keys : Referencing primary composite keys #1389 🎉
However it is still needed to manually install the extension, then manually convert tables into hypertables using raw sql, and some TimescaleDB features are implemented using Postgres Functions.
I haven't tested it personally, but going by the docs prisma should work out of the box once the hypertable is properly created when it comes to writing data.
For reading data, falling back to raw sql is needed for some of the features implemented with functions (docs here).
Suggested solution
It would be nice if the prisma schema supported the following :
- Ability to add the extension from the prisma schema directly
- Ability to define a hypertable directly, so that prisma migrate would handle them without having to fallback to raw sql after running a migration
Regarding the prisma client, it would be great if it supported the use of functions.
I don't know what the syntax would look like, but in raw sql it's pretty straightforward:
SELECT location, last(temperature, time)
FROM conditions
GROUP BY location;Function support might need it's own issue thread, but I'm curious to see what a potential client API would look like.
Not that in this case, prisma doesn't support groupBy yet (prisma/prisma-client-js#1)
It might be worth mentioning than the schema could also support function creation as well, however this would be out of the scope of the TimescaleDB support, as the function are created when the extension is installed.
Alternatives
It's still possible to use raw sql and manually handle the hypertable creation/functions call
Additional context
It would be really nice to have a more generic approach to add some of the most popular extensions directly though the prisma schema, postGIS comes to mind. Right now if you want to use prisma + postgis + timescale together, there's a lot of manual wiring to do.
Relevant :
- Postgis support : PostGIS/GIS support #2789
- Native DataTypes : EPIC: Support for native database types #446
I do not know if something needs to happen on the timescaleDB end, as it is just a regular postgreSQL extension (as far as I know). However @akulkarni from the timescaleDB team said he was willing to help.
Activity
Sytten commentedon Aug 6, 2020
I also love timescaledb and feel the same pain points. I think that is out of scope for the prisma "core". What I think would be awesome is if the prisma team gears the project more toward a plugin architecture similar to what Hashicorp did with terraform providers. So the community could implement support for new databases, new primitives, etc.
Hebilicious commentedon Aug 6, 2020
Since timescaledb/postgis are native postgres extensions, I believe they will be in prisma scope at some point (pretty sure I've heard that the team wants to fully support postgis).
In the native datatype thread, the proposed syntax by @mavilein looks like this :
maybe we could include extension support there, something like
Similarly for postGIS, there's a large list of functions, so having an extension agnostic way to deal with postgres functions from the prisma client would be a great feature to have.
Note than the prisma type Geometry doesn't actually exist, but there's this open issue #1798
[-]PostgreSQL : First-class TimescaleDB support[/-][+]Support (PostgreSQL) TimescaleDB[/+]akulkarni commentedon Dec 28, 2020
👋🏽 Hi there, Ajay from TimescaleDB. If there are things we can to do help with this issue please let us know. We also have an active community on Slack: https://slack.timescale.com/
vorillaz commentedon Feb 9, 2021
I am a little late to the party but I am trying to build an application using Prisma along with Timescale. The thing is that I am a little confused regarding using the schema and Prisma migrations. As far as I can tell I can't use the schema declaration defining the schema changes as Prisma does not allow declaring a model without using
@idor@unique.I am basically trying to follow the Node.js + Timescale tutorial.
I love the suggested solution and I would love to have some feedback or a sample repository if anyone's already tried using Prisma along with Timescale cc @akulkarni
janpio commentedon Apr 4, 2021
(🌃 🐰 Although I work for Prisma during the week and day, I write this as a private person that played around with Prisma and TimescaleDB during the Easter brake - so please do not consider this official information. 🌃 🐰)
As TimescaleDB is really just PostgreSQL plus an extension, I was quite positive this would just work ™️ and played with it a bit to learn how TimescaleDB works and what I can do with it.
While doing so, I used the above linked "Node + Timescale" tutorial. As I of course adapted that for Prisma, I created a small Gist based off the original that shows the current steps necessary for Prisma:
https://gist.github.com/janpio/2a425f22673f2de54469772f16af8118
The diff between the original and the Prisma version gives a good overview that not much really had to be changed, but this is really just doing it "the Prisma way", using the appropriate CLI commands and Prisma Client code: https://gist.github.com/janpio/2a425f22673f2de54469772f16af8118/revisions#diff-15f6c3367f1237852c83d79bf8e0fd698a694a378ded36abf802fbef4b024cc2
Conclusion: With the new Prisma Migrate, activating the extension and creating a Hypertable is pretty simple - it executes a plain SQL file after all. Prisma Client works as expected against that hypertable.
I did not get to adding a TimescaleDB native function call via a raw query yet.
vorillaz commentedon Apr 5, 2021
Hey, @janpio thanks for the lovely comment.
My main concern with the current state of Prisma along with Timescale is that due to Prisma schema limitation we are creating inconsistencies between the models and the actual DB schema.
More precisely since models without
idsare not supported. They should be dropped through the migration script or even worse models should not include the timescale tables.janpio commentedon Apr 5, 2021
Ah, I wanted to reply to that:
Tables/models without a primary key or Id are supported, you just need any unique identifier for the data. In my version of the tutorial, I chose a compound unique key across both fields. You usually always want to have a way to uniquely identify data in a table, so this makes sense to add in my opinion anyway.
For Prisma there is no difference between a “normal” table and a hypertable (right now). They look and work the same to Prisma - you just make sure to convert them once manually in a migration.
vorillaz commentedon Apr 5, 2021
Yeah, that makes sense, but I guess depends on the use case.
In my scenario, I am using the hyper for analytics parsing thus there is no need for ids or unique keys, just plain and fast writes. Following the docs from Prisma v1 I am dropping the ids when migrating but that does not feel right IMHO.
janpio commentedon Apr 5, 2021
Do you see a measurable performance impact by maintaining a unique index in that table?
Whatever is documented for Prisma 1 does not necessarily apply to Prisma 2 - so if you let me know what exactly you refer to, I can check what the correct current way would be.
6 remaining items
pfried commentedon May 6, 2021
I do not want to clutter this issue further so I created a discussion here: #6963
@janpio Would be really great if you could have a quick look!
pfried commentedon Mar 16, 2022
Related issue since an application using timescale will most likely use materialized views: #678 currently the ony sensible workaround is raw sql
I am using prisma with timescale now and it basically works well if you look at it as a postgresql database, the most heavylifiting is done in a custom sql migration, as discussed earlier. As I see it now there are two blocking things:
janpio commentedon Mar 16, 2022
PostgreSQL extension management we are tracking in #12308
leppaott commentedon Jan 4, 2023
Yeah a schema without @id/@unique would be nice. The problem is that ID<->TIMESTAMP index doesn't work if we have many items on the same millisecond etc. But thinking an index using SERIAL could work.
pfried commentedon Jan 19, 2023
With the new
viewsand extensions feature I created a showcase here: https://github.com/Nantis-GmbH/cdk-prisma-timescale-demoIt shows how to use TimescaleDB with Prisma on AWS using the latest features from prisma and timescale
The usability greatly improved but I am still hitting this one issue #8325
@akulkarni You might want to look into this, as it showcases all the great features of timescale in combination with prisma as an ORM
leppaott commentedon Feb 6, 2023
@pfried what do you get by using
extensions = [timescaledb]when you still need migration to do everything (create hypertable)? Does that help with something else?pfried commentedon Feb 6, 2023
@leppaott This is part of Prismas extension management (e.g. inspect db), see #12308). If you call Timescale function in your migration file without activating the extiensions it will simply error saying that those function (e.g. hypertable stuff) do not exist
leppaott commentedon Feb 8, 2023
@pfried right but still use
CREATE EXTENSION IF NOT EXISTS "timescaledb";on the migration. But I see now that it's automatically created however since you need to manually edit the file anyway...pfried commentedon Feb 8, 2023
@leppaott I think the whole point of inspection / pulling is to remove more and more manual steps (and by this possible errors too)
Krister-Johansson commentedon Jun 22, 2026
I ran into this same gap, so I built a small Prisma Client extension to make TimescaleDB nicer to work with: prisma-extension-timescaledb (https://www.npmjs.com/package/prisma-extension-timescaledb).
It's brand-new and community-built (I'm the author), not an official solution, but it covers some of what's asked for in this thread like defining hypertables, continuous aggregates, compression and retention policies without hand-writing all the raw SQL. Would love feedback or bug reports from anyone here who's been doing this manually. 👋