@@ -47,22 +47,22 @@ export const DATA_EXPORT_CONFIG = {
47
47
relationTables : [
48
48
{
49
49
relations : [
50
- { sourceField : 'id ' , sourceTable : 'agents ' , field : 'agentId ' } ,
50
+ { field : 'agentId ' , sourceField : 'id ' , sourceTable : 'agents ' } ,
51
51
{ sourceField : 'sessionId' , sourceTable : 'sessions' } ,
52
52
] ,
53
53
table : 'agentsToSessions' ,
54
54
} ,
55
- // {
56
- // relations: [
57
- // { sourceField: 'agentId', sourceTable: 'agents' },
58
- // { sourceField: 'fileId', sourceTable: 'files' },
59
- // ],
60
- // table: 'agentsFiles',
61
- // },
62
- // {
63
- // relations: [{ field: 'sessionId', sourceTable: 'sessions' }],
64
- // table: 'filesToSessions',
65
- // },
55
+ {
56
+ relations : [
57
+ { sourceField : 'agentId' , sourceTable : 'agents' } ,
58
+ { sourceField : 'fileId' , sourceTable : 'files' } ,
59
+ ] ,
60
+ table : 'agentsFiles' ,
61
+ } ,
62
+ {
63
+ relations : [ { field : 'sessionId' , sourceTable : 'sessions' } ] ,
64
+ table : 'filesToSessions' ,
65
+ } ,
66
66
// {
67
67
// relations: [{ field: 'id', sourceTable: 'chunks' }],
68
68
// table: 'fileChunks',
@@ -115,10 +115,13 @@ export const DATA_EXPORT_CONFIG = {
115
115
} ;
116
116
117
117
export class DataExporterRepos {
118
- constructor (
119
- private db : LobeChatDatabase ,
120
- private userId : string ,
121
- ) { }
118
+ private userId : string ;
119
+ private db : LobeChatDatabase ;
120
+
121
+ constructor ( db : LobeChatDatabase , userId : string ) {
122
+ this . db = db ;
123
+ this . userId = userId ;
124
+ }
122
125
123
126
private removeUserId ( data : any [ ] ) {
124
127
return data . map ( ( item ) => {
@@ -134,17 +137,31 @@ export class DataExporterRepos {
134
137
if ( ! tableObj ) throw new Error ( `Table ${ table } not found` ) ;
135
138
136
139
try {
137
- let where ;
140
+ const conditions = [ ] ;
138
141
139
- const conditions = config . relations . map ( ( relation ) => {
142
+ // 处理每个关联条件
143
+ for ( const relation of config . relations ) {
140
144
const sourceData = existingData [ relation . sourceTable ] || [ ] ;
141
145
146
+ // 如果源数据为空,这个表可能无法查询到任何数据
147
+ if ( sourceData . length === 0 ) {
148
+ console . log (
149
+ `Source table ${ relation . sourceTable } has no data, skipping query for ${ table } ` ,
150
+ ) ;
151
+ return [ ] ;
152
+ }
153
+
142
154
const sourceIds = sourceData . map ( ( item ) => item [ relation . sourceField || 'id' ] ) ;
143
- console . log ( sourceIds ) ;
144
- return inArray ( tableObj [ relation . field ] , sourceIds ) ;
145
- } ) ;
155
+ conditions . push ( inArray ( tableObj [ relation . field ] , sourceIds ) ) ;
156
+ }
157
+
158
+ // 如果表有userId字段并且不是users表,添加用户过滤
159
+ if ( 'userId' in tableObj && table !== 'users' && ! config . relations ) {
160
+ conditions . push ( eq ( tableObj . userId , this . userId ) ) ;
161
+ }
146
162
147
- where = conditions . length === 1 ? conditions [ 0 ] : and ( ...conditions ) ;
163
+ // 组合所有条件
164
+ const where = conditions . length === 1 ? conditions [ 0 ] : and ( ...conditions ) ;
148
165
149
166
// @ts -expect-error query
150
167
const result = await this . db . query [ table ] . findMany ( { where } ) ;
@@ -204,10 +221,22 @@ export class DataExporterRepos {
204
221
console . log ( 'Querying relation tables...' ) ;
205
222
const relationResults = await pMap (
206
223
DATA_EXPORT_CONFIG . relationTables ,
207
- async ( config ) => ( {
208
- data : await this . queryTable ( config , result ) ,
209
- table : config . table ,
210
- } ) ,
224
+ async ( config ) => {
225
+ // 检查所有依赖的源表是否有数据
226
+ const allSourcesHaveData = config . relations . every (
227
+ ( relation ) => ( result [ relation . sourceTable ] || [ ] ) . length > 0 ,
228
+ ) ;
229
+
230
+ if ( ! allSourcesHaveData ) {
231
+ console . log ( `Skipping table ${ config . table } as some source tables have no data` ) ;
232
+ return { data : [ ] , table : config . table } ;
233
+ }
234
+
235
+ return {
236
+ data : await this . queryTable ( config , result ) ,
237
+ table : config . table ,
238
+ } ;
239
+ } ,
211
240
{ concurrency } ,
212
241
) ;
213
242
0 commit comments