Commit d0c44370 authored by nanahira's avatar nanahira

fix multi conn conflict

parent 17fbe4af
...@@ -83,6 +83,7 @@ export default class TypeORMPlugin extends BasePlugin<TypeORMPluginConfigLike> { ...@@ -83,6 +83,7 @@ export default class TypeORMPlugin extends BasePlugin<TypeORMPluginConfigLike> {
entities, entities,
entityPrefix: `koishi_${token}_`, entityPrefix: `koishi_${token}_`,
metadataTableName: `koishi_typeorm_metadata_${token}`, metadataTableName: `koishi_typeorm_metadata_${token}`,
name: `koishi_${token}`,
...extraOptions, ...extraOptions,
}; };
const connection = await createConnection(connectionOptions); const connection = await createConnection(connectionOptions);
......
...@@ -33,6 +33,14 @@ class Book { ...@@ -33,6 +33,14 @@ class Book {
author: User; author: User;
} }
@Entity()
class Magic {
@PrimaryGeneratedColumn()
id: number;
@Column('varchar', { length: 32 })
name: string;
}
describe('Koishi typeorm', () => { describe('Koishi typeorm', () => {
let app: App; let app: App;
...@@ -46,7 +54,7 @@ describe('Koishi typeorm', () => { ...@@ -46,7 +54,7 @@ describe('Koishi typeorm', () => {
username: 'koishi', username: 'koishi',
password: 'koishi@114514', password: 'koishi@114514',
database: 'koishi', database: 'koishi',
dropSchema: true, //dropSchema: true,
}); });
}); });
...@@ -67,12 +75,27 @@ describe('Koishi typeorm', () => { ...@@ -67,12 +75,27 @@ describe('Koishi typeorm', () => {
expect(gotUser.name).toBe('Shigma'); expect(gotUser.name).toBe('Shigma');
}); });
it('should able to open more than 1 connection', async () => {
await app.typeorm.create('fooo', [User, Book]);
await app.typeorm.create('barr', [Magic]);
const connection1 = app.typeorm.getConnection('fooo');
const connection2 = app.typeorm.getConnection('barr');
expect(connection1).toBeInstanceOf(Connection);
expect(connection2).toBeInstanceOf(Connection);
const userRepo = app.typeorm.getRepository(User);
const magicRepo = app.typeorm.getRepository(Magic);
expect(userRepo).toBeInstanceOf(Repository);
expect(magicRepo).toBeInstanceOf(Repository);
expect(userRepo.manager).toEqual(connection1.manager);
expect(magicRepo.manager).toEqual(connection2.manager);
});
it('should connect with custom options', async () => { it('should connect with custom options', async () => {
await app.typeorm.create('foo', [User, Book], { await app.typeorm.create('baz', [User, Book], {
entityPrefix: 'fooooo_', entityPrefix: 'fooooo_',
metadataTableName: 'fooooo_metadata', metadataTableName: 'fooooo_metadata',
}); });
const connection = app.typeorm.getConnection('foo'); const connection = app.typeorm.getConnection('baz');
expect(connection.metadataTableName).toBe('fooooo_metadata'); expect(connection.metadataTableName).toBe('fooooo_metadata');
}); });
...@@ -97,4 +120,8 @@ describe('Koishi typeorm', () => { ...@@ -97,4 +120,8 @@ describe('Koishi typeorm', () => {
expect(app.typeorm.getConnection('bar')).toBeUndefined(); expect(app.typeorm.getConnection('bar')).toBeUndefined();
expect(app.typeorm.getRepository(User)).toBeUndefined(); expect(app.typeorm.getRepository(User)).toBeUndefined();
}); });
afterEach(async () => {
await app.stop();
});
}); });
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