-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.ts
More file actions
27 lines (22 loc) · 927 Bytes
/
Copy pathdb.ts
File metadata and controls
27 lines (22 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Prisma 7 client singleton.
// `bun add @prisma/adapter-better-sqlite3` does not work under Bun yet
// (https://github.com/oven-sh/bun/issues/4290), so we use the libSQL adapter,
// which is pure-JS and runs both in Bun and Node.
import { PrismaClient } from "@prisma/client";
import { PrismaLibSql } from "@prisma/adapter-libsql";
let _client: PrismaClient | null = null;
function urlFromEnv(): string {
return process.env.DATABASE_URL ?? "file:./data/dev.sqlite";
}
export function getPrisma(): PrismaClient {
if (!_client) {
const adapter = new PrismaLibSql({ url: urlFromEnv() });
_client = new PrismaClient({ adapter });
}
return _client;
}
// Test helper: returns a client backed by a specific DB URL. Caller owns it.
export function newPrisma(databaseUrl?: string): PrismaClient {
const adapter = new PrismaLibSql({ url: databaseUrl ?? urlFromEnv() });
return new PrismaClient({ adapter });
}