Commit 1d5006bf authored by nanahira's avatar nanahira

dont use inbuilt connection manager

parent b879566e
...@@ -73,6 +73,9 @@ export default class TypeORMPlugin extends BasePlugin<TypeORMPluginConfigLike> { ...@@ -73,6 +73,9 @@ export default class TypeORMPlugin extends BasePlugin<TypeORMPluginConfigLike> {
entities: TypeORMEntity[], entities: TypeORMEntity[],
extraOptions: Partial<ConnectionOptions> = {}, extraOptions: Partial<ConnectionOptions> = {},
) { ) {
if (this.registryMap.has(token)) {
return this.getConnection(token);
}
const ctx = this.caller; const ctx = this.caller;
ctx.on('dispose', () => this.close(token)); ctx.on('dispose', () => this.close(token));
...@@ -86,7 +89,7 @@ export default class TypeORMPlugin extends BasePlugin<TypeORMPluginConfigLike> { ...@@ -86,7 +89,7 @@ export default class TypeORMPlugin extends BasePlugin<TypeORMPluginConfigLike> {
name: `koishi_${token}`, name: `koishi_${token}`,
...extraOptions, ...extraOptions,
}; };
const connection = await createConnection(connectionOptions); const connection = await new Connection(connectionOptions).connect();
this.registryMap.set(token, { connection, entities }); this.registryMap.set(token, { connection, entities });
for (const entity of entities) { for (const entity of entities) {
this.entityMap.set(entity, token); this.entityMap.set(entity, token);
......
...@@ -53,7 +53,7 @@ describe('Koishi typeorm', () => { ...@@ -53,7 +53,7 @@ describe('Koishi typeorm', () => {
port: 5432, port: 5432,
username: 'koishi', username: 'koishi',
password: 'koishi@114514', password: 'koishi@114514',
database: 'koishi', database: 'test-koishi',
//dropSchema: true, //dropSchema: true,
}); });
}); });
...@@ -121,7 +121,12 @@ describe('Koishi typeorm', () => { ...@@ -121,7 +121,12 @@ describe('Koishi typeorm', () => {
expect(app.typeorm.getRepository(User)).toBeUndefined(); expect(app.typeorm.getRepository(User)).toBeUndefined();
}); });
afterEach(async () => { it('should reload the same connection', async () => {
await app.stop(); await app.typeorm.create('yuzu', [User, Book]);
expect(app.typeorm.getConnection('yuzu')).toBeInstanceOf(Connection);
await app.typeorm.close('yuzu');
expect(app.typeorm.getConnection('yuzu')).toBeUndefined();
await app.typeorm.create('yuzu', [User, Book]);
expect(app.typeorm.getConnection('yuzu')).toBeInstanceOf(Connection);
}); });
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment