2021-10-16 22:13:34 +02:00
"use strict" ;
2021-10-17 14:44:59 +02:00
const sql = require ( '../sql' ) ;
2021-10-16 22:13:34 +02:00
const shaca = require ( './shaca.js' ) ;
2021-10-17 14:44:59 +02:00
const log = require ( '../../services/log' ) ;
2021-10-16 22:13:34 +02:00
const Note = require ( './entities/note' ) ;
const Branch = require ( './entities/branch' ) ;
const Attribute = require ( './entities/attribute' ) ;
2021-10-17 14:44:59 +02:00
const shareRoot = require ( '../share_root' ) ;
2021-12-07 23:03:49 +01:00
const eventService = require ( "../../services/events" ) ;
2021-10-16 22:13:34 +02:00
function load ( ) {
const start = Date . now ( ) ;
shaca . reset ( ) ;
// using raw query and passing arrays to avoid allocating new objects
2021-10-17 14:44:59 +02:00
const noteIds = sql . getColumn ( `
WITH RECURSIVE
tree ( noteId ) AS (
SELECT ?
UNION
SELECT branches . noteId FROM branches
JOIN tree ON branches . parentNoteId = tree . noteId
WHERE branches . isDeleted = 0
)
SELECT noteId FROM tree ` , [shareRoot.SHARE_ROOT_NOTE_ID]);
2021-10-16 22:13:34 +02:00
2021-10-17 14:44:59 +02:00
if ( noteIds . length === 0 ) {
shaca . loaded = true ;
2021-10-16 22:13:34 +02:00
return ;
}
2021-10-17 14:44:59 +02:00
const noteIdStr = noteIds . map ( noteId => ` ' ${ noteId } ' ` ) . join ( "," ) ;
2021-10-16 22:13:34 +02:00
2021-12-06 22:53:17 +01:00
for ( const row of sql . getRawRows ( ` SELECT noteId, title, type, mime, utcDateModified FROM notes WHERE isDeleted = 0 AND noteId IN ( ${ noteIdStr } ) ` ) ) {
2021-10-17 14:44:59 +02:00
new Note ( row ) ;
2021-10-16 22:13:34 +02:00
}
2021-12-20 17:30:47 +01:00
for ( const row of sql . getRawRows ( ` SELECT branchId, noteId, parentNoteId, prefix, isExpanded, utcDateModified FROM branches WHERE isDeleted = 0 AND parentNoteId IN ( ${ noteIdStr } ) ORDER BY notePosition ` ) ) {
2021-10-17 14:44:59 +02:00
new Branch ( row ) ;
2021-10-16 22:13:34 +02:00
}
2021-12-07 23:03:49 +01:00
const attributes = sql . getRawRows ( `
SELECT attributeId , noteId , type , name , value , isInheritable , position , utcDateModified
FROM attributes
WHERE isDeleted = 0
AND noteId IN ( $ { noteIdStr } )
AND (
2021-12-22 11:43:21 +01:00
( type = 'label' AND name IN ( 'archived' , 'shareHiddenFromTree' , 'shareAlias' , 'shareOmitDefaultCss' ) )
2021-12-22 09:10:38 +01:00
OR ( type = 'relation' AND name IN ( 'imageLink' , 'template' , 'shareCss' ) )
2021-12-07 23:03:49 +01:00
) ` , []);
for ( const row of attributes ) {
2021-10-17 14:44:59 +02:00
new Attribute ( row ) ;
2021-10-16 22:13:34 +02:00
}
2021-10-17 14:44:59 +02:00
shaca . loaded = true ;
2021-10-16 22:13:34 +02:00
2021-10-17 14:44:59 +02:00
log . info ( ` Shaca load took ${ Date . now ( ) - start } ms ` ) ;
2021-10-16 22:13:34 +02:00
}
2021-10-17 14:44:59 +02:00
function ensureLoad ( ) {
if ( ! shaca . loaded ) {
load ( ) ;
2021-10-16 22:13:34 +02:00
}
}
2021-12-07 23:03:49 +01:00
eventService . subscribe ( [ eventService . ENTITY _CREATED , eventService . ENTITY _CHANGED , eventService . ENTITY _DELETED , eventService . ENTITY _CHANGE _SYNCED , eventService . ENTITY _DELETE _SYNCED ] , ( { entityName , entity } ) => {
shaca . reset ( ) ;
} ) ;
2021-10-16 22:13:34 +02:00
module . exports = {
load ,
2021-10-17 14:44:59 +02:00
ensureLoad
2021-10-16 22:13:34 +02:00
} ;