| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  | const log = require('./log'); | 
					
						
							|  |  |  | const fs = require('fs'); | 
					
						
							|  |  |  | const resourceDir = require('./resource_dir'); | 
					
						
							|  |  |  | const sql = require('./sql'); | 
					
						
							| 
									
										
										
										
											2019-04-23 21:27:45 +02:00
										 |  |  | const utils = require('./utils'); | 
					
						
							| 
									
										
										
										
											2018-07-24 20:35:03 +02:00
										 |  |  | const optionService = require('./options'); | 
					
						
							| 
									
										
										
										
											2020-04-08 20:38:50 +02:00
										 |  |  | const port = require('./port'); | 
					
						
							| 
									
										
										
										
											2021-06-29 22:15:57 +02:00
										 |  |  | const Option = require('../becca/entities/option'); | 
					
						
							|  |  |  | const TaskContext = require('./task_context'); | 
					
						
							| 
									
										
										
										
											2020-07-02 22:57:17 +02:00
										 |  |  | const migrationService = require('./migration'); | 
					
						
							| 
									
										
										
										
											2020-07-23 22:31:06 +02:00
										 |  |  | const cls = require('./cls'); | 
					
						
							| 
									
										
										
										
											2021-01-12 21:50:05 +01:00
										 |  |  | const config = require('./config'); | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 21:42:41 +02:00
										 |  |  | const dbReady = utils.deferred(); | 
					
						
							| 
									
										
										
										
											2020-04-04 21:49:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-23 22:31:06 +02:00
										 |  |  | cls.init(initDbConnection); | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function schemaExists() { | 
					
						
							| 
									
										
										
										
											2020-06-20 21:42:41 +02:00
										 |  |  |     return !!sql.getValue(`SELECT name FROM sqlite_master
 | 
					
						
							|  |  |  |                                  WHERE type = 'table' AND name = 'options'`);
 | 
					
						
							| 
									
										
										
										
											2018-07-22 19:56:20 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-21 08:55:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function isDbInitialized() { | 
					
						
							|  |  |  |     if (!schemaExists()) { | 
					
						
							| 
									
										
										
										
											2018-07-24 08:12:36 +02:00
										 |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const initialized = sql.getValue("SELECT value FROM options WHERE name = 'initialized'"); | 
					
						
							| 
									
										
										
										
											2018-07-24 08:12:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 21:42:41 +02:00
										 |  |  |     return initialized === 'true'; | 
					
						
							| 
									
										
										
										
											2018-07-24 08:12:36 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-02 22:57:17 +02:00
										 |  |  | async function initDbConnection() { | 
					
						
							| 
									
										
										
										
											2020-06-20 21:42:41 +02:00
										 |  |  |     if (!isDbInitialized()) { | 
					
						
							| 
									
										
										
										
											2021-05-02 22:47:57 +02:00
										 |  |  |         log.info(`DB not initialized, please visit setup page` + | 
					
						
							|  |  |  |             (utils.isElectron() ? '' : ` - http://[your-server-host]:${await port} to see instructions on how to initialize Trilium.`)); | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 21:42:41 +02:00
										 |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-02 22:57:17 +02:00
										 |  |  |     await migrationService.migrateIfNecessary(); | 
					
						
							| 
									
										
										
										
											2018-04-03 22:15:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-10 21:27:21 +01:00
										 |  |  |     sql.execute('CREATE TEMP TABLE "param_list" (`paramId` TEXT NOT NULL PRIMARY KEY)'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 21:42:41 +02:00
										 |  |  |     dbReady.resolve(); | 
					
						
							| 
									
										
										
										
											2018-07-22 19:56:20 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-28 22:59:38 +01:00
										 |  |  | async function createInitialDatabase() { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     if (isDbInitialized()) { | 
					
						
							| 
									
										
										
										
											2018-07-24 08:12:36 +02:00
										 |  |  |         throw new Error("DB is already initialized"); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-02 22:33:54 -04:00
										 |  |  |     const schema = fs.readFileSync(resourceDir.DB_INIT_DIR + '/schema.sql', 'UTF-8'); | 
					
						
							| 
									
										
										
										
											2020-03-26 17:05:17 +01:00
										 |  |  |     const demoFile = fs.readFileSync(resourceDir.DB_INIT_DIR + '/demo.zip'); | 
					
						
							| 
									
										
										
										
											2018-04-02 22:33:54 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-02 21:08:18 +02:00
										 |  |  |     let rootNote; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     sql.transactional(() => { | 
					
						
							| 
									
										
										
										
											2021-12-28 22:59:38 +01:00
										 |  |  |         log.info("Creating database schema ..."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         sql.executeScript(schema); | 
					
						
							| 
									
										
										
										
											2018-11-16 14:36:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-29 22:15:57 +02:00
										 |  |  |         require("../becca/becca_loader").load(); | 
					
						
							| 
									
										
										
										
											2021-05-02 22:47:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-29 22:15:57 +02:00
										 |  |  |         const Note = require("../becca/entities/note"); | 
					
						
							|  |  |  |         const Branch = require("../becca/entities/branch"); | 
					
						
							| 
									
										
										
										
											2018-11-16 14:36:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-28 22:59:38 +01:00
										 |  |  |         log.info("Creating root note ..."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-02 21:08:18 +02:00
										 |  |  |         rootNote = new Note({ | 
					
						
							| 
									
										
										
										
											2018-11-16 14:36:50 +01:00
										 |  |  |             noteId: 'root', | 
					
						
							|  |  |  |             title: 'root', | 
					
						
							|  |  |  |             type: 'text', | 
					
						
							|  |  |  |             mime: 'text/html' | 
					
						
							|  |  |  |         }).save(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         rootNote.setContent(''); | 
					
						
							| 
									
										
										
										
											2019-02-20 23:07:57 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         new Branch({ | 
					
						
							| 
									
										
										
										
											2018-11-16 14:36:50 +01:00
										 |  |  |             branchId: 'root', | 
					
						
							|  |  |  |             noteId: 'root', | 
					
						
							|  |  |  |             parentNoteId: 'none', | 
					
						
							|  |  |  |             isExpanded: true, | 
					
						
							| 
									
										
										
										
											2019-10-19 12:36:16 +02:00
										 |  |  |             notePosition: 10 | 
					
						
							| 
									
										
										
										
											2018-11-16 14:36:50 +01:00
										 |  |  |         }).save(); | 
					
						
							| 
									
										
										
										
											2021-05-30 22:03:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         const optionsInitService = require('./options_init'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         optionsInitService.initDocumentOptions(); | 
					
						
							| 
									
										
										
										
											2021-12-28 22:59:38 +01:00
										 |  |  |         optionsInitService.initNotSyncedOptions(true, {}); | 
					
						
							| 
									
										
										
										
											2021-05-30 22:03:41 +02:00
										 |  |  |         optionsInitService.initStartupOptions(); | 
					
						
							| 
									
										
										
										
											2021-12-30 22:54:08 +01:00
										 |  |  |         require("./password").resetPassword(); | 
					
						
							| 
									
										
										
										
											2020-07-02 21:08:18 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-30 21:21:20 +02:00
										 |  |  |     log.info("Importing demo content ..."); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-21 16:12:59 +01:00
										 |  |  |     const dummyTaskContext = new TaskContext("no-progress-reporting", 'import', false); | 
					
						
							| 
									
										
										
										
											2018-11-16 14:36:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-02 21:08:18 +02:00
										 |  |  |     const zipImportService = require("./import/zip"); | 
					
						
							|  |  |  |     await zipImportService.importZip(dummyTaskContext, demoFile, rootNote); | 
					
						
							| 
									
										
										
										
											2019-02-24 12:24:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-02 21:08:18 +02:00
										 |  |  |     sql.transactional(() => { | 
					
						
							| 
									
										
										
										
											2021-05-30 22:03:41 +02:00
										 |  |  |         // this needs to happen after ZIP import
 | 
					
						
							|  |  |  |         // previous solution was to move option initialization here but then the important parts of initialization
 | 
					
						
							|  |  |  |         // are not all in one transaction (because ZIP import is async and thus not transactional)
 | 
					
						
							| 
									
										
										
										
											2018-04-02 22:33:54 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-30 22:03:41 +02:00
										 |  |  |         const startNoteId = sql.getValue("SELECT noteId FROM branches WHERE parentNoteId = 'root' AND isDeleted = 0 ORDER BY notePosition"); | 
					
						
							| 
									
										
										
										
											2018-07-23 21:15:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-30 22:03:41 +02:00
										 |  |  |         const optionService = require("./options"); | 
					
						
							|  |  |  |         optionService.setOption('openTabs', JSON.stringify([ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 notePath: startNoteId, | 
					
						
							|  |  |  |                 active: true | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ])); | 
					
						
							| 
									
										
										
										
											2018-04-02 22:33:54 -04:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-23 21:15:32 +02:00
										 |  |  |     log.info("Schema and initial content generated."); | 
					
						
							| 
									
										
										
										
											2018-04-02 22:33:54 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     initDbConnection(); | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function createDatabaseForSync(options, syncServerHost = '', syncProxy = '') { | 
					
						
							| 
									
										
										
										
											2018-07-24 20:35:03 +02:00
										 |  |  |     log.info("Creating database for sync"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     if (isDbInitialized()) { | 
					
						
							| 
									
										
										
										
											2018-07-24 20:35:03 +02:00
										 |  |  |         throw new Error("DB is already initialized"); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-23 21:15:32 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const schema = fs.readFileSync(resourceDir.DB_INIT_DIR + '/schema.sql', 'UTF-8'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     sql.transactional(() => { | 
					
						
							|  |  |  |         sql.executeScript(schema); | 
					
						
							| 
									
										
										
										
											2018-07-23 21:15:32 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-30 22:03:41 +02:00
										 |  |  |         require('./options_init').initNotSyncedOptions(false,  { syncServerHost, syncProxy }); | 
					
						
							| 
									
										
										
										
											2018-07-24 20:35:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // document options required for sync to kick off
 | 
					
						
							|  |  |  |         for (const opt of options) { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |             new Option(opt).save(); | 
					
						
							| 
									
										
										
										
											2018-07-24 20:35:03 +02:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-07-23 21:15:32 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     log.info("Schema and not synced options generated."); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 21:42:41 +02:00
										 |  |  | function setDbAsInitialized() { | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     if (!isDbInitialized()) { | 
					
						
							|  |  |  |         optionService.setOption('initialized', 'true'); | 
					
						
							| 
									
										
										
										
											2018-07-24 20:35:03 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |         initDbConnection(); | 
					
						
							| 
									
										
										
										
											2019-11-30 11:36:36 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-07-24 20:35:03 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-02 22:57:17 +02:00
										 |  |  | dbReady.then(() => { | 
					
						
							| 
									
										
										
										
											2021-01-12 21:50:05 +01:00
										 |  |  |     if (config.General && config.General.noBackup === true) { | 
					
						
							|  |  |  |         log.info("Disabling scheduled backups."); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-02 22:57:17 +02:00
										 |  |  |     setInterval(() => require('./backup').regularBackup(), 4 * 60 * 60 * 1000); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // kickoff first backup soon after start up
 | 
					
						
							|  |  |  |     setTimeout(() => require('./backup').regularBackup(), 5 * 60 * 1000); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | log.info("DB size: " + sql.getValue("SELECT page_count * page_size / 1000 as size FROM pragma_page_count(), pragma_page_size()") + " KB"); | 
					
						
							| 
									
										
										
										
											2019-01-15 20:00:24 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-02 21:25:20 -04:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |     dbReady, | 
					
						
							| 
									
										
										
										
											2018-07-24 08:12:36 +02:00
										 |  |  |     schemaExists, | 
					
						
							| 
									
										
										
										
											2018-07-22 19:56:20 +02:00
										 |  |  |     isDbInitialized, | 
					
						
							| 
									
										
										
										
											2018-07-23 21:15:32 +02:00
										 |  |  |     createInitialDatabase, | 
					
						
							| 
									
										
										
										
											2018-07-24 20:35:03 +02:00
										 |  |  |     createDatabaseForSync, | 
					
						
							| 
									
										
										
										
											2021-12-30 22:54:08 +01:00
										 |  |  |     setDbAsInitialized | 
					
						
							| 
									
										
										
										
											2020-06-17 23:03:46 +02:00
										 |  |  | }; |