Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
killagu committed Mar 20, 2024
1 parent b96c094 commit e0ea1b7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
6 changes: 2 additions & 4 deletions plugin/dal/lib/DalModuleLoadUnitHook.ts
Original file line number Diff line number Diff line change
@@ -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<LoadUnitLifecycleContext, LoadUnit> {
Expand All @@ -17,12 +17,10 @@ export class DalModuleLoadUnitHook implements LifecycleHook<LoadUnitLifecycleCon
const dataSourceConfig: Record<string, DataSourceOptions> | 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);
}));
}
}
53 changes: 43 additions & 10 deletions plugin/dal/lib/MysqlDataSourceManager.ts
Original file line number Diff line number Diff line change
@@ -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<string, Map<string, MysqlDataSource>>;
private readonly dataSourceIndices: Map<string/* moduleName */, Map<string/* dataSourceName */, string/* dataSourceIndex */>>;
private readonly dataSources: Map<string/* dataSourceIndex */, MysqlDataSource>;

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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ dataSource:
host: '127.0.0.1'
user: root
port: 3306
timezone: '+08:00'

0 comments on commit e0ea1b7

Please sign in to comment.