mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2024-12-20 19:06:06 +01:00
Sequelize and sqlite init with test user model
This commit is contained in:
parent
a2d4a4a906
commit
b8ab72a141
@ -14,7 +14,10 @@ RUN apk update && \
|
||||
apk add --no-cache --update \
|
||||
curl \
|
||||
tzdata \
|
||||
ffmpeg
|
||||
ffmpeg \
|
||||
make \
|
||||
python3 \
|
||||
g++
|
||||
|
||||
COPY --from=tone /usr/local/bin/tone /usr/local/bin/
|
||||
COPY --from=build /client/dist /client/dist
|
||||
@ -23,6 +26,8 @@ COPY server server
|
||||
|
||||
RUN npm ci --only=production
|
||||
|
||||
RUN apk del make python3 g++
|
||||
|
||||
EXPOSE 80
|
||||
HEALTHCHECK \
|
||||
--interval=30s \
|
||||
|
2378
package-lock.json
generated
2378
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -35,10 +35,12 @@
|
||||
"graceful-fs": "^4.2.10",
|
||||
"htmlparser2": "^8.0.1",
|
||||
"node-tone": "^1.0.1",
|
||||
"sequelize": "^6.29.1",
|
||||
"socket.io": "^4.5.4",
|
||||
"sqlite3": "^5.1.4",
|
||||
"xml2js": "^0.4.23"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^2.0.20"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
63
server/Database.js
Normal file
63
server/Database.js
Normal file
@ -0,0 +1,63 @@
|
||||
const Path = require('path')
|
||||
const { Sequelize } = require('sequelize')
|
||||
|
||||
const Logger = require('./Logger')
|
||||
|
||||
class Database {
|
||||
constructor() {
|
||||
this.sequelize = null
|
||||
}
|
||||
|
||||
async init() {
|
||||
if (!await this.connect()) {
|
||||
throw new Error('Database connection failed')
|
||||
}
|
||||
|
||||
await this.buildModels()
|
||||
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',
|
||||
storage: dbPath
|
||||
})
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
buildModels() {
|
||||
require('./models/User')(this.sequelize)
|
||||
|
||||
return this.sequelize.sync()
|
||||
}
|
||||
|
||||
async createTestUser() {
|
||||
const User = this.sequelize.models.User
|
||||
|
||||
let user = await User.findOne({
|
||||
where: {
|
||||
username: 'Tester'
|
||||
}
|
||||
})
|
||||
|
||||
if (user) {
|
||||
Logger.info(`[Database] Tester user was found`, user.toJSON())
|
||||
} else {
|
||||
user = await User.create({ username: 'Tester' })
|
||||
Logger.info(`[Database] Created Tester user`, user.toJSON())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
module.exports = new Database()
|
@ -17,6 +17,7 @@ const Auth = require('./Auth')
|
||||
const Watcher = require('./Watcher')
|
||||
const Scanner = require('./scanner/Scanner')
|
||||
const Db = require('./Db')
|
||||
const Database = require('./Database')
|
||||
const SocketAuthority = require('./SocketAuthority')
|
||||
|
||||
const ApiRouter = require('./routers/ApiRouter')
|
||||
@ -110,6 +111,10 @@ class Server {
|
||||
await this.db.init()
|
||||
}
|
||||
|
||||
// TODO: Test new db connection
|
||||
await Database.init()
|
||||
await Database.createTestUser()
|
||||
|
||||
// Create token secret if does not exist (Added v2.1.0)
|
||||
if (!this.db.serverSettings.tokenSecret) {
|
||||
await this.auth.initTokenSecret()
|
||||
|
14
server/models/User.js
Normal file
14
server/models/User.js
Normal file
@ -0,0 +1,14 @@
|
||||
const { DataTypes, Model } = require('sequelize')
|
||||
|
||||
module.exports = (sequelize) => {
|
||||
class User extends Model { }
|
||||
|
||||
User.init({
|
||||
username: DataTypes.STRING
|
||||
}, {
|
||||
sequelize,
|
||||
modelName: 'User'
|
||||
})
|
||||
|
||||
return User
|
||||
}
|
Loading…
Reference in New Issue
Block a user