diff --git a/client/src/index.js b/client/src/index.js index e774f928..eb4f5f34 100644 --- a/client/src/index.js +++ b/client/src/index.js @@ -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() { diff --git a/client/src/modules/database.js b/client/src/modules/database.js new file mode 100644 index 00000000..87786c9e --- /dev/null +++ b/client/src/modules/database.js @@ -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; + } + } +} diff --git a/client/src/modules/modules.js b/client/src/modules/modules.js index db1b5b7c..b93216a3 100644 --- a/client/src/modules/modules.js +++ b/client/src/modules/modules.js @@ -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'; diff --git a/core/src/main.js b/core/src/main.js index cca1689c..9d69928f 100644 --- a/core/src/main.js +++ b/core/src/main.js @@ -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) { diff --git a/core/src/modules/database.js b/core/src/modules/database.js new file mode 100644 index 00000000..6e114698 --- /dev/null +++ b/core/src/modules/database.js @@ -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 }; diff --git a/core/src/modules/index.js b/core/src/modules/index.js index a2e0d7bc..5f2029d9 100644 --- a/core/src/modules/index.js +++ b/core/src/modules/index.js @@ -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'; diff --git a/package.json b/package.json index 55e0f401..a14d9fb0 100644 --- a/package.json +++ b/package.json @@ -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",