TanStack / TanStack/db

Allow joins with an `and()` condition

Open
#593 2 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
3.9k
Forks
266
Avg merge
1d 4h
Merged PRs (30d)
55

Description

Trying to join collections using a condition with an and() is not supported right now.

Reproduction

Adding packages/db/tests/query/join.test.ts:

type Product = {
  productId: number;
  region: 'A' | 'B' | 'C';
  sku: string;
  title: string;
}

type ProductInventory = {
  inventoryId: number;
  region: 'A' | 'B' | 'C';
  sku: string;
  quantity: number;
};

test('join with and', () => {
  const productsCollection = createCollection(
    mockSyncCollectionOptions<Product>({
      id: `test-products-join`,
      getKey: (product) => product.productId,
      initialData: [
        { productId: 1, region: 'A', sku: 'sku1', title: 'A1' },
        { productId: 2, region: 'B', sku: 'sku1', title: 'B1' },
        { productId: 3, region: 'C', sku: 'sku2', title: 'C2' },
        { productId: 4, region: 'A', sku: 'sku2', title: 'A2' },
      ],
    })
  );
  const inventoriesCollection = createCollection(
    mockSyncCollectionOptions<ProductInventory>({
      id: `test-inventories-join`,
      getKey: (inventory) => inventory.inventoryId,
      initialData: [
        { inventoryId: 1, region: 'A', sku: 'sku1', quantity: 10 },
        { inventoryId: 3, region: 'C', sku: 'sku2', quantity: 30 },
      ],
    })
  );

  const joinQuery = createLiveQueryCollection({
    startSync: true,
    query: (q) =>
      q
        .from({ product: productsCollection })
        .join(
          { inventory: inventoriesCollection },
          ({ product, inventory }) =>
            and(
              eq(product.region, product.sku),
              eq(inventory.region, inventory.sku)
            ),
        )
  });

  expect(joinQuery.toArray).toHaveLength(2);
});

And it fails with:

QueryBuilderError: Join condition must be an equality expression
 ❯ BaseQueryBuilder.join src/query/builder/index.ts:277:3
    275|   > {
    276|     return this.join(source, onCallback, `inner`)
    277|   }
       |   ^
    278|
    279|   /**
 ❯ query tests/query/join.test.ts:1459:10
 ❯ buildQuery src/query/builder/index.ts:3822:45
 ❯ buildQueryFromConfig src/query/live/collection-config-builder.ts:3737:48
 ❯ new CollectionConfigBuilder src/query/live/collection-config-builder.ts:139:9
 ❯ liveQueryCollectionOptions src/query/live-query-collection.ts:269:60
 ❯ createLiveQueryCollection src/query/live-query-collection.ts:287:46

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproduction in packages/db/tests/query/join.test.ts and inspect the join implementation at src/query/builder/index.ts:277, where the equality-expression error is raised. Run the join tests first; done means the supplied and() join case builds and returns the expected two results without the QueryBuilderError.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.