@@ -221,26 +221,36 @@ export default class NoteRelationsSequelizeStorage {
221221
222222 // get all note ids via a singe sql query instead of many
223223 const query = `
224- WITH RECURSIVE note_tree AS (
225- SELECT noteId, parentId
226- FROM NoteRelations
227- WHERE noteId = :startNoteId
228- UNION ALL
229- SELECT nr.noteId, nr.parentId
230- FROM NoteRelations nr
231- INNER JOIN note_tree nt ON nt.parentId = nr.noteId
232- )
233- SELECT noteId FROM note_tree;
234- ` ;
235-
236- const result = await this . model . sequelize ?. query ( query , {
237- replacements : { startNoteId : noteId } ,
238- type : QueryTypes . SELECT ,
239- } ) ;
224+ WITH RECURSIVE note_parents AS (
225+ SELECT np.note_id, np.parent_id
226+ FROM ${ String ( this . database . literal ( this . tableName ) . val ) } np
227+ WHERE np.note_id = :startNoteId
228+ UNION ALL
229+ SELECT nr.note_id, nr.parent_id
230+ FROM ${ String ( this . database . literal ( this . tableName ) . val ) } nr
231+ INNER JOIN note_parents np ON np.parent_id = nr.note_id
232+ )
233+ SELECT np.note_id, np.parent_id
234+ FROM note_parents np;` ;
235+
236+ try {
237+ const result = await this . database . query ( query , {
238+ replacements : { startNoteId : noteId } ,
239+ type : QueryTypes . SELECT ,
240+ } ) ;
240241
241- parentNotes = ( result as { noteId : number } [ ] ) ?. map ( note => note . noteId ) ?? [ ] ;
242+ // eslint-disable-next-line @typescript-eslint/naming-convention
243+ const output = ( result as { note_id : number ; parent_id : number } [ ] ) ?. map ( note => note . parent_id ) ?? [ ] ;
242244
243- parentNotes . reverse ( ) ;
245+ if ( output . find ( note => ( note == noteId ) ) == undefined ) {
246+ parentNotes = [ noteId , ...output ] ;
247+ } else {
248+ parentNotes = output ;
249+ }
250+ parentNotes . reverse ( ) ;
251+ } catch {
252+ console . log ( `something wrong happened with sql query` ) ;
253+ }
244254
245255 return parentNotes ;
246256 }
0 commit comments