mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
Fixed replica logic.
This commit is contained in:
parent
33cb195fd0
commit
c427c6f547
1 changed files with 33 additions and 27 deletions
|
|
@ -206,6 +206,10 @@ async function rawQuery(sql: string, data: Record<string, any>, name?: string):
|
||||||
return `$${params.length}${type ?? ''}`;
|
return `$${params.length}${type ?? ''}`;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (process.env.DATABASE_REPLICA_URL && '$replica' in client) {
|
||||||
|
return client.$replica().$queryRawUnsafe(query, ...params);
|
||||||
|
}
|
||||||
|
|
||||||
return client.$queryRawUnsafe(query, ...params);
|
return client.$queryRawUnsafe(query, ...params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -296,10 +300,6 @@ function getSchema() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getClient() {
|
function getClient() {
|
||||||
if (!process.env.DATABASE_URL) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const url = process.env.DATABASE_URL;
|
const url = process.env.DATABASE_URL;
|
||||||
const replicaUrl = process.env.DATABASE_REPLICA_URL;
|
const replicaUrl = process.env.DATABASE_REPLICA_URL;
|
||||||
const logQuery = process.env.LOG_QUERY;
|
const logQuery = process.env.LOG_QUERY;
|
||||||
|
|
@ -307,43 +307,49 @@ function getClient() {
|
||||||
const connectionUrl = new URL(url);
|
const connectionUrl = new URL(url);
|
||||||
const schema = connectionUrl.searchParams.get('schema') ?? undefined;
|
const schema = connectionUrl.searchParams.get('schema') ?? undefined;
|
||||||
|
|
||||||
const adapter = new PrismaPg({ connectionString: url.toString() }, { schema });
|
const baseAdapter = new PrismaPg({ connectionString: url }, { schema });
|
||||||
|
|
||||||
const prisma = new PrismaClient({
|
const baseClient = new PrismaClient({
|
||||||
adapter,
|
adapter: baseAdapter,
|
||||||
errorFormat: 'pretty',
|
errorFormat: 'pretty',
|
||||||
...(logQuery ? PRISMA_LOG_OPTIONS : {}),
|
...(logQuery ? PRISMA_LOG_OPTIONS : {}),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (replicaUrl) {
|
if (logQuery) {
|
||||||
const replicaAdapter = new PrismaPg({ connectionString: replicaUrl.toString() }, { schema });
|
baseClient.$on('query', log);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!replicaUrl) {
|
||||||
|
log('Prisma initialized');
|
||||||
|
globalThis[PRISMA] ??= baseClient;
|
||||||
|
return baseClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
const replicaAdapter = new PrismaPg({ connectionString: replicaUrl }, { schema });
|
||||||
|
|
||||||
const replicaClient = new PrismaClient({
|
const replicaClient = new PrismaClient({
|
||||||
adapter: replicaAdapter,
|
adapter: replicaAdapter,
|
||||||
|
errorFormat: 'pretty',
|
||||||
...(logQuery ? PRISMA_LOG_OPTIONS : {}),
|
...(logQuery ? PRISMA_LOG_OPTIONS : {}),
|
||||||
});
|
});
|
||||||
|
|
||||||
prisma.$extends(
|
if (logQuery) {
|
||||||
|
replicaClient.$on('query', log);
|
||||||
|
}
|
||||||
|
|
||||||
|
const extended = baseClient.$extends(
|
||||||
readReplicas({
|
readReplicas({
|
||||||
replicas: [replicaClient],
|
replicas: [replicaClient],
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
if (logQuery) {
|
log('Prisma initialized (with replica)');
|
||||||
prisma.$on('query' as never, log);
|
globalThis[PRISMA] ??= extended;
|
||||||
}
|
|
||||||
|
|
||||||
log('Prisma initialized');
|
return extended;
|
||||||
|
|
||||||
if (!globalThis[PRISMA]) {
|
|
||||||
globalThis[PRISMA] = prisma;
|
|
||||||
}
|
|
||||||
|
|
||||||
return prisma;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const client: PrismaClient = globalThis[PRISMA] || getClient();
|
const client = (globalThis[PRISMA] || getClient()) as ReturnType<typeof getClient>;
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
client,
|
client,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue