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

feat(core): allow populating collections with references #4776

Merged
merged 1 commit into from
Oct 2, 2023
Merged

Conversation

B4nan
Copy link
Member

@B4nan B4nan commented Oct 2, 2023

Sometimes we might want to know only what items are part of a collection, and we don't care about the values of those items. For this, we can populate the collection only with references:

const book1 = await em.findOne(Book, 1, { populate: ['tags:ref'] });
console.log(book1.tags.isInitialized()); // true
console.log(wrap(book1.tags[0]).isInitialized()); // false

// or alternatively use `init({ ref: true })`
const book2 = await em.findOne(Book, 1);
await book2.tags.init({ ref: true });
console.log(book2.tags.isInitialized()); // true
console.log(wrap(book2.tags[0]).isInitialized()); // false

Closes #1158

@B4nan B4nan mentioned this pull request Sep 30, 2023
22 tasks
Sometimes we might want to know only what items are part of a collection, and we don't care about the values of those items. For this, we can populate the collection only with references:

```ts
const book1 = await em.findOne(Book, 1, { populate: ['tags:ref'] });
console.log(book1.tags.isInitialized()); // true
console.log(wrap(book1.tags[0]).isInitialized()); // false

// or alternatively use `init({ ref: true })`
const book2 = await em.findOne(Book, 1);
await book2.tags.init({ ref: true });
console.log(book2.tags.isInitialized()); // true
console.log(wrap(book2.tags[0]).isInitialized()); // false
```

Closes #1158
@codecov
Copy link

codecov bot commented Oct 2, 2023

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Files Coverage Δ
packages/core/src/EntityManager.ts 97.42% <100.00%> (+0.01%) ⬆️
packages/core/src/drivers/DatabaseDriver.ts 99.43% <100.00%> (-0.02%) ⬇️
packages/core/src/drivers/IDatabaseDriver.ts 100.00% <ø> (ø)
packages/core/src/entity/Collection.ts 97.41% <100.00%> (ø)
packages/core/src/entity/EntityLoader.ts 100.00% <100.00%> (ø)
packages/core/src/metadata/MetadataDiscovery.ts 99.07% <100.00%> (+<0.01%) ⬆️
packages/core/src/typings.ts 100.00% <ø> (ø)
packages/core/src/utils/QueryHelper.ts 99.31% <100.00%> (ø)
packages/knex/src/AbstractSqlDriver.ts 99.07% <100.00%> (+0.01%) ⬆️
packages/knex/src/query/QueryBuilder.ts 99.14% <ø> (+0.42%) ⬆️
... and 1 more

📢 Thoughts on this report? Let us know!.

@B4nan B4nan merged commit 4272b76 into v6 Oct 2, 2023
7 of 8 checks passed
@B4nan B4nan deleted the populate-ref branch October 2, 2023 19:01
B4nan added a commit that referenced this pull request Oct 2, 2023
Sometimes we might want to know only what items are part of a
collection, and we don't care about the values of those items. For this,
we can populate the collection only with references:

```ts
const book1 = await em.findOne(Book, 1, { populate: ['tags:ref'] });
console.log(book1.tags.isInitialized()); // true
console.log(wrap(book1.tags[0]).isInitialized()); // false

// or alternatively use `init({ ref: true })`
const book2 = await em.findOne(Book, 1);
await book2.tags.init({ ref: true });
console.log(book2.tags.isInitialized()); // true
console.log(wrap(book2.tags[0]).isInitialized()); // false
```

Closes #1158
B4nan added a commit that referenced this pull request Oct 17, 2023
Sometimes we might want to know only what items are part of a
collection, and we don't care about the values of those items. For this,
we can populate the collection only with references:

```ts
const book1 = await em.findOne(Book, 1, { populate: ['tags:ref'] });
console.log(book1.tags.isInitialized()); // true
console.log(wrap(book1.tags[0]).isInitialized()); // false

// or alternatively use `init({ ref: true })`
const book2 = await em.findOne(Book, 1);
await book2.tags.init({ ref: true });
console.log(book2.tags.isInitialized()); // true
console.log(wrap(book2.tags[0]).isInitialized()); // false
```

Closes #1158
B4nan added a commit that referenced this pull request Oct 21, 2023
Sometimes we might want to know only what items are part of a
collection, and we don't care about the values of those items. For this,
we can populate the collection only with references:

```ts
const book1 = await em.findOne(Book, 1, { populate: ['tags:ref'] });
console.log(book1.tags.isInitialized()); // true
console.log(wrap(book1.tags[0]).isInitialized()); // false

// or alternatively use `init({ ref: true })`
const book2 = await em.findOne(Book, 1);
await book2.tags.init({ ref: true });
console.log(book2.tags.isInitialized()); // true
console.log(wrap(book2.tags[0]).isInitialized()); // false
```

Closes #1158
B4nan added a commit that referenced this pull request Oct 25, 2023
Sometimes we might want to know only what items are part of a
collection, and we don't care about the values of those items. For this,
we can populate the collection only with references:

```ts
const book1 = await em.findOne(Book, 1, { populate: ['tags:ref'] });
console.log(book1.tags.isInitialized()); // true
console.log(wrap(book1.tags[0]).isInitialized()); // false

// or alternatively use `init({ ref: true })`
const book2 = await em.findOne(Book, 1);
await book2.tags.init({ ref: true });
console.log(book2.tags.isInitialized()); // true
console.log(wrap(book2.tags[0]).isInitialized()); // false
```

Closes #1158
B4nan added a commit that referenced this pull request Nov 2, 2023
Sometimes we might want to know only what items are part of a
collection, and we don't care about the values of those items. For this,
we can populate the collection only with references:

```ts
const book1 = await em.findOne(Book, 1, { populate: ['tags:ref'] });
console.log(book1.tags.isInitialized()); // true
console.log(wrap(book1.tags[0]).isInitialized()); // false

// or alternatively use `init({ ref: true })`
const book2 = await em.findOne(Book, 1);
await book2.tags.init({ ref: true });
console.log(book2.tags.isInitialized()); // true
console.log(wrap(book2.tags[0]).isInitialized()); // false
```

Closes #1158
B4nan added a commit that referenced this pull request Nov 5, 2023
Sometimes we might want to know only what items are part of a
collection, and we don't care about the values of those items. For this,
we can populate the collection only with references:

```ts
const book1 = await em.findOne(Book, 1, { populate: ['tags:ref'] });
console.log(book1.tags.isInitialized()); // true
console.log(wrap(book1.tags[0]).isInitialized()); // false

// or alternatively use `init({ ref: true })`
const book2 = await em.findOne(Book, 1);
await book2.tags.init({ ref: true });
console.log(book2.tags.isInitialized()); // true
console.log(wrap(book2.tags[0]).isInitialized()); // false
```

Closes #1158
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 this pull request may close these issues.

None yet

1 participant