2023-03-08 19:33:52 +01:00
|
|
|
const Path = require('path')
|
|
|
|
const { Sequelize } = require('sequelize')
|
|
|
|
|
|
|
|
const Logger = require('./Logger')
|
|
|
|
|
|
|
|
class Database {
|
|
|
|
constructor() {
|
|
|
|
this.sequelize = null
|
|
|
|
}
|
|
|
|
|
2023-03-15 23:42:35 +01:00
|
|
|
get models() {
|
|
|
|
return this.sequelize?.models || {}
|
|
|
|
}
|
|
|
|
|
2023-03-19 21:19:22 +01:00
|
|
|
async init(force = false) {
|
2023-03-08 19:33:52 +01:00
|
|
|
if (!await this.connect()) {
|
|
|
|
throw new Error('Database connection failed')
|
|
|
|
}
|
|
|
|
|
2023-03-19 21:19:22 +01:00
|
|
|
await this.buildModels(force)
|
2023-03-08 19:33:52 +01:00
|
|
|
Logger.info(`[Database] Db initialized`, Object.keys(this.sequelize.models))
|
|
|
|
}
|
|
|
|
|
|
|
|
async connect() {
|
|
|
|
const dbPath = Path.join(global.ConfigPath, 'database.sqlite')
|
|
|
|
Logger.info(`[Database] Initializing db at "${dbPath}"`)
|
|
|
|
this.sequelize = new Sequelize({
|
|
|
|
dialect: 'sqlite',
|
2023-03-18 00:04:39 +01:00
|
|
|
storage: dbPath,
|
|
|
|
logging: false
|
2023-03-08 19:33:52 +01:00
|
|
|
})
|
|
|
|
|
2023-03-19 21:19:22 +01:00
|
|
|
// Helper function
|
|
|
|
this.sequelize.uppercaseFirst = str => str ? `${str[0].toUpperCase()}${str.substr(1)}` : ''
|
|
|
|
|
2023-03-08 19:33:52 +01:00
|
|
|
try {
|
|
|
|
await this.sequelize.authenticate()
|
|
|
|
Logger.info(`[Database] Db connection was successful`)
|
|
|
|
return true
|
|
|
|
} catch (error) {
|
|
|
|
Logger.error(`[Database] Failed to connect to db`, error)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-19 21:19:22 +01:00
|
|
|
buildModels(force = false) {
|
2023-03-08 19:33:52 +01:00
|
|
|
require('./models/User')(this.sequelize)
|
2023-03-12 20:51:45 +01:00
|
|
|
require('./models/FileMetadata')(this.sequelize)
|
|
|
|
require('./models/EBookFile')(this.sequelize)
|
|
|
|
require('./models/Book')(this.sequelize)
|
|
|
|
require('./models/Podcast')(this.sequelize)
|
2023-03-19 21:19:22 +01:00
|
|
|
require('./models/Library')(this.sequelize)
|
|
|
|
require('./models/LibraryFolder')(this.sequelize)
|
|
|
|
require('./models/LibraryItem')(this.sequelize)
|
2023-03-12 20:51:45 +01:00
|
|
|
require('./models/PodcastEpisode')(this.sequelize)
|
|
|
|
require('./models/MediaProgress')(this.sequelize)
|
|
|
|
require('./models/LibraryFile')(this.sequelize)
|
|
|
|
require('./models/Person')(this.sequelize)
|
|
|
|
require('./models/AudioBookmark')(this.sequelize)
|
2023-03-15 23:42:35 +01:00
|
|
|
require('./models/MediaFile')(this.sequelize)
|
|
|
|
require('./models/MediaStream')(this.sequelize)
|
2023-03-12 20:51:45 +01:00
|
|
|
require('./models/AudioTrack')(this.sequelize)
|
|
|
|
require('./models/BookAuthor')(this.sequelize)
|
|
|
|
require('./models/BookChapter')(this.sequelize)
|
|
|
|
require('./models/Genre')(this.sequelize)
|
|
|
|
require('./models/BookGenre')(this.sequelize)
|
2023-03-18 22:56:57 +01:00
|
|
|
require('./models/PodcastGenre')(this.sequelize)
|
2023-03-12 20:51:45 +01:00
|
|
|
require('./models/BookNarrator')(this.sequelize)
|
|
|
|
require('./models/Series')(this.sequelize)
|
|
|
|
require('./models/BookSeries')(this.sequelize)
|
|
|
|
require('./models/Tag')(this.sequelize)
|
|
|
|
require('./models/BookTag')(this.sequelize)
|
|
|
|
require('./models/PodcastTag')(this.sequelize)
|
|
|
|
require('./models/Collection')(this.sequelize)
|
|
|
|
require('./models/CollectionBook')(this.sequelize)
|
2023-03-12 23:55:12 +01:00
|
|
|
require('./models/Playlist')(this.sequelize)
|
|
|
|
require('./models/PlaylistMediaItem')(this.sequelize)
|
|
|
|
require('./models/Device')(this.sequelize)
|
|
|
|
require('./models/PlaybackSession')(this.sequelize)
|
2023-03-13 23:13:31 +01:00
|
|
|
require('./models/PlaybackSessionListenTime')(this.sequelize)
|
|
|
|
require('./models/Feed')(this.sequelize)
|
|
|
|
require('./models/FeedEpisode')(this.sequelize)
|
|
|
|
require('./models/Setting')(this.sequelize)
|
|
|
|
require('./models/LibrarySetting')(this.sequelize)
|
2023-03-15 23:42:35 +01:00
|
|
|
require('./models/Notification')(this.sequelize)
|
|
|
|
require('./models/UserPermission')(this.sequelize)
|
2023-03-08 19:33:52 +01:00
|
|
|
|
2023-03-19 21:19:22 +01:00
|
|
|
return this.sequelize.sync({ force })
|
2023-03-08 19:33:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = new Database()
|