@@ -185,16 +185,16 @@ export function useStudioHost(user: StudioUser, repository: Repository): StudioH
185185 } ,
186186
187187 document : {
188- get : async ( fsPath : string ) : Promise < DatabaseItem > => {
188+ get : async ( fsPath : string ) : Promise < DatabaseItem | undefined > => {
189189 const collectionInfo = getCollectionByFilePath ( fsPath , useContentCollections ( ) )
190190 if ( ! collectionInfo ) {
191191 throw new Error ( `Collection not found for fsPath: ${ fsPath } ` )
192192 }
193193
194194 const id = generateIdFromFsPath ( fsPath , collectionInfo )
195- const item = await useContentCollectionQuery ( id . split ( '/' ) [ 0 ] as string ) . where ( 'id' , '=' , id ) . first ( )
195+ const item = await useContentCollectionQuery ( collectionInfo . name ) . where ( 'id' , '=' , id ) . first ( )
196196
197- return normalizeDocument ( fsPath , item as DatabaseItem )
197+ return item ? normalizeDocument ( fsPath , item as DatabaseItem ) : undefined
198198 } ,
199199 list : async ( ) : Promise < DatabaseItem [ ] > => {
200200 const collections = Object . values ( useContentCollections ( ) ) . filter ( collection => collection . name !== 'info' )
@@ -212,9 +212,10 @@ export function useStudioHost(user: StudioUser, repository: Repository): StudioH
212212 return documentsByCollection . flat ( )
213213 } ,
214214 create : async ( fsPath : string , content : string ) => {
215- const collections = useContentCollections ( )
216-
217- const collectionInfo = getCollectionByFilePath ( fsPath , collections )
215+ const collectionInfo = getCollectionByFilePath ( fsPath , useContentCollections ( ) )
216+ if ( ! collectionInfo ) {
217+ throw new Error ( `Collection not found for fsPath: ${ fsPath } ` )
218+ }
218219
219220 const id = generateIdFromFsPath ( fsPath , collectionInfo ! )
220221
@@ -224,24 +225,24 @@ export function useStudioHost(user: StudioUser, repository: Repository): StudioH
224225 }
225226
226227 const document = await generateDocumentFromContent ( id , content )
227- const collectionDocument = createCollectionDocument ( collectionInfo ! , id , document ! )
228+ const collectionDocument = createCollectionDocument ( id , collectionInfo , document ! )
228229
229- await host . document . upsert ( id , collectionDocument ! )
230+ await host . document . upsert ( fsPath , collectionDocument )
230231
231- return collectionDocument !
232+ return normalizeDocument ( fsPath , collectionDocument ! )
232233 } ,
233234 upsert : async ( fsPath : string , document : CollectionItemBase ) => {
234- const collection = getCollectionByFilePath ( fsPath , useContentCollections ( ) )
235- if ( ! collection ) {
235+ const collectionInfo = getCollectionByFilePath ( fsPath , useContentCollections ( ) )
236+ if ( ! collectionInfo ) {
236237 throw new Error ( `Collection not found for fsPath: ${ fsPath } ` )
237238 }
238239
239- const id = generateIdFromFsPath ( fsPath , collection )
240+ const id = generateIdFromFsPath ( fsPath , collectionInfo )
240241
241- const doc = createCollectionDocument ( collection , id , document )
242+ const doc = createCollectionDocument ( id , collectionInfo , document )
242243
243- await useContentDatabaseAdapter ( collection . name ) . exec ( generateRecordDeletion ( collection , id ) )
244- await useContentDatabaseAdapter ( collection . name ) . exec ( generateRecordInsert ( collection , doc ) )
244+ await useContentDatabaseAdapter ( collectionInfo . name ) . exec ( generateRecordDeletion ( collectionInfo , id ) )
245+ await useContentDatabaseAdapter ( collectionInfo . name ) . exec ( generateRecordInsert ( collectionInfo , doc ) )
245246 } ,
246247 delete : async ( fsPath : string ) => {
247248 const collection = getCollectionByFilePath ( fsPath , useContentCollections ( ) )
0 commit comments