Database.defaultConnection: implicit conn for the one-connection model#92
Merged
Conversation
ryanrasti
force-pushed
the
ryan_2_insert_expressions
branch
from
July 23, 2026 00:17
6a3ae9a to
3a5a8d4
Compare
There was a problem hiding this comment.
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 exposeDatabase.defaultConnection(throws on 0 or >1 attached). - Update execute-family terminators (query + insert/update/delete) to accept an optional
Connectionand fall back toDatabase.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 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 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 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 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
force-pushed
the
ryan_3_default_connection
branch
2 times, most recently
from
July 23, 2026 03:05
eddc1d2 to
c6b73cc
Compare
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
force-pushed
the
ryan_3_default_connection
branch
from
July 23, 2026 03:08
c6b73cc to
f3aba28
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.