Skip to content

Remote D1 binding: withSession() ignores constraints and bookmarks #403

Closed

Description

@Brooooooklyn

Problem

In remote mode (remote: true), the D1 binding accepts withSession(), but it does not keep the D1 session contract:

app code                                   remote D1 binding today
-----------------------------------------  ------------------------------------------
db.withSession('first-primary')         →  returns the same binding; constraint ignored
session.prepare(sql).run()              →  proxy request has binding/sql/params/method only
session.getBookmark()                   →  always null
db.withSession(savedBookmark)           →  bookmark ignored

Each statement is a separate proxy call that runs on the deployed binding without a session. void/db and auth work (#402 added withSession() so they no longer crash). But code that depends on bookmarks sees a different contract in remote development than in production. For example, it cannot carry a bookmark between requests, and getBookmark() never returns a value.

Code: packages/void/src/runtime/remote/d1.ts (ProxyD1Database.withSession, getBookmark) and the proxy handler platform/packages/proxy/src/d1.ts.

What a fix needs

client                                  proxy (deployed binding)
ProxyD1DatabaseSession ── query/batch ─► db.withSession(constraint or bookmark)
  { constraint | bookmark }               run the statement(s)
        ◄──────── result + bookmark ───── session.getBookmark()
  store the new bookmark
  1. withSession() returns a new session object for each call. Two sessions do not share state.
  2. Each query and batch request sends the session's constraint (first call) or its latest bookmark.
  3. The proxy handler runs the request in db.withSession(...) and returns session.getBookmark() with the result. The client stores it.
  4. An older proxy that does not know sessions fails with a clear error. The client must not act as if bookmarks work.

Tests

  • first-primary, first-unconstrained, and a bookmark from an earlier request.
  • A query and then a batch move the bookmark forward.
  • Two sessions keep separate bookmarks.
  • A real client → proxy round trip, and the error against an older proxy.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions