11import type { CreationOptional , InferAttributes , InferCreationAttributes , ModelStatic , NonAttribute , Sequelize } from 'sequelize' ;
22import { DataTypes , Model , Op , QueryTypes } from 'sequelize' ;
33import type Orm from '@repository/storage/postgres/orm/sequelize/index.js' ;
4- import type { Note , NoteContent , NoteCreationAttributes , NoteInternalId , NotePublicId } from '@domain/entities/note.js' ;
4+ import type { Note , NoteCreationAttributes , NoteInternalId , NotePublicId , NotePreview } from '@domain/entities/note.js' ;
55import { UserModel } from '@repository/storage/postgres/orm/sequelize/user.js' ;
66import type { NoteSettingsModel } from './noteSettings.js' ;
77import type { NoteVisitsModel } from './noteVisits.js' ;
88import type { NoteHistoryModel } from './noteHistory.js' ;
9- import type { NoteHierarchy } from '@domain/entities/NoteHierarchy.js' ;
109
1110/* eslint-disable @typescript-eslint/naming-convention */
1211
@@ -349,33 +348,33 @@ export default class NoteSequelizeStorage {
349348 }
350349
351350 /**
352- * Creates a tree of notes
353- * @param noteId - public note id
354- * @returns NoteHierarchy
351+ * Get note and all of its children recursively
352+ * @param noteId - note id
353+ * @returns an array of NotePreview
355354 */
356- public async getNoteHierarchybyNoteId ( noteId : NoteInternalId ) : Promise < NoteHierarchy | null > {
355+ public async getNoteTreebyNoteId ( noteId : NoteInternalId ) : Promise < NotePreview [ ] | null > {
357356 // Fetch all notes and relations in a recursive query
358357 const query = `
359358 WITH RECURSIVE note_tree AS (
360359 SELECT
361- n.id AS noteId,
360+ n.id AS " noteId" ,
362361 n.content,
363- n.public_id,
364- nr.parent_id
362+ n.public_id AS "publicId" ,
363+ nr.parent_id AS "parentId"
365364 FROM ${ String ( this . database . literal ( this . tableName ) . val ) } n
366365 LEFT JOIN ${ String ( this . database . literal ( 'note_relations' ) . val ) } nr ON n.id = nr.note_id
367366 WHERE n.id = :startNoteId
368367
369368 UNION ALL
370369
371370 SELECT
372- n.id AS noteId,
371+ n.id AS " noteId" ,
373372 n.content,
374- n.public_id,
375- nr.parent_id
373+ n.public_id AS "publicId" ,
374+ nr.parent_id AS "parentId"
376375 FROM ${ String ( this . database . literal ( this . tableName ) . val ) } n
377376 INNER JOIN ${ String ( this . database . literal ( 'note_relations' ) . val ) } nr ON n.id = nr.note_id
378- INNER JOIN note_tree nt ON nr.parent_id = nt.noteId
377+ INNER JOIN note_tree nt ON nr.parent_id = nt." noteId"
379378 )
380379 SELECT * FROM note_tree;
381380 ` ;
@@ -388,46 +387,8 @@ export default class NoteSequelizeStorage {
388387 if ( ! result || result . length === 0 ) {
389388 return null ; // No data found
390389 }
390+ const notes = result as NotePreview [ ] ;
391391
392- type NoteRow = {
393- noteid : NoteInternalId ;
394- public_id : NotePublicId ;
395- content : NoteContent ;
396- parent_id : NoteInternalId | null ;
397- } ;
398-
399- const notes = result as NoteRow [ ] ;
400-
401- const notesMap = new Map < NoteInternalId , NoteHierarchy > ( ) ;
402-
403- let root : NoteHierarchy | null = null ;
404-
405- // Step 1: Parse and initialize all notes
406- notes . forEach ( ( note ) => {
407- notesMap . set ( note . noteid , {
408- id : note . public_id ,
409- content : note . content ,
410- childNotes : null ,
411- } ) ;
412- } ) ;
413-
414- // Step 2: Build hierarchy
415- notes . forEach ( ( note ) => {
416- if ( note . parent_id === null ) {
417- root = notesMap . get ( note . noteid ) ?? null ;
418- } else {
419- const parent = notesMap . get ( note . parent_id ) ;
420-
421- if ( parent ) {
422- // Initialize childNotes as an array if it's null
423- if ( parent . childNotes === null ) {
424- parent . childNotes = [ ] ;
425- }
426- parent . childNotes ?. push ( notesMap . get ( note . noteid ) ! ) ;
427- }
428- }
429- } ) ;
430-
431- return root ;
392+ return notes ;
432393 }
433394}
0 commit comments