Skip to content

Commit

Permalink
♻️ refactor: refactor data importer to repos (lobehub#4974)
Browse files Browse the repository at this point in the history
* refactor data importer

* Update importer.ts

* fix tests
  • Loading branch information
arvinxx authored Dec 11, 2024
1 parent ba095b8 commit 0259fec
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @vitest-environment node
import { eq, inArray } from 'drizzle-orm';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { getTestDBInstance } from '@/database/server/core/dbForTest';
import {
Expand All @@ -13,22 +13,15 @@ import {
users,
} from '@/database/schemas';
import { CURRENT_CONFIG_VERSION } from '@/migrations';
import { ImportResult } from '@/services/config';
import { ImporterEntryData } from '@/types/importer';

import { DataImporterService } from '../index';
import { DataImporterRepos } from '../index';
import mockImportData from './fixtures/messages.json';

let serverDB = await getTestDBInstance();

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

const userId = 'test-user-id';
let importer: DataImporterService;
let importer: DataImporterRepos;

beforeEach(async () => {
await serverDB.delete(users);
Expand All @@ -38,7 +31,7 @@ beforeEach(async () => {
await tx.insert(users).values({ id: userId });
});

importer = new DataImporterService(userId);
importer = new DataImporterRepos(serverDB, userId);
});

describe('DataImporter', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { eq, inArray, sql } from 'drizzle-orm';
import { and } from 'drizzle-orm/expressions';

import { serverDB } from '@/database/server';
import {
agents,
agentsToSessions,
Expand All @@ -12,19 +11,22 @@ import {
sessions,
topics,
} from '@/database/schemas';
import { LobeChatDatabase } from '@/database/type';
import { ImportResult } from '@/services/config';
import { ImporterEntryData } from '@/types/importer';

export class DataImporterService {
export class DataImporterRepos {
private userId: string;
private db: LobeChatDatabase;

/**
* The version of the importer that this module supports
*/
supportVersion = 7;

constructor(userId: string) {
constructor(db: LobeChatDatabase, userId: string) {
this.userId = userId;
this.db = db;
}

importData = async (data: ImporterEntryData) => {
Expand All @@ -40,7 +42,7 @@ export class DataImporterService {
let topicIdMap: Record<string, string> = {};

// import sessionGroups
await serverDB.transaction(async (trx) => {
await this.db.transaction(async (trx) => {
if (data.sessionGroups && data.sessionGroups.length > 0) {
const query = await trx.query.sessionGroups.findMany({
where: and(
Expand Down
5 changes: 3 additions & 2 deletions src/server/routers/lambda/importer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { TRPCError } from '@trpc/server';
import { z } from 'zod';

import { DataImporterRepos } from '@/database/repositories/dataImporter';
import { serverDB } from '@/database/server';
import { authedProcedure, router } from '@/libs/trpc';
import { S3 } from '@/server/modules/S3';
import { DataImporterService } from '@/server/services/dataImporter';
import { ImportResults, ImporterEntryData } from '@/types/importer';

const importProcedure = authedProcedure.use(async (opts) => {
const { ctx } = opts;
const dataImporterService = new DataImporterService(ctx.userId);
const dataImporterService = new DataImporterRepos(serverDB, ctx.userId);

return opts.next({
ctx: { dataImporterService },
Expand Down
2 changes: 1 addition & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default defineConfig({
'**/dist/**',
'**/build/**',
'src/database/server/**/**',
'src/server/services/dataImporter/**/**',
'src/database/repositories/dataImporter/**/**',
],
globals: true,
server: {
Expand Down

0 comments on commit 0259fec

Please sign in to comment.