Skip to content

Database.defaultConnection: implicit conn for the one-connection model#92

Merged
ryanrasti merged 1 commit into
mainfrom
ryan_3_default_connection
Jul 23, 2026
Merged

Database.defaultConnection: implicit conn for the one-connection model#92
ryanrasti merged 1 commit into
mainfrom
ryan_3_default_connection

Conversation

@ryanrasti

Copy link
Copy Markdown
Owner

attach() registers each pool-backed connection; defaultConnection resolves it when exactly one is attached (the Durable Object model — one object, one conn) and throws otherwise; close() deregisters. All execute-family terminators (QueryBuilder execute/hydrate/one/maybeOne/ live + insert/update/delete execute/hydrate) take an optional Connection, defaulting through the builder's own Database provenance. Explicit conn still wins — transaction bodies keep passing tx, which is required since the default resolves the pool-backed connection, not the active txn.

Covered in default-connection.test.ts.

@ryanrasti
ryanrasti requested a review from Copilot July 23, 2026 00:15
@ryanrasti
ryanrasti force-pushed the ryan_2_insert_expressions branch from 6a3ae9a to 3a5a8d4 Compare July 23, 2026 00:17
@ryanrasti
ryanrasti changed the base branch from ryan_2_insert_expressions to main July 23, 2026 00:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds an implicit Database.defaultConnection to support a “one Durable Object, one connection” model, allowing execute-family terminators to omit an explicit Connection when exactly one pool-backed connection is attached.

Changes:

  • Track pool-backed attached connections in Database.attach() and expose Database.defaultConnection (throws on 0 or >1 attached).
  • Update execute-family terminators (query + insert/update/delete) to accept an optional Connection and fall back to Database.defaultConnection.
  • Add tests validating default-connection resolution, ambiguity errors, and close() deregistration behavior.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/database.ts Tracks attached pool connections, adds defaultConnection, and deregisters on Connection.close().
src/builder/query.ts Makes query terminators accept optional Connection and default via Database.defaultConnection.
src/builder/insert.ts Makes insert execute/hydrate accept optional Connection and default via Database.defaultConnection.
src/builder/update.ts Makes update execute/hydrate accept optional Connection and default via Database.defaultConnection.
src/builder/delete.ts Makes delete execute/hydrate accept optional Connection and default via Database.defaultConnection.
src/default-connection.test.ts Adds Vitest coverage for default connection resolution and ambiguity handling.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/builder/query.ts
Comment on lines +409 to +411
@expose(zConn)
override async execute(conn?: Connection<any>): Promise<RowTypeToTsType<O>[]> {
return (conn ?? this.opts.database.defaultConnection).execute(this);
Comment thread src/builder/insert.ts
Comment on lines +163 to 166
@expose(z.lazy(() => z.instanceof(Connection)).optional())
override async execute(conn?: Connection<any>): Promise<RowTypeToTsType<R>[]> {
return (conn ?? this.table.database.defaultConnection).execute(this);
}
Comment thread src/builder/update.ts
Comment on lines +171 to 174
@expose(z.lazy(() => z.instanceof(Connection)).optional())
override async execute(conn?: Connection<any>): Promise<RowTypeToTsType<R>[]> {
return (conn ?? this.table.database.defaultConnection).execute(this);
}
Comment thread src/builder/delete.ts
Comment on lines +131 to 134
@expose(z.lazy(() => z.instanceof(Connection)).optional())
override async execute(conn?: Connection<any>): Promise<RowTypeToTsType<R>[]> {
return (conn ?? this.table.database.defaultConnection).execute(this);
}
@ryanrasti
ryanrasti force-pushed the ryan_3_default_connection branch 2 times, most recently from eddc1d2 to c6b73cc Compare July 23, 2026 03:05
attach() registers each pool-backed connection; defaultConnection
resolves it when exactly one is attached (the Durable Object model —
one object, one conn) and throws otherwise; close() deregisters. All
execute-family terminators (QueryBuilder execute/hydrate/one/maybeOne/
live + insert/update/delete execute/hydrate) take an optional Connection,
defaulting through the builder's own Database provenance. Explicit conn
still wins — transaction bodies keep passing tx, which is required since
the default resolves the pool-backed connection, not the active txn.

Covered by the defaultConnection describe block in database.test.ts.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@ryanrasti
ryanrasti force-pushed the ryan_3_default_connection branch from c6b73cc to f3aba28 Compare July 23, 2026 03:08
@ryanrasti
ryanrasti merged commit 47dd0f9 into main Jul 23, 2026
3 checks passed
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.

2 participants