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> {
entities: TypeORMEntity[],
extraOptions: Partial<ConnectionOptions> = {},
) {
if (this.registryMap.has(token)) {
return this.getConnection(token);
}
const ctx = this.caller;
ctx.on('dispose', () => this.close(token));
......@@ -86,7 +89,7 @@ export default class TypeORMPlugin extends BasePlugin<TypeORMPluginConfigLike> {
name: `koishi_${token}`,
...extraOptions,
};
const connection = await createConnection(connectionOptions);
const connection = await new Connection(connectionOptions).connect();
this.registryMap.set(token, { connection, entities });
for (const entity of entities) {
this.entityMap.set(entity, token);
......
......@@ -53,7 +53,7 @@ describe('Koishi typeorm', () => {
port: 5432,
username: 'koishi',
password: 'koishi@114514',
database: 'koishi',
database: 'test-koishi',
//dropSchema: true,
});
});
......@@ -121,7 +121,12 @@ describe('Koishi typeorm', () => {
expect(app.typeorm.getRepository(User)).toBeUndefined();
});
afterEach(async () => {
await app.stop();
it('should reload the same connection', async () => {
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