undeexcepturi
Version:
TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.
125 lines (93 loc) • 3.1 kB
text/typescript
import {
MikroORM,
Entity,
PrimaryKey,
Unique,
Collection,
ManyToOne,
OneToMany,
Property,
Filter,
LoadStrategy,
OptionalProps,
PrimaryKeyProp,
} from '@mikro-orm/sqlite';
class UserEntity {
id!: number;
name!: string;
email!: string;
items = new Collection<UserTenantEntity>(this);
}
class TenantEntity {
[OptionalProps]?: 'isEnabled';
id!: number;
name!: string;
schema!: string;
isEnabled: boolean = true;
items = new Collection<UserTenantEntity>(this);
}
class UserTenantEntity {
user!: UserEntity;
tenant!: TenantEntity;
[PrimaryKeyProp]?: ['user', 'tenant'];
[OptionalProps]?: 'isActive';
isActive: boolean = true;
}
describe('GH issue 1902', () => {
let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
entities: [UserEntity, TenantEntity, UserTenantEntity],
dbName: ':memory:',
});
await orm.schema.createSchema();
});
afterAll(async () => {
await orm.close(true);
});
test(`GH issue 1902`, async () => {
const user = orm.em.create(UserEntity, { name: 'user one', email: 'one@email' });
await orm.em.flush();
const tenant1 = orm.em.create(TenantEntity, { name: 'tenant one', schema: 'tenant_one' });
await orm.em.flush();
const tenant2 = orm.em.create(TenantEntity, { name: 'tenant two', schema: 'tenant_two' });
await orm.em.flush();
const repoUserTenant = orm.em.getRepository(UserTenantEntity);
orm.em.create(UserTenantEntity, { user, tenant: tenant1, isActive: true });
await orm.em.flush();
orm.em.create(UserTenantEntity, { user, tenant: tenant2, isActive: false });
await orm.em.flush();
orm.em.clear();
const findOpts = {
filters: {
byUser: { id: 1 },
},
populate: ['tenant'] as const,
};
const f1 = await repoUserTenant.findAll(findOpts);
expect(f1.length).toBe(2); // succeeds
orm.em.clear();
const f2 = await repoUserTenant.findAll({ ...findOpts, strategy: LoadStrategy.JOINED });
expect(f2.length).toBe(2); // fails
return;
});
});