浏览代码

feat(wise-cost-connect): 动态连接数据库插件

zhangweicheng 3 年之前
父节点
当前提交
7b22c752fe

+ 3 - 0
wise-cost-connect/.eslintrc.json

@@ -3,5 +3,8 @@
   "ignorePatterns": ["node_modules", "dist", "test", "jest.config.js", "typings"],
   "env": {
     "jest": true
+  },
+  "rules": {
+    "prettier/prettier": "off",
   }
 }

+ 2 - 2
wise-cost-connect/package.json

@@ -1,6 +1,6 @@
 {
   "name": "@sc/connect",
-  "version": "1.0.1",
+  "version": "2.0.1",
   "description": "",
   "main": "dist/index.js",
   "typings": "dist/index.d.ts",
@@ -34,5 +34,5 @@
     "ts-jest": "^26.2.0",
     "typescript": "^4.0.0"
   }
- 
+
 }

+ 7 - 1
wise-cost-connect/src/configuration.ts

@@ -16,6 +16,9 @@ export class AutoConfiguration implements ILifeCycle {
   @Config('mongoose')
   mongoDBConfig;
 
+  @Config('dbOptions')
+  dbOptions;
+
   @Config('baseDir')
   baseDir: string;
 
@@ -26,7 +29,10 @@ export class AutoConfiguration implements ILifeCycle {
   logger;
 
   async onReady() {
-    const connect = new MongoDBConnect(this.mongoDBConfig, this.app); //生成连接对像
+    const connect = new MongoDBConnect(
+      { dbOptions: this.dbOptions, ...this.mongoDBConfig },
+      this.app
+    ); //生成连接对像
     this.app.getApplicationContext().registerObject('mongooseDB', connect); //注册到app中
     this.loadModelFile(); //加载model文件
   }

+ 31 - 3
wise-cost-connect/src/connect/mongoDBConnect.ts

@@ -1,6 +1,11 @@
 import { IMidwayApplication } from '@midwayjs/core';
 import { Inject } from '@midwayjs/decorator';
-import mongoose, { Connection, ConnectOptions, Mongoose } from 'mongoose';
+import mongoose, {
+  Connection,
+  ConnectOptions,
+  Mongoose,
+  Schema,
+} from 'mongoose';
 
 export interface IMongooseApp extends IMidwayApplication {
   mongoose: Mongoose;
@@ -13,6 +18,12 @@ export class MongoDBConnect {
 
   private clients: Map<string, Connection>;
 
+  private dbOptions: ConnectOptions;
+
+  dynamicModels: { model: string; schema: Schema }[] = [];
+
+  subDomainMap: Map<string, { dbName: string; url: string }>;
+
   private newConnection(
     name = 'default',
     config: { url: string; options: ConnectOptions }
@@ -38,15 +49,32 @@ export class MongoDBConnect {
     }
   }
 
+  private dynamicConnect(dbName: string, url: string) {
+    //连接新的数据库
+    const conn = mongoose.createConnection(`${url}/${dbName}`, this.dbOptions);
+    this.clients[dbName] = conn;
+    // 注册models
+    for (const dm of this.dynamicModels) {
+      conn.model(dm.model, dm.schema, dm.model);
+    }
+    return conn;
+  }
+
   constructor(config: any, app: IMongooseApp) {
     this.clients = new Map();
+    this.subDomainMap = new Map();
     this.createClients(config);
+    this.dbOptions = config.dbOptions;
     app.mongoose = mongoose;
     app.mongooseDB = this;
   }
 
-  get(dbName: string) {
-    return this.clients[dbName];
+  get(dbName: string, url?: string) {
+    const db = this.clients[dbName];
+    if (db) return db;
+
+    if (!url) throw new Error('数据库链接不能为空');
+    return this.dynamicConnect(dbName, url);
   }
 
   async closeDB() {