@@ -6,18 +6,7 @@ import type { StorageContext } from '../shared/types'
66
77const logger = createLogger ( 'FileMetadata' )
88
9- export interface FileMetadataRecord {
10- id : string
11- key : string
12- userId : string
13- workspaceId : string | null
14- context : string
15- originalName : string
16- contentType : string
17- size : number
18- deletedAt ?: Date | null
19- uploadedAt : Date
20- }
9+ export type FileMetadataRecord = typeof workspaceFiles . $inferSelect
2110
2211export interface FileMetadataInsertOptions {
2312 key : string
@@ -52,7 +41,7 @@ export async function insertFileMetadata(
5241 . limit ( 1 )
5342
5443 if ( existingDeleted . length > 0 && existingDeleted [ 0 ] . deletedAt ) {
55- await db
44+ const [ restored ] = await db
5645 . update ( workspaceFiles )
5746 . set ( {
5847 userId,
@@ -65,19 +54,9 @@ export async function insertFileMetadata(
6554 uploadedAt : new Date ( ) ,
6655 } )
6756 . where ( eq ( workspaceFiles . id , existingDeleted [ 0 ] . id ) )
57+ . returning ( )
6858
69- return {
70- id : existingDeleted [ 0 ] . id ,
71- key,
72- userId,
73- workspaceId : workspaceId || null ,
74- context,
75- originalName,
76- contentType,
77- size,
78- deletedAt : null ,
79- uploadedAt : new Date ( ) ,
80- }
59+ return restored
8160 }
8261
8362 const existing = await db
@@ -87,48 +66,29 @@ export async function insertFileMetadata(
8766 . limit ( 1 )
8867
8968 if ( existing . length > 0 ) {
90- return {
91- id : existing [ 0 ] . id ,
92- key : existing [ 0 ] . key ,
93- userId : existing [ 0 ] . userId ,
94- workspaceId : existing [ 0 ] . workspaceId ,
95- context : existing [ 0 ] . context ,
96- originalName : existing [ 0 ] . originalName ,
97- contentType : existing [ 0 ] . contentType ,
98- size : existing [ 0 ] . size ,
99- deletedAt : existing [ 0 ] . deletedAt ,
100- uploadedAt : existing [ 0 ] . uploadedAt ,
101- }
69+ return existing [ 0 ]
10270 }
10371
10472 const fileId = id || ( await import ( 'uuid' ) ) . v4 ( )
10573
10674 try {
107- await db . insert ( workspaceFiles ) . values ( {
108- id : fileId ,
109- key,
110- userId,
111- workspaceId : workspaceId || null ,
112- context,
113- originalName,
114- contentType,
115- size,
116- deletedAt : null ,
117- uploadedAt : new Date ( ) ,
118- } )
75+ const [ inserted ] = await db
76+ . insert ( workspaceFiles )
77+ . values ( {
78+ id : fileId ,
79+ key,
80+ userId,
81+ workspaceId : workspaceId || null ,
82+ context,
83+ originalName,
84+ contentType,
85+ size,
86+ deletedAt : null ,
87+ uploadedAt : new Date ( ) ,
88+ } )
89+ . returning ( )
11990
120- return {
121- id : fileId ,
122- key,
123- userId,
124- workspaceId : workspaceId || null ,
125- context,
126- originalName,
127- contentType,
128- size,
129- deletedAt : null ,
130- uploadedAt : new Date ( ) ,
131- }
91+ return inserted
13292 } catch ( error ) {
13393 if (
13494 ( error as any ) ?. code === '23505' ||
@@ -141,18 +101,7 @@ export async function insertFileMetadata(
141101 . limit ( 1 )
142102
143103 if ( existingAfterError . length > 0 ) {
144- return {
145- id : existingAfterError [ 0 ] . id ,
146- key : existingAfterError [ 0 ] . key ,
147- userId : existingAfterError [ 0 ] . userId ,
148- workspaceId : existingAfterError [ 0 ] . workspaceId ,
149- context : existingAfterError [ 0 ] . context ,
150- originalName : existingAfterError [ 0 ] . originalName ,
151- contentType : existingAfterError [ 0 ] . contentType ,
152- size : existingAfterError [ 0 ] . size ,
153- deletedAt : existingAfterError [ 0 ] . deletedAt ,
154- uploadedAt : existingAfterError [ 0 ] . uploadedAt ,
155- }
104+ return existingAfterError [ 0 ]
156105 }
157106 }
158107
@@ -186,22 +135,7 @@ export async function getFileMetadataByKey(
186135 . where ( conditions . length > 1 ? and ( ...conditions ) : conditions [ 0 ] )
187136 . limit ( 1 )
188137
189- if ( ! record ) {
190- return null
191- }
192-
193- return {
194- id : record . id ,
195- key : record . key ,
196- userId : record . userId ,
197- workspaceId : record . workspaceId ,
198- context : record . context ,
199- originalName : record . originalName ,
200- contentType : record . contentType ,
201- size : record . size ,
202- deletedAt : record . deletedAt ,
203- uploadedAt : record . uploadedAt ,
204- }
138+ return record ?? null
205139}
206140
207141/**
@@ -225,24 +159,11 @@ export async function getFileMetadataByContext(
225159 conditions . push ( isNull ( workspaceFiles . deletedAt ) )
226160 }
227161
228- const records = await db
162+ return db
229163 . select ( )
230164 . from ( workspaceFiles )
231165 . where ( conditions . length > 1 ? and ( ...conditions ) : conditions [ 0 ] )
232166 . orderBy ( workspaceFiles . uploadedAt )
233-
234- return records . map ( ( record ) => ( {
235- id : record . id ,
236- key : record . key ,
237- userId : record . userId ,
238- workspaceId : record . workspaceId ,
239- context : record . context ,
240- originalName : record . originalName ,
241- contentType : record . contentType ,
242- size : record . size ,
243- deletedAt : record . deletedAt ,
244- uploadedAt : record . uploadedAt ,
245- } ) )
246167}
247168
248169/**
0 commit comments