Open BracketJohn opened 4 months ago
- from this part of the Readme I'm a bit unsure on how to apply the schema (aka: run
prisma db push
) - it does not seem to be supported, but if it isn't, how can I apply the schema my app needs to run?
I just researched as well, @lucasthevenet has this nice piece of code to run migrations on the fly:
https://github.com/lucasthevenet/pglite-utils/blob/77421105f04b8fe2ab82a282db9542eb0cd9b181/sandbox/src/pglite.ts#L6-L12
It uses the PGlite
API directly without Prisma, but runs a migration.sql
generated by Prisma.
Real-world use cases would probably be a bit more complex.
(Prisma has plans to support migrations in code, so that they can also use driver adapters - but that is not a thing that will happen in the next weeks.)
Thanks for taking some time to test this! I apologize for the naming errors, I changed the name at the last moment and forgot to change it on some places.
As Jan correctly states, currently there is no way to run migrations through Prisma CLI for PGlite. My idea is to make a wrapper so you can run prisma cli commands targeting a PGlite db (haven't found the time yet).
In the meantime, one workaround you can use is to generate a sql file with
npx prisma migrate diff --from-empty --to-schema-datamodel prisma/schema.prisma --script > migrate.sql
And then run the generated file through code.
As for the error you're getting, could you please tell me what type of dataDir are you targeting? (memory:// or file dir)
Sure thing & no worries - I've worked on a few open-source things & this kind of stuff always happens in the beginning of projects (:
As Jan correctly states, currently there is no way to run migrations through Prisma CLI for PGlite. My idea is to make a wrapper so you can run prisma cli commands targeting a PGlite db (haven't found the time yet).
I thought so - that makes it a bit hard to use, as you can imagine. How would this command work? Would it also rely on migrations/
?
In the meantime, one workaround you can use is to generate a sql file with npx prisma migrate diff --from-empty --to-schema-datamodel prisma/schema.prisma --script > migrate.sql And then run the generated file through code.
Thanks, makes sense!
As for the error you're getting, could you please tell me what type of dataDir are you targeting? (memory:// or file dir)
Yes, I was using:
DATABASE_URL="./prisma/pgdata"
I also ran resolve
on the path before passing it to the adapter to ensure that it is reprensented correctly (eg: with \
on Windows machines):
// Taken from https://www.npmjs.com/package/pathe
import { resolve } from 'pathe'
const client = new PGlite(resolve(process.env.DATABASE_URL));
const adapter = new PrismaPGlite(client);
const prisma = new PrismaClient({ adapter });
One additional note, I don't know if this changes anything: In our project we extend the prisma-client using prisma.$extend(...)
, which entered GA in 4.16.0
, see more here
I tried reproducing your error thinking it was related to nuxt/nitro, but had no success, everything seems to work ok for me.
It would be great if you could create a reproduction so we can point out where the error is happening precisely.
I added an example nuxt project that uses Prisma and PGlite, if you want you can base a reproduction out of that example.
Thanks @lucasthevenet -> I'll check it out and test it further + report back!
Hey, PGlite maintainer here.
This is really awesome!
the ERROR [nitro] [unhandledRejection] memory access out of bounds
that @BracketJohn saw is a bug in the current release of PGlite related to TOAST (the way Postgres handles large value), it's fixed in the alpha we just released based on PG v16.3: https://github.com/electric-sql/pglite/releases/tag/v0.2.0-alpha.0
Is there any update on how to best update the database tables with the prisma schema?
My use case is to use pglite
for local development as a in-memory database, so I would be fine with running this as javascript code instead of a proper cli command. Also in such a scenario, how would one create new migrations (to be applied in the production db) if the schema changes?
Hey @lucasthevenet 👋
First of all, again, thanks for this very nice push for a community driven
pglite
adapter!I started to test this for our production-app (>70k LOC, >2.4k LOC prisma schema) and am collecting some feedback here, as requested by you here.
Points I noticed:
README
suggests to install@pglite-utils/prisma-adapter
which does not publicly exist. It should bepglite-prisma-adapter
AFAIKimport { PrismaPGlite } from '@pglite-utils/prisma-adapter'
should beimport { PrismaPGlite } from 'pglite-prisma-adapter'
AFAIKprisma db push
) - it does not seem to be supported, but if it isn't, how can I apply the schema my app needs to run?./...
-path notation for theDATABASE_URL
, just like sqlite doesTechnical info: We are running a Nuxt 3 app. The adapter + prisma client is created on startup of the nitro server we are using.
So at this point I'm sadly stuck with testing it, as the server does not start up correctly, or rather: The first prisma query we run (creating a default user, see logs below) fails.
Thanks again though for creating this great adapter & putting your time into it. I'm happy to test & use this further, once it makes sense (note: I'm not assuming free support / resolution from your side, just trying to give helpful feedback in case you want it).
[1] logs from error on
pnpm dev