2021-08-18 00:01:11 +02:00
|
|
|
const Path = require('path')
|
|
|
|
const njodb = require("njodb")
|
2021-11-04 01:12:17 +01:00
|
|
|
const fs = require('fs-extra')
|
2021-08-18 00:01:11 +02:00
|
|
|
const jwt = require('jsonwebtoken')
|
|
|
|
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 Audiobook = require('./objects/Audiobook')
|
|
|
|
const LibraryItem = require('./objects/LibraryItem')
|
2022-03-16 00:57:15 +01:00
|
|
|
const User = require('./objects/user/User')
|
2021-11-06 02:24:02 +01:00
|
|
|
const UserCollection = require('./objects/UserCollection')
|
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')
|
2021-09-05 02:58:39 +02:00
|
|
|
const ServerSettings = require('./objects/ServerSettings')
|
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')
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2022-03-10 02:23:17 +01:00
|
|
|
this.libraryItemsDb = new njodb.Database(this.LibraryItemsPath)
|
2021-08-18 00:01:11 +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-05 05:11:42 +02:00
|
|
|
this.librariesDb = new njodb.Database(this.LibrariesPath, { datastores: 2 })
|
2021-08-18 00:01:11 +02:00
|
|
|
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 })
|
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
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
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'
|
2021-11-06 02:24:02 +01:00
|
|
|
return null
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
getDefaultUser(token) {
|
|
|
|
return new User({
|
|
|
|
id: 'root',
|
|
|
|
type: 'root',
|
|
|
|
username: 'root',
|
|
|
|
pash: '',
|
|
|
|
stream: null,
|
|
|
|
token,
|
2021-08-27 14:01:47 +02:00
|
|
|
isActive: true,
|
2021-08-18 00:01:11 +02:00
|
|
|
createdAt: Date.now()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
getDefaultLibrary() {
|
|
|
|
var defaultLibrary = new Library()
|
|
|
|
defaultLibrary.setData({
|
|
|
|
id: 'main',
|
|
|
|
name: 'Main',
|
|
|
|
folder: { // Generates default folder
|
|
|
|
id: 'audiobooks',
|
2022-02-27 20:47:52 +01:00
|
|
|
fullPath: global.AudiobookPath,
|
2021-10-05 05:11:42 +02:00
|
|
|
libraryId: 'main'
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return defaultLibrary
|
|
|
|
}
|
|
|
|
|
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 })
|
2021-10-09 00:30:20 +02:00
|
|
|
return this.init()
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
async init() {
|
|
|
|
await this.load()
|
|
|
|
|
|
|
|
// Insert Defaults
|
2021-10-16 14:49:12 +02:00
|
|
|
var rootUser = this.users.find(u => u.type === 'root')
|
|
|
|
if (!rootUser) {
|
2021-08-18 00:01:11 +02:00
|
|
|
var token = await jwt.sign({ userId: 'root' }, process.env.TOKEN_SECRET)
|
|
|
|
Logger.debug('Generated default token', token)
|
2021-10-16 14:49:12 +02:00
|
|
|
Logger.info('[Db] Root user created')
|
2021-10-05 05:11:42 +02:00
|
|
|
await this.insertEntity('user', this.getDefaultUser(token))
|
2021-10-16 14:49:12 +02:00
|
|
|
} else {
|
|
|
|
Logger.info(`[Db] Root user exists, pw: ${rootUser.hasPw}`)
|
2021-10-05 05:11:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.libraries.length) {
|
|
|
|
await this.insertEntity('library', this.getDefaultLibrary())
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
2021-09-05 02:58:39 +02:00
|
|
|
|
|
|
|
if (!this.serverSettings) {
|
|
|
|
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-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))
|
|
|
|
Logger.info(`[DB] ${this.libraries.length} Libraries Loaded`)
|
2021-08-18 00:01:11 +02:00
|
|
|
})
|
2021-10-05 05:11:42 +02:00
|
|
|
var p4 = this.settingsDb.select(() => true).then((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'
|
|
|
|
}
|
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) => {
|
|
|
|
this.collections = results.data.map(l => new UserCollection(l))
|
|
|
|
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-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)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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-01-24 23:53:35 +01:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
Logger.debug(`[DB] Updated ${entityName}: ${results.updated} | Selected: ${results.selected}`)
|
|
|
|
} else {
|
|
|
|
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)
|
|
|
|
return entityDb.delete((record) => record.id === entityId).then((results) => {
|
|
|
|
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-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
|
|
|
|
|
|
|
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
|