Skip to content

Commit

Permalink
feat(core): createInstance() accepts 3rd param options
Browse files Browse the repository at this point in the history
```ts
interface CreateInstanceOptions {
  /**
   * @default true
   */
  cacheInstance?: boolean | undefined;
}
```
  • Loading branch information
waitingsong committed Aug 9, 2022
1 parent 5aad947 commit 05fe7e1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 35 deletions.
24 changes: 17 additions & 7 deletions packages/core/src/common/dataSourceManager.ts
Expand Up @@ -38,7 +38,10 @@ export abstract class DataSourceManager<T> {
dataSourceOptions['entities'] = Array.from(entities);
}
// create data source
await this.createInstance(dataSourceOptions, dataSourceName);
const opts: CreateInstanceOptions = {
cacheInstance: options.cacheInstance, // will default true
};
await this.createInstance(dataSourceOptions, dataSourceName, opts);
}
} else {
throw new MidwayParameterError(
Expand Down Expand Up @@ -76,14 +79,17 @@ export abstract class DataSourceManager<T> {
}

public async createInstance(
config,
clientName,
cacheInstance = true
config: any,
clientName: any,
options?: CreateInstanceOptions
): Promise<T | void> {
const { cacheInstance } = options;
const cache = cacheInstance ?? true;

// options.default will be merge in to options.clients[id]
config = extend(true, {}, this.options['default'], config);
const client = await this.createDataSource(config, clientName);
if (cacheInstance && clientName && client) {
const configNow = extend(true, {}, this.options['default'], config);
const client = await this.createDataSource(configNow, clientName);
if (cache && clientName && client) {
this.dataSource.set(clientName, client);
}
return client;
Expand Down Expand Up @@ -139,3 +145,7 @@ export function globModels(globString: string, appDir: string) {
}
return models;
}

export interface CreateInstanceOptions {
cacheInstance?: boolean | undefined;
}
84 changes: 56 additions & 28 deletions packages/core/test/common/dataSourceManager.test.ts
Expand Up @@ -110,25 +110,51 @@ describe('test/common/dataSourceManager.test.ts', () => {
expect(instance.getDataSource('test')).toMatchSnapshot();
});

it('should createInstance() with cacheInstance: default true', async () => {
it('should createInstance() without cacheInstance (default true)', async () => {
class EntityA {}

const instance = new CustomDataSourceFactory();
expect(instance.getName()).toEqual('test');

const name ='test'
await instance.createInstance({
host: 'localhost', //数据库地址,默认本机
port:'3306',
dialect: 'mysql',
pool: { //连接池设置
max: 5, //最大连接数
min: 0, //最小连接数
idle: 10000
},
entities: [EntityA, EntityA, '/abc']
}, name)
expect(instance.getDataSourceNames()).toEqual([name]);

const clientName ='test'
const config = {
host: 'localhost', //数据库地址,默认本机
port: '3306',
dialect: 'mysql',
pool: { //连接池设置
max: 5, //最大连接数
min: 0, //最小连接数
idle: 10000
},
entities: [EntityA, EntityA, '/abc']
}

await instance.createInstance(config, clientName)
expect(instance.getDataSourceNames()).toEqual([clientName]);
});

it('should createInstance() with cacheInstance: true', async () => {
class EntityA {}

const instance = new CustomDataSourceFactory();
expect(instance.getName()).toEqual('test');

const clientName ='test'
const config = {
host: 'localhost', //数据库地址,默认本机
port: '3306',
dialect: 'mysql',
pool: { //连接池设置
max: 5, //最大连接数
min: 0, //最小连接数
idle: 10000
},
entities: [EntityA, EntityA, '/abc']
}

await instance.createInstance(config, clientName, { cacheInstance: true })
expect(instance.getDataSourceNames()).toEqual([clientName]);
});

it('should createInstance() with cacheInstance: false', async () => {
Expand All @@ -137,20 +163,22 @@ describe('test/common/dataSourceManager.test.ts', () => {
const instance = new CustomDataSourceFactory();
expect(instance.getName()).toEqual('test');

const name ='test'
await instance.createInstance({
host: 'localhost', //数据库地址,默认本机
port:'3306',
dialect: 'mysql',
pool: { //连接池设置
max: 5, //最大连接数
min: 0, //最小连接数
idle: 10000
},
entities: [EntityA, EntityA, '/abc']
}, name, false)
expect(instance.getDataSourceNames()).not.toEqual([name]);
expect(instance.getDataSource(name)).toBeUndefined();
const clientName ='test'
const config = {
host: 'localhost', //数据库地址,默认本机
port: '3306',
dialect: 'mysql',
pool: { //连接池设置
max: 5, //最大连接数
min: 0, //最小连接数
idle: 10000
},
entities: [EntityA, EntityA, '/abc']
}

await instance.createInstance(config, clientName, { cacheInstance: false })
expect(instance.getDataSourceNames()).not.toEqual([clientName]);
expect(instance.getDataSource(clientName)).toBeUndefined();
});

it('should test will got error when no data source', async () => {
Expand Down

0 comments on commit 05fe7e1

Please sign in to comment.