change how db its initialized

This commit is contained in:
les 2021-09-27 11:14:11 +02:00
parent 5e0e08480a
commit c3c05451db
No known key found for this signature in database
GPG key ID: 352918250B012177
15 changed files with 37 additions and 52 deletions

View file

@ -1,4 +1,4 @@
const sequelize = require('./index')
const sequelize = require('./index').sequelize
const { Model, DataTypes } = require('sequelize')
class Announcement extends Model {}

View file

@ -1,4 +1,4 @@
const sequelize = require('./index')
const sequelize = require('./index').sequelize
const { Model, DataTypes } = require('sequelize')
class APUser extends Model {}

View file

@ -1,11 +1,11 @@
const config = require('config')
const config = require('../../config')
const moment = require('dayjs')
const { htmlToText } = require('html-to-text')
const { Model, DataTypes } = require('sequelize')
const SequelizeSlugify = require('sequelize-slugify')
const sequelize = require('./index')
const sequelize = require('./index').sequelize
const Resource = require('./resource')
const Notification = require('./notification')

View file

@ -1,4 +1,4 @@
const sequelize = require('./index')
const sequelize = require('./index').sequelize
const { Model, DataTypes } = require('sequelize')
class EventNotification extends Model {}

View file

@ -1,41 +1,27 @@
// const fs = require('fs')
// const path = require('path')
const Sequelize = require('sequelize')
// const basename = path.basename(__filename)
const config = require('config')
const consola = require('consola')
// const db = {}
let sequelize = null
const config = require('../../config')
const log = require('../../log')
try {
sequelize = new Sequelize(config.db)
} catch (e) {
consola.warn(` ⚠️ Cannot connect to db, check your configuration => ${e}`)
process.exit(-1)
const db = {
sequelize: null,
close () {
return db.sequelize.close()
},
async connect (dbConf = config.db) {
log.debug(`Connecting to DB: ${dbConf}`)
db.sequelize = new Sequelize(dbConf)
await db.sequelize.authenticate()
return db.sequelize
}
}
sequelize.authenticate().catch(e => {
consola.error(' ⚠ Error connecting to DB: ', String(e))
process.exit(-1)
})
if (!config.firstrun) {
try {
db.connect()
} catch (e) {
log.warn(` ⚠️ Cannot connect to db, check your configuration => ${e}`)
process.exit(1)
}
}
// fs
// .readdirSync(__dirname)
// .filter(file => {
// return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js')
// })
// .forEach(file => {
// const model = sequelize.import(path.join(__dirname, file))
// db[model.name] = model
// })
// Object.keys(db).forEach(modelName => {
// if (db[modelName].associate) {
// db[modelName].associate(db)
// }
// })
// db.sequelize = sequelize
// db.Sequelize = Sequelize
module.exports = sequelize
module.exports = db

View file

@ -1,5 +1,5 @@
const sequelize = require('./index')
const sequelize = require('./index').sequelize
const { Model, DataTypes } = require('sequelize')
const APUser = require('./ap_user')

View file

@ -1,5 +1,5 @@
const sequelize = require('./index')
const sequelize = require('./index').sequelize
const { Model, DataTypes } = require('sequelize')
class Notification extends Model {}

View file

@ -1,5 +1,5 @@
const sequelize = require('./index')
const sequelize = require('./index').sequelize
const { Model, DataTypes } = require('sequelize')
class OAuthClient extends Model {}

View file

@ -1,5 +1,5 @@
const sequelize = require('./index')
const sequelize = require('./index').sequelize
const { Model, DataTypes } = require('sequelize')
const User = require('./user')

View file

@ -1,5 +1,5 @@
const sequelize = require('./index')
const sequelize = require('./index').sequelize
const { Model, DataTypes } = require('sequelize')
const User = require('./user')

View file

@ -1,5 +1,5 @@
const { Model, DataTypes } = require('sequelize')
const sequelize = require('./index')
const sequelize = require('./index').sequelize
class Place extends Model {}

View file

@ -1,7 +1,6 @@
const { Model, DataTypes } = require('sequelize')
const sequelize = require('./index')
const sequelize = require('./index').sequelize
// const Event = require('./event')
const APUser = require('./ap_user')
class Resource extends Model {}

View file

@ -1,5 +1,5 @@
const { Model, DataTypes } = require('sequelize')
const sequelize = require('./index')
const sequelize = require('./index').sequelize
class Setting extends Model {}

View file

@ -1,5 +1,5 @@
const { Model, DataTypes } = require('sequelize')
const sequelize = require('./index')
const sequelize = require('./index').sequelize
class Tag extends Model {}

View file

@ -1,7 +1,7 @@
const bcrypt = require('bcryptjs')
const { Model, DataTypes } = require('sequelize')
const sequelize = require('./index')
const sequelize = require('./index').sequelize
class User extends Model {}