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 , NoteRow } 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+ * Fetches the raw recursive note tree data from DB
352+ * @param noteId - note id
353+ * @returns Array of raw note rows (note with parent_id)
355354 */
356- public async getNoteHierarchybyNoteId ( noteId : NoteInternalId ) : Promise < NoteHierarchy | null > {
355+ public async getNoteRowbyNoteId ( noteId : NoteInternalId ) : Promise < NoteRow [ ] | 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,58 +387,8 @@ export default class NoteSequelizeStorage {
388387 if ( ! result || result . length === 0 ) {
389388 return null ; // No data found
390389 }
391-
392- type NoteRow = {
393- noteid : NoteInternalId ;
394- public_id : NotePublicId ;
395- content : NoteContent ;
396- parent_id : NoteInternalId | null ;
397- } ;
398-
399390 const notes = result as NoteRow [ ] ;
400391
401- const notesMap = new Map < NoteInternalId , NoteHierarchy > ( ) ;
402-
403- let root : NoteHierarchy | null = null ;
404-
405- const getTitleFromContent = ( content : NoteContent ) : string => {
406- const limitCharsForNoteTitle = 50 ;
407- const firstNoteBlock = content . blocks [ 0 ] ;
408- const text = ( firstNoteBlock ?. data as { text ?: string } ) ?. text ;
409-
410- if ( text === undefined || text . trim ( ) === '' ) {
411- return 'Untitled' ;
412- }
413-
414- return text . replace ( / & n b s p ; / g, ' ' ) . slice ( 0 , limitCharsForNoteTitle ) ;
415- } ;
416-
417- // Step 1: Parse and initialize all notes
418- notes . forEach ( ( note ) => {
419- notesMap . set ( note . noteid , {
420- noteId : note . public_id ,
421- noteTitle : getTitleFromContent ( note . content ) ,
422- childNotes : null ,
423- } ) ;
424- } ) ;
425-
426- // Step 2: Build hierarchy
427- notes . forEach ( ( note ) => {
428- if ( note . parent_id === null ) {
429- root = notesMap . get ( note . noteid ) ?? null ;
430- } else {
431- const parent = notesMap . get ( note . parent_id ) ;
432-
433- if ( parent ) {
434- // Initialize childNotes as an array if it's null
435- if ( parent . childNotes === null ) {
436- parent . childNotes = [ ] ;
437- }
438- parent . childNotes ?. push ( notesMap . get ( note . noteid ) ! ) ;
439- }
440- }
441- } ) ;
442-
443- return root ;
392+ return notes ;
444393 }
445394}
0 commit comments