Skip to content

Commit 5cbf5a5

Browse files
committed
Upgrade Prisma v6.6.0.
1 parent be3972b commit 5cbf5a5

5 files changed

Lines changed: 150 additions & 54 deletions

File tree

next.config.mjs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import dotenv from 'dotenv';
1+
import 'dotenv/config';
22
import { createRequire } from 'module';
33

4-
dotenv.config();
5-
64
const require = createRequire(import.meta.url);
75
const pkg = require('./package.json');
86

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@
7272
"@dicebear/core": "^9.2.1",
7373
"@fontsource/inter": "^4.5.15",
7474
"@hello-pangea/dnd": "^17.0.0",
75-
"@prisma/client": "6.1.0",
76-
"@prisma/extension-read-replicas": "^0.4.0",
75+
"@prisma/client": "6.6.0",
76+
"@prisma/extension-read-replicas": "^0.4.1",
7777
"@react-spring/web": "^9.7.3",
7878
"@tanstack/react-query": "^5.28.6",
7979
"@umami/prisma-client": "^0.14.0",
@@ -107,7 +107,7 @@
107107
"next": "15.3.0",
108108
"node-fetch": "^3.2.8",
109109
"npm-run-all": "^4.1.5",
110-
"prisma": "6.1.0",
110+
"prisma": "6.6.0",
111111
"pure-rand": "^6.1.0",
112112
"react": "^19.0.0",
113113
"react-basics": "^0.126.0",

pnpm-lock.yaml

Lines changed: 78 additions & 43 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prisma.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import path from 'node:path';
2+
import { defineConfig } from 'prisma/config';
3+
4+
export default defineConfig({
5+
earlyAccess: true,
6+
schema: path.join('prisma', 'schema.prisma'),
7+
});

src/lib/prisma.ts

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import debug from 'debug';
2-
import prisma from '@umami/prisma-client';
2+
import { PrismaClient } from '@prisma/client';
3+
import { readReplicas } from '@prisma/extension-read-replicas';
34
import { formatInTimeZone } from 'date-fns-tz';
45
import { MYSQL, POSTGRESQL, getDatabaseType } from '@/lib/db';
56
import { SESSION_COLUMNS, OPERATORS, DEFAULT_PAGE_SIZE } from './constants';
@@ -10,6 +11,16 @@ import { filtersToArray } from './params';
1011

1112
const log = debug('umami:prisma');
1213

14+
const PRISMA = 'prisma';
15+
const PRISMA_LOG_OPTIONS = {
16+
log: [
17+
{
18+
emit: 'event',
19+
level: 'query',
20+
},
21+
],
22+
};
23+
1324
const MYSQL_DATE_FORMATS = {
1425
minute: '%Y-%m-%dT%H:%i:00',
1526
hour: '%Y-%m-%d %H:00:00',
@@ -234,14 +245,16 @@ async function rawQuery(sql: string, data: object): Promise<any> {
234245
return db === MYSQL ? '?' : `$${params.length}${type ?? ''}`;
235246
});
236247

237-
return prisma.rawQuery(query, params);
248+
return process.env.DATABASE_REPLICA_URL
249+
? client.$replica().$queryRawUnsafe(query, params)
250+
: client.$queryRawUnsafe(query, params);
238251
}
239252

240253
async function pagedQuery<T>(model: string, criteria: T, pageParams: PageParams) {
241254
const { page = 1, pageSize, orderBy, sortDescending = false } = pageParams || {};
242255
const size = +pageSize || DEFAULT_PAGE_SIZE;
243256

244-
const data = await prisma.client[model].findMany({
257+
const data = await client[model].findMany({
245258
...criteria,
246259
...{
247260
...(size > 0 && { take: +size, skip: +size * (+page - 1) }),
@@ -255,7 +268,7 @@ async function pagedQuery<T>(model: string, criteria: T, pageParams: PageParams)
255268
},
256269
});
257270

258-
const count = await prisma.client[model].count({ where: (criteria as any).where });
271+
const count = await client[model].count({ where: (criteria as any).where });
259272

260273
return { data, count, page: +page, pageSize: size, orderBy };
261274
}
@@ -323,8 +336,51 @@ function getSearchParameters(query: string, filters: { [key: string]: any }[]) {
323336
};
324337
}
325338

339+
function transaction(input: any, options?: any) {
340+
return client.$transaction(input, options);
341+
}
342+
343+
function getClient(params?: {
344+
logQuery?: boolean;
345+
queryLogger?: () => void;
346+
replicaUrl?: string;
347+
options?: any;
348+
}): PrismaClient {
349+
const {
350+
logQuery = !!process.env.LOG_QUERY,
351+
queryLogger,
352+
replicaUrl = process.env.DATABASE_REPLICA_URL,
353+
options,
354+
} = params || {};
355+
356+
const prisma = new PrismaClient({
357+
errorFormat: 'pretty',
358+
...(logQuery && PRISMA_LOG_OPTIONS),
359+
...options,
360+
});
361+
362+
if (replicaUrl) {
363+
prisma.$extends(
364+
readReplicas({
365+
url: replicaUrl,
366+
}),
367+
);
368+
}
369+
370+
if (logQuery) {
371+
prisma.$on('query' as never, queryLogger || log);
372+
}
373+
374+
log('Prisma initialized');
375+
376+
return prisma;
377+
}
378+
379+
const client = global[PRISMA] || getClient();
380+
326381
export default {
327-
...prisma,
382+
client,
383+
transaction,
328384
getAddIntervalQuery,
329385
getCastColumnQuery,
330386
getDayDiffQuery,

0 commit comments

Comments
 (0)