Merge pull request #159 from JsSucks/database

Database
This commit is contained in:
Alexei Stukov 2018-03-07 10:39:33 +02:00 committed by GitHub
commit b5a5baa1bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 141 additions and 18 deletions

View File

@ -10,12 +10,13 @@
import { DOM, BdUI, Modals } from 'ui';
import BdCss from './styles/index.scss';
import { Events, CssEditor, Globals, ExtModuleManager, PluginManager, ThemeManager, ModuleManager, WebpackModules, Settings } from 'modules';
import { Events, CssEditor, Globals, ExtModuleManager, PluginManager, ThemeManager, ModuleManager, WebpackModules, Settings, Database } from 'modules';
import { ClientLogger as Logger, ClientIPC } from 'common';
class BetterDiscord {
constructor() {
window.bddb = Database;
window.bdglobals = Globals;
window.ClientIPC = ClientIPC;
window.css = CssEditor;
@ -32,14 +33,19 @@ class BetterDiscord {
}
async init() {
await Settings.loadSettings();
await ModuleManager.initModules();
await ExtModuleManager.loadAllModules(true);
await PluginManager.loadAllPlugins(true);
await ThemeManager.loadAllThemes(true);
Modals.showContentManagerErrors();
Events.emit('ready');
Events.emit('discord-ready');
try {
await Database.init();
await Settings.loadSettings();
await ModuleManager.initModules();
await ExtModuleManager.loadAllModules(true);
await PluginManager.loadAllPlugins(true);
await ThemeManager.loadAllThemes(true);
Modals.showContentManagerErrors();
Events.emit('ready');
Events.emit('discord-ready');
} catch (err) {
console.log('FAILED TO LOAD!', err);
}
}
globalReady() {

View File

@ -10,6 +10,7 @@
import { Utils, FileUtils, ClientLogger as Logger, AsyncEventEmitter } from 'common';
import { Modals } from 'ui';
import Database from './database';
export default class Content {
@ -72,12 +73,21 @@ export default class Content {
*/
async saveConfiguration() {
try {
/*
await FileUtils.writeFile(`${this.contentPath}/user.config.json`, JSON.stringify({
enabled: this.enabled,
config: this.settings.strip().settings,
data: this.data
}));
*/
Database.insertOrUpdate({ type: 'contentconfig', $or: [{ id: this.id }, { name: this.name }] }, {
type: 'contentconfig',
id: this.id,
name: this.name,
enabled: this.enabled,
config: this.settings.strip().settings,
data: this.data
});
this.settings.setSaved();
} catch (err) {
Logger.err(this.name, ['Failed to save configuration', err]);

View File

@ -15,6 +15,7 @@ import path from 'path';
import { Events } from 'modules';
import { SettingsSet, ErrorEvent } from 'structs';
import { Modals } from 'ui';
import Database from './database';
/**
* Base class for external content managing
@ -189,13 +190,16 @@ export default class {
};
try {
const readUserConfig = await this.readUserConfig(contentPath);
userConfig.enabled = readUserConfig.enabled || false;
// await userConfig.config.merge({ settings: readUserConfig.config });
// userConfig.config.setSaved();
// userConfig.config = userConfig.config.clone({ settings: readUserConfig.config });
userConfig.config = readUserConfig.config;
userConfig.data = readUserConfig.data || {};
//const readUserConfig = await this.readUserConfig(contentPath);
const readUserConfig = await Database.find({ type: 'contentconfig', name: readConfig.info.name });
if (readUserConfig.length) {
userConfig.enabled = readUserConfig[0].enabled || false;
// await userConfig.config.merge({ settings: readUserConfig.config });
// userConfig.config.setSaved();
// userConfig.config = userConfig.config.clone({ settings: readUserConfig.config });
userConfig.config = readUserConfig[0].config;
userConfig.data = readUserConfig[0].data || {};
}
} catch (err) { /*We don't care if this fails it either means that user config doesn't exist or there's something wrong with it so we revert to default config*/
Logger.info(this.moduleName, `Failed reading config for ${this.contentType} ${readConfig.info.name} in ${dirName}`);
Logger.err(this.moduleName, err);

View File

@ -0,0 +1,34 @@
/**
* BetterDiscord Database Module
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { ClientIPC } from 'bdipc';
export default class {
static async init() {
return true;
}
static async insertOrUpdate(args, data) {
try {
return ClientIPC.send('bd-dba', { action: 'update', args, data });
} catch (err) {
throw err;
}
}
static async find(args) {
try {
return ClientIPC.send('bd-dba', { action: 'find', args });
} catch (err) {
throw err;
}
}
}

View File

@ -12,3 +12,4 @@ export { default as EventListener } from './eventlistener';
export { default as SocketProxy } from './socketproxy';
export { default as EventHook } from './eventhook';
export { default as Permissions } from './permissionmanager';
export { default as Database } from './database';

View File

@ -26,7 +26,7 @@ const __pluginPath = path.resolve(__dirname, '..', '..', 'tests', 'plugins');
const __themePath = path.resolve(__dirname, '..', '..', 'tests', 'themes');
const __modulePath = path.resolve(__dirname, '..', '..', 'tests', 'modules');
const { Utils, FileUtils, BDIpc, Config, WindowUtils, CSSEditor } = require('./modules');
const { Utils, FileUtils, BDIpc, Config, WindowUtils, CSSEditor, Database } = require('./modules');
const { BrowserWindow, dialog } = require('electron');
const Common = {};
@ -42,6 +42,8 @@ const dummyArgs = {
]
};
const dbInstance = new Database(dummyArgs.paths.find(path => path.id === 'data').path + '/storage');
console.log(dummyArgs);
@ -87,6 +89,17 @@ class Comms {
o.reply(result);
});
});
BDIpc.on('bd-dba', o => {
(async () => {
try {
const ret = await dbInstance.exec(o.args);
o.reply(ret);
} catch (err) {
o.reply({err});
}
})();
});
}
async readFile(o, json) {

View File

@ -0,0 +1,53 @@
/**
* BetterDiscord Database Module
* Copyright (c) 2015-present JsSucks - https://github.com/JsSucks
* All rights reserved.
* https://github.com/JsSucks - https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
const Datastore = require('nedb');
class Database {
constructor(dbPath) {
this.exec = this.exec.bind(this);
this.update = this.update.bind(this);
this.db = new Datastore({ filename: dbPath, autoload: true });
}
async update(cmd) {
return new Promise((resolve, reject) => {
this.db.update(cmd.args, cmd.data, { upsert: true }, (err, docs) => {
if (err) return reject(err);
this.db.persistence.compactDatafile();
resolve(docs);
});
});
}
async find(cmd) {
console.log('FIND', cmd);
return new Promise((resolve, reject) => {
this.db.find(cmd.args, (err, docs) => {
if (err) return reject(err);
resolve(docs);
});
});
}
async exec(cmd) {
switch (cmd.action) {
case 'update':
return this.update(cmd);
case 'find':
return this.find(cmd);
}
throw 'Invalid Command';
}
}
module.exports = { Database };

View File

@ -2,3 +2,4 @@ export { BDIpc } from './bdipc';
export { Utils, FileUtils, WindowUtils } from './utils';
export { Config } from './config';
export { CSSEditor } from './csseditor';
export { Database } from './database';

View File

@ -35,6 +35,7 @@
"gulp-watch": "^5.0.0",
"jquery": "^3.2.1",
"lodash": "^4.17.4",
"nedb": "^1.8.0",
"node-gyp": "^3.6.2",
"node-sass": "^4.7.2",
"pump": "^2.0.0",