-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(pglite): promote to prisma submodule
Make pglite part of the core codebase, so that we can use it in environments where postgres may not be available, such as in browser-only environments like StackBlitz. - Rename prisma -> prisma/index to host submodules - Move pglite-related logic to pglite - Wrap actual init of PGlite as factory, and not within module load - Rework vitest.setup to depend on new submodule - Keep resetDb, since that is only useful to testing
- Loading branch information
Showing
3 changed files
with
29 additions
and
21 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { PGlite } from '@electric-sql/pglite' | ||
import { PrismaPGlite } from 'pglite-prisma-adapter' | ||
import { PrismaClient } from '@prisma/client' | ||
import { readdirSync, readFileSync, statSync } from 'node:fs' | ||
|
||
export const makePgliteClient = () => { | ||
const client = new PGlite() | ||
const adapter = new PrismaPGlite(client) | ||
return { | ||
client, | ||
prisma: new PrismaClient({ adapter }), | ||
} | ||
} | ||
|
||
export const applyMigrations = async (client: PGlite) => { | ||
const prismaMigrationDir = './prisma/migrations' | ||
const directory = readdirSync(prismaMigrationDir).sort() | ||
for (const file of directory) { | ||
const name = `${prismaMigrationDir}/${file}` | ||
if (statSync(name).isDirectory()) { | ||
const migration = readFileSync(`${name}/migration.sql`, 'utf8') | ||
await client.exec(migration) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters