Skip to content

Commit 3b5c01d

Browse files
committed
fix(edgedb-client): create a single client and expose it instead of recreating
1 parent 5100f93 commit 3b5c01d

4 files changed

Lines changed: 26 additions & 15 deletions

File tree

src/module.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { existsSync } from 'node:fs'
2-
import { addComponentsDir, addImportsDir, addPlugin, addServerHandler, addServerImports, createResolver, defineNuxtModule } from '@nuxt/kit'
2+
import { addComponentsDir, addImportsDir, addPlugin, addServerHandler, addServerImports, addServerPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
33
import { createConsola } from 'consola'
44
import { join } from 'pathe'
55
import chalk from 'chalk'
@@ -418,6 +418,9 @@ export default defineNuxtModule<ModuleOptions>({
418418
}
419419

420420
if (options.composables) {
421+
// Add server plugin for EdgeDB client
422+
addServerPlugin(resolveLocal('./runtime/server/plugins/edgedb-client'))
423+
421424
// Add server imports manually
422425
addServerImports([
423426
{
@@ -513,7 +516,7 @@ export default defineNuxtModule<ModuleOptions>({
513516

514517
// Runtime
515518
addPlugin({
516-
src: resolveLocal('./runtime/plugin/edgedb-auth'),
519+
src: resolveLocal('./runtime/plugins/edgedb-auth'),
517520
mode: 'all',
518521
})
519522
addComponentsDir({
Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
import { getCookie } from 'h3'
22
import type { EventHandlerRequest, H3Event } from 'h3'
3-
import { createClient } from 'edgedb'
4-
import type { ConnectOptions } from 'edgedb'
5-
import { useEdgeDbEnv } from './useEdgeDbEnv'
3+
import type { Client, ConnectOptions } from 'edgedb'
64

7-
export function useEdgeDb(
8-
req: H3Event<EventHandlerRequest> | undefined = undefined,
9-
clientOptions: ConnectOptions = {},
10-
) {
11-
const { dsn } = useEdgeDbEnv()
5+
export function useEdgeDb(req: H3Event<EventHandlerRequest> | undefined = undefined) {
6+
// @ts-expect-error - untyped global
7+
const client = globalThis.__nuxt_edgedb_client__ as Client
128

13-
if (dsn)
14-
clientOptions.dsn = dsn
9+
if (req) {
10+
return client.withGlobals({
11+
'ext::auth::client_token': req ? getCookie(req, 'edgedb-auth-token') : undefined,
12+
})
13+
}
1514

16-
return createClient(clientOptions).withGlobals({
17-
'ext::auth::client_token': req ? getCookie(req, 'edgedb-auth-token') : undefined,
18-
})
15+
return client
1916
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { createClient } from 'edgedb'
2+
import { defineNitroPlugin } from 'nitropack/dist/runtime/plugin'
3+
4+
export default defineNitroPlugin(() => {
5+
const { dsn } = useEdgeDbEnv()
6+
7+
const client = createClient({ dsn })
8+
9+
// @ts-expect-error - untyped global
10+
globalThis.__nuxt_edgedb_client__ = client
11+
})

0 commit comments

Comments
 (0)