feat(db): web和gm添加只读库支持

This commit is contained in:
liangtongchuan
2023-05-06 16:04:57 +08:00
parent e65efa4192
commit 96407c470e
8 changed files with 62 additions and 2 deletions
+16 -1
View File
@@ -2,7 +2,7 @@ import 'reflect-metadata'
import * as mongoose from 'mongoose';
import { Application, IBoot } from 'egg';
import { connectRedis } from './app/pubUtils/redis';
import { loadGmDb, loadSubDb } from '@db/index';
import { loadGmDb, loadRODb, loadSubDb } from '@db/index';
import { SDK_TA_CONST, THINKING_DATA_MODE, THINKING_DATA_MODE_LIST } from '@consts';
const ThinkingAnalytics = require("thinkingdata-node");
@@ -20,6 +20,7 @@ export default class FooBoot implements IBoot {
await this.connectDB(this.app);
await this.connectGMDB(this.app);
await this.connectSubDB(this.app);
await this.connectRODB(this.app);
await this.connectRedis(this.app);
this.app.config.realEnv = this.app.config.env;
@@ -104,6 +105,20 @@ export default class FooBoot implements IBoot {
}
}
public async connectRODB(app: Application) {
const { url, options } = app.config.romongoose||app.config.mongoose
try {
if (url) {
const connection = await mongoose.createConnection(url, options)
app.context.connectionRO = connection;
loadRODb(connection);
console.log('******connectSubDB suc', url, options)
}
} catch(e) {
console.log(e)
}
}
public async connectRedis(app: Application) {
const { url, pw } = app.config.redis
if (url) {