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 declarative partial loading of collection items #5210

Merged
merged 1 commit into from
Feb 4, 2024

Conversation

B4nan
Copy link
Member

@B4nan B4nan commented Feb 4, 2024

Collections can also represent only a subset of the target entities:

@Entity()
class Author {

  @OneToMany(() => Book, b => b.author)
  books = new Collection<Book>(this);

  @OneToMany(() => Book, b => b.author, { where: { favorite: true } })
  favoriteBooks = new Collection<Book>(this);

}

This works also for M:N relations. Note that if you want to declare more relations mapping to the same pivot table, you need to explicitly specify its name (or use the same pivot entity):

@Entity()
class Book {

  @ManyToMany(() => BookTag)
  tags = new Collection<BookTag>(this);

  @ManyToMany({
    entity: () => BookTag, 
    pivotTable: 'book_tags',
    where: { popular: true },
  })
  popularTags = new Collection<BookTag>(this);

}

Closes #4963

packages/knex/src/AbstractSqlDriver.ts Dismissed Show dismissed Hide dismissed
Collections can also represent only a subset of the target entities:

```ts
@entity()
class Author {

  @OneToMany(() => Book, b => b.author)
  books = new Collection<Book>(this);

  @OneToMany(() => Book, b => b.author, { where: { favorite: true } })
  favoriteBooks = new Collection<Book>(this);

}
```

This works also for M:N relations. Note that if you want to declare more relations mapping to the same pivot table, you need to explicitly specify its name (or use the same pivot entity):

```ts
@entity()
class Book {

  @manytomany(() => BookTag)
  tags = new Collection<BookTag>(this);

  @manytomany({
    entity: () => BookTag,
    pivotTable: 'book_tags',
    where: { popular: true },
  })
  popularTags = new Collection<BookTag>(this);

}
```

Closes #4963
@B4nan B4nan merged commit 5e4fa60 into master Feb 4, 2024
9 checks passed
@B4nan B4nan deleted the partial-collections branch February 4, 2024 17:03
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.

Relationship conditions
1 participant