D1 batch types: preserve heterogeneous result tuples

Open Beginner friendly
#8,575 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript

Research direction

The issue points to both batch declarations in types/defines/d1.d.ts, including the current Promise<D1Result<T>[]> return type at line 65. Read the overloads for D1Database.batch and D1DatabaseSession.batch, then check the workers-types test or type-check entry point for declaration tests. Done means tuple inputs infer the corresponding result type at each position while the existing homogeneous array overload still works.

Written by the indexing model from the issue text.

Description

D1Database.batch and D1DatabaseSession.batch apply one result type to every statement. Callers must use unions or casts when a batch returns different row shapes.

Reproduce with workers-types 5.20261117.1:

type User = {name: string};
type Count = {n: number};
declare const db: D1Database;

const result = await db.batch<User | Count>([
  db.prepare("SELECT 'Ada' AS name"),
  db.prepare('SELECT 1 AS n')
]);
result[0].results[0].name;
// TS2339: Property 'name' does not exist on type 'User | Count'

Both declarations currently return Promise<D1Result[]>.

Requested: a tuple-aware overload so callers can specify the row type at each statement position and receive [D1Result<User>, D1Result<Count>]. Preserve the current homogeneous array overload.

This is a declaration improvement; the SQL batch executes normally. No runtime change is requested.

Dominant language
C++
Stars
9.3k
Forks
845
Avg merge
1d 23h
Merged PRs (30d)
558

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

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.

More from cloudflare/workerd

All issues in cloudflare/workerd

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.