Skip to content

OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.

improve(db): add read replica

Carl-Gerhard Lindesvärd38a93c0b2f91
diff --git a/packages/db/package.json b/packages/db/package.json
index 6c5c64aa4..8b2f754f2 100644
--- a/packages/db/package.json
+++ b/packages/db/package.json
@@ -22,6 +22,7 @@
"@openpanel/redis": "workspace:*",
"@openpanel/validation": "workspace:*",
"@prisma/client": "^5.1.1",
+ "@prisma/extension-read-replicas": "^0.4.0",
"jiti": "^2.4.1",
"prisma-json-types-generator": "^3.1.1",
"ramda": "^0.29.1",
diff --git a/packages/db/src/prisma-client.ts b/packages/db/src/prisma-client.ts
index 05f3a8530..85b53a40a 100644
--- a/packages/db/src/prisma-client.ts
+++ b/packages/db/src/prisma-client.ts
@@ -1,15 +1,22 @@
import { PrismaClient } from '@prisma/client';
+import { readReplicas } from '@prisma/extension-read-replicas';
export * from '@prisma/client';
+const getPrismaClient = () => {
+ return new PrismaClient({
+ log: ['error'],
+ }).$extends(
+ readReplicas({
+ url: process.env.DATABASE_URL_REPLICA ?? process.env.DATABASE_URL!,
+ }),
+ );
+};
+
const globalForPrisma = globalThis as unknown as {
- prisma: PrismaClient | undefined;
+ prisma: ReturnType<typeof getPrismaClient> | undefined;
};
-export const db =
- globalForPrisma.prisma ??
- new PrismaClient({
- log: ['error'],
- });
+export const db = globalForPrisma.prisma ?? getPrismaClient();
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = db;
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index bcddfe54f..5899f1682 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -895,6 +895,9 @@ importers:
'@prisma/client':
specifier: ^5.1.1
version: 5.9.1([email protected])
+ '@prisma/extension-read-replicas':
+ specifier: ^0.4.0
+ version: 0.4.0(@prisma/[email protected]([email protected]))
jiti:
specifier: ^2.4.1
version: 2.4.1
@@ -4038,6 +4041,11 @@ packages:
'@prisma/[email protected]':
resolution: {integrity: sha512-gkdXmjxQ5jktxWNdDA5aZZ6R8rH74JkoKq6LD5mACSvxd2vbqWeWIOV0Py5wFC8vofOYShbt6XUeCIUmrOzOnQ==}
+ '@prisma/[email protected]':
+ resolution: {integrity: sha512-0KVjOgYJO9ymOACwLXebXB1XZCrfXXZMDkZrdZQOm9rqraC1rguwpPzXUdgv2txMLyJNSe+i5DOruDCU4FfvCA==}
+ peerDependencies:
+ '@prisma/client': ^5.2.0
+
'@prisma/[email protected]':
resolution: {integrity: sha512-l0goQOMcNVOJs1kAcwqpKq3ylvkD9F04Ioe1oJoCqmz05mw22bNAKKGWuDd3zTUoUZr97va0c/UfLNru+PDmNA==}
@@ -10882,12 +10890,15 @@ packages:
resolution: {integrity: sha512-rlBo3HU/1zAJUrkY6jNxDOC9eVYliG6nS4JA8u8KAshITd07tafMc/Br7xQwCSseXwJ2iCcHCE8SNWX3q8Z+kw==}
+ deprecated: Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.
resolution: {integrity: sha512-es33J1g2HjMpyAhz8lOR+ICmXXAqTuKbuXuUWLhOLew20oN9oUCgCJx615U/v7aioZg7IX5lIh9x34vwneu4pA==}
+ deprecated: Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.
resolution: {integrity: sha512-Mu7R0g4ig9TUuGSxJavny5Rv0egCEtpZRNMrZaYS1vxkiIxGiGUwoezU3LazIQ+KE04hTrTfNPgxU5gzi7F5Pw==}
+ deprecated: Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.
resolution: {integrity: sha512-mJiVjfd2vokfDxsQPOwJ/PtanO87LhpYY88ubI5dUB1Ab58Txbyje3+jpm+/83R/fevaq/107NNhtYBLuoTrFg==}
@@ -15407,6 +15418,10 @@ snapshots:
'@prisma/fetch-engine': 5.9.1
'@prisma/get-platform': 5.9.1
+ dependencies:
+ '@prisma/client': 5.9.1([email protected])
+
'@prisma/[email protected]':
dependencies:
'@prisma/debug': 5.9.1