Store: Move `nedb`'s `Datastore` instances to their own file
This commit is contained in:
parent
8b785bd71d
commit
5e105f5584
|
@ -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
|
||||
}
|
|
@ -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: []
|
||||
|
|
|
@ -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 {
|
|
@ -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) {
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue