Skip to content

Commit

Permalink
feat(core): do not map array types as Loaded when partially loaded
Browse files Browse the repository at this point in the history
Closes #5123
  • Loading branch information
B4nan committed Jan 13, 2024
1 parent b293789 commit 75d035d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/typings.ts
Expand Up @@ -965,7 +965,7 @@ type LoadedLoadable<T, E extends object> =
? LoadedReference<E>
: T extends ScalarReference<infer U>
? LoadedScalarReference<U>
: T extends Scalar | Scalar[]
: T extends Scalar | any[]
? T
: E;

Expand Down
38 changes: 38 additions & 0 deletions tests/features/partial-loading/GH5123.test.ts
@@ -0,0 +1,38 @@
import { Entity, PrimaryKey, Property, JsonType } from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/sqlite';

@Entity()
class A {

@PrimaryKey()
id!: number;

@Property({ type: JsonType })
array!: B[];

}

interface B {
test: string;
}

let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [A],
dbName: ':memory:',
});
await orm.schema.createSchema();
});

test('GH #5123', async () => {
const a = orm.em.create(A, { array: [{ test: 'test' }] });
await orm.em.persistAndFlush(a);

const a1 = await orm.em.fork().findOneOrFail(A, 1);
const a2 = await orm.em.fork().findOneOrFail(A, 1, { fields: ['array'] });

expect(a1.array.filter(i => i)).toEqual([{ test: 'test' }]);
expect(a2.array.filter(i => i)).toEqual([{ test: 'test' }]);
});

0 comments on commit 75d035d

Please sign in to comment.