Skip to content

Commit

Permalink
refactor(pglite): promote to prisma submodule
Browse files Browse the repository at this point in the history
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
LoneRifle committed Oct 4, 2024
1 parent a09f1b5 commit 9dfa709
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 21 deletions.
File renamed without changes.
25 changes: 25 additions & 0 deletions src/server/prisma/pglite.ts
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)
}
}
}
25 changes: 4 additions & 21 deletions vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { vi } from 'vitest'
import { PGlite } from '@electric-sql/pglite'
import { PrismaPGlite } from 'pglite-prisma-adapter'
import { PrismaClient } from '@prisma/client'
import { readdirSync, readFileSync, statSync } from 'node:fs'
import { makePgliteClient, applyMigrations } from '~/server/prisma/pglite'

const client = new PGlite()
const adapter = new PrismaPGlite(client)
const prisma = new PrismaClient({ adapter })
const { client, prisma } = makePgliteClient()

vi.mock('./src/server/prisma', () => ({
prisma,
}))

export const resetDb = async () => {
const resetDb = async () => {
try {
await client.exec(`DROP SCHEMA public CASCADE`)
await client.exec(`CREATE SCHEMA public`)
Expand All @@ -21,21 +16,9 @@ export const resetDb = async () => {
}
}

const applyMigrations = async () => {
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)
}
}
}

// Apply migrations before each test
beforeEach(async () => {
await applyMigrations()
await applyMigrations(client)
})

// Clean up the database after each test
Expand Down

0 comments on commit 9dfa709

Please sign in to comment.