Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fixes for DB initalization and import of demo data
azivner79a4a6eb0176
diff --git a/routes/api/login.js b/routes/api/login.jsindex 69a433ec13..e62d2053b1 100644--- a/routes/api/login.js+++ b/routes/api/login.js@@ -42,7 +42,7 @@ router.post('/sync', async (req, res, next) => {req.session.loggedIn = true;res.send({- sourceId: await source_id.getCurrentSourceId()+ sourceId: source_id.getCurrentSourceId()});});diff --git a/routes/api/sync.js b/routes/api/sync.jsindex 347f07a4a8..6646f54a72 100644--- a/routes/api/sync.js+++ b/routes/api/sync.js@@ -24,10 +24,7 @@ router.post('/now', auth.checkApiAuth, async (req, res, next) => {router.post('/fill-sync-rows', auth.checkApiAuth, async (req, res, next) => {await sql.doInTransaction(async () => {- await sync_table.fillSyncRows("notes", "note_id");- await sync_table.fillSyncRows("notes_tree", "note_tree_id");- await sync_table.fillSyncRows("notes_history", "note_history_id");- await sync_table.fillSyncRows("recent_notes", "note_tree_id");+ await sync_table.fillAllSyncRows();});log.info("Sync rows have been filled.");diff --git a/services/options.js b/services/options.jsindex 7355aeeb48..83f6dfd55a 100644--- a/services/options.js+++ b/services/options.js@@ -3,8 +3,9 @@ const utils = require('./utils');const sync_table = require('./sync_table');const app_info = require('./app_info');-const SYNCED_OPTIONS = [ 'username', 'password_verification_hash', 'encrypted_data_key', 'protected_session_timeout',- 'history_snapshot_time_interval' ];+const SYNCED_OPTIONS = [ 'username', 'password_verification_hash', 'password_verification_salt',+ 'password_derived_key_salt', 'encrypted_data_key', 'encrypted_data_key_iv',+ 'protected_session_timeout', 'history_snapshot_time_interval' ];async function getOption(optName) {const row = await sql.getFirstOrNull("SELECT opt_value FROM options WHERE opt_name = ?", [optName]);diff --git a/services/source_id.js b/services/source_id.jsindex 6cb8712f72..ce23ea86a1 100644--- a/services/source_id.js+++ b/services/source_id.js@@ -2,11 +2,7 @@ const utils = require('./utils');const log = require('./log');const sql = require('./sql');-async function generateSourceId() {- const sourceId = utils.randomString(12);-- log.info("Generated sourceId=" + sourceId);-+async function saveSourceId(sourceId) {await sql.doInTransaction(async () => {await sql.insert("source_ids", {source_id: sourceId,@@ -15,6 +11,19 @@ async function generateSourceId() {});await refreshSourceIds();+}++function createSourceId() {+ const sourceId = utils.randomString(12);++ log.info("Generated sourceId=" + sourceId);+ return sourceId;+}++async function generateSourceId() {+ const sourceId = createSourceId();++ await saveSourceId(sourceId);return sourceId;}@@ -25,16 +34,17 @@ async function refreshSourceIds() {let allSourceIds = [];-sql.dbReady.then(refreshSourceIds);-function isLocalSourceId(srcId) {return allSourceIds.includes(srcId);}-const currentSourceIdPromise = generateSourceId();+const currentSourceId = createSourceId();++// this will also refresh source IDs+sql.dbReady.then(() => saveSourceId(currentSourceId));-async function getCurrentSourceId() {- return await currentSourceIdPromise;+function getCurrentSourceId() {+ return currentSourceId;}module.exports = {diff --git a/services/sql.js b/services/sql.jsindex ff930cac7d..6e2c1b8ef3 100644--- a/services/sql.js+++ b/services/sql.js@@ -28,36 +28,23 @@ const dbReady = new Promise((resolve, reject) => {if (tableResults.length !== 1) {log.info("Connected to db, but schema doesn't exist. Initializing schema ...");- const schema = fs.readFileSync('schema.sql', 'UTF-8');+ const schema = fs.readFileSync('db/schema.sql', 'UTF-8');+ const notesSql = fs.readFileSync('db/main_notes.sql', 'UTF-8');+ const notesTreeSql = fs.readFileSync('db/main_notes_tree.sql', 'UTF-8');await doInTransaction(async () => {await executeScript(schema);+ await executeScript(notesSql);+ await executeScript(notesTreeSql);- const noteId = utils.newNoteId();- const now = utils.nowDate();-- await insert('notes_tree', {- note_tree_id: utils.newNoteTreeId(),- note_id: noteId,- parent_note_id: 'root',- note_position: 1,- is_deleted: 0,- date_modified: now- });-- await insert('notes', {- note_id: noteId,- note_title: 'Welcome to Trilium!',- note_text: 'Text',- is_protected: 0,- is_deleted: 0,- date_created: now,- date_modified: now- });+ const noteId = await getFirstValue("SELECT note_id FROM notes_tree WHERE parent_note_id = 'root' ORDER BY note_position");await require('./options').initOptions(noteId);+ await require('./sync_table').fillAllSyncRows();});+ log.info("Schema and initial content generated. Waiting for user to enter username/password to finish setup.");+// we don't resolve dbReady promise because user needs to setup the username and password to initialize// the database}diff --git a/services/sync.js b/services/sync.jsindex 479c17606f..f0895284f2 100644--- a/services/sync.js+++ b/services/sync.js@@ -235,7 +235,7 @@ async function pushEntity(sync, syncContext) {log.info(`Pushing changes in sync #${sync.id} ${sync.entity_name} ${sync.entity_id}`);const payload = {- sourceId: await source_id.getCurrentSourceId(),+ sourceId: source_id.getCurrentSourceId(),entity: entity};diff --git a/services/sync_table.js b/services/sync_table.jsindex 95fb26dd3b..960f4b7f96 100644--- a/services/sync_table.js+++ b/services/sync_table.js@@ -33,7 +33,7 @@ async function addEntitySync(entityName, entityId, sourceId) {entity_name: entityName,entity_id: entityId,sync_date: utils.nowDate(),- source_id: sourceId || await source_id.getCurrentSourceId()+ source_id: sourceId || source_id.getCurrentSourceId()});if (!sync_setup.isSyncSetup) {@@ -73,6 +73,13 @@ async function fillSyncRows(entityName, entityKey) {}}+async function fillAllSyncRows() {+ await fillSyncRows("notes", "note_id");+ await fillSyncRows("notes_tree", "note_tree_id");+ await fillSyncRows("notes_history", "note_history_id");+ await fillSyncRows("recent_notes", "note_tree_id");+}+module.exports = {addNoteSync,addNoteTreeSync,@@ -81,5 +88,5 @@ module.exports = {addOptionsSync,addRecentNoteSync,cleanupSyncRowsForMissingEntities,- fillSyncRows+ fillAllSyncRows};\ No newline at end of filediff --git a/views/setup.ejs b/views/setup.ejsindex 8974ef94e6..06e3e51a63 100644--- a/views/setup.ejs+++ b/views/setup.ejs@@ -31,6 +31,9 @@<script type="text/javascript">const baseApiUrl = 'api/';+ const glob = {+ sourceId: ''+ };</script><!-- Required for correct loading of scripts in Electron -->