Store: Move `nedb`'s `Datastore` instances to their own file

This commit is contained in:
Svallinn 2021-06-09 01:33:42 +01:00
parent 8b785bd71d
commit 5e105f5584
No known key found for this signature in database
GPG Key ID: 09FB527F34037CCA
5 changed files with 74 additions and 123 deletions

View File

@ -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
}

View File

@ -1,27 +1,4 @@
import Datastore from 'nedb' import { historyDb } from '../datastores'
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
})
const state = { const state = {
historyCache: [] historyCache: []

View File

@ -1,29 +1,4 @@
import Datastore from 'nedb' import { playlistsDb } from '../datastores'
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
})
const state = { const state = {
playlists: [ playlists: [
@ -50,7 +25,7 @@ const getters = {
const actions = { const actions = {
addPlaylist ({ commit }, payload) { addPlaylist ({ commit }, payload) {
playlistDb.insert(payload, (err, payload) => { playlistsDb.insert(payload, (err, payload) => {
if (err) { if (err) {
console.error(err) console.error(err)
} else { } else {
@ -59,7 +34,7 @@ const actions = {
}) })
}, },
addPlaylists ({ commit }, payload) { addPlaylists ({ commit }, payload) {
playlistDb.insert(payload, (err, payload) => { playlistsDb.insert(payload, (err, payload) => {
if (err) { if (err) {
console.error(err) console.error(err)
} else { } else {
@ -68,7 +43,7 @@ const actions = {
}) })
}, },
addVideo ({ commit }, payload) { 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) { if (err) {
console.error(err) console.error(err)
} else { } else {
@ -77,7 +52,7 @@ const actions = {
}) })
}, },
addVideos ({ commit }, payload) { 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) { if (err) {
console.error(err) console.error(err)
} else { } else {
@ -86,7 +61,7 @@ const actions = {
}) })
}, },
grabAllPlaylists({ commit, dispatch }) { grabAllPlaylists({ commit, dispatch }) {
playlistDb.find({}, (err, payload) => { playlistsDb.find({}, (err, payload) => {
if (err) { if (err) {
console.error(err) console.error(err)
} else { } else {
@ -100,7 +75,7 @@ const actions = {
}) })
}, },
removeAllPlaylists ({ commit }) { removeAllPlaylists ({ commit }) {
playlistDb.remove({ protected: { $ne: true } }, err => { playlistsDb.remove({ protected: { $ne: true } }, err => {
if (err) { if (err) {
console.error(err) console.error(err)
} else { } else {
@ -109,7 +84,7 @@ const actions = {
}) })
}, },
removeAllVideos ({ commit }, playlistName) { removeAllVideos ({ commit }, playlistName) {
playlistDb.update({ playlistName: playlistName }, { $set: { videos: [] } }, { upsert: true }, err => { playlistsDb.update({ playlistName: playlistName }, { $set: { videos: [] } }, { upsert: true }, err => {
if (err) { if (err) {
console.error(err) console.error(err)
} else { } else {
@ -118,7 +93,7 @@ const actions = {
}) })
}, },
removePlaylist ({ commit }, playlistId) { removePlaylist ({ commit }, playlistId) {
playlistDb.remove({ _id: playlistId, protected: { $ne: true } }, (err, playlistId) => { playlistsDb.remove({ _id: playlistId, protected: { $ne: true } }, (err, playlistId) => {
if (err) { if (err) {
console.error(err) console.error(err)
} else { } else {
@ -127,7 +102,7 @@ const actions = {
}) })
}, },
removePlaylists ({ commit }, playlistIds) { 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) { if (err) {
console.error(err) console.error(err)
} else { } else {
@ -136,7 +111,7 @@ const actions = {
}) })
}, },
removeVideo ({ commit }, payload) { 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) { if (err) {
console.error(err) console.error(err)
} else { } else {
@ -145,7 +120,7 @@ const actions = {
}) })
}, },
removeVideos ({ commit }, payload) { 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) { if (err) {
console.error(err) console.error(err)
} else { } else {

View File

@ -1,27 +1,4 @@
import Datastore from 'nedb' import { profilesDb } from '../datastores'
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
})
const state = { const state = {
profileList: [{ profileList: [{
@ -47,7 +24,7 @@ const getters = {
const actions = { const actions = {
grabAllProfiles ({ rootState, dispatch, commit }, defaultName = null) { grabAllProfiles ({ rootState, dispatch, commit }, defaultName = null) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
profileDb.find({}, (err, results) => { profilesDb.find({}, (err, results) => {
if (!err) { if (!err) {
if (results.length === 0) { if (results.length === 0) {
dispatch('createDefaultProfile', defaultName) dispatch('createDefaultProfile', defaultName)
@ -90,7 +67,7 @@ const actions = {
grabProfileInfo (_, profileId) { grabProfileInfo (_, profileId) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
console.log(profileId) console.log(profileId)
profileDb.findOne({ _id: profileId }, (err, results) => { profilesDb.findOne({ _id: profileId }, (err, results) => {
if (!err) { if (!err) {
resolve(results) resolve(results)
} }
@ -109,7 +86,7 @@ const actions = {
subscriptions: [] subscriptions: []
} }
profileDb.update({ _id: 'allChannels' }, defaultProfile, { upsert: true }, (err, numReplaced) => { profilesDb.update({ _id: 'allChannels' }, defaultProfile, { upsert: true }, (err, numReplaced) => {
if (!err) { if (!err) {
dispatch('grabAllProfiles') dispatch('grabAllProfiles')
} }
@ -117,7 +94,7 @@ const actions = {
}, },
updateProfile ({ dispatch }, profile) { 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) { if (!err) {
dispatch('grabAllProfiles') dispatch('grabAllProfiles')
} }
@ -125,7 +102,7 @@ const actions = {
}, },
insertProfile ({ dispatch }, profile) { insertProfile ({ dispatch }, profile) {
profileDb.insert(profile, (err, newDocs) => { profilesDb.insert(profile, (err, newDocs) => {
if (!err) { if (!err) {
dispatch('grabAllProfiles') dispatch('grabAllProfiles')
} }
@ -133,7 +110,7 @@ const actions = {
}, },
removeProfile ({ dispatch }, profileId) { removeProfile ({ dispatch }, profileId) {
profileDb.remove({ _id: profileId }, (err, numReplaced) => { profilesDb.remove({ _id: profileId }, (err, numReplaced) => {
if (!err) { if (!err) {
dispatch('grabAllProfiles') dispatch('grabAllProfiles')
} }
@ -141,7 +118,7 @@ const actions = {
}, },
compactProfiles (_) { compactProfiles (_) {
profileDb.persistence.compactDatafile() profilesDb.persistence.compactDatafile()
}, },
updateActiveProfile ({ commit }, index) { updateActiveProfile ({ commit }, index) {

View File

@ -1,31 +1,4 @@
import Datastore from 'nedb' import { settingsDb } from '../datastores'
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
})
/** /**
* NOTE: If someone wants to add a new setting to the app, * 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 // Custom state
Object.assign(state, { Object.assign(state, {
// Add `usingElectron` to the state // Add `usingElectron` to the state
usingElectron: usingElectron usingElectron: window?.process?.type === 'renderer'
}) })
// Custom getters // Custom getters
@ -172,7 +145,7 @@ Object.assign(getters, {
// Custom actions // Custom actions
Object.assign(actions, { Object.assign(actions, {
// Add `grabUserSettings` to actions // Add `grabUserSettings` to actions
grabUserSettings: ({ commit }) => { grabUserSettings: ({ commit, getters }) => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
settingsDb.find( settingsDb.find(
{ _id: { $ne: 'bounds' } }, { _id: { $ne: 'bounds' } },
@ -185,7 +158,7 @@ Object.assign(actions, {
const specialSettings = new Map([ const specialSettings = new Map([
['uiScale', ['uiScale',
(value) => { (value) => {
if (usingElectron) { if (getters.getUsingElectron) {
const { webFrame } = require('electron') const { webFrame } = require('electron')
webFrame.setZoomFactor(parseInt(value) / 100) webFrame.setZoomFactor(parseInt(value) / 100)
} }