@efesto-cloud/database-context
v0.0.4
Published
Usecase type for efesto-cloud
Readme
@efesto-cloud/database-context
Database-agnostic transaction abstraction. Defines the interface that use cases depend on when they need atomicity — concrete implementations live in the adapter packages (e.g. @efesto-cloud/mongodb-database-context).
Installation
pnpm add @efesto-cloud/database-contextAPI
interface IDatabaseContext {
runWithTransaction<T>(fn: () => Promise<T>): Promise<T>;
}That's the whole contract:
runWithTransaction(fn)— executefninside a transaction. Iffnthrows, the transaction is rolled back; otherwise committed. Nested calls must be safe (the implementation should reuse the outer transaction).
Usage
Depend on IDatabaseContext from your application layer — not on a concrete driver. This keeps use cases testable and lets you swap database adapters.
import type { IDatabaseContext } from "@efesto-cloud/database-context";
class CreatePost {
constructor(private readonly db: IDatabaseContext) {}
async execute(input: Input) {
return this.db.runWithTransaction(async () => {
// …load, mutate, save…
});
}
}Adapters
@efesto-cloud/mongodb-database-context— MongoDB implementation backed byClientSession.withTransaction.
