Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v6 - using populate true in find returns wrong reference type #4920

Closed
boopeshmahendran227 opened this issue Nov 12, 2023 · 2 comments · Fixed by #4927
Closed

v6 - using populate true in find returns wrong reference type #4920

boopeshmahendran227 opened this issue Nov 12, 2023 · 2 comments · Fixed by #4927

Comments

@boopeshmahendran227
Copy link

boopeshmahendran227 commented Nov 12, 2023

Describe the bug
using populate true in find returns wrong reference type

export class User extends BaseEntity {
  @OneToOne(() => JobSeeker, (e) => e.user, { nullable: true, ref: true })
  jobSeeker!: Ref<JobSeeker> | null;
}

const user = await this.em.findOneOrFail(User, userId, {
  populate: true,
});
if (!user.jobSeeker) {
  throw new BadRequestException('JobSeeker not found');
}
const x = user.jobSeeker.$.city;

Error:
Property '$' does not exist on type '{} & Reference'

But when i use populate: ['jobSeeker'] it works

@B4nan
Copy link
Member

B4nan commented Nov 13, 2023

This is actually pretty hard to fix (spent the whole afternoon on this yesterday and it's still not done) due to the populate hint being string | boolean instead of just string, it breaks the inference heavily.

I wonder if it would be better to remove the boolean signature in favor of populate: '*', which is what the partial loading hint is using anyway.

edit: we could even keep it as is so its not breaking, but only respect populate: '*' in the Loaded type

@B4nan
Copy link
Member

B4nan commented Nov 14, 2023

I will end up with populate: ['*'], that's the only way I can keep all the connected types working correctly, at least without adding a lot of method overrides.

B4nan added a commit that referenced this issue Nov 14, 2023
When populating all relations, the `Loaded` type will now respect `*` in the populate hint. The old boolean variant is now removed in favor of new `populate: ['*']`.

> This also applies to the `serialize()` helper and its `populate` parameter.

```diff
const users = await em.find(User, {}, {
-  populate: true,
+  populate: ['*'],
});
```

`populate: false` is still allowed and serves as a way to disable eager loaded properties.

Closes #4920
B4nan added a commit that referenced this issue Nov 14, 2023
When populating all relations, the `Loaded` type will now respect `*` in the populate hint. The old boolean variant is now removed in favor of new `populate: ['*']`.

> This also applies to the `serialize()` helper and its `populate` parameter.

```diff
const users = await em.find(User, {}, {
-  populate: true,
+  populate: ['*'],
});
```

`populate: false` is still allowed and serves as a way to disable eager loaded properties.

Closes #4920
B4nan added a commit that referenced this issue Nov 14, 2023
When populating all relations, the `Loaded` type will now respect `*` in the populate hint. The old boolean variant is now removed in favor of new `populate: ['*']`.

> This also applies to the `serialize()` helper and its `populate` parameter.

```diff
const users = await em.find(User, {}, {
-  populate: true,
+  populate: ['*'],
});
```

`populate: false` is still allowed and serves as a way to disable eager loaded properties.

Closes #4920
B4nan added a commit that referenced this issue Nov 14, 2023
When populating all relations, the `Loaded` type will now respect `*` in the populate hint. The old boolean variant is now removed in favor of new `populate: ['*']`.

> This also applies to the `serialize()` helper and its `populate` parameter.

```diff
const users = await em.find(User, {}, {
-  populate: true,
+  populate: ['*'],
});
```

`populate: false` is still allowed and serves as a way to disable eager loaded properties.

Closes #4920
B4nan added a commit that referenced this issue Nov 14, 2023
When populating all relations, the `Loaded` type will now respect `*` in
the populate hint. The old boolean variant is now removed in favor of
new `populate: ['*']`.

> This also applies to the `serialize()` helper and its `populate`
parameter.

```diff
const users = await em.find(User, {}, {
-  populate: true,
+  populate: ['*'],
});
```

`populate: false` is still allowed and serves as a way to disable eager
loaded properties.

Closes #4920
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants