-
-
Notifications
You must be signed in to change notification settings - Fork 526
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sql): support uuid like PKs in M:N references (#272)
Closes #268
- Loading branch information
Showing
2 changed files
with
76 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { unlinkSync } from 'fs'; | ||
import { v4 } from 'uuid'; | ||
|
||
import { Collection, Entity, ManyToMany, MikroORM, PrimaryKey, Property, ReflectMetadataProvider, UuidEntity } from '../../lib'; | ||
import { BASE_DIR } from '../bootstrap'; | ||
import { SqliteDriver } from '../../lib/drivers/SqliteDriver'; | ||
|
||
@Entity() | ||
export class A implements UuidEntity<A> { | ||
|
||
@PrimaryKey() | ||
uuid: string = v4(); | ||
|
||
@Property() | ||
name!: string; | ||
|
||
@ManyToMany(() => B, b => b.aCollection) | ||
bCollection = new Collection<B>(this); | ||
|
||
} | ||
|
||
@Entity() | ||
export class B implements UuidEntity<B> { | ||
|
||
@PrimaryKey() | ||
uuid: string = v4(); | ||
|
||
@Property() | ||
name!: string; | ||
|
||
@ManyToMany(() => A, undefined, { fixedOrder: true }) | ||
aCollection = new Collection<A>(this); | ||
|
||
} | ||
|
||
describe('GH issue 268', () => { | ||
|
||
let orm: MikroORM<SqliteDriver>; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [A, B], | ||
dbName: BASE_DIR + '/../temp/mikro_orm_test_gh268.db', | ||
debug: false, | ||
highlight: false, | ||
type: 'sqlite', | ||
metadataProvider: ReflectMetadataProvider, | ||
cache: { enabled: false }, | ||
}); | ||
await orm.getSchemaGenerator().dropSchema(); | ||
await orm.getSchemaGenerator().createSchema(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await orm.close(true); | ||
unlinkSync(orm.config.get('dbName')); | ||
}); | ||
|
||
test('m:n with uuid PKs', async () => { | ||
const a1 = new A(); | ||
a1.name = 'a1'; | ||
const a2 = new A(); | ||
a2.name = 'a2'; | ||
const a3 = new A(); | ||
a3.name = 'a3'; | ||
const b = new B(); | ||
b.name = 'b'; | ||
b.aCollection.add(a1, a2, a3); | ||
await orm.em.persistAndFlush(b); | ||
|
||
const res = await orm.em.getConnection().execute('select * from b_to_a'); | ||
expect(res[0]).toEqual({ id: 1, a_uuid: a1.uuid, b_uuid: b.uuid }); | ||
}); | ||
|
||
}); |