Skip to content

Conversation

@lousydropout
Copy link

Allow joining on multiple fields simultaneously using and() to combine multiple eq() expressions in join conditions.

Example:
.join(
{ inventory: inventoriesCollection },
({ product, inventory }) =>
and(
eq(product.region, inventory.region),
eq(product.sku, inventory.sku)
)
)

  • Add extractJoinConditions() helper to parse and() expressions
  • Extend JoinClause IR with additionalConditions field
  • Implement composite key extraction with JSON.stringify
  • Preserve fast path for single conditions (no serialization)
  • Disable lazy loading for compound joins

Fixes #593

🎯 Changes

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

@changeset-bot
Copy link

changeset-bot bot commented Nov 19, 2025

🦋 Changeset detected

Latest commit: 82d50c0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
@tanstack/db Minor
@tanstack/angular-db Patch
@tanstack/db-collection-e2e Patch
@tanstack/electric-db-collection Patch
@tanstack/offline-transactions Major
@tanstack/powersync-db-collection Patch
@tanstack/query-db-collection Major
@tanstack/react-db Patch
@tanstack/rxdb-db-collection Patch
@tanstack/solid-db Patch
@tanstack/svelte-db Patch
@tanstack/trailbase-db-collection Patch
@tanstack/vue-db Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@lousydropout lousydropout changed the title feat: support compound join conditions with and() feat: support compound join conditions with and() (issue #593) Nov 19, 2025
Allow joining on multiple fields simultaneously using and() to combine
multiple eq() expressions in join conditions.

Example:
  .join(
    { inventory: inventoriesCollection },
    ({ product, inventory }) =>
      and(
        eq(product.region, inventory.region),
        eq(product.sku, inventory.sku)
      )
  )

- Add extractJoinConditions() helper to parse and() expressions
- Extend JoinClause IR with additionalConditions field
- Implement composite key extraction with JSON.stringify
- Preserve fast path for single conditions (no serialization)
- Disable lazy loading for compound joins

Fixes TanStack#593
@lousydropout lousydropout force-pushed the feat/compound-join-conditions branch from f74ded2 to 9a9e362 Compare November 19, 2025 19:41
Enhance the original compound join test to verify actual row contents
and add 4 additional test cases covering edge cases:
- Partial field matches (ensures AND semantics)
- LEFT join behavior with unmatched rows
- Null value handling in compound conditions
- Joining on 3+ fields simultaneously

These tests validate the compound join implementation across different
join types (INNER, LEFT) and ensure correct behavior with complex
data scenarios.
@samwillis
Copy link
Collaborator

Hey @lousydropout thanks for this, on quick skim it looks close to what I was thinking (composite key for and'ed join predicates - so very much the right track.

I'm on bug fixes at the moment after we released the incremental sync stuff, and so it will be a few days before I get a chance to look in detail.

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.

Allow joins with an and() condition

2 participants