Add: Experimental collections add/remove & db #151

This commit is contained in:
advplyr
2021-11-05 20:24:02 -05:00
parent 3d35b7dc3d
commit bf0893d759
27 changed files with 784 additions and 62 deletions

View File

@ -5,6 +5,7 @@ const jwt = require('jsonwebtoken')
const Logger = require('./Logger')
const Audiobook = require('./objects/Audiobook')
const User = require('./objects/User')
const UserCollection = require('./objects/UserCollection')
const Library = require('./objects/Library')
const ServerSettings = require('./objects/ServerSettings')
@ -16,16 +17,19 @@ class Db {
this.UsersPath = Path.join(ConfigPath, 'users')
this.LibrariesPath = Path.join(ConfigPath, 'libraries')
this.SettingsPath = Path.join(ConfigPath, 'settings')
this.CollectionsPath = Path.join(ConfigPath, 'collections')
this.audiobooksDb = new njodb.Database(this.AudiobooksPath)
this.usersDb = new njodb.Database(this.UsersPath)
this.librariesDb = new njodb.Database(this.LibrariesPath, { datastores: 2 })
this.settingsDb = new njodb.Database(this.SettingsPath, { datastores: 2 })
this.collectionsDb = new njodb.Database(this.CollectionsPath, { datastores: 2 })
this.users = []
this.libraries = []
this.audiobooks = []
this.settings = []
this.collections = []
this.serverSettings = null
}
@ -34,14 +38,18 @@ class Db {
if (entityName === 'user') return this.usersDb
else if (entityName === 'audiobook') return this.audiobooksDb
else if (entityName === 'library') return this.librariesDb
return this.settingsDb
else if (entityName === 'settings') return this.settingsDb
else if (entityName === 'collection') return this.collectionsDb
return null
}
getEntityArrayKey(entityName) {
if (entityName === 'user') return 'users'
else if (entityName === 'audiobook') return 'audiobooks'
else if (entityName === 'library') return 'libraries'
return 'settings'
else if (entityName === 'settings') return 'settings'
else if (entityName === 'collection') return 'collections'
return null
}
getDefaultUser(token) {
@ -76,6 +84,7 @@ class Db {
this.usersDb = new njodb.Database(this.UsersPath)
this.librariesDb = new njodb.Database(this.LibrariesPath, { datastores: 2 })
this.settingsDb = new njodb.Database(this.SettingsPath, { datastores: 2 })
this.collectionsDb = new njodb.Database(this.CollectionsPath, { datastores: 2 })
return this.init()
}
@ -125,7 +134,11 @@ class Db {
}
}
})
await Promise.all([p1, p2, p3, p4])
var p5 = this.collectionsDb.select(() => true).then((results) => {
this.collections = results.data.map(l => new UserCollection(l))
Logger.info(`[DB] ${this.collections.length} Collections Loaded`)
})
await Promise.all([p1, p2, p3, p4, p5])
}
updateAudiobook(audiobook) {
@ -286,23 +299,5 @@ class Db {
return false
})
}
getGenres() {
var allGenres = []
this.db.audiobooks.forEach((audiobook) => {
allGenres = allGenres.concat(audiobook.genres)
})
allGenres = [...new Set(allGenres)] // Removes duplicates
return allGenres
}
getTags() {
var allTags = []
this.db.audiobooks.forEach((audiobook) => {
allTags = allTags.concat(audiobook.tags)
})
allTags = [...new Set(allTags)] // Removes duplicates
return allTags
}
}
module.exports = Db