Better handling of default folder

This commit is contained in:
Laurent Cozic committed 2017-06-27 20:16:03 +00:00
1 parent 0e93a70556
commit 32b7207ea1
8 files changed
+50 -15

No files matched your search

+17 -2
View File
@@ -425,6 +425,22 @@ class Database {
});
}
static defaultFolderData() {
let now = time.unixMs();
return {
id: uuid.create(),
title: _('Notebook'),
created_time: now,
updated_time: now,
};
}
static defaultFolderQuery() {
return Database.insertQuery('folders', this.defaultFolderData());
}
initialize() {
this.logger().info('Checking for database schema update...');
@@ -461,10 +477,9 @@ class Database {
this.logger().info('Database is new - creating the schema...');
let now = time.unixMs();
let queries = this.wrapQueries(this.sqlStringToLines(structureSql));
queries.push(this.wrapQuery('INSERT INTO settings (`key`, `value`, `type`) VALUES ("clientId", "' + uuid.create() + '", "' + Database.enumId('settings', 'string') + '")'));
queries.push(this.wrapQuery('INSERT INTO folders (`id`, `title`, `created_time`, `updated_time`) VALUES ("' + uuid.create() + '", "' + _('Notebook') + '", ' + now + ', ' + now + ')'));
queries.push(Database.defaultFolderQuery());
return this.transactionExecBatch(queries).then(() => {
this.logger().info('Database schema created successfully');
+6 -1
View File
@@ -89,10 +89,15 @@ class Folder extends BaseItem {
return folders.concat(notes);
}
static async defaultFolder() {
static defaultFolder() {
return this.modelSelectOne('SELECT * FROM folders ORDER BY created_time DESC LIMIT 1');
}
static createDefaultFolder() {
let folder = Database.defaultFolderData();
return Folder.save(folder, { isNew: true });
}
static save(o, options = null) {
return Folder.loadByField('title', o.title).then((existingFolder) => {
if (existingFolder && existingFolder.id != o.id) throw new Error(_('A notebook with title "%s" already exists', o.title));
+15 -7
View File
@@ -28,6 +28,15 @@ class Setting extends BaseModel {
return this.keys_;
}
static publicKeys() {
let output = [];
for (let n in this.defaults_) {
if (!this.defaults_.hasOwnProperty(n)) continue;
if (this.defaults_[n].public) output.push(n);
}
return output;
}
static load() {
this.cancelScheduleUpdate();
this.cache_ = [];
@@ -110,8 +119,8 @@ class Setting extends BaseModel {
return BaseModel.db().transactionExecBatch(queries).then(() => {
this.logger().info('Settings have been saved.');
}).catch((error) => {
this.logger().warn('Could not save settings', error);
reject(error);
this.logger().error('Could not save settings');
this.logger().error(error);
});
}
@@ -131,11 +140,10 @@ class Setting extends BaseModel {
}
Setting.defaults_ = {
'clientId': { value: '', type: 'string' },
'sessionId': { value: '', type: 'string' },
'activeFolderId': { value: '', type: 'string' },
'sync.onedrive.auth': { value: '', type: 'string' },
'sync.target': { value: 'onedrive', type: 'string' },
'clientId': { value: '', type: 'string', public: false },
'activeFolderId': { value: '', type: 'string', public: false },
'sync.onedrive.auth': { value: '', type: 'string', public: false },
'sync.target': { value: 'onedrive', type: 'string', public: true },
};
// Contains constants that are set by the application and