From 5e105f5584bdd7bd88c68e15c467b944c5118ab6 Mon Sep 17 00:00:00 2001 From: Svallinn <41585298+Svallinn@users.noreply.github.com> Date: Wed, 9 Jun 2021 01:33:42 +0100 Subject: [PATCH] Store: Move `nedb`'s `Datastore` instances to their own file --- src/renderer/store/datastores.js | 49 +++++++++++++++++++ src/renderer/store/modules/history.js | 25 +--------- .../modules/{playlist.js => playlists.js} | 49 +++++-------------- .../store/modules/{profile.js => profiles.js} | 39 +++------------ src/renderer/store/modules/settings.js | 35 ++----------- 5 files changed, 74 insertions(+), 123 deletions(-) create mode 100644 src/renderer/store/datastores.js rename src/renderer/store/modules/{playlist.js => playlists.js} (71%) rename src/renderer/store/modules/{profile.js => profiles.js} (72%) diff --git a/src/renderer/store/datastores.js b/src/renderer/store/datastores.js new file mode 100644 index 00000000..8ec225eb --- /dev/null +++ b/src/renderer/store/datastores.js @@ -0,0 +1,49 @@ +import Datastore from 'nedb' + +// Initialize all datastores and export their references +// Current dbs: +// `settings.db` +// `profiles.db` +// `playlists.db` +// `history.db` + +// Check if using Electron +let userDataPath +const usingElectron = window?.process?.type === 'renderer' +if (usingElectron) { + const { ipcRenderer } = require('electron') + userDataPath = ipcRenderer.sendSync('getUserDataPathSync') +} + +const buildFileName = (dbName) => { + return usingElectron + ? userDataPath + '/' + dbName + '.db' + : dbName + '.db' +} + +const settingsDb = new Datastore({ + filename: buildFileName('settings'), + autoload: true +}) + +const playlistsDb = new Datastore({ + filename: buildFileName('playlists'), + autoload: true +}) + +const profilesDb = new Datastore({ + filename: buildFileName('profiles'), + autoload: true +}) + +const historyDb = new Datastore({ + filename: buildFileName('history'), + autoload: true +}) + +export { + settingsDb, + profilesDb, + playlistsDb, + historyDb +} diff --git a/src/renderer/store/modules/history.js b/src/renderer/store/modules/history.js index c54accaf..47bddcf9 100644 --- a/src/renderer/store/modules/history.js +++ b/src/renderer/store/modules/history.js @@ -1,27 +1,4 @@ -import Datastore from 'nedb' - -let dbLocation - -if (window && window.process && window.process.type === 'renderer') { - // Electron is being used - /* let dbLocation = localStorage.getItem('dbLocation') - - if (dbLocation === null) { - const electron = require('electron') - dbLocation = electron.remote.app.getPath('userData') - } */ - - const { ipcRenderer } = require('electron') - dbLocation = ipcRenderer.sendSync('getUserDataPathSync') - dbLocation = dbLocation + '/history.db' -} else { - dbLocation = 'history.db' -} - -const historyDb = new Datastore({ - filename: dbLocation, - autoload: true -}) +import { historyDb } from '../datastores' const state = { historyCache: [] diff --git a/src/renderer/store/modules/playlist.js b/src/renderer/store/modules/playlists.js similarity index 71% rename from src/renderer/store/modules/playlist.js rename to src/renderer/store/modules/playlists.js index d66bb2ac..50f457a5 100644 --- a/src/renderer/store/modules/playlist.js +++ b/src/renderer/store/modules/playlists.js @@ -1,29 +1,4 @@ -import Datastore from 'nedb' - -let dbLocation - -if (window && window.process && window.process.type === 'renderer') { - // Electron is being used - // let dbLocation = localStorage.getItem('dbLocation') - // - // if (dbLocation === null) { - // const electron = require('electron') - // dbLocation = electron.remote.app.getPath('userData') - // } - // - // dbLocation += '/playlists.db' - - const { ipcRenderer } = require('electron') - dbLocation = ipcRenderer.sendSync('getUserDataPathSync') - dbLocation = dbLocation + '/playlists.db' -} else { - dbLocation = 'playlists.db' -} - -const playlistDb = new Datastore({ - filename: dbLocation, - autoload: true -}) +import { playlistsDb } from '../datastores' const state = { playlists: [ @@ -50,7 +25,7 @@ const getters = { const actions = { addPlaylist ({ commit }, payload) { - playlistDb.insert(payload, (err, payload) => { + playlistsDb.insert(payload, (err, payload) => { if (err) { console.error(err) } else { @@ -59,7 +34,7 @@ const actions = { }) }, addPlaylists ({ commit }, payload) { - playlistDb.insert(payload, (err, payload) => { + playlistsDb.insert(payload, (err, payload) => { if (err) { console.error(err) } else { @@ -68,7 +43,7 @@ const actions = { }) }, addVideo ({ commit }, payload) { - playlistDb.update({ playlistName: payload.playlistName }, { $push: { videos: payload.videoData } }, { upsert: true }, err => { + playlistsDb.update({ playlistName: payload.playlistName }, { $push: { videos: payload.videoData } }, { upsert: true }, err => { if (err) { console.error(err) } else { @@ -77,7 +52,7 @@ const actions = { }) }, addVideos ({ commit }, payload) { - playlistDb.update({ _id: payload.playlistId }, { $push: { videos: { $each: payload.videosIds } } }, { upsert: true }, err => { + playlistsDb.update({ _id: payload.playlistId }, { $push: { videos: { $each: payload.videosIds } } }, { upsert: true }, err => { if (err) { console.error(err) } else { @@ -86,7 +61,7 @@ const actions = { }) }, grabAllPlaylists({ commit, dispatch }) { - playlistDb.find({}, (err, payload) => { + playlistsDb.find({}, (err, payload) => { if (err) { console.error(err) } else { @@ -100,7 +75,7 @@ const actions = { }) }, removeAllPlaylists ({ commit }) { - playlistDb.remove({ protected: { $ne: true } }, err => { + playlistsDb.remove({ protected: { $ne: true } }, err => { if (err) { console.error(err) } else { @@ -109,7 +84,7 @@ const actions = { }) }, removeAllVideos ({ commit }, playlistName) { - playlistDb.update({ playlistName: playlistName }, { $set: { videos: [] } }, { upsert: true }, err => { + playlistsDb.update({ playlistName: playlistName }, { $set: { videos: [] } }, { upsert: true }, err => { if (err) { console.error(err) } else { @@ -118,7 +93,7 @@ const actions = { }) }, removePlaylist ({ commit }, playlistId) { - playlistDb.remove({ _id: playlistId, protected: { $ne: true } }, (err, playlistId) => { + playlistsDb.remove({ _id: playlistId, protected: { $ne: true } }, (err, playlistId) => { if (err) { console.error(err) } else { @@ -127,7 +102,7 @@ const actions = { }) }, removePlaylists ({ commit }, playlistIds) { - playlistDb.remove({ _id: { $in: playlistIds }, protected: { $ne: true } }, (err, playlistIds) => { + playlistsDb.remove({ _id: { $in: playlistIds }, protected: { $ne: true } }, (err, playlistIds) => { if (err) { console.error(err) } else { @@ -136,7 +111,7 @@ const actions = { }) }, removeVideo ({ commit }, payload) { - playlistDb.update({ playlistName: payload.playlistName }, { $pull: { videos: { videoId: payload.videoId } } }, { upsert: true }, (err, numRemoved) => { + playlistsDb.update({ playlistName: payload.playlistName }, { $pull: { videos: { videoId: payload.videoId } } }, { upsert: true }, (err, numRemoved) => { if (err) { console.error(err) } else { @@ -145,7 +120,7 @@ const actions = { }) }, removeVideos ({ commit }, payload) { - playlistDb.update({ _id: payload.playlistName }, { $pull: { videos: { $in: payload.videoId } } }, { upsert: true }, err => { + playlistsDb.update({ _id: payload.playlistName }, { $pull: { videos: { $in: payload.videoId } } }, { upsert: true }, err => { if (err) { console.error(err) } else { diff --git a/src/renderer/store/modules/profile.js b/src/renderer/store/modules/profiles.js similarity index 72% rename from src/renderer/store/modules/profile.js rename to src/renderer/store/modules/profiles.js index ae9a6a76..ed112a03 100644 --- a/src/renderer/store/modules/profile.js +++ b/src/renderer/store/modules/profiles.js @@ -1,27 +1,4 @@ -import Datastore from 'nedb' - -let dbLocation - -if (window && window.process && window.process.type === 'renderer') { - // Electron is being used - /* let dbLocation = localStorage.getItem('dbLocation') - - if (dbLocation === null) { - const electron = require('electron') - dbLocation = electron.remote.app.getPath('userData') - } */ - - const { ipcRenderer } = require('electron') - dbLocation = ipcRenderer.sendSync('getUserDataPathSync') - dbLocation = dbLocation + '/profiles.db' -} else { - dbLocation = 'profiles.db' -} - -const profileDb = new Datastore({ - filename: dbLocation, - autoload: true -}) +import { profilesDb } from '../datastores' const state = { profileList: [{ @@ -47,7 +24,7 @@ const getters = { const actions = { grabAllProfiles ({ rootState, dispatch, commit }, defaultName = null) { return new Promise((resolve, reject) => { - profileDb.find({}, (err, results) => { + profilesDb.find({}, (err, results) => { if (!err) { if (results.length === 0) { dispatch('createDefaultProfile', defaultName) @@ -90,7 +67,7 @@ const actions = { grabProfileInfo (_, profileId) { return new Promise((resolve, reject) => { console.log(profileId) - profileDb.findOne({ _id: profileId }, (err, results) => { + profilesDb.findOne({ _id: profileId }, (err, results) => { if (!err) { resolve(results) } @@ -109,7 +86,7 @@ const actions = { subscriptions: [] } - profileDb.update({ _id: 'allChannels' }, defaultProfile, { upsert: true }, (err, numReplaced) => { + profilesDb.update({ _id: 'allChannels' }, defaultProfile, { upsert: true }, (err, numReplaced) => { if (!err) { dispatch('grabAllProfiles') } @@ -117,7 +94,7 @@ const actions = { }, updateProfile ({ dispatch }, profile) { - profileDb.update({ _id: profile._id }, profile, { upsert: true }, (err, numReplaced) => { + profilesDb.update({ _id: profile._id }, profile, { upsert: true }, (err, numReplaced) => { if (!err) { dispatch('grabAllProfiles') } @@ -125,7 +102,7 @@ const actions = { }, insertProfile ({ dispatch }, profile) { - profileDb.insert(profile, (err, newDocs) => { + profilesDb.insert(profile, (err, newDocs) => { if (!err) { dispatch('grabAllProfiles') } @@ -133,7 +110,7 @@ const actions = { }, removeProfile ({ dispatch }, profileId) { - profileDb.remove({ _id: profileId }, (err, numReplaced) => { + profilesDb.remove({ _id: profileId }, (err, numReplaced) => { if (!err) { dispatch('grabAllProfiles') } @@ -141,7 +118,7 @@ const actions = { }, compactProfiles (_) { - profileDb.persistence.compactDatafile() + profilesDb.persistence.compactDatafile() }, updateActiveProfile ({ commit }, index) { diff --git a/src/renderer/store/modules/settings.js b/src/renderer/store/modules/settings.js index 1d2e1b31..c69e5657 100644 --- a/src/renderer/store/modules/settings.js +++ b/src/renderer/store/modules/settings.js @@ -1,31 +1,4 @@ -import Datastore from 'nedb' - -let dbLocation - -const usingElectron = window?.process?.type === 'renderer' - -if (usingElectron) { - // Electron is being used - /* let dbLocation = localStorage.getItem('dbLocation') - - if (dbLocation === null) { - const electron = require('electron') - dbLocation = electron.remote.app.getPath('userData') - } */ - - const { ipcRenderer } = require('electron') - dbLocation = ipcRenderer.sendSync('getUserDataPathSync') - dbLocation = dbLocation + '/settings.db' -} else { - dbLocation = 'settings.db' -} - -console.log(dbLocation) - -const settingsDb = new Datastore({ - filename: dbLocation, - autoload: true -}) +import { settingsDb } from '../datastores' /** * NOTE: If someone wants to add a new setting to the app, @@ -157,7 +130,7 @@ for (const settingId of Object.keys(state)) { // Custom state Object.assign(state, { // Add `usingElectron` to the state - usingElectron: usingElectron + usingElectron: window?.process?.type === 'renderer' }) // Custom getters @@ -172,7 +145,7 @@ Object.assign(getters, { // Custom actions Object.assign(actions, { // Add `grabUserSettings` to actions - grabUserSettings: ({ commit }) => { + grabUserSettings: ({ commit, getters }) => { return new Promise((resolve, reject) => { settingsDb.find( { _id: { $ne: 'bounds' } }, @@ -185,7 +158,7 @@ Object.assign(actions, { const specialSettings = new Map([ ['uiScale', (value) => { - if (usingElectron) { + if (getters.getUsingElectron) { const { webFrame } = require('electron') webFrame.setZoomFactor(parseInt(value) / 100) }