|
| 1 | +import { Collection, Entity, ManyToMany, ManyToOne, MikroORM, OneToMany, PrimaryKey, Property, Reference, t } from '@mikro-orm/core'; |
| 2 | +import { mockLogger } from '../../helpers'; |
| 3 | + |
| 4 | +@Entity({ schema: '*' }) |
| 5 | +class UserAccessProfile { |
| 6 | + |
| 7 | + @PrimaryKey() |
| 8 | + id!: number; |
| 9 | + |
| 10 | + // eslint-disable-next-line @typescript-eslint/no-use-before-define |
| 11 | + @OneToMany(() => User, user => user.accessProfile) |
| 12 | + users = new Collection<User>(this); |
| 13 | + |
| 14 | + // eslint-disable-next-line @typescript-eslint/no-use-before-define |
| 15 | + @ManyToMany({ entity: () => Permission, pivotEntity: () => AccessProfilePermission }) |
| 16 | + permissions = new Collection<Permission>(this); |
| 17 | + |
| 18 | +} |
| 19 | + |
| 20 | +@Entity({ schema: '*' }) |
| 21 | +class User { |
| 22 | + |
| 23 | + @PrimaryKey() |
| 24 | + id!: number; |
| 25 | + |
| 26 | + // eslint-disable-next-line @typescript-eslint/no-use-before-define |
| 27 | + @ManyToOne(() => UserAccessProfile) |
| 28 | + accessProfile!: UserAccessProfile; |
| 29 | + |
| 30 | +} |
| 31 | + |
| 32 | +@Entity({ schema: 'public' }) |
| 33 | +class Permission { |
| 34 | + |
| 35 | + @PrimaryKey() |
| 36 | + id!: number; |
| 37 | + |
| 38 | + // eslint-disable-next-line @typescript-eslint/no-use-before-define |
| 39 | + @ManyToMany({ entity: () => UserAccessProfile, mappedBy: p => p.permissions }) |
| 40 | + accessProfiles = new Collection<UserAccessProfile>(this); |
| 41 | + |
| 42 | +} |
| 43 | + |
| 44 | +@Entity({ schema: '*' }) |
| 45 | +class AccessProfilePermission { |
| 46 | + |
| 47 | + @ManyToOne(() => UserAccessProfile, { primary: true }) |
| 48 | + accessProfile!: UserAccessProfile; |
| 49 | + |
| 50 | + @ManyToOne(() => Permission, { primary: true }) |
| 51 | + permission!: Permission; |
| 52 | + |
| 53 | +} |
| 54 | + |
| 55 | +let orm: MikroORM; |
| 56 | + |
| 57 | +beforeAll(async () => { |
| 58 | + orm = await MikroORM.init({ |
| 59 | + entities: [User], |
| 60 | + dbName: 'mikro_orm_test_3177', |
| 61 | + type: 'postgresql', |
| 62 | + schema: 'tenant_01', |
| 63 | + }); |
| 64 | + await orm.getSchemaGenerator().ensureDatabase(); |
| 65 | + await orm.getSchemaGenerator().dropSchema(); |
| 66 | + await orm.getSchemaGenerator().dropSchema({ schema: 'tenant_01' }); |
| 67 | + await orm.getSchemaGenerator().updateSchema(); |
| 68 | + await orm.getSchemaGenerator().updateSchema({ schema: 'tenant_01' }); |
| 69 | +}); |
| 70 | + |
| 71 | +afterAll(async () => { |
| 72 | + await orm.close(true); |
| 73 | +}); |
| 74 | + |
| 75 | +test(`GH issue 3177`, async () => { |
| 76 | + const user = new User(); |
| 77 | + user.id = 1; |
| 78 | + user.accessProfile = new UserAccessProfile(); |
| 79 | + user.accessProfile.permissions.add(new Permission(), new Permission(), new Permission()); |
| 80 | + orm.em.persist(user); |
| 81 | + |
| 82 | + const mock = mockLogger(orm); |
| 83 | + |
| 84 | + const u1 = await orm.em.findOneOrFail(User, 1, { populate: ['accessProfile'] }); |
| 85 | + const permissions = await u1.accessProfile.permissions.init(); |
| 86 | + |
| 87 | + expect(permissions.getItems()).toHaveLength(3); |
| 88 | + orm.em.clear(); |
| 89 | + |
| 90 | + const u2 = await orm.em.findOneOrFail(User, 1, { populate: ['accessProfile.permissions'] }); |
| 91 | + expect(u2.accessProfile.permissions.getItems()).toHaveLength(3); |
| 92 | + |
| 93 | + expect(mock.mock.calls[0][0]).toMatch(`begin`); |
| 94 | + expect(mock.mock.calls[1][0]).toMatch(`insert into "tenant_01"."user_access_profile" default values returning "id"`); |
| 95 | + expect(mock.mock.calls[2][0]).toMatch(`insert into "tenant_01"."user" ("access_profile_id", "id") values (1, 1) returning "id"`); |
| 96 | + expect(mock.mock.calls[3][0]).toMatch(`insert into "public"."permission" ("id") values (default), (default), (default) returning "id"`); |
| 97 | + expect(mock.mock.calls[4][0]).toMatch(`insert into "tenant_01"."user_access_profile_permissions" ("user_access_profile_id", "permission_id") values (1, 1), (1, 2), (1, 3) returning "user_access_profile_id", "permission_id"`); |
| 98 | + expect(mock.mock.calls[5][0]).toMatch(`commit`); |
| 99 | + expect(mock.mock.calls[6][0]).toMatch(`select "u0".* from "tenant_01"."user" as "u0" where "u0"."id" = 1 limit 1`); |
| 100 | + expect(mock.mock.calls[7][0]).toMatch(`select "p0".* from "public"."permission" as "p0" where "p0"."id" in (1, 2, 3)`); |
| 101 | + expect(mock.mock.calls[8][0]).toMatch(`select "u0".* from "tenant_01"."user" as "u0" where "u0"."id" = 1 limit 1`); |
| 102 | + expect(mock.mock.calls[9][0]).toMatch(`select "u0".* from "tenant_01"."user_access_profile" as "u0" where "u0"."id" in (1) order by "u0"."id" asc`); |
| 103 | + expect(mock.mock.calls[10][0]).toMatch(`select "p0".*, "u1"."permission_id" as "fk__permission_id", "u1"."user_access_profile_id" as "fk__user_access_profile_id" from "public"."permission" as "p0" left join "tenant_01"."user_access_profile_permissions" as "u1" on "p0"."id" = "u1"."permission_id" where "u1"."user_access_profile_id" in (1)`); |
| 104 | +}); |
0 commit comments