Skip to content

Commit

Permalink
✨ feat: support to use pglite as client db (lobehub#4873)
Browse files Browse the repository at this point in the history
* ✨ feat: pglite 迁移实现

refactor modal name

pin pglite@0.2.13

add serverExternalPackages for pglite

fix tests

update model

remove migration

back previous test file

* 💚 build: fix build with ReactScan

* clean

* refactor with isDBinit

* fix test
  • Loading branch information
arvinxx authored Dec 22, 2024
1 parent dba432f commit 4131f20
Show file tree
Hide file tree
Showing 66 changed files with 4,940 additions and 171 deletions.
5 changes: 4 additions & 1 deletion next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ReactComponentName from 'react-scan/react-component-name/webpack';
const isProd = process.env.NODE_ENV === 'production';
const buildWithDocker = process.env.DOCKER === 'true';
const enableReactScan = !!process.env.REACT_SCAN_MONITOR_API_KEY;
const isUsePglite = process.env.NEXT_PUBLIC_CLIENT_DB === 'pglite';

// if you need to proxy the api endpoint to remote server
const API_PROXY_ENDPOINT = process.env.API_PROXY_ENDPOINT || '';
Expand All @@ -26,6 +27,7 @@ const nextConfig = {
'gpt-tokenizer',
'chroma-js',
],
serverComponentsExternalPackages: ['@electric-sql/pglite'],
webVitalsAttribution: ['CLS', 'LCP'],
},

Expand Down Expand Up @@ -180,7 +182,8 @@ const nextConfig = {
layers: true,
};

if (enableReactScan) {
// 开启该插件会导致 pglite 的 fs bundler 被改表
if (enableReactScan && !isUsePglite) {
config.plugins.push(ReactComponentName({}));
}

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"build-sitemap": "tsx ./scripts/buildSitemapIndex/index.ts",
"build:analyze": "ANALYZE=true next build",
"build:docker": "DOCKER=true next build && npm run build-sitemap",
"db:generate": "drizzle-kit generate",
"db:generate": "drizzle-kit generate && npm run db:generate-client",
"db:generate-client": "tsx ./scripts/migrateClientDB/compile-migrations.ts",
"db:migrate": "MIGRATION_DB=1 tsx ./scripts/migrateServerDB/index.ts",
"db:push": "drizzle-kit push",
"db:push-test": "NODE_ENV=test drizzle-kit push",
Expand Down Expand Up @@ -117,6 +118,7 @@
"@clerk/themes": "^2.1.37",
"@codesandbox/sandpack-react": "^2.19.9",
"@cyntler/react-doc-viewer": "^1.17.0",
"@electric-sql/pglite": "0.2.13",
"@google/generative-ai": "^0.21.0",
"@huggingface/inference": "^2.8.1",
"@icons-pack/react-simple-icons": "9.6.0",
Expand Down
14 changes: 14 additions & 0 deletions scripts/migrateClientDB/compile-migrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { readMigrationFiles } from 'drizzle-orm/migrator';
import { writeFileSync } from 'node:fs';
import { join } from 'node:path';

const dbBase = join(__dirname, '../../src/database');
const migrationsFolder = join(dbBase, './migrations');
const migrations = readMigrationFiles({ migrationsFolder: migrationsFolder });

writeFileSync(
join(dbBase, './client/migrations.json'),
JSON.stringify(migrations, null, 2), // null, 2 adds indentation for better readability
);

console.log('🏁 client migrations.json compiled!');
2 changes: 2 additions & 0 deletions src/app/(main)/(mobile)/me/(home)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { PropsWithChildren } from 'react';

import MobileContentLayout from '@/components/server/MobileNavLayout';
import InitClientDB from '@/features/InitClientDB';

import Header from './features/Header';

const Layout = ({ children }: PropsWithChildren) => {
return (
<MobileContentLayout header={<Header />} withNav>
{children}
<InitClientDB />
</MobileContentLayout>
);
};
Expand Down
5 changes: 3 additions & 2 deletions src/app/(main)/chat/_layout/Desktop/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Flexbox } from 'react-layout-kit';

import Migration from '../../features/Migration';
import InitClientDB from '@/features/InitClientDB';

import { LayoutProps } from '../type';
import SessionPanel from './SessionPanel';

Expand All @@ -18,7 +19,7 @@ const Layout = ({ children, session }: LayoutProps) => {
{children}
</Flexbox>
</Flexbox>
<Migration />
<InitClientDB bottom={60} />
{/* ↓ cloud slot ↓ */}

{/* ↑ cloud slot ↑ */}
Expand Down
8 changes: 5 additions & 3 deletions src/app/(main)/chat/_layout/Mobile.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use client';

import { createStyles } from 'antd-style';
import { memo } from 'react';
import { Suspense, memo } from 'react';
import { Flexbox } from 'react-layout-kit';

import Migration from '@/app/(main)/chat/features/Migration';
import InitClientDB from '@/features/InitClientDB';
import { useQuery } from '@/hooks/useQuery';

import { LayoutProps } from './type';
Expand Down Expand Up @@ -39,7 +39,9 @@ const Layout = memo<LayoutProps>(({ children, session }) => {
>
{children}
</Flexbox>
<Migration />
<Suspense fallback={null}>
<InitClientDB bottom={100} />
</Suspense>
</>
);
});
Expand Down
Loading

0 comments on commit 4131f20

Please sign in to comment.