2021-08-18 00:01:11 +02:00
|
|
|
const Path = require('path')
|
2022-06-08 03:15:00 +02:00
|
|
|
const njodb = require('./libs/njodb')
|
2021-08-18 00:01:11 +02:00
|
|
|
const Logger = require('./Logger')
|
2021-11-16 03:09:42 +01:00
|
|
|
const { version } = require('../package.json')
|
2022-03-10 02:23:17 +01:00
|
|
|
const LibraryItem = require('./objects/LibraryItem')
|
2022-03-16 00:57:15 +01:00
|
|
|
const User = require('./objects/user/User')
|
2022-11-12 00:13:10 +01:00
|
|
|
const Collection = require('./objects/Collection')
|
2021-10-05 05:11:42 +02:00
|
|
|
const Library = require('./objects/Library')
|
2022-03-10 02:23:17 +01:00
|
|
|
const Author = require('./objects/entities/Author')
|
|
|
|
const Series = require('./objects/entities/Series')
|
2022-04-15 00:15:52 +02:00
|
|
|
const ServerSettings = require('./objects/settings/ServerSettings')
|
2022-09-22 01:01:10 +02:00
|
|
|
const NotificationSettings = require('./objects/settings/NotificationSettings')
|
2022-04-10 00:56:51 +02:00
|
|
|
const PlaybackSession = require('./objects/PlaybackSession')
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
class Db {
|
2022-02-27 20:47:52 +01:00
|
|
|
constructor() {
|
2022-03-10 02:23:17 +01:00
|
|
|
this.LibraryItemsPath = Path.join(global.ConfigPath, 'libraryItems')
|
2022-02-27 20:47:52 +01:00
|
|
|
this.UsersPath = Path.join(global.ConfigPath, 'users')
|
|
|
|
this.SessionsPath = Path.join(global.ConfigPath, 'sessions')
|
|
|
|
this.LibrariesPath = Path.join(global.ConfigPath, 'libraries')
|
|
|
|
this.SettingsPath = Path.join(global.ConfigPath, 'settings')
|
|
|
|
this.CollectionsPath = Path.join(global.ConfigPath, 'collections')
|
|
|
|
this.AuthorsPath = Path.join(global.ConfigPath, 'authors')
|
2022-03-10 02:23:17 +01:00
|
|
|
this.SeriesPath = Path.join(global.ConfigPath, 'series')
|
2022-06-08 01:29:43 +02:00
|
|
|
this.FeedsPath = Path.join(global.ConfigPath, 'feeds')
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2022-10-30 00:37:56 +02:00
|
|
|
const staleTime = 1000 * 60 * 2
|
|
|
|
this.libraryItemsDb = new njodb.Database(this.LibraryItemsPath, { lockoptions: { stale: staleTime } })
|
|
|
|
this.usersDb = new njodb.Database(this.UsersPath, { lockoptions: { stale: staleTime } })
|
|
|
|
this.sessionsDb = new njodb.Database(this.SessionsPath, { lockoptions: { stale: staleTime } })
|
|
|
|
this.librariesDb = new njodb.Database(this.LibrariesPath, { datastores: 2, lockoptions: { stale: staleTime } })
|
|
|
|
this.settingsDb = new njodb.Database(this.SettingsPath, { datastores: 2, lockoptions: { stale: staleTime } })
|
|
|
|
this.collectionsDb = new njodb.Database(this.CollectionsPath, { datastores: 2, lockoptions: { stale: staleTime } })
|
|
|
|
this.authorsDb = new njodb.Database(this.AuthorsPath, { lockoptions: { stale: staleTime } })
|
|
|
|
this.seriesDb = new njodb.Database(this.SeriesPath, { datastores: 2, lockoptions: { stale: staleTime } })
|
|
|
|
this.feedsDb = new njodb.Database(this.FeedsPath, { datastores: 2, lockoptions: { stale: staleTime } })
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2022-03-10 02:23:17 +01:00
|
|
|
this.libraryItems = []
|
2021-08-18 00:01:11 +02:00
|
|
|
this.users = []
|
2021-10-05 05:11:42 +02:00
|
|
|
this.libraries = []
|
2021-08-18 00:01:11 +02:00
|
|
|
this.settings = []
|
2021-11-06 02:24:02 +01:00
|
|
|
this.collections = []
|
2021-11-18 02:19:24 +01:00
|
|
|
this.authors = []
|
2022-03-10 02:23:17 +01:00
|
|
|
this.series = []
|
2021-09-05 02:58:39 +02:00
|
|
|
|
|
|
|
this.serverSettings = null
|
2022-09-22 01:01:10 +02:00
|
|
|
this.notificationSettings = null
|
2021-11-16 03:09:42 +01:00
|
|
|
|
|
|
|
// Stores previous version only if upgraded
|
|
|
|
this.previousVersion = null
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
2022-05-15 00:23:22 +02:00
|
|
|
get hasRootUser() {
|
|
|
|
return this.users.some(u => u.id === 'root')
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
getEntityDb(entityName) {
|
|
|
|
if (entityName === 'user') return this.usersDb
|
2021-11-13 02:43:16 +01:00
|
|
|
else if (entityName === 'session') return this.sessionsDb
|
2022-03-10 02:23:17 +01:00
|
|
|
else if (entityName === 'libraryItem') return this.libraryItemsDb
|
2021-10-05 05:11:42 +02:00
|
|
|
else if (entityName === 'library') return this.librariesDb
|
2021-11-06 02:24:02 +01:00
|
|
|
else if (entityName === 'settings') return this.settingsDb
|
|
|
|
else if (entityName === 'collection') return this.collectionsDb
|
2021-11-18 02:19:24 +01:00
|
|
|
else if (entityName === 'author') return this.authorsDb
|
2022-03-10 02:23:17 +01:00
|
|
|
else if (entityName === 'series') return this.seriesDb
|
2022-06-08 01:29:43 +02:00
|
|
|
else if (entityName === 'feed') return this.feedsDb
|
2021-11-06 02:24:02 +01:00
|
|
|
return null
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getEntityArrayKey(entityName) {
|
|
|
|
if (entityName === 'user') return 'users'
|
2021-11-13 02:43:16 +01:00
|
|
|
else if (entityName === 'session') return 'sessions'
|
2022-03-10 02:23:17 +01:00
|
|
|
else if (entityName === 'libraryItem') return 'libraryItems'
|
2021-10-05 05:11:42 +02:00
|
|
|
else if (entityName === 'library') return 'libraries'
|
2021-11-06 02:24:02 +01:00
|
|
|
else if (entityName === 'settings') return 'settings'
|
|
|
|
else if (entityName === 'collection') return 'collections'
|
2021-11-18 02:19:24 +01:00
|
|
|
else if (entityName === 'author') return 'authors'
|
2022-03-10 02:23:17 +01:00
|
|
|
else if (entityName === 'series') return 'series'
|
2022-06-08 01:29:43 +02:00
|
|
|
else if (entityName === 'feed') return 'feeds'
|
2021-11-06 02:24:02 +01:00
|
|
|
return null
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
2021-10-09 00:30:20 +02:00
|
|
|
reinit() {
|
2022-03-10 02:23:17 +01:00
|
|
|
this.libraryItemsDb = new njodb.Database(this.LibraryItemsPath)
|
2021-10-09 00:30:20 +02:00
|
|
|
this.usersDb = new njodb.Database(this.UsersPath)
|
2021-11-13 02:43:16 +01:00
|
|
|
this.sessionsDb = new njodb.Database(this.SessionsPath)
|
2021-10-09 00:30:20 +02:00
|
|
|
this.librariesDb = new njodb.Database(this.LibrariesPath, { datastores: 2 })
|
|
|
|
this.settingsDb = new njodb.Database(this.SettingsPath, { datastores: 2 })
|
2021-11-06 02:24:02 +01:00
|
|
|
this.collectionsDb = new njodb.Database(this.CollectionsPath, { datastores: 2 })
|
2021-11-18 02:19:24 +01:00
|
|
|
this.authorsDb = new njodb.Database(this.AuthorsPath)
|
2022-03-10 02:23:17 +01:00
|
|
|
this.seriesDb = new njodb.Database(this.SeriesPath, { datastores: 2 })
|
2022-06-08 01:29:43 +02:00
|
|
|
this.feedsDb = new njodb.Database(this.FeedsPath, { datastores: 2 })
|
2021-10-09 00:30:20 +02:00
|
|
|
return this.init()
|
|
|
|
}
|
|
|
|
|
2022-04-20 15:31:57 +02:00
|
|
|
// Get previous server version before loading DB to check whether a db migration is required
|
|
|
|
// returns null if server was not upgraded
|
|
|
|
checkPreviousVersion() {
|
|
|
|
return this.settingsDb.select(() => true).then((results) => {
|
|
|
|
if (results.data && results.data.length) {
|
|
|
|
var serverSettings = results.data.find(s => s.id === 'server-settings')
|
|
|
|
if (serverSettings && serverSettings.version && serverSettings.version !== version) {
|
|
|
|
return serverSettings.version
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-15 00:23:22 +02:00
|
|
|
createRootUser(username, pash, token) {
|
|
|
|
const newRoot = new User({
|
|
|
|
id: 'root',
|
|
|
|
type: 'root',
|
|
|
|
username,
|
|
|
|
pash,
|
|
|
|
token,
|
|
|
|
isActive: true,
|
|
|
|
createdAt: Date.now()
|
|
|
|
})
|
|
|
|
return this.insertEntity('user', newRoot)
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
async init() {
|
|
|
|
await this.load()
|
|
|
|
|
2022-08-13 20:56:37 +02:00
|
|
|
if (!this.serverSettings) { // Create first load server settings
|
2021-09-05 02:58:39 +02:00
|
|
|
this.serverSettings = new ServerSettings()
|
2021-10-05 05:11:42 +02:00
|
|
|
await this.insertEntity('settings', this.serverSettings)
|
2021-09-05 02:58:39 +02:00
|
|
|
}
|
2022-09-22 01:01:10 +02:00
|
|
|
if (!this.notificationSettings) {
|
|
|
|
this.notificationSettings = new NotificationSettings()
|
|
|
|
await this.insertEntity('settings', this.notificationSettings)
|
|
|
|
}
|
2022-02-27 21:28:18 +01:00
|
|
|
global.ServerSettings = this.serverSettings.toJSON()
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
2022-03-10 02:23:17 +01:00
|
|
|
var p1 = this.libraryItemsDb.select(() => true).then((results) => {
|
|
|
|
this.libraryItems = results.data.map(a => new LibraryItem(a))
|
|
|
|
Logger.info(`[DB] ${this.libraryItems.length} Library Items Loaded`)
|
2021-08-18 00:01:11 +02:00
|
|
|
})
|
|
|
|
var p2 = this.usersDb.select(() => true).then((results) => {
|
|
|
|
this.users = results.data.map(u => new User(u))
|
2021-10-05 05:11:42 +02:00
|
|
|
Logger.info(`[DB] ${this.users.length} Users Loaded`)
|
|
|
|
})
|
|
|
|
var p3 = this.librariesDb.select(() => true).then((results) => {
|
|
|
|
this.libraries = results.data.map(l => new Library(l))
|
2022-03-27 16:45:28 +02:00
|
|
|
this.libraries.sort((a, b) => a.displayOrder - b.displayOrder)
|
2021-10-05 05:11:42 +02:00
|
|
|
Logger.info(`[DB] ${this.libraries.length} Libraries Loaded`)
|
2021-08-18 00:01:11 +02:00
|
|
|
})
|
2022-08-13 20:56:37 +02:00
|
|
|
var p4 = this.settingsDb.select(() => true).then(async (results) => {
|
2021-09-05 02:58:39 +02:00
|
|
|
if (results.data && results.data.length) {
|
|
|
|
this.settings = results.data
|
|
|
|
var serverSettings = this.settings.find(s => s.id === 'server-settings')
|
|
|
|
if (serverSettings) {
|
|
|
|
this.serverSettings = new ServerSettings(serverSettings)
|
2021-11-16 03:09:42 +01:00
|
|
|
|
|
|
|
// Check if server was upgraded
|
|
|
|
if (!this.serverSettings.version || this.serverSettings.version !== version) {
|
|
|
|
this.previousVersion = this.serverSettings.version || '1.0.0'
|
2022-08-13 20:56:37 +02:00
|
|
|
|
|
|
|
// Library settings and server settings updated in 2.1.3 - run migration
|
|
|
|
if (this.previousVersion.localeCompare('2.1.3') < 0) {
|
|
|
|
Logger.info(`[Db] Running servers & library settings migration`)
|
|
|
|
for (const library of this.libraries) {
|
|
|
|
if (library.settings.coverAspectRatio !== serverSettings.coverAspectRatio) {
|
|
|
|
library.settings.coverAspectRatio = serverSettings.coverAspectRatio
|
|
|
|
await this.updateEntity('library', library)
|
|
|
|
Logger.debug(`[Db] Library ${library.name} migrated`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-16 03:09:42 +01:00
|
|
|
}
|
2021-09-05 02:58:39 +02:00
|
|
|
}
|
2022-09-22 01:01:10 +02:00
|
|
|
|
|
|
|
var notificationSettings = this.settings.find(s => s.id === 'notification-settings')
|
|
|
|
if (notificationSettings) {
|
|
|
|
this.notificationSettings = new NotificationSettings(notificationSettings)
|
|
|
|
}
|
2021-09-05 02:58:39 +02:00
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
})
|
2021-11-06 02:24:02 +01:00
|
|
|
var p5 = this.collectionsDb.select(() => true).then((results) => {
|
2022-11-12 00:13:10 +01:00
|
|
|
this.collections = results.data.map(l => new Collection(l))
|
2021-11-06 02:24:02 +01:00
|
|
|
Logger.info(`[DB] ${this.collections.length} Collections Loaded`)
|
|
|
|
})
|
2021-11-18 02:19:24 +01:00
|
|
|
var p6 = this.authorsDb.select(() => true).then((results) => {
|
|
|
|
this.authors = results.data.map(l => new Author(l))
|
|
|
|
Logger.info(`[DB] ${this.authors.length} Authors Loaded`)
|
|
|
|
})
|
2022-03-10 02:23:17 +01:00
|
|
|
var p7 = this.seriesDb.select(() => true).then((results) => {
|
|
|
|
this.series = results.data.map(l => new Series(l))
|
|
|
|
Logger.info(`[DB] ${this.series.length} Series Loaded`)
|
|
|
|
})
|
|
|
|
await Promise.all([p1, p2, p3, p4, p5, p6, p7])
|
2021-11-16 03:09:42 +01:00
|
|
|
|
|
|
|
// Update server version in server settings
|
|
|
|
if (this.previousVersion) {
|
|
|
|
this.serverSettings.version = version
|
2022-02-27 20:47:52 +01:00
|
|
|
await this.updateServerSettings()
|
2021-11-16 03:09:42 +01:00
|
|
|
}
|
2021-09-05 02:58:39 +02:00
|
|
|
}
|
|
|
|
|
2022-03-27 01:58:59 +01:00
|
|
|
getLibraryItem(id) {
|
|
|
|
return this.libraryItems.find(li => li.id === id)
|
|
|
|
}
|
2022-04-21 01:05:09 +02:00
|
|
|
getLibraryItemsInLibrary(libraryId) {
|
|
|
|
return this.libraryItems.filter(li => li.libraryId === libraryId)
|
|
|
|
}
|
2022-03-27 01:58:59 +01:00
|
|
|
|
2022-03-12 02:46:32 +01:00
|
|
|
async updateLibraryItem(libraryItem) {
|
2022-03-13 00:45:32 +01:00
|
|
|
return this.updateLibraryItems([libraryItem])
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateLibraryItems(libraryItems) {
|
|
|
|
await Promise.all(libraryItems.map(async (li) => {
|
|
|
|
if (li && li.saveMetadata) return li.saveMetadata()
|
|
|
|
return null
|
|
|
|
}))
|
|
|
|
|
|
|
|
var libraryItemIds = libraryItems.map(li => li.id)
|
|
|
|
return this.libraryItemsDb.update((record) => libraryItemIds.includes(record.id), (record) => {
|
|
|
|
return libraryItems.find(li => li.id === record.id)
|
|
|
|
}).then((results) => {
|
|
|
|
Logger.debug(`[DB] Library Items updated ${results.updated}`)
|
|
|
|
return true
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[DB] Library Items update failed ${error}`)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async insertLibraryItem(libraryItem) {
|
|
|
|
return this.insertLibraryItems([libraryItem])
|
|
|
|
}
|
|
|
|
|
|
|
|
async insertLibraryItems(libraryItems) {
|
|
|
|
await Promise.all(libraryItems.map(async (li) => {
|
|
|
|
if (li && li.saveMetadata) return li.saveMetadata()
|
|
|
|
return null
|
|
|
|
}))
|
2022-03-12 02:46:32 +01:00
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
return this.libraryItemsDb.insert(libraryItems).then((results) => {
|
|
|
|
Logger.debug(`[DB] Library Items inserted ${results.inserted}`)
|
|
|
|
this.libraryItems = this.libraryItems.concat(libraryItems)
|
2022-03-12 02:46:32 +01:00
|
|
|
return true
|
|
|
|
}).catch((error) => {
|
2022-03-13 00:45:32 +01:00
|
|
|
Logger.error(`[DB] Library Items insert failed ${error}`)
|
2022-03-12 02:46:32 +01:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
removeLibraryItem(id) {
|
|
|
|
return this.libraryItemsDb.delete((record) => record.id === id).then((results) => {
|
|
|
|
Logger.debug(`[DB] Deleted Library Items: ${results.deleted}`)
|
|
|
|
this.libraryItems = this.libraryItems.filter(li => li.id !== id)
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[DB] Remove Library Items Failed: ${error}`)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
updateUserStream(userId, streamId) {
|
|
|
|
return this.usersDb.update((record) => record.id === userId, (user) => {
|
|
|
|
user.stream = streamId
|
|
|
|
return user
|
|
|
|
}).then((results) => {
|
|
|
|
Logger.debug(`[DB] Updated user ${results.updated}`)
|
|
|
|
this.users = this.users.map(u => {
|
|
|
|
if (u.id === userId) {
|
|
|
|
u.stream = streamId
|
|
|
|
}
|
|
|
|
return u
|
|
|
|
})
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[DB] Update user Failed ${error}`)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-02-27 20:47:52 +01:00
|
|
|
updateServerSettings() {
|
|
|
|
global.ServerSettings = this.serverSettings.toJSON()
|
|
|
|
return this.updateEntity('settings', this.serverSettings)
|
|
|
|
}
|
|
|
|
|
2022-06-08 01:29:43 +02:00
|
|
|
getAllEntities(entityName) {
|
|
|
|
var entityDb = this.getEntityDb(entityName)
|
|
|
|
return entityDb.select(() => true).then((results) => results.data).catch((error) => {
|
|
|
|
Logger.error(`[DB] Failed to get all ${entityName}`, error)
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-25 03:15:50 +01:00
|
|
|
insertEntities(entityName, entities) {
|
|
|
|
var entityDb = this.getEntityDb(entityName)
|
|
|
|
return entityDb.insert(entities).then((results) => {
|
|
|
|
Logger.debug(`[DB] Inserted ${results.inserted} ${entityName}`)
|
|
|
|
|
|
|
|
var arrayKey = this.getEntityArrayKey(entityName)
|
2022-03-18 01:10:47 +01:00
|
|
|
if (this[arrayKey]) this[arrayKey] = this[arrayKey].concat(entities)
|
2021-11-25 03:15:50 +01:00
|
|
|
return true
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[DB] Failed to insert ${entityName}`, error)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
insertEntity(entityName, entity) {
|
|
|
|
var entityDb = this.getEntityDb(entityName)
|
|
|
|
return entityDb.insert([entity]).then((results) => {
|
|
|
|
Logger.debug(`[DB] Inserted ${results.inserted} ${entityName}`)
|
|
|
|
|
|
|
|
var arrayKey = this.getEntityArrayKey(entityName)
|
2022-03-18 01:10:47 +01:00
|
|
|
if (this[arrayKey]) this[arrayKey].push(entity)
|
2021-10-05 05:11:42 +02:00
|
|
|
return true
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[DB] Failed to insert ${entityName}`, error)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-04-23 13:25:16 +02:00
|
|
|
async bulkInsertEntities(entityName, entities, batchSize = 500) {
|
|
|
|
// Group entities in batches of size batchSize
|
|
|
|
var entityBatches = []
|
|
|
|
var batch = []
|
|
|
|
var index = 0
|
|
|
|
entities.forEach((ent) => {
|
|
|
|
batch.push(ent)
|
|
|
|
index++
|
|
|
|
if (index >= batchSize) {
|
|
|
|
entityBatches.push(batch)
|
|
|
|
index = 0
|
|
|
|
batch = []
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if (batch.length) entityBatches.push(batch)
|
|
|
|
|
|
|
|
Logger.info(`[Db] bulkInsertEntities: ${entities.length} ${entityName} to ${entityBatches.length} batches of max size ${batchSize}`)
|
|
|
|
|
|
|
|
// Start inserting batches
|
|
|
|
var batchIndex = 1
|
|
|
|
for (const entityBatch of entityBatches) {
|
|
|
|
Logger.info(`[Db] bulkInsertEntities: Start inserting batch ${batchIndex} of ${entityBatch.length} for ${entityName}`)
|
|
|
|
var success = await this.insertEntities(entityName, entityBatch)
|
|
|
|
if (success) {
|
|
|
|
Logger.info(`[Db] bulkInsertEntities: Success inserting batch ${batchIndex} for ${entityName}`)
|
|
|
|
} else {
|
|
|
|
Logger.info(`[Db] bulkInsertEntities: Failed inserting batch ${batchIndex} for ${entityName}`)
|
|
|
|
}
|
|
|
|
batchIndex++
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-11-25 03:15:50 +01:00
|
|
|
updateEntities(entityName, entities) {
|
|
|
|
var entityDb = this.getEntityDb(entityName)
|
|
|
|
|
|
|
|
var entityIds = entities.map(ent => ent.id)
|
|
|
|
return entityDb.update((record) => entityIds.includes(record.id), (record) => {
|
|
|
|
return entities.find(ent => ent.id === record.id)
|
|
|
|
}).then((results) => {
|
|
|
|
Logger.debug(`[DB] Updated ${entityName}: ${results.updated}`)
|
|
|
|
var arrayKey = this.getEntityArrayKey(entityName)
|
2022-03-18 01:10:47 +01:00
|
|
|
if (this[arrayKey]) {
|
|
|
|
this[arrayKey] = this[arrayKey].map(e => {
|
|
|
|
if (entityIds.includes(e.id)) return entities.find(_e => _e.id === e.id)
|
|
|
|
return e
|
|
|
|
})
|
|
|
|
}
|
2021-11-25 03:15:50 +01:00
|
|
|
return true
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[DB] Update ${entityName} Failed: ${error}`)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
updateEntity(entityName, entity) {
|
|
|
|
var entityDb = this.getEntityDb(entityName)
|
2021-11-03 23:45:47 +01:00
|
|
|
|
|
|
|
var jsonEntity = entity
|
|
|
|
if (entity && entity.toJSON) {
|
|
|
|
jsonEntity = entity.toJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
return entityDb.update((record) => record.id === entity.id, () => jsonEntity).then((results) => {
|
2022-08-13 20:56:37 +02:00
|
|
|
Logger.debug(`[DB] Updated ${entityName}: ${results.updated}`)
|
2021-08-18 00:01:11 +02:00
|
|
|
var arrayKey = this.getEntityArrayKey(entityName)
|
2022-03-18 01:10:47 +01:00
|
|
|
if (this[arrayKey]) {
|
|
|
|
this[arrayKey] = this[arrayKey].map(e => {
|
|
|
|
return e.id === entity.id ? entity : e
|
|
|
|
})
|
|
|
|
}
|
2021-08-22 17:46:04 +02:00
|
|
|
return true
|
2021-08-18 00:01:11 +02:00
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[DB] Update entity ${entityName} Failed: ${error}`)
|
2021-08-22 17:46:04 +02:00
|
|
|
return false
|
2021-08-18 00:01:11 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
removeEntity(entityName, entityId) {
|
|
|
|
var entityDb = this.getEntityDb(entityName)
|
2022-04-28 02:42:34 +02:00
|
|
|
return entityDb.delete((record) => {
|
|
|
|
return record.id === entityId
|
|
|
|
}).then((results) => {
|
2021-08-18 00:01:11 +02:00
|
|
|
Logger.debug(`[DB] Deleted entity ${entityName}: ${results.deleted}`)
|
|
|
|
var arrayKey = this.getEntityArrayKey(entityName)
|
2022-03-18 01:10:47 +01:00
|
|
|
if (this[arrayKey]) {
|
|
|
|
this[arrayKey] = this[arrayKey].filter(e => {
|
|
|
|
return e.id !== entityId
|
|
|
|
})
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[DB] Remove entity ${entityName} Failed: ${error}`)
|
|
|
|
})
|
|
|
|
}
|
2021-08-20 00:29:36 +02:00
|
|
|
|
2022-07-30 00:13:46 +02:00
|
|
|
removeEntities(entityName, selectFunc) {
|
|
|
|
var entityDb = this.getEntityDb(entityName)
|
|
|
|
return entityDb.delete(selectFunc).then((results) => {
|
|
|
|
Logger.debug(`[DB] Deleted entities ${entityName}: ${results.deleted}`)
|
|
|
|
var arrayKey = this.getEntityArrayKey(entityName)
|
|
|
|
if (this[arrayKey]) {
|
|
|
|
this[arrayKey] = this[arrayKey].filter(e => {
|
|
|
|
return !selectFunc(e)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return results.deleted
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[DB] Remove entities ${entityName} Failed: ${error}`)
|
|
|
|
return 0
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-10 02:23:17 +01:00
|
|
|
recreateLibraryItemsDb() {
|
|
|
|
return this.libraryItemsDb.drop().then((results) => {
|
|
|
|
Logger.info(`[DB] Dropped library items db`, results)
|
|
|
|
this.libraryItemsDb = new njodb.Database(this.LibraryItemsPath)
|
|
|
|
this.libraryItems = []
|
2021-08-21 23:23:35 +02:00
|
|
|
return true
|
|
|
|
}).catch((error) => {
|
2022-03-10 02:23:17 +01:00
|
|
|
Logger.error(`[DB] Failed to drop library items db`, error)
|
2021-08-21 23:23:35 +02:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
2021-11-13 02:43:16 +01:00
|
|
|
|
2022-07-30 00:13:46 +02:00
|
|
|
getAllSessions(selectFunc = () => true) {
|
|
|
|
return this.sessionsDb.select(selectFunc).then((results) => {
|
2022-06-04 19:44:42 +02:00
|
|
|
return results.data || []
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error('[Db] Failed to select sessions', error)
|
|
|
|
return []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-13 19:24:19 +02:00
|
|
|
getPlaybackSession(id) {
|
|
|
|
return this.sessionsDb.select((pb) => pb.id == id).then((results) => {
|
|
|
|
if (results.data.length) {
|
|
|
|
return new PlaybackSession(results.data[0])
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error('Failed to get session', error)
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-13 02:43:16 +01:00
|
|
|
selectUserSessions(userId) {
|
|
|
|
return this.sessionsDb.select((session) => session.userId === userId).then((results) => {
|
|
|
|
return results.data || []
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[Db] Failed to select user sessions "${userId}"`, error)
|
|
|
|
return []
|
|
|
|
})
|
|
|
|
}
|
2022-02-27 21:28:18 +01:00
|
|
|
|
|
|
|
// Check if server was updated and previous version was earlier than param
|
|
|
|
checkPreviousVersionIsBefore(version) {
|
|
|
|
if (!this.previousVersion) return false
|
|
|
|
// true if version > previousVersion
|
|
|
|
return version.localeCompare(this.previousVersion) >= 0
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
2021-11-04 23:35:59 +01:00
|
|
|
module.exports = Db
|