Skip to content

Commit 69e9a78

Browse files
committed
try the desktop db
1 parent fbb4b45 commit 69e9a78

File tree

12 files changed

+64
-12
lines changed

12 files changed

+64
-12
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,5 @@ public/swe-worker*
6868
*.patch
6969
*.pdf
7070
vertex-ai-key.json
71-
.pnpm-store
71+
.pnpm-store
72+
lobechat-db

src/database/server/core/db.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1+
import { PGlite } from '@electric-sql/pglite';
12
import { Pool as NeonPool, neonConfig } from '@neondatabase/serverless';
23
import { NeonDatabase, drizzle as neonDrizzle } from 'drizzle-orm/neon-serverless';
34
import { drizzle as nodeDrizzle } from 'drizzle-orm/node-postgres';
5+
import { drizzle as pgliteDrizzle } from 'drizzle-orm/pglite';
46
import { Pool as NodePool } from 'pg';
57
import ws from 'ws';
68

79
import { serverDBEnv } from '@/config/db';
8-
import { isServerMode } from '@/const/version';
10+
import { isDesktop, isServerMode } from '@/const/version';
911

1012
import * as schema from '../../schemas';
1113

14+
export const getPgliteInstance = () => {
15+
const client = new PGlite('lobechat-db');
16+
return pgliteDrizzle({ client, schema });
17+
};
18+
1219
export const getDBInstance = (): NeonDatabase<typeof schema> => {
1320
if (!isServerMode) return {} as any;
1421

@@ -41,4 +48,4 @@ If you don't have it, please run \`openssl rand -base64 32\` to create one.
4148
return neonDrizzle(client, { schema });
4249
};
4350

44-
export const serverDB = getDBInstance();
51+
export const serverDB = isDesktop ? getPgliteInstance() : getDBInstance();

src/libs/trpc/middleware/userAuth.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
import { TRPCError } from '@trpc/server';
22

33
import { enableClerk } from '@/const/auth';
4+
import { isDesktop } from '@/const/version';
45

56
import { trpc } from '../init';
67

78
export const userAuth = trpc.middleware(async (opts) => {
89
const { ctx } = opts;
10+
if (isDesktop) {
11+
return opts.next({
12+
ctx: {
13+
userId: 'desktop_user',
14+
},
15+
});
16+
}
917
// `ctx.user` is nullable
1018
if (!ctx.userId) {
1119
if (enableClerk) {

src/services/aiModel/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { isDesktop } from '@/const/version';
2+
13
import { ClientService } from './client';
24
import { ServerService } from './server';
35

46
export const aiModelService =
5-
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' ? new ServerService() : new ClientService();
7+
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' || isDesktop
8+
? new ServerService()
9+
: new ClientService();

src/services/aiProvider/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { isDesktop } from '@/const/version';
2+
13
import { ClientService } from './client';
24
import { ServerService } from './server';
35

46
export const aiProviderService =
5-
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' ? new ServerService() : new ClientService();
7+
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' || isDesktop
8+
? new ServerService()
9+
: new ClientService();

src/services/file/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isDesktop } from '@/const/version';
2+
13
import { ClientService as DeprecatedService } from './_deprecated';
24
import { ClientService } from './client';
35
import { ServerService } from './server';
@@ -6,4 +8,6 @@ const clientService =
68
process.env.NEXT_PUBLIC_CLIENT_DB === 'pglite' ? new ClientService() : new DeprecatedService();
79

810
export const fileService =
9-
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' ? new ServerService() : clientService;
11+
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' || isDesktop
12+
? new ServerService()
13+
: clientService;

src/services/message/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isDesktop } from '@/const/version';
2+
13
import { ClientService as DeprecatedService } from './_deprecated';
24
import { ClientService } from './client';
35
import { ServerService } from './server';
@@ -6,4 +8,6 @@ const clientService =
68
process.env.NEXT_PUBLIC_CLIENT_DB === 'pglite' ? new ClientService() : new DeprecatedService();
79

810
export const messageService =
9-
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' ? new ServerService() : clientService;
11+
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' || isDesktop
12+
? new ServerService()
13+
: clientService;

src/services/plugin/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isDesktop } from '@/const/version';
2+
13
import { ClientService as DeprecatedService } from './_deprecated';
24
import { ClientService } from './client';
35
import { ServerService } from './server';
@@ -6,4 +8,6 @@ const clientService =
68
process.env.NEXT_PUBLIC_CLIENT_DB === 'pglite' ? new ClientService() : new DeprecatedService();
79

810
export const pluginService =
9-
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' ? new ServerService() : clientService;
11+
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' || isDesktop
12+
? new ServerService()
13+
: clientService;

src/services/session/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isDesktop } from '@/const/version';
2+
13
import { ClientService as DeprecatedService } from './_deprecated';
24
import { ClientService } from './client';
35
import { ServerService } from './server';
@@ -6,4 +8,6 @@ const clientService =
68
process.env.NEXT_PUBLIC_CLIENT_DB === 'pglite' ? new ClientService() : new DeprecatedService();
79

810
export const sessionService =
9-
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' ? new ServerService() : clientService;
11+
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' || isDesktop
12+
? new ServerService()
13+
: clientService;

src/services/thread/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { isDesktop } from '@/const/version';
2+
13
import { ClientService } from './client';
24
import { ServerService } from './server';
35

46
export const threadService =
5-
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' ? new ServerService() : new ClientService();
7+
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' || isDesktop
8+
? new ServerService()
9+
: new ClientService();

src/services/topic/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isDesktop } from '@/const/version';
2+
13
import { ClientService as DeprecatedService } from './_deprecated';
24
import { ClientService } from './client';
35
import { ServerService } from './server';
@@ -6,4 +8,6 @@ const clientService =
68
process.env.NEXT_PUBLIC_CLIENT_DB === 'pglite' ? new ClientService() : new DeprecatedService();
79

810
export const topicService =
9-
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' ? new ServerService() : clientService;
11+
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' || isDesktop
12+
? new ServerService()
13+
: clientService;

src/services/user/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isDesktop } from '@/const/version';
2+
13
import { ClientService as DeprecatedService } from './_deprecated';
24
import { ClientService } from './client';
35
import { ServerService } from './server';
@@ -6,6 +8,8 @@ const clientService =
68
process.env.NEXT_PUBLIC_CLIENT_DB === 'pglite' ? new ClientService() : new DeprecatedService();
79

810
export const userService =
9-
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' ? new ServerService() : clientService;
11+
process.env.NEXT_PUBLIC_SERVICE_MODE === 'server' || isDesktop
12+
? new ServerService()
13+
: clientService;
1014

1115
export const userClientService = clientService;

0 commit comments

Comments
 (0)