1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-12-02 22:49:09 +02:00

Applied prettier to code base

This commit is contained in:
Laurent Cozic
2018-03-09 17:49:35 +00:00
parent e868102c98
commit c4f19465a6
203 changed files with 13395 additions and 7927 deletions

View File

@@ -1,7 +1,7 @@
const { uuid } = require('lib/uuid.js');
const { promiseChain } = require('lib/promise-utils.js');
const { time } = require('lib/time-utils.js');
const { Database } = require('lib/database.js');
const { uuid } = require("lib/uuid.js");
const { promiseChain } = require("lib/promise-utils.js");
const { time } = require("lib/time-utils.js");
const { Database } = require("lib/database.js");
const structureSql = `
CREATE TABLE folders (
@@ -118,7 +118,6 @@ INSERT INTO version (version) VALUES (1);
`;
class JoplinDatabase extends Database {
constructor(driver) {
super(driver);
this.initialized_ = false;
@@ -144,48 +143,50 @@ class JoplinDatabase extends Database {
}
tableFields(tableName) {
if (!this.tableFields_) throw new Error('Fields have not been loaded yet');
if (!this.tableFields_[tableName]) throw new Error('Unknown table: ' + tableName);
if (!this.tableFields_) throw new Error("Fields have not been loaded yet");
if (!this.tableFields_[tableName]) throw new Error("Unknown table: " + tableName);
return this.tableFields_[tableName];
}
refreshTableFields() {
this.logger().info('Initializing tables...');
this.logger().info("Initializing tables...");
let queries = [];
queries.push(this.wrapQuery('DELETE FROM table_fields'));
queries.push(this.wrapQuery("DELETE FROM table_fields"));
return this.selectAll('SELECT name FROM sqlite_master WHERE type="table"').then((tableRows) => {
let chain = [];
for (let i = 0; i < tableRows.length; i++) {
let tableName = tableRows[i].name;
if (tableName == 'android_metadata') continue;
if (tableName == 'table_fields') continue;
if (tableName == 'sqlite_sequence') continue;
chain.push(() => {
return this.selectAll('PRAGMA table_info("' + tableName + '")').then((pragmas) => {
for (let i = 0; i < pragmas.length; i++) {
let item = pragmas[i];
// In SQLite, if the default value is a string it has double quotes around it, so remove them here
let defaultValue = item.dflt_value;
if (typeof defaultValue == 'string' && defaultValue.length >= 2 && defaultValue[0] == '"' && defaultValue[defaultValue.length - 1] == '"') {
defaultValue = defaultValue.substr(1, defaultValue.length - 2);
return this.selectAll('SELECT name FROM sqlite_master WHERE type="table"')
.then(tableRows => {
let chain = [];
for (let i = 0; i < tableRows.length; i++) {
let tableName = tableRows[i].name;
if (tableName == "android_metadata") continue;
if (tableName == "table_fields") continue;
if (tableName == "sqlite_sequence") continue;
chain.push(() => {
return this.selectAll('PRAGMA table_info("' + tableName + '")').then(pragmas => {
for (let i = 0; i < pragmas.length; i++) {
let item = pragmas[i];
// In SQLite, if the default value is a string it has double quotes around it, so remove them here
let defaultValue = item.dflt_value;
if (typeof defaultValue == "string" && defaultValue.length >= 2 && defaultValue[0] == '"' && defaultValue[defaultValue.length - 1] == '"') {
defaultValue = defaultValue.substr(1, defaultValue.length - 2);
}
let q = Database.insertQuery("table_fields", {
table_name: tableName,
field_name: item.name,
field_type: Database.enumId("fieldType", item.type),
field_default: defaultValue,
});
queries.push(q);
}
let q = Database.insertQuery('table_fields', {
table_name: tableName,
field_name: item.name,
field_type: Database.enumId('fieldType', item.type),
field_default: defaultValue,
});
queries.push(q);
}
});
});
});
}
}
return promiseChain(chain);
}).then(() => {
return this.transactionExecBatch(queries);
});
return promiseChain(chain);
})
.then(() => {
return this.transactionExecBatch(queries);
});
}
async upgradeDatabase(fromVersion) {
@@ -196,7 +197,7 @@ class JoplinDatabase extends Database {
// IMPORTANT:
//
// Whenever adding a new database property, some additional logic might be needed
// Whenever adding a new database property, some additional logic might be needed
// in the synchronizer to handle this property. For example, when adding a property
// that should have a default value, existing remote items will not have this
// default value and thus might cause problems. In that case, the default value
@@ -206,7 +207,10 @@ class JoplinDatabase extends Database {
let currentVersionIndex = existingDatabaseVersions.indexOf(fromVersion);
if (currentVersionIndex < 0) throw new Error('Unknown profile version. Most likely this is an old version of Joplin, while the profile was created by a newer version. Please upgrade Joplin at http://joplin.cozic.net and try again.');
if (currentVersionIndex < 0)
throw new Error(
"Unknown profile version. Most likely this is an old version of Joplin, while the profile was created by a newer version. Please upgrade Joplin at http://joplin.cozic.net and try again."
);
// currentVersionIndex < 0 if for the case where an old version of Joplin used with a newer
// version of the database, so that migration is not run in this case.
@@ -221,7 +225,7 @@ class JoplinDatabase extends Database {
if (targetVersion == 1) {
queries = this.wrapQueries(this.sqlStringToLines(structureSql));
}
if (targetVersion == 2) {
const newTableSql = `
CREATE TABLE deleted_items (
@@ -233,13 +237,13 @@ class JoplinDatabase extends Database {
);
`;
queries.push({ sql: 'DROP TABLE deleted_items' });
queries.push({ sql: "DROP TABLE deleted_items" });
queries.push({ sql: this.sqlStringToLines(newTableSql)[0] });
queries.push({ sql: "CREATE INDEX deleted_items_sync_target ON deleted_items (sync_target)" });
}
if (targetVersion == 3) {
queries = this.alterColumnQueries('settings', { key: 'TEXT PRIMARY KEY', value: 'TEXT' });
queries = this.alterColumnQueries("settings", { key: "TEXT PRIMARY KEY", value: "TEXT" });
}
if (targetVersion == 4) {
@@ -248,20 +252,20 @@ class JoplinDatabase extends Database {
}
if (targetVersion == 5) {
const tableNames = ['notes', 'folders', 'tags', 'note_tags', 'resources'];
const tableNames = ["notes", "folders", "tags", "note_tags", "resources"];
for (let i = 0; i < tableNames.length; i++) {
const n = tableNames[i];
queries.push('ALTER TABLE ' + n + ' ADD COLUMN user_created_time INT NOT NULL DEFAULT 0');
queries.push('ALTER TABLE ' + n + ' ADD COLUMN user_updated_time INT NOT NULL DEFAULT 0');
queries.push('UPDATE ' + n + ' SET user_created_time = created_time');
queries.push('UPDATE ' + n + ' SET user_updated_time = updated_time');
queries.push('CREATE INDEX ' + n + '_user_updated_time ON ' + n + ' (user_updated_time)');
queries.push("ALTER TABLE " + n + " ADD COLUMN user_created_time INT NOT NULL DEFAULT 0");
queries.push("ALTER TABLE " + n + " ADD COLUMN user_updated_time INT NOT NULL DEFAULT 0");
queries.push("UPDATE " + n + " SET user_created_time = created_time");
queries.push("UPDATE " + n + " SET user_updated_time = updated_time");
queries.push("CREATE INDEX " + n + "_user_updated_time ON " + n + " (user_updated_time)");
}
}
if (targetVersion == 6) {
queries.push('CREATE TABLE alarms (id INTEGER PRIMARY KEY AUTOINCREMENT, note_id TEXT NOT NULL, trigger_time INT NOT NULL)');
queries.push('CREATE INDEX alarm_note_id ON alarms (note_id)');
queries.push("CREATE TABLE alarms (id INTEGER PRIMARY KEY AUTOINCREMENT, note_id TEXT NOT NULL, trigger_time INT NOT NULL)");
queries.push("CREATE INDEX alarm_note_id ON alarms (note_id)");
}
if (targetVersion == 7) {
@@ -286,19 +290,19 @@ class JoplinDatabase extends Database {
);
`;
queries.push(this.sqlStringToLines(newTableSql)[0]);
const tableNames = ['notes', 'folders', 'tags', 'note_tags', 'resources'];
const tableNames = ["notes", "folders", "tags", "note_tags", "resources"];
for (let i = 0; i < tableNames.length; i++) {
const n = tableNames[i];
queries.push('ALTER TABLE ' + n + ' ADD COLUMN encryption_cipher_text TEXT NOT NULL DEFAULT ""');
queries.push('ALTER TABLE ' + n + ' ADD COLUMN encryption_applied INT NOT NULL DEFAULT 0');
queries.push('CREATE INDEX ' + n + '_encryption_applied ON ' + n + ' (encryption_applied)');
queries.push("ALTER TABLE " + n + ' ADD COLUMN encryption_cipher_text TEXT NOT NULL DEFAULT ""');
queries.push("ALTER TABLE " + n + " ADD COLUMN encryption_applied INT NOT NULL DEFAULT 0");
queries.push("CREATE INDEX " + n + "_encryption_applied ON " + n + " (encryption_applied)");
}
queries.push('ALTER TABLE sync_items ADD COLUMN force_sync INT NOT NULL DEFAULT 0');
queries.push('ALTER TABLE resources ADD COLUMN encryption_blob_encrypted INT NOT NULL DEFAULT 0');
queries.push("ALTER TABLE sync_items ADD COLUMN force_sync INT NOT NULL DEFAULT 0");
queries.push("ALTER TABLE resources ADD COLUMN encryption_blob_encrypted INT NOT NULL DEFAULT 0");
}
queries.push({ sql: 'UPDATE version SET version = ?', params: [targetVersion] });
queries.push({ sql: "UPDATE version SET version = ?", params: [targetVersion] });
await this.transactionExecBatch(queries);
currentVersionIndex++;
@@ -308,14 +312,14 @@ class JoplinDatabase extends Database {
}
async initialize() {
this.logger().info('Checking for database schema update...');
this.logger().info("Checking for database schema update...");
let versionRow = null;
try {
// Will throw if the database has not been created yet, but this is handled below
versionRow = await this.selectOne('SELECT * FROM version LIMIT 1');
versionRow = await this.selectOne("SELECT * FROM version LIMIT 1");
} catch (error) {
if (error.message && (error.message.indexOf('no such table: version') >= 0)) {
if (error.message && error.message.indexOf("no such table: version") >= 0) {
// Ignore
} else {
console.info(error);
@@ -323,14 +327,14 @@ class JoplinDatabase extends Database {
}
const version = !versionRow ? 0 : versionRow.version;
this.logger().info('Current database version', version);
this.logger().info("Current database version", version);
const upgraded = await this.upgradeDatabase(version);
if (upgraded) await this.refreshTableFields();
this.tableFields_ = {};
let rows = await this.selectAll('SELECT * FROM table_fields');
let rows = await this.selectAll("SELECT * FROM table_fields");
for (let i = 0; i < rows.length; i++) {
let row = rows[i];
@@ -342,11 +346,10 @@ class JoplinDatabase extends Database {
});
}
}
}
Database.TYPE_INT = 1;
Database.TYPE_TEXT = 2;
Database.TYPE_NUMERIC = 3;
module.exports = { JoplinDatabase };
module.exports = { JoplinDatabase };