Skip to content

Commit

Permalink
Main+Store: NeDB persistence and integrity + dependency update (#3845)
Browse files Browse the repository at this point in the history
* Chore/Settings+Store: Remove unnecessary actions

* Store: Replace nedb-promises with @seald-io/nedb

* Main+Store: Ensure datastores' persistence before quitting

* Fix/Store: Fix db identifier oopsie

Co-authored-by: PikachuEXE <pikachuexe@gmail.com>

---------

Co-authored-by: PikachuEXE <pikachuexe@gmail.com>
  • Loading branch information
Svallinn and PikachuEXE authored Aug 10, 2023
1 parent d3a8988 commit 079417d
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 85 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@
"@fortawesome/free-brands-svg-icons": "^6.4.2",
"@fortawesome/free-solid-svg-icons": "^6.4.2",
"@fortawesome/vue-fontawesome": "^2.0.10",
"@seald-io/nedb": "^4.0.2",
"@silvermine/videojs-quality-selector": "^1.3.0",
"autolinker": "^4.0.0",
"electron-context-menu": "^3.6.1",
"lodash.debounce": "^4.0.8",
"marked": "^7.0.0",
"nedb-promises": "^6.2.1",
"path-browserify": "^1.0.1",
"process": "^0.11.10",
"video.js": "7.21.5",
Expand Down
87 changes: 53 additions & 34 deletions src/datastores/handlers/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ import db from '../index'

class Settings {
static find() {
return db.settings.find({ _id: { $ne: 'bounds' } })
return db.settings.findAsync({ _id: { $ne: 'bounds' } })
}

static upsert(_id, value) {
return db.settings.update({ _id }, { _id, value }, { upsert: true })
return db.settings.updateAsync({ _id }, { _id, value }, { upsert: true })
}

static persist() {
return db.settings.compactDatafileAsync()
}

// ******************** //
// Unique Electron main process handlers
static _findAppReadyRelatedSettings() {
return db.settings.find({
return db.settings.findAsync({
$or: [
{ _id: 'disableSmoothScrolling' },
{ _id: 'useProxy' },
Expand All @@ -24,148 +28,163 @@ class Settings {
}

static _findBounds() {
return db.settings.findOne({ _id: 'bounds' })
return db.settings.findOneAsync({ _id: 'bounds' })
}

static _findTheme() {
return db.settings.findOne({ _id: 'baseTheme' })
return db.settings.findOneAsync({ _id: 'baseTheme' })
}

static _findSidenavSettings() {
return {
hideTrendingVideos: db.settings.findOne({ _id: 'hideTrendingVideos' }),
hidePopularVideos: db.settings.findOne({ _id: 'hidePopularVideos' }),
backendFallback: db.settings.findOne({ _id: 'backendFallback' }),
backendPreference: db.settings.findOne({ _id: 'backendPreference' }),
hidePlaylists: db.settings.findOne({ _id: 'hidePlaylists' }),
hideTrendingVideos: db.settings.findOneAsync({ _id: 'hideTrendingVideos' }),
hidePopularVideos: db.settings.findOneAsync({ _id: 'hidePopularVideos' }),
backendFallback: db.settings.findOneAsync({ _id: 'backendFallback' }),
backendPreference: db.settings.findOneAsync({ _id: 'backendPreference' }),
hidePlaylists: db.settings.findOneAsync({ _id: 'hidePlaylists' }),
}
}

static _updateBounds(value) {
return db.settings.update({ _id: 'bounds' }, { _id: 'bounds', value }, { upsert: true })
return db.settings.updateAsync({ _id: 'bounds' }, { _id: 'bounds', value }, { upsert: true })
}
// ******************** //
}

class History {
static find() {
return db.history.find({}).sort({ timeWatched: -1 })
return db.history.findAsync({}).sort({ timeWatched: -1 })
}

static upsert(record) {
return db.history.update({ videoId: record.videoId }, record, { upsert: true })
return db.history.updateAsync({ videoId: record.videoId }, record, { upsert: true })
}

static updateWatchProgress(videoId, watchProgress) {
return db.history.update({ videoId }, { $set: { watchProgress } }, { upsert: true })
return db.history.updateAsync({ videoId }, { $set: { watchProgress } }, { upsert: true })
}

static updateLastViewedPlaylist(videoId, lastViewedPlaylistId) {
return db.history.update({ videoId }, { $set: { lastViewedPlaylistId } }, { upsert: true })
return db.history.updateAsync({ videoId }, { $set: { lastViewedPlaylistId } }, { upsert: true })
}

static delete(videoId) {
return db.history.remove({ videoId })
return db.history.removeAsync({ videoId })
}

static deleteAll() {
return db.history.remove({}, { multi: true })
return db.history.removeAsync({}, { multi: true })
}

static persist() {
db.history.persistence.compactDatafile()
return db.history.compactDatafileAsync()
}
}

class Profiles {
static create(profile) {
return db.profiles.insert(profile)
return db.profiles.insertAsync(profile)
}

static find() {
return db.profiles.find({})
return db.profiles.findAsync({})
}

static upsert(profile) {
return db.profiles.update({ _id: profile._id }, profile, { upsert: true })
return db.profiles.updateAsync({ _id: profile._id }, profile, { upsert: true })
}

static delete(id) {
return db.profiles.remove({ _id: id })
return db.profiles.removeAsync({ _id: id })
}

static persist() {
db.profiles.persistence.compactDatafile()
return db.profiles.compactDatafileAsync()
}
}

class Playlists {
static create(playlists) {
return db.playlists.insert(playlists)
return db.playlists.insertAsync(playlists)
}

static find() {
return db.playlists.find({})
return db.playlists.findAsync({})
}

static upsertVideoByPlaylistName(playlistName, videoData) {
return db.playlists.update(
return db.playlists.updateAsync(
{ playlistName },
{ $push: { videos: videoData } },
{ upsert: true }
)
}

static upsertVideoIdsByPlaylistId(_id, videoIds) {
return db.playlists.update(
return db.playlists.updateAsync(
{ _id },
{ $push: { videos: { $each: videoIds } } },
{ upsert: true }
)
}

static delete(_id) {
return db.playlists.remove({ _id, protected: { $ne: true } })
return db.playlists.removeAsync({ _id, protected: { $ne: true } })
}

static deleteVideoIdByPlaylistName(playlistName, videoId) {
return db.playlists.update(
return db.playlists.updateAsync(
{ playlistName },
{ $pull: { videos: { videoId } } },
{ upsert: true }
)
}

static deleteVideoIdsByPlaylistName(playlistName, videoIds) {
return db.playlists.update(
return db.playlists.updateAsync(
{ playlistName },
{ $pull: { videos: { $in: videoIds } } },
{ upsert: true }
)
}

static deleteAllVideosByPlaylistName(playlistName) {
return db.playlists.update(
return db.playlists.updateAsync(
{ playlistName },
{ $set: { videos: [] } },
{ upsert: true }
)
}

static deleteMultiple(ids) {
return db.playlists.remove({ _id: { $in: ids }, protected: { $ne: true } })
return db.playlists.removeAsync({ _id: { $in: ids }, protected: { $ne: true } })
}

static deleteAll() {
return db.playlists.remove({ protected: { $ne: true } })
return db.playlists.removeAsync({ protected: { $ne: true } })
}

static persist() {
return db.playlists.compactDatafileAsync()
}
}

function compactAllDatastores() {
return Promise.allSettled([
Settings.persist(),
History.persist(),
Profiles.persist(),
Playlists.persist()
])
}

const baseHandlers = {
settings: Settings,
history: History,
profiles: Profiles,
playlists: Playlists
playlists: Playlists,

compactAllDatastores
}

export default baseHandlers
10 changes: 5 additions & 5 deletions src/datastores/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Datastore from 'nedb-promises'
import Datastore from '@seald-io/nedb'

let dbPath = null

Expand All @@ -23,9 +23,9 @@ if (process.env.IS_ELECTRON_MAIN) {
}

const db = {}
db.settings = Datastore.create({ filename: dbPath('settings'), autoload: true })
db.profiles = Datastore.create({ filename: dbPath('profiles'), autoload: true })
db.playlists = Datastore.create({ filename: dbPath('playlists'), autoload: true })
db.history = Datastore.create({ filename: dbPath('history'), autoload: true })
db.settings = new Datastore({ filename: dbPath('settings'), autoload: true })
db.profiles = new Datastore({ filename: dbPath('profiles'), autoload: true })
db.playlists = new Datastore({ filename: dbPath('playlists'), autoload: true })
db.history = new Datastore({ filename: dbPath('history'), autoload: true })

export default db
43 changes: 24 additions & 19 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -993,28 +993,33 @@ function runApp() {
// ************************************************* //

app.on('window-all-closed', () => {
// Clear cache and storage if it's the last window
session.defaultSession.clearCache()
session.defaultSession.clearStorageData({
storages: [
'appcache',
'cookies',
'filesystem',
'indexdb',
'shadercache',
'websql',
'serviceworkers',
'cachestorage'
]
// Clean up resources (datastores' compaction + Electron cache and storage data clearing)
cleanUpResources().finally(() => {
if (process.platform !== 'darwin') {
app.quit()
}
})

// For MacOS the app would still "run in background"
// and create new window on event `activate`
if (process.platform !== 'darwin') {
app.quit()
}
})

function cleanUpResources() {
return Promise.allSettled([
baseHandlers.compactAllDatastores(),
session.defaultSession.clearCache(),
session.defaultSession.clearStorageData({
storages: [
'appcache',
'cookies',
'filesystem',
'indexdb',
'shadercache',
'websql',
'serviceworkers',
'cachestorage'
]
})
])
}

// MacOS event
// https://www.electronjs.org/docs/latest/api/app#event-activate-macos
app.on('activate', () => {
Expand Down
2 changes: 0 additions & 2 deletions src/renderer/components/data-settings/data-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -1137,10 +1137,8 @@ export default defineComponent({

...mapActions([
'updateProfile',
'compactProfiles',
'updateShowProgressBar',
'updateHistory',
'compactHistory',
'addPlaylist',
'addVideo'
]),
Expand Down
4 changes: 0 additions & 4 deletions src/renderer/store/modules/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ const actions = {
} catch (errMessage) {
console.error(errMessage)
}
},

compactHistory(_) {
DBHistoryHandlers.persist()
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/renderer/store/modules/profiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,6 @@ const actions = {
}
},

compactProfiles(_) {
DBProfileHandlers.persist()
},

updateActiveProfile({ commit }, id) {
commit('setActiveProfile', id)
}
Expand Down
25 changes: 9 additions & 16 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1379,17 +1379,17 @@
resolved "https://registry.yarnpkg.com/@pkgjs/parseargs/-/parseargs-0.11.0.tgz#a77ea742fab25775145434eb1d2328cf5013ac33"
integrity sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==

"@seald-io/binary-search-tree@^1.0.2":
version "1.0.2"
resolved "https://registry.yarnpkg.com/@seald-io/binary-search-tree/-/binary-search-tree-1.0.2.tgz#9f0e5cec5e0acf97f1b495f2f6d3476ddb6a94ed"
integrity sha512-+pYGvPFAk7wUR+ONMOlc6A+LUN4kOCFwyPLjyaeS7wVibADPHWYJNYsNtyIAwjF1AXQkuaXElnIc4XjKt55QZA==
"@seald-io/binary-search-tree@^1.0.3":
version "1.0.3"
resolved "https://registry.yarnpkg.com/@seald-io/binary-search-tree/-/binary-search-tree-1.0.3.tgz#165a9a456eaa30d15885b25db83861bcce2c6a74"
integrity sha512-qv3jnwoakeax2razYaMsGI/luWdliBLHTdC6jU55hQt1hcFqzauH/HsBollQ7IR4ySTtYhT+xyHoijpA16C+tA==

"@seald-io/nedb@^3.1.0":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@seald-io/nedb/-/nedb-3.1.0.tgz#6105345a18596f6f9d69f46d7f572efdae32af8e"
integrity sha512-5G0hCQGJjOelOutvW1l4VD581XMhTPxpj1BUaCWTEM2MPXR9TzIr0MKMnEjnTA5nEKfujPyvVW7iF3etm1/gKQ==
"@seald-io/nedb@^4.0.2":
version "4.0.2"
resolved "https://registry.yarnpkg.com/@seald-io/nedb/-/nedb-4.0.2.tgz#44bc5f9b86e44f7434c5af8064cc7f8e079fc3a8"
integrity sha512-gJ91fT1sgh2cLXYVcTSh7khZ8LdemI8+SojCdpZ5wy+DUQ4fSrEwGqOwbdV49NDs2BBO6GeBpSb8CnhG2IW1rw==
dependencies:
"@seald-io/binary-search-tree" "^1.0.2"
"@seald-io/binary-search-tree" "^1.0.3"
localforage "^1.9.0"
util "^0.12.4"

Expand Down Expand Up @@ -5938,13 +5938,6 @@ natural-compare@^1.4.0:
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"
integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=

nedb-promises@^6.2.1:
version "6.2.1"
resolved "https://registry.yarnpkg.com/nedb-promises/-/nedb-promises-6.2.1.tgz#5280a32fb9c9996e77a93195f595860f88e8454a"
integrity sha512-vurL/Hfsk37mbsjYTu+MKnMUytboKBjWWEA0N35ArCBMdDW5x2BAE7Xny4qnlpR90ZuaI8gdWbbGQ2ZoJSp/FQ==
dependencies:
"@seald-io/nedb" "^3.1.0"

negotiator@0.6.3:
version "0.6.3"
resolved "https://registry.yarnpkg.com/negotiator/-/negotiator-0.6.3.tgz#58e323a72fedc0d6f9cd4d31fe49f51479590ccd"
Expand Down

0 comments on commit 079417d

Please sign in to comment.