Skip to content

Commit

Permalink
fix(core): fix partial loading of embedded properties with joined str…
Browse files Browse the repository at this point in the history
…ategy
  • Loading branch information
B4nan committed Nov 2, 2023
1 parent 5e2d04b commit f887e77
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/knex/src/AbstractSqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ export abstract class AbstractSqlDriver<Connection extends AbstractSqlConnection
joinedProps<T>(meta: EntityMetadata, populate: PopulateOptions<T>[]): PopulateOptions<T>[] {
return populate.filter(p => {
const prop = meta.properties[p.field] || {};
return (p.strategy || prop.strategy || this.config.get('loadStrategy')) === LoadStrategy.JOINED && prop.reference !== ReferenceType.SCALAR;
return (p.strategy || prop.strategy || this.config.get('loadStrategy')) === LoadStrategy.JOINED && prop.reference !== ReferenceType.SCALAR && prop.reference !== ReferenceType.EMBEDDED;
});
}

Expand Down
19 changes: 19 additions & 0 deletions tests/features/embeddables/embedded-entities.postgres.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Embedded,
Entity,
expr,
LoadStrategy,
ManyToOne,
MikroORM,
PrimaryKey,
Expand Down Expand Up @@ -298,9 +299,27 @@ describe('embedded entities in postgresql', () => {
await orm.em.fork().find(User, {}, { fields: ['address2'] });
await orm.em.fork().find(User, {}, { fields: [{ address2: ['street', 'city'] }] });
await orm.em.fork().find(User, {}, { fields: ['address2.street', 'address2.city'] });
await orm.em.fork().find(User, {}, { fields: ['addresses'] });
expect(mock.mock.calls[0][0]).toMatch('select "u0"."id", "u0"."addr_street", "u0"."addr_postal_code", "u0"."addr_city", "u0"."addr_country" from "user" as "u0"');
expect(mock.mock.calls[1][0]).toMatch('select "u0"."id", "u0"."addr_street", "u0"."addr_city" from "user" as "u0"');
expect(mock.mock.calls[2][0]).toMatch('select "u0"."id", "u0"."addr_street", "u0"."addr_city" from "user" as "u0"');
expect(mock.mock.calls[3][0]).toMatch('select "u0"."id", "u0"."addresses" from "user" as "u0"');
});

test('partial loading (joined strategy)', async () => {
const user = createUser();
await orm.em.persistAndFlush(user);
orm.em.clear();

const mock = mockLogger(orm, ['query']);
await orm.em.fork().find(User, {}, { fields: ['address2'], strategy: LoadStrategy.JOINED });
await orm.em.fork().find(User, {}, { fields: [{ address2: ['street', 'city'] }], strategy: LoadStrategy.JOINED });
await orm.em.fork().find(User, {}, { fields: ['address2.street', 'address2.city'], strategy: LoadStrategy.JOINED });
await orm.em.fork().find(User, {}, { fields: ['addresses'], strategy: LoadStrategy.JOINED });
expect(mock.mock.calls[0][0]).toMatch('select "u0"."id", "u0"."addr_street", "u0"."addr_postal_code", "u0"."addr_city", "u0"."addr_country" from "user" as "u0"');
expect(mock.mock.calls[1][0]).toMatch('select "u0"."id", "u0"."addr_street", "u0"."addr_city" from "user" as "u0"');
expect(mock.mock.calls[2][0]).toMatch('select "u0"."id", "u0"."addr_street", "u0"."addr_city" from "user" as "u0"');
expect(mock.mock.calls[3][0]).toMatch('select "u0"."id", "u0"."addresses" from "user" as "u0"');
});

test('partial loading 2', async () => {
Expand Down

0 comments on commit f887e77

Please sign in to comment.