-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
46 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ dataSource: | |
host: '127.0.0.1' | ||
user: root | ||
port: 3306 | ||
timezone: '+08:00' |