后台:web-server连接数据库
This commit is contained in:
+24
-3
@@ -2,6 +2,7 @@ import 'reflect-metadata'
|
||||
import * as mongoose from 'mongoose';
|
||||
import { Application, IBoot } from 'egg';
|
||||
import { connectRedis } from './app/pubUtils/redis';
|
||||
import { loadGmDb } from '@db/index';
|
||||
|
||||
export default class FooBoot implements IBoot {
|
||||
private readonly app: Application;
|
||||
@@ -15,6 +16,7 @@ export default class FooBoot implements IBoot {
|
||||
// Config, plugin files are referred,`
|
||||
// this is the last chance to modify the config.
|
||||
await this.connectDB(this.app);
|
||||
await this.connectGMDB(this.app);
|
||||
await this.connectRedis(this.app);
|
||||
|
||||
}
|
||||
@@ -51,9 +53,28 @@ export default class FooBoot implements IBoot {
|
||||
|
||||
public async connectDB(app: Application) {
|
||||
const { url, options } = app.config.mongoose
|
||||
if (url) {
|
||||
const connection = await mongoose.connect(url, options)
|
||||
app.context.connection = connection
|
||||
try {
|
||||
if (url) {
|
||||
const connection = await mongoose.connect(url, options)
|
||||
console.log('******connectDB suc', url, options)
|
||||
app.context.connection = connection
|
||||
}
|
||||
} catch(e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
public async connectGMDB(app: Application) {
|
||||
const { url, options } = app.config.gmmongoose
|
||||
try {
|
||||
if (url) {
|
||||
const connection = await mongoose.createConnection(url, options)
|
||||
app.context.connectionGM = connection;
|
||||
loadGmDb(connection);
|
||||
console.log('******connectGMDB suc', url, options)
|
||||
}
|
||||
} catch(e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user