Skip to content

Commit 028a161

Browse files
committed
feat: Convex integration with backend
1 parent 6ef6abc commit 028a161

9 files changed

Lines changed: 54 additions & 2 deletions

File tree

apps/backend/.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
# STREAMING_ENABLED=true
44
# FRONTEND_URL=https://YOUR_FRONTEND_URL
5+
# CONVEX_URL=https://YOUR_CONVEX_URL
6+
57

68
# KINDE_DOMAIN=https://<YOUR_SUBDOMAIN>.kinde.com
79
# KINDE_CLIENT_ID=<YOUR_CLIENT_ID>

apps/backend/.env.local

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ APP_DEV_host=127.0.0.1
33
APP_DEV_port=3301
44

55
FRONTEND_URL=https://127.0.0.1:3300
6+
CONVEX_URL=
67

78
# KINDE_DOMAIN=https://<YOUR_SUBDOMAIN>.kinde.com
89
# KINDE_CLIENT_ID=<YOUR_CLIENT_ID>

apps/backend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
"@scalar/hono-api-reference": "^0.9.3",
3232
"@vitest/coverage-v8": "^3.2.2",
3333
"arktype": "^2.1.20",
34+
"backend-convex": "workspace:*",
3435
"consola": "^3.4.2",
36+
"convex": "^1.24.8",
3537
"hono": "^4.7.11",
3638
"hono-adapter-aws-lambda": "^1.3.3",
3739
"hono-openapi": "^0.4.8",
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { i18nComposer } from '#src/helpers/i18n.js'
22

33
// This isa sample for structuring guide
4-
export const getHelloMessage = () => `${i18nComposer.t('hello-from-/x', { x: 'i18n and Hono' })}! - ${Date.now()}`
4+
export const getHelloMessage = (from: string) => `${i18nComposer.t('hello-from-/x', { x: from })}! - ${Date.now()}`
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import { appFactory } from '#src/helpers/factory.js'
2+
import { getCachedConvexClient } from '#src/providers/baas/convex-main.js'
23
import { getHelloMessage } from './hello.helper'
34

45
export const dummyHelloRouteApp = appFactory.createApp()
5-
.get('', async c => c.text(getHelloMessage()))
6+
.get('', async (c) => {
7+
const convexClient = getCachedConvexClient()
8+
9+
return c.text(getHelloMessage(`i18n and Hono ${convexClient ? '(+ Convex detected)' : ''}`))
10+
})

apps/backend/src/dev.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { localcertKeyPath, localcertPath } from '@local/common/dev/cert'
2+
import { getConvexEnvs } from 'backend-convex/_util'
23
import { serve } from 'srvx'
34
import { env } from 'std-env'
45
import { app } from './app'
56

7+
// Try load CONVEX_URL from local dev env
8+
env.CONVEX_URL ||= (await getConvexEnvs()).CONVEX_URL
9+
610
// Serve local server
711
serve({
812
fetch(request) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { logger } from '#src/helpers/logger.js'
2+
import { ConvexHttpClient } from 'convex/browser'
3+
import { env } from 'std-env'
4+
import { cacheProvider, getCachedProvider, isNonSharingPlatforms } from '..'
5+
6+
export function getCachedConvexClient() {
7+
return getCachedProvider<ConvexHttpClient>('convex-main--client')
8+
}
9+
10+
export async function getConvexClient() {
11+
const cachedClient = getCachedConvexClient()
12+
13+
if (!cachedClient && !isNonSharingPlatforms)
14+
throw new Error('Not initialized')
15+
16+
return cachedClient ?? await initConvexClient()
17+
}
18+
19+
export async function initConvexClient() {
20+
const cachedClient = getCachedConvexClient()
21+
22+
if (!isNonSharingPlatforms && cachedClient)
23+
logger.warn('Already initialized')
24+
25+
if (!env.CONVEX_URL)
26+
return logger.error('CONVEX_URL is not set')
27+
28+
const client = new ConvexHttpClient(env.CONVEX_URL!)
29+
30+
cacheProvider('convex-main--client', client)
31+
32+
return client
33+
}

apps/backend/src/providers/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { env as honoEnv } from 'hono/adapter'
22
import { createMiddleware } from 'hono/factory'
33
import { runtime } from 'std-env'
44
import { initKindeClient } from './auth/kinde-main'
5+
import { initConvexClient } from './baas/convex-main'
56

67
export const nonSharingPlatforms = new Set(['workerd'])
78
export const isNonSharingPlatforms = nonSharingPlatforms.has(runtime)
@@ -31,6 +32,7 @@ export const providersInit = createMiddleware(async (c, next) => {
3132
if (!providersState.initialized) {
3233
await Promise.all([
3334
initKindeClient(),
35+
initConvexClient(),
3436
])
3537

3638
providersState.initialized = true

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)