From e0ea1b7fa0f175a750c2c1b59f234f5954c8cc77 Mon Sep 17 00:00:00 2001 From: killagu Date: Wed, 20 Mar 2024 23:03:39 +0800 Subject: [PATCH] f --- plugin/dal/lib/DalModuleLoadUnitHook.ts | 6 +-- plugin/dal/lib/MysqlDataSourceManager.ts | 53 +++++++++++++++---- .../apps/dal-app/modules/dal/module.yml | 1 + 3 files changed, 46 insertions(+), 14 deletions(-) diff --git a/plugin/dal/lib/DalModuleLoadUnitHook.ts b/plugin/dal/lib/DalModuleLoadUnitHook.ts index 27f1953f..cdc17d67 100644 --- a/plugin/dal/lib/DalModuleLoadUnitHook.ts +++ b/plugin/dal/lib/DalModuleLoadUnitHook.ts @@ -1,7 +1,7 @@ import { MysqlDataSourceManager } from './MysqlDataSourceManager'; import { LifecycleHook } from '@eggjs/tegg-lifecycle'; import { ModuleConfigHolder } from '@eggjs/tegg-common-util'; -import { DataSourceOptions, MysqlDataSource } from '@eggjs/dal-runtime'; +import { DataSourceOptions } from '@eggjs/dal-runtime'; import { LoadUnit, LoadUnitLifecycleContext } from '@eggjs/tegg/helper'; export class DalModuleLoadUnitHook implements LifecycleHook { @@ -17,12 +17,10 @@ export class DalModuleLoadUnitHook implements LifecycleHook | undefined = (moduleConfigHolder.config as any).dataSource; if (!dataSourceConfig) return; await Promise.all(Object.entries(dataSourceConfig).map(async ([ name, config ]) => { - const dataSource = new MysqlDataSource({ + await MysqlDataSourceManager.instance.createDataSource(loadUnit.name, name, { ...config, name, }); - await dataSource.ready(); - MysqlDataSourceManager.instance.set(loadUnit.name, dataSource); })); } } diff --git a/plugin/dal/lib/MysqlDataSourceManager.ts b/plugin/dal/lib/MysqlDataSourceManager.ts index 26f2e054..9880ac99 100644 --- a/plugin/dal/lib/MysqlDataSourceManager.ts +++ b/plugin/dal/lib/MysqlDataSourceManager.ts @@ -1,28 +1,61 @@ -import { MysqlDataSource } from '@eggjs/dal-runtime'; +import { DataSourceOptions, MysqlDataSource } from '@eggjs/dal-runtime'; +import crypto from 'node:crypto'; export class MysqlDataSourceManager { static instance = new MysqlDataSourceManager(); - readonly dataSources: Map>; + private readonly dataSourceIndices: Map>; + private readonly dataSources: Map; constructor() { + this.dataSourceIndices = new Map(); this.dataSources = new Map(); } get(moduleName: string, dataSourceName: string): MysqlDataSource | undefined { - return this.dataSources.get(moduleName)?.get(dataSourceName); + const dataSourceIndex = this.dataSourceIndices.get(moduleName) + ?.get(dataSourceName); + if (dataSourceIndex) { + return this.dataSources.get(dataSourceIndex); + } } - set(moduleName: string, mysqlDataSource: MysqlDataSource) { - let dataSources = this.dataSources.get(moduleName); - if (!dataSources) { - dataSources = new Map(); - this.dataSources.set(moduleName, dataSources); + async createDataSource(moduleName: string, dataSourceName: string, config: DataSourceOptions) { + const dataSourceConfig = { + ...config, + name: dataSourceName, + }; + const index = MysqlDataSourceManager.createDataSourceKey(dataSourceConfig); + let dataSource = this.dataSources.get(index); + if (!dataSource) { + dataSource = new MysqlDataSource(dataSourceConfig); + this.dataSources.set(index, dataSource); + } + let moduledataSourceIndices = this.dataSourceIndices.get(moduleName); + if (!moduledataSourceIndices) { + moduledataSourceIndices = new Map(); + this.dataSourceIndices.set(moduleName, moduledataSourceIndices); } - dataSources.set(mysqlDataSource.name, mysqlDataSource); + moduledataSourceIndices.set(dataSourceName, index); + + await dataSource.ready(); } clear() { - this.dataSources.clear(); + this.dataSourceIndices.clear(); + } + + static createDataSourceKey(dataSourceOptions: DataSourceOptions): string { + const hash = crypto.createHash('md5'); + const keys = Object.keys(dataSourceOptions) + .sort(); + for (const key of keys) { + const value = dataSourceOptions[key]; + if (value) { + hash.update(key); + hash.update(String(value)); + } + } + return hash.digest('hex'); } } diff --git a/plugin/dal/test/fixtures/apps/dal-app/modules/dal/module.yml b/plugin/dal/test/fixtures/apps/dal-app/modules/dal/module.yml index 736c48e0..191f15f1 100644 --- a/plugin/dal/test/fixtures/apps/dal-app/modules/dal/module.yml +++ b/plugin/dal/test/fixtures/apps/dal-app/modules/dal/module.yml @@ -5,3 +5,4 @@ dataSource: host: '127.0.0.1' user: root port: 3306 + timezone: '+08:00'