Skip to content

Commit 76ad329

Browse files
committed
feat: support multiple database configurations
1 parent db8a21d commit 76ad329

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

packages/nuxt/src/index.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ export declare const openFeatureIntegration: typeof clientSdk.openFeatureIntegra
2626
export declare const OpenFeatureIntegrationHook: typeof clientSdk.OpenFeatureIntegrationHook;
2727
export declare const statsigIntegration: typeof clientSdk.statsigIntegration;
2828
export declare const unleashIntegration: typeof clientSdk.unleashIntegration;
29+
export declare const growthbookIntegration: typeof clientSdk.growthbookIntegration;

packages/nuxt/src/runtime/plugins/database.server.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import {
1212
import type { Database, PreparedStatement } from 'db0';
1313
// eslint-disable-next-line import/no-extraneous-dependencies
1414
import { defineNitroPlugin, useDatabase } from 'nitropack/runtime';
15+
// @ts-expect-error - This is a virtual module
16+
import { databaseInstances } from '#sentry/database-config.mjs';
1517

1618
/**
1719
* Keeps track of prepared statements that have been patched.
@@ -28,13 +30,15 @@ const SENTRY_ORIGIN = 'auto.db.nuxt';
2830
*/
2931
export default defineNitroPlugin(() => {
3032
try {
31-
debug.log('@sentry/nuxt: Instrumenting database...');
33+
debug.log('@sentry/nuxt: Instrumenting databases...');
3234

33-
const db = useDatabase();
34-
35-
instrumentDatabase(db);
35+
for (const instance of databaseInstances) {
36+
debug.log('@sentry/nuxt: Instrumenting database instance:', instance);
37+
const db = useDatabase(instance);
38+
instrumentDatabase(db);
39+
}
3640

37-
debug.log('@sentry/nuxt: Database instrumented.');
41+
debug.log('@sentry/nuxt: Databases instrumented.');
3842
} catch (error) {
3943
// During build time, we can't use the useDatabase function, so we just log an error.
4044
if (error instanceof Error && /Cannot access 'instances'/.test(error.message)) {

packages/nuxt/src/vite/databaseConfig.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { addServerPlugin, createResolver } from '@nuxt/kit';
22
import { consoleSandbox } from '@sentry/core';
33
import type { Nuxt } from 'nuxt/schema';
4+
import { addServerTemplate } from '../vendor/server-template';
45

56
/**
67
* Sets up the database instrumentation.
@@ -15,5 +16,20 @@ export function addDatabaseInstrumentation(nuxt: Nuxt): void {
1516
return;
1617
}
1718

19+
/**
20+
* This is a different flag than the one in experimental.database, this configures multiple database instances.
21+
* keys represent database names to be passed to `useDatabase(name?)`.
22+
* https://nitro.build/guide/database#configuration
23+
*/
24+
const databaseInstances = Object.keys(nuxt.options.nitro.database || { default: {} });
25+
26+
// Create a virtual module to pass this data to runtime
27+
addServerTemplate({
28+
filename: '#sentry/database-config.mjs',
29+
getContents: () => {
30+
return `export const databaseInstances = ${JSON.stringify(databaseInstances)};`;
31+
},
32+
});
33+
1834
addServerPlugin(createResolver(import.meta.url).resolve('./runtime/plugins/database.server'));
1935
}

0 commit comments

Comments
 (0)