fixed initial demo import

This commit is contained in:
zadam 2020-07-02 21:08:18 +02:00
parent ef847b9904
commit c4cc48dfc9
5 changed files with 27 additions and 23 deletions

View File

@ -13,10 +13,10 @@ function getStatus() {
}; };
} }
function setupNewDocument(req) { async function setupNewDocument(req) {
const { username, password, theme } = req.body; const { username, password, theme } = req.body;
sqlInit.createInitialDatabase(username, password, theme); await sqlInit.createInitialDatabase(username, password, theme);
} }
function setupSyncFromServer(req) { function setupSyncFromServer(req) {

View File

@ -1,5 +1,3 @@
const utils = require('./utils');
function getOption(name) { function getOption(name) {
const option = require('./repository').getOption(name); const option = require('./repository').getOption(name);

View File

@ -55,7 +55,7 @@ function initDbConnection() {
dbReady.resolve(); dbReady.resolve();
} }
function createInitialDatabase(username, password, theme) { async function createInitialDatabase(username, password, theme) {
log.info("Creating initial database ..."); log.info("Creating initial database ...");
if (isDbInitialized()) { if (isDbInitialized()) {
@ -65,13 +65,15 @@ function createInitialDatabase(username, password, theme) {
const schema = fs.readFileSync(resourceDir.DB_INIT_DIR + '/schema.sql', 'UTF-8'); const schema = fs.readFileSync(resourceDir.DB_INIT_DIR + '/schema.sql', 'UTF-8');
const demoFile = fs.readFileSync(resourceDir.DB_INIT_DIR + '/demo.zip'); const demoFile = fs.readFileSync(resourceDir.DB_INIT_DIR + '/demo.zip');
let rootNote;
sql.transactional(() => { sql.transactional(() => {
sql.executeScript(schema); sql.executeScript(schema);
const Note = require("../entities/note"); const Note = require("../entities/note");
const Branch = require("../entities/branch"); const Branch = require("../entities/branch");
const rootNote = new Note({ rootNote = new Note({
noteId: 'root', noteId: 'root',
title: 'root', title: 'root',
type: 'text', type: 'text',
@ -87,12 +89,16 @@ function createInitialDatabase(username, password, theme) {
isExpanded: true, isExpanded: true,
notePosition: 10 notePosition: 10
}).save(); }).save();
});
const dummyTaskContext = new TaskContext("1", 'import', false); const dummyTaskContext = new TaskContext("initial-demo-import", 'import', false);
const zipImportService = require("./import/zip"); const zipImportService = require("./import/zip");
zipImportService.importZip(dummyTaskContext, demoFile, rootNote); await zipImportService.importZip(dummyTaskContext, demoFile, rootNote);
require('./sync_table').fillAllSyncRows();
sql.transactional(() => {
const startNoteId = sql.getValue("SELECT noteId FROM branches WHERE parentNoteId = 'root' AND isDeleted = 0 ORDER BY notePosition"); const startNoteId = sql.getValue("SELECT noteId FROM branches WHERE parentNoteId = 'root' AND isDeleted = 0 ORDER BY notePosition");
const optionsInitService = require('./options_init'); const optionsInitService = require('./options_init');
@ -100,8 +106,6 @@ function createInitialDatabase(username, password, theme) {
optionsInitService.initDocumentOptions(); optionsInitService.initDocumentOptions();
optionsInitService.initSyncedOptions(username, password); optionsInitService.initSyncedOptions(username, password);
optionsInitService.initNotSyncedOptions(true, startNoteId, { theme }); optionsInitService.initNotSyncedOptions(true, startNoteId, { theme });
require('./sync_table').fillAllSyncRows();
}); });
log.info("Schema and initial content generated."); log.info("Schema and initial content generated.");

View File

@ -99,6 +99,7 @@ function fillSyncRows(entityName, entityPrimaryKey, condition = '') {
} }
function fillAllSyncRows() { function fillAllSyncRows() {
sql.transactional(() => {
sql.execute("DELETE FROM sync"); sql.execute("DELETE FROM sync");
fillSyncRows("notes", "noteId"); fillSyncRows("notes", "noteId");
@ -110,6 +111,7 @@ function fillAllSyncRows() {
fillSyncRows("attributes", "attributeId"); fillSyncRows("attributes", "attributeId");
fillSyncRows("api_tokens", "apiTokenId"); fillSyncRows("api_tokens", "apiTokenId");
fillSyncRows("options", "name", 'isSynced = 1'); fillSyncRows("options", "name", 'isSynced = 1');
});
} }
module.exports = { module.exports = {

View File

@ -34,7 +34,7 @@ class TaskContext {
increaseProgressCount() { increaseProgressCount() {
this.progressCount++; this.progressCount++;
if (Date.now() - this.lastSentCountTs >= 300) { if (Date.now() - this.lastSentCountTs >= 300 && this.taskId !== 'initial-demo-import') {
this.lastSentCountTs = Date.now(); this.lastSentCountTs = Date.now();
ws.sendMessageToAllClients({ ws.sendMessageToAllClients({