ORM-06: Add Database connection management (ambient connection) #6
Labels
No labels
area:core
area:database
area:migrations
area:model
area:relations
area:schema
area:tests
breaking-change
size:M
size:S
tracking
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
NoctisWolfshadow/tessera-orm#6
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Depends on: #5
Context
Models should be used without passing a connection around (
User::find(1)), like Eloquent. rusqlite'sConnectionisSendbut notSync, so the design is: config is process-wide, the connection is per thread.Database::withis the single choke point that every model and query call goes through.Tasks
Database::connect(config) -> Result<()>opens one connection to fail early and stores the config in aOnceLock. A second call returnsError::AlreadyConnected.thread_local!slot holdsOption<Connection>. The firstwithon a thread opens a connection from the global config.Database::connect_thread(config)opens a connection immediately and stores it in the calling thread's slot, never touching the global config.Database::disconnect_thread()clears the slot. Tests use this.Database::with(f)handsfa shared&Connectionso nested calls work, and returnsError::NotConnectedwhen nothing is configured.:memory:databases are per connection, so multi-threaded apps need a file; never hold a transaction across an.await; callingconnect_threadinside awithclosure panics.Example
Acceptance criteria
:memory:and.awaitcaveats.Database#7Database::transactionwith savepoint nesting #8Migrator::status()#10Queryterminals and a singlehydratepath #14