mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
225 lines
6.9 KiB
225 lines
6.9 KiB
2 years ago
|
import { get, has, isEmpty, isPlainObject } from 'lodash-es'
|
||
|
import path from 'node:path'
|
||
|
import knex from 'knex'
|
||
|
import fs from 'node:fs/promises'
|
||
|
import Objection from 'objection'
|
||
|
import PGPubSub from 'pg-pubsub'
|
||
|
|
||
|
import migrationSource from '../db/migrator-source.mjs'
|
||
|
// const migrateFromLegacy = require('../db/legacy')
|
||
|
import { setTimeout } from 'node:timers/promises'
|
||
6 years ago
|
|
||
7 years ago
|
/**
|
||
6 years ago
|
* ORM DB module
|
||
7 years ago
|
*/
|
||
2 years ago
|
export default {
|
||
7 years ago
|
Objection,
|
||
|
knex: null,
|
||
5 years ago
|
listener: null,
|
||
2 years ago
|
config: null,
|
||
7 years ago
|
/**
|
||
|
* Initialize DB
|
||
|
*/
|
||
2 years ago
|
async init (workerMode = false) {
|
||
7 years ago
|
let self = this
|
||
|
|
||
3 years ago
|
WIKI.logger.info('Checking DB configuration...')
|
||
|
|
||
5 years ago
|
// Fetch DB Config
|
||
|
|
||
2 years ago
|
this.config = (!isEmpty(process.env.DATABASE_URL)) ? process.env.DATABASE_URL : {
|
||
5 years ago
|
host: WIKI.config.db.host.toString(),
|
||
|
user: WIKI.config.db.user.toString(),
|
||
|
password: WIKI.config.db.pass.toString(),
|
||
|
database: WIKI.config.db.db.toString(),
|
||
6 years ago
|
port: WIKI.config.db.port
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
5 years ago
|
// Handle SSL Options
|
||
|
|
||
|
let dbUseSSL = (WIKI.config.db.ssl === true || WIKI.config.db.ssl === 'true' || WIKI.config.db.ssl === 1 || WIKI.config.db.ssl === '1')
|
||
5 years ago
|
let sslOptions = null
|
||
2 years ago
|
if (dbUseSSL && isPlainObject(this.config) && get(WIKI.config.db, 'sslOptions.auto', null) === false) {
|
||
5 years ago
|
sslOptions = WIKI.config.db.sslOptions
|
||
4 years ago
|
sslOptions.rejectUnauthorized = sslOptions.rejectUnauthorized !== false
|
||
5 years ago
|
if (sslOptions.ca && sslOptions.ca.indexOf('-----') !== 0) {
|
||
2 years ago
|
sslOptions.ca = await fs.readFile(path.resolve(WIKI.ROOTPATH, sslOptions.ca), 'utf-8')
|
||
5 years ago
|
}
|
||
|
if (sslOptions.cert) {
|
||
2 years ago
|
sslOptions.cert = await fs.readFile(path.resolve(WIKI.ROOTPATH, sslOptions.cert), 'utf-8')
|
||
5 years ago
|
}
|
||
|
if (sslOptions.key) {
|
||
2 years ago
|
sslOptions.key = await fs.readFile(path.resolve(WIKI.ROOTPATH, sslOptions.key), 'utf-8')
|
||
5 years ago
|
}
|
||
|
if (sslOptions.pfx) {
|
||
2 years ago
|
sslOptions.pfx = await fs.readFile(path.resolve(WIKI.ROOTPATH, sslOptions.pfx), 'utf-8')
|
||
5 years ago
|
}
|
||
|
} else {
|
||
|
sslOptions = true
|
||
|
}
|
||
6 years ago
|
|
||
5 years ago
|
// Handle inline SSL CA Certificate mode
|
||
2 years ago
|
if (!isEmpty(process.env.DB_SSL_CA)) {
|
||
5 years ago
|
const chunks = []
|
||
|
for (let i = 0, charsLength = process.env.DB_SSL_CA.length; i < charsLength; i += 64) {
|
||
|
chunks.push(process.env.DB_SSL_CA.substring(i, i + 64))
|
||
|
}
|
||
|
|
||
5 years ago
|
dbUseSSL = true
|
||
|
sslOptions = {
|
||
|
rejectUnauthorized: true,
|
||
5 years ago
|
ca: '-----BEGIN CERTIFICATE-----\n' + chunks.join('\n') + '\n-----END CERTIFICATE-----\n'
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
if (dbUseSSL && isPlainObject(this.config)) {
|
||
2 years ago
|
this.config.ssl = (sslOptions === true) ? { rejectUnauthorized: true } : sslOptions
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
5 years ago
|
// Initialize Knex
|
||
2 years ago
|
this.knex = knex({
|
||
3 years ago
|
client: 'pg',
|
||
7 years ago
|
useNullAsDefault: true,
|
||
6 years ago
|
asyncStackTraces: WIKI.IS_DEBUG,
|
||
2 years ago
|
connection: this.config,
|
||
3 years ago
|
searchPath: [WIKI.config.db.schemas.wiki],
|
||
7 years ago
|
pool: {
|
||
2 years ago
|
...workerMode ? { min: 0, max: 1 } : WIKI.config.pool,
|
||
7 years ago
|
async afterCreate(conn, done) {
|
||
|
// -> Set Connection App Name
|
||
2 years ago
|
if (workerMode) {
|
||
|
await conn.query(`set application_name = 'Wiki.js - ${WIKI.INSTANCE_ID}'`)
|
||
|
} else {
|
||
|
await conn.query(`set application_name = 'Wiki.js - ${WIKI.INSTANCE_ID}:MAIN'`)
|
||
|
}
|
||
3 years ago
|
done()
|
||
7 years ago
|
}
|
||
|
},
|
||
7 years ago
|
debug: WIKI.IS_DEBUG
|
||
7 years ago
|
})
|
||
|
|
||
7 years ago
|
Objection.Model.knex(this.knex)
|
||
7 years ago
|
|
||
7 years ago
|
// Load DB Models
|
||
7 years ago
|
|
||
3 years ago
|
WIKI.logger.info('Loading DB models...')
|
||
2 years ago
|
const models = (await import(path.join(WIKI.SERVERPATH, 'models/index.mjs'))).default
|
||
7 years ago
|
|
||
7 years ago
|
// Set init tasks
|
||
5 years ago
|
let conAttempts = 0
|
||
2 years ago
|
const initTasks = {
|
||
5 years ago
|
// -> Attempt initial connection
|
||
5 years ago
|
async connect () {
|
||
5 years ago
|
try {
|
||
|
WIKI.logger.info('Connecting to database...')
|
||
|
await self.knex.raw('SELECT 1 + 1;')
|
||
|
WIKI.logger.info('Database Connection Successful [ OK ]')
|
||
|
} catch (err) {
|
||
|
if (conAttempts < 10) {
|
||
5 years ago
|
if (err.code) {
|
||
|
WIKI.logger.error(`Database Connection Error: ${err.code} ${err.address}:${err.port}`)
|
||
|
} else {
|
||
|
WIKI.logger.error(`Database Connection Error: ${err.message}`)
|
||
|
}
|
||
5 years ago
|
WIKI.logger.warn(`Will retry in 3 seconds... [Attempt ${++conAttempts} of 10]`)
|
||
2 years ago
|
await setTimeout(3000)
|
||
5 years ago
|
await initTasks.connect()
|
||
|
} else {
|
||
|
throw err
|
||
|
}
|
||
|
}
|
||
5 years ago
|
},
|
||
|
// -> Migrate DB Schemas
|
||
|
async syncSchemas () {
|
||
3 years ago
|
WIKI.logger.info('Ensuring DB schema exists...')
|
||
|
await self.knex.raw(`CREATE SCHEMA IF NOT EXISTS ${WIKI.config.db.schemas.wiki}`)
|
||
3 years ago
|
WIKI.logger.info('Ensuring DB migrations have been applied...')
|
||
5 years ago
|
return self.knex.migrate.latest({
|
||
|
tableName: 'migrations',
|
||
3 years ago
|
migrationSource,
|
||
|
schemaName: WIKI.config.db.schemas.wiki
|
||
5 years ago
|
})
|
||
|
},
|
||
3 years ago
|
// -> Migrate DB Schemas from 2.x
|
||
|
async migrateFromLegacy () {
|
||
2 years ago
|
// return migrateFromLegacy.migrate(self.knex)
|
||
7 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// Perform init tasks
|
||
|
|
||
2 years ago
|
this.onReady = workerMode ? Promise.resolve() : (async () => {
|
||
2 years ago
|
await initTasks.connect()
|
||
|
await initTasks.migrateFromLegacy()
|
||
|
await initTasks.syncSchemas()
|
||
|
})()
|
||
7 years ago
|
|
||
7 years ago
|
return {
|
||
|
...this,
|
||
|
...models
|
||
|
}
|
||
5 years ago
|
},
|
||
|
/**
|
||
|
* Subscribe to database LISTEN / NOTIFY for multi-instances events
|
||
|
*/
|
||
|
async subscribeToNotifications () {
|
||
2 years ago
|
let connSettings = this.knex.client.connectionSettings
|
||
|
if (typeof connSettings === 'string') {
|
||
|
const encodedName = encodeURIComponent(`Wiki.js - ${WIKI.INSTANCE_ID}:PSUB`)
|
||
|
if (connSettings.indexOf('?') > 0) {
|
||
|
connSettings = `${connSettings}&ApplicationName=${encodedName}`
|
||
|
} else {
|
||
|
connSettings = `${connSettings}?ApplicationName=${encodedName}`
|
||
|
}
|
||
|
} else {
|
||
|
connSettings.application_name = `Wiki.js - ${WIKI.INSTANCE_ID}:PSUB`
|
||
|
}
|
||
|
this.listener = new PGPubSub(connSettings, {
|
||
5 years ago
|
log (ev) {
|
||
|
WIKI.logger.debug(ev)
|
||
|
}
|
||
|
})
|
||
5 years ago
|
|
||
|
// -> Outbound events handling
|
||
|
|
||
5 years ago
|
this.listener.addChannel('wiki', payload => {
|
||
2 years ago
|
if (has(payload, 'event') && payload.source !== WIKI.INSTANCE_ID) {
|
||
5 years ago
|
WIKI.logger.info(`Received event ${payload.event} from instance ${payload.source}: [ OK ]`)
|
||
5 years ago
|
WIKI.events.inbound.emit(payload.event, payload.value)
|
||
5 years ago
|
}
|
||
|
})
|
||
5 years ago
|
WIKI.events.outbound.onAny(this.notifyViaDB)
|
||
|
|
||
|
// -> Listen to inbound events
|
||
|
|
||
|
WIKI.auth.subscribeToEvents()
|
||
|
WIKI.configSvc.subscribeToEvents()
|
||
2 years ago
|
WIKI.db.pages.subscribeToEvents()
|
||
5 years ago
|
|
||
3 years ago
|
WIKI.logger.info(`PG PubSub Listener initialized successfully: [ OK ]`)
|
||
5 years ago
|
},
|
||
|
/**
|
||
|
* Unsubscribe from database LISTEN / NOTIFY
|
||
|
*/
|
||
|
async unsubscribeToNotifications () {
|
||
|
if (this.listener) {
|
||
5 years ago
|
WIKI.events.outbound.offAny(this.notifyViaDB)
|
||
|
WIKI.events.inbound.removeAllListeners()
|
||
5 years ago
|
this.listener.close()
|
||
|
}
|
||
|
},
|
||
|
/**
|
||
|
* Publish event via database NOTIFY
|
||
|
*
|
||
|
* @param {string} event Event fired
|
||
|
* @param {object} value Payload of the event
|
||
|
*/
|
||
|
notifyViaDB (event, value) {
|
||
2 years ago
|
WIKI.db.listener.publish('wiki', {
|
||
5 years ago
|
source: WIKI.INSTANCE_ID,
|
||
|
event,
|
||
|
value
|
||
|
})
|
||
7 years ago
|
}
|
||
|
}
|