Database wrappers

This commit is contained in:
Jiiks 2018-03-07 10:12:44 +02:00
parent bb2c3e54a5
commit 30a886d84a
7 changed files with 116 additions and 10 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

@ -0,0 +1,30 @@
/**
* 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 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

@ -34,8 +34,10 @@
"gulp-plumber": "^1.2.0",
"gulp-watch": "^5.0.0",
"jquery": "^3.2.1",
"level": "^3.0.0",
"lodash": "^4.17.4",
"nedb": "^1.8.0",
"nedb-core": "^3.0.6",
"node-gyp": "^3.6.2",
"node-sass": "^4.7.2",
"pump": "^2.0.0",