Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] main from lobehub:main #112

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,31 @@

# Changelog

### [Version 1.35.10](https://github.com/lobehub/lobe-chat/compare/v1.35.9...v1.35.10)

<sup>Released on **2024-12-03**</sup>

#### ♻ Code Refactoring

- **misc**: Refactor the server db model implement.

<br/>

<details>
<summary><kbd>Improvements and Fixes</kbd></summary>

#### Code refactoring

- **misc**: Refactor the server db model implement, closes [#4878](https://github.com/lobehub/lobe-chat/issues/4878) ([3814853](https://github.com/lobehub/lobe-chat/commit/3814853))

</details>

<div align="right">

[![](https://img.shields.io/badge/-BACK_TO_TOP-151515?style=flat-square)](#readme-top)

</div>

### [Version 1.35.9](https://github.com/lobehub/lobe-chat/compare/v1.35.8...v1.35.9)

<sup>Released on **2024-12-03**</sup>
Expand Down
7 changes: 7 additions & 0 deletions changelog/v1.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
[
{
"children": {
"improvements": ["Refactor the server db model implement."]
},
"date": "2024-12-03",
"version": "1.35.10"
},
{
"children": {},
"date": "2024-12-03",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lobehub/chat",
"version": "1.35.9",
"version": "1.35.10",
"description": "Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.",
"keywords": [
"framework",
Expand Down
3 changes: 2 additions & 1 deletion src/app/(main)/repos/[id]/@menu/default.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { notFound } from 'next/navigation';
import { Flexbox } from 'react-layout-kit';

import { serverDB } from '@/database/server';
import { KnowledgeBaseModel } from '@/database/server/models/knowledgeBase';

import Head from './Head';
Expand All @@ -14,7 +15,7 @@ type Props = { params: Params };

const MenuPage = async ({ params }: Props) => {
const id = params.id;
const item = await KnowledgeBaseModel.findById(params.id);
const item = await KnowledgeBaseModel.findById(serverDB, params.id);

if (!item) return notFound();

Expand Down
3 changes: 2 additions & 1 deletion src/app/(main)/repos/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { redirect } from 'next/navigation';

import { serverDB } from '@/database/server';
import { KnowledgeBaseModel } from '@/database/server/models/knowledgeBase';
import FileManager from '@/features/FileManager';

Expand All @@ -10,7 +11,7 @@ interface Params {
type Props = { params: Params };

export default async ({ params }: Props) => {
const item = await KnowledgeBaseModel.findById(params.id);
const item = await KnowledgeBaseModel.findById(serverDB, params.id);

if (!item) return redirect('/repos');

Expand Down
4 changes: 3 additions & 1 deletion src/database/schemas/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { boolean, jsonb, pgTable, text, unique } from 'drizzle-orm/pg-core';
import { createInsertSchema } from 'drizzle-zod';

import { idGenerator } from '@/database/utils/idGenerator';
import { ChatTopicMetadata } from '@/types/topic';

import { timestamps, timestamptz } from './_helpers';
import { sessions } from './session';
import { users } from './user';
Expand All @@ -21,7 +23,7 @@ export const topics = pgTable(
.notNull(),
clientId: text('client_id'),
historySummary: text('history_summary'),
metadata: jsonb('metadata'),
metadata: jsonb('metadata').$type<ChatTopicMetadata | undefined>(),
...timestamps,
},
(t) => ({
Expand Down
10 changes: 4 additions & 6 deletions src/database/server/core/dbForTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { serverDBEnv } from '@/config/db';

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

const migrationsFolder = join(__dirname, '../../migrations');

export const getTestDBInstance = async () => {
let connectionString = serverDBEnv.DATABASE_TEST_URL;

Expand All @@ -23,9 +25,7 @@ export const getTestDBInstance = async () => {

const db = nodeDrizzle(client, { schema });

await nodeMigrator.migrate(db, {
migrationsFolder: join(__dirname, '../../migrations'),
});
await nodeMigrator.migrate(db, { migrationsFolder });

return db;
}
Expand All @@ -37,9 +37,7 @@ export const getTestDBInstance = async () => {

const db = neonDrizzle(client, { schema });

await migrator.migrate(db, {
migrationsFolder: join(__dirname, '../migrations'),
});
await migrator.migrate(db, { migrationsFolder });

return db;
};
12 changes: 3 additions & 9 deletions src/database/server/models/__tests__/_test_template.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @vitest-environment node
import { eq } from 'drizzle-orm';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';

import { getTestDBInstance } from '@/database/server/core/dbForTest';

Expand All @@ -9,14 +9,8 @@ import { SessionGroupModel } from '../sessionGroup';

let serverDB = await getTestDBInstance();

vi.mock('@/database/server/core/db', async () => ({
get serverDB() {
return serverDB;
},
}));

const userId = 'session-group-model-test-user-id';
const sessionGroupModel = new SessionGroupModel(userId);
const sessionGroupModel = new SessionGroupModel(serverDB, userId);

beforeEach(async () => {
await serverDB.delete(users);
Expand Down Expand Up @@ -74,7 +68,7 @@ describe('SessionGroupModel', () => {
await sessionGroupModel.create({ name: 'Test Group 1' });
await sessionGroupModel.create({ name: 'Test Group 333' });

const anotherSessionGroupModel = new SessionGroupModel('user2');
const anotherSessionGroupModel = new SessionGroupModel(serverDB, 'user2');
await anotherSessionGroupModel.create({ name: 'Test Group 2' });

await sessionGroupModel.deleteAll();
Expand Down
10 changes: 2 additions & 8 deletions src/database/server/models/__tests__/agent.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @vitest-environment node
import { eq } from 'drizzle-orm';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';

import { getTestDBInstance } from '@/database/server/core/dbForTest';

Expand All @@ -18,14 +18,8 @@ import { AgentModel } from '../agent';

let serverDB = await getTestDBInstance();

vi.mock('@/database/server/core/db', async () => ({
get serverDB() {
return serverDB;
},
}));

const userId = 'agent-model-test-user-id';
const agentModel = new AgentModel(userId);
const agentModel = new AgentModel(serverDB, userId);

const knowledgeBase = { id: 'kb1', userId, name: 'knowledgeBase' };
const fileList = [
Expand Down
8 changes: 1 addition & 7 deletions src/database/server/models/__tests__/asyncTask.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,8 @@ import { ASYNC_TASK_TIMEOUT, AsyncTaskModel } from '../asyncTask';

let serverDB = await getTestDBInstance();

vi.mock('@/database/server/core/db', async () => ({
get serverDB() {
return serverDB;
},
}));

const userId = 'async-task-model-test-user-id';
const asyncTaskModel = new AsyncTaskModel(userId);
const asyncTaskModel = new AsyncTaskModel(serverDB, userId);

beforeEach(async () => {
await serverDB.delete(users);
Expand Down
Loading
Loading