Skip to content

Commit

Permalink
fix(core): respect global schema option in first level cache
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Nov 25, 2023
1 parent 90ec766 commit 1833455
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/unit-of-work/UnitOfWork.ts
Expand Up @@ -172,7 +172,7 @@ export class UnitOfWork {
hash = Utils.getPrimaryKeyHash(keys);
}

schema ??= meta.schema;
schema ??= meta.schema ?? this.em.config.get('schema');

if (schema) {
hash = `${schema}:${hash}`;
Expand Down
24 changes: 22 additions & 2 deletions tests/features/multiple-schemas/reloading-entity.postgres.test.ts
@@ -1,4 +1,14 @@
import { Collection, Entity, Ref, ManyToOne, MikroORM, OneToMany, PrimaryKey, Reference } from '@mikro-orm/core';
import {
Collection,
Entity,
Ref,
ManyToOne,
MikroORM,
OneToMany,
PrimaryKey,
Reference,
Property,
} from '@mikro-orm/core';
import { PostgreSqlDriver } from '@mikro-orm/postgresql';

@Entity()
Expand All @@ -7,6 +17,9 @@ export class Customer {
@PrimaryKey()
id!: number;

@Property()
name!: string;

@OneToMany({ entity: 'License', mappedBy: 'customer' })
licenses = new Collection<License>(this);

Expand Down Expand Up @@ -43,14 +56,21 @@ afterAll(() => orm.close(true));

test('entity is retrieved from identity map', async () => {
const customer = new Customer();
customer.name = 'foo';
await orm.em.persistAndFlush(customer);
expect(orm.em.getUnitOfWork().getIdentityMap().keys()).toEqual(['Customer-myschema:1']);

const check1 = await orm.em.findOneOrFail(Customer, customer.id);
expect(orm.em.getUnitOfWork().getIdentityMap().keys()).toEqual(['Customer-myschema:1']);
expect(check1).toBe(customer);
expect(check1.name).toBe('foo');

const check2 = await orm.em.findOneOrFail(Customer, customer.id, { refresh: true });
// simulate change in the database from outside
await orm.em.nativeUpdate(Customer, { id: customer.id }, { name: 'bar' });
const check2 = await orm.em.findOneOrFail(Customer, customer.id, {
refresh: true, // will force reloading the values from database
});
expect(check2.name).toBe('bar');
expect(orm.em.getUnitOfWork().getIdentityMap().keys()).toEqual(['Customer-myschema:1']);
expect(check2).toBe(customer);
});

0 comments on commit 1833455

Please sign in to comment.