Skip to content

feat: batchedQuery - #607

Open
lxsmnsyc wants to merge 1 commit into
solidjs:mainfrom
lxsmnsyc:feat-batched-query
Open

lxsmnsyc wants to merge 1 commit into
solidjs:mainfrom
lxsmnsyc:feat-batched-query

Conversation

@lxsmnsyc

Copy link
Copy Markdown
Member

Adds batchedQuery. It collects calls made at the same time and answers them with one read. This helps when many components each fetch one item as they mount, like rows in a list.

Usage

const getUser = query(
  batchedQuery(
    async (ids: string[]) => {
      const users = await fetchUsers(ids);
      return new Map(users.map(user => [user.id, user]));
    },
    (users, id) => users.get(id)
  ),
  "user"
);

Changes

  • batchedQuery(callback, lookup, options?) returns a function that takes one query and resolves with its result.
  • callback gets every query collected in one tick and reads them together.
  • lookup picks each caller's result from the data. It also gets the query's position in the list.
  • Repeated queries are sent to callback once. By default they match by hashKey, the same function query uses for its keys. Pass key to match them another way.
  • limit caps how many queries one callback call gets. Extra queries go to more calls. This is useful when ids go into a URL.
  • wait sets how long calls are collected, in milliseconds. The default is 0.
  • A failed callback rejects only the callers in that call. A lookup that throws rejects only the callers of that query.
  • A call made while callback is running starts a new batch. It never gets data from a batch it was not part of.
  • On the server, each call runs on its own. The queue is shared by the whole module, so batching there would mix queries from separate requests and run them with the first request's context.
  • batchedQuery and the BatchOptions type are exported from the package.

Testing

  • Added test/data/batched-query.spec.ts with tests for batching, repeated queries, limit, failed reads, failed lookups, calls made after or during a read, and custom keys.

@changeset-bot

changeset-bot Bot commented Sep 15, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 2f56dd4

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

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

@lxsmnsyc lxsmnsyc mentioned this pull request Sep 15, 2026
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.

1 participant