@fluojs/drizzle
v1.0.0-beta.4
Published
Drizzle ORM integration for Fluo with ALS transaction context, async module factory, and optional dispose hook.
Maintainers
Readme
@fluojs/drizzle
Drizzle ORM integration for fluo with a transaction-aware database wrapper and an optional dispose hook.
Table of Contents
- Installation
- When to Use
- Quick Start
- Common Patterns
- Manual Module Composition
- Public API Overview
- Related Packages
- Example Sources
Installation
npm install @fluojs/drizzleWhen to Use
- when Drizzle should participate in the same module, DI, and lifecycle model as the rest of the app
- when repositories need a single
current()seam that switches between the root handle and the active transaction handle - when application shutdown should also run an explicit cleanup hook for the underlying driver resources
Quick Start
import { ConfigService } from '@fluojs/config';
import { Module } from '@fluojs/core';
import { DrizzleModule } from '@fluojs/drizzle';
import { drizzle } from 'drizzle-orm/node-postgres';
import { Pool } from 'pg';
@Module({
imports: [
DrizzleModule.forRootAsync({
inject: [ConfigService],
useFactory: async (config: ConfigService) => {
const pool = new Pool({
connectionString: config.getOrThrow<string>('DATABASE_URL'),
});
return {
database: drizzle(pool),
dispose: async () => {
await pool.end();
},
};
},
}),
],
})
export class AppModule {}Common Patterns
Use DrizzleDatabase.current() inside repositories
import { DrizzleDatabase } from '@fluojs/drizzle';
import { eq } from 'drizzle-orm';
import { users } from './schema';
export class UserRepository {
constructor(private readonly db: DrizzleDatabase) {}
async findById(id: string) {
return this.db.current().select().from(users).where(eq(users.id, id));
}
}Manual transaction boundaries
await this.db.transaction(async () => {
const tx = this.db.current();
await tx.insert(users).values(user);
await tx.insert(profiles).values(profile);
});Nested calls reuse the active transaction boundary. If a nested call passes transaction options while a boundary is already active, the package rejects those nested options instead of silently changing the existing transaction.
When database.transaction(...) is unavailable and strictTransactions is false, transaction() and requestTransaction() fall back to direct execution; request-scoped calls still honor AbortSignal.
Request-scoped transactions with an interceptor
import { UseInterceptors } from '@fluojs/http';
import { DrizzleTransactionInterceptor } from '@fluojs/drizzle';
@UseInterceptors(DrizzleTransactionInterceptor)
class UsersController {}Shutdown and status contracts
DrizzleTransactionInterceptor runs each HTTP request through DrizzleDatabase.requestTransaction(...). During application shutdown, DrizzleDatabase aborts any still-active request transaction, waits for its transaction callback to settle or roll back, and only then runs the optional dispose(database) hook. This ordering lets drivers finish rollback/cleanup work before pools or externally managed resources are closed.
Nested requestTransaction(...) calls opened inside an existing manual transaction boundary also join shutdown tracking, so shutdown aborts and drains them before dispose(database) runs without opening a second Drizzle transaction.
New requestTransaction(...) calls are rejected once shutdown begins, so disposal cannot overtake a late request transaction that starts after the shutdown boundary is crossed.
If the request signal aborts after the request callback has completed but before the underlying Drizzle transaction runner finishes committing or rolling back, requestTransaction(...) waits for that runner to settle first and then rejects with the abort reason. This keeps Drizzle cleanup serialized with request cancellation while making the late request abort visible to the caller instead of returning the completed callback result.
createDrizzlePlatformStatusSnapshot(...) and DrizzleDatabase.createPlatformStatusSnapshot() expose the same contract to diagnostics surfaces:
readiness.statusisnot-readywhile Drizzle is shutting down or stopped, and whenstrictTransactionsis enabled withoutdatabase.transaction(...)support.health.statusisdegradedwhile request transactions are draining during shutdown andunhealthyafter disposal.details.activeRequestTransactions,details.lifecycleState,details.strictTransactions, anddetails.supportsTransactiondescribe the current request transaction and transaction-capability state.details.transactionContext: 'als'identifies the async-local transaction context used by request and service transaction boundaries.ownership.externallyManaged: trueandownership.ownsResources: falsemean the package runs your configured dispose hook but does not claim ownership of the underlying driver resources.
Manual Module Composition
Use DrizzleModule.forRoot(...) / forRootAsync(...) to register Drizzle. When you need to compose Drizzle support inside a custom defineModule(...) registration, import the module entrypoint there as well.
import { defineModule } from '@fluojs/runtime';
import { DrizzleDatabase, DrizzleModule, DrizzleTransactionInterceptor } from '@fluojs/drizzle';
const database = {
transaction: async <T>(callback: (tx: typeof database) => Promise<T>) => callback(database),
};
class ManualDrizzleModule {}
defineModule(ManualDrizzleModule, {
exports: [DrizzleDatabase, DrizzleTransactionInterceptor],
imports: [DrizzleModule.forRoot({ database })],
});Public API Overview
DrizzleModule.forRoot(options)/DrizzleModule.forRootAsync(options)DrizzleDatabaseDrizzleTransactionInterceptorDRIZZLE_DATABASE,DRIZZLE_DISPOSE,DRIZZLE_HANDLE_PROVIDER,DRIZZLE_OPTIONScreateDrizzlePlatformStatusSnapshot(...)DrizzleDatabaseLikeDrizzleModuleOptionsDrizzleHandleProvider
DRIZZLE_HANDLE_PROVIDER is an alias token for the lifecycle-aware DrizzleDatabase wrapper. Health integrations such as @fluojs/terminus use this token to read createPlatformStatusSnapshot() before falling back to raw database pings.
DrizzleModule
DrizzleModule.forRoot(options)/DrizzleModule.forRootAsync(options)forRootAsync(...)accepts DI-aware Drizzle options whose factory returns the database/dispose/transaction settings; passglobalon the top-level async registration when the providers should be visible globally.- Supports
strictTransactions: trueto throw if transaction support is missing.
Related Packages
@fluojs/runtime: owns module startup and shutdown sequencing@fluojs/http: provides the interceptor pipeline used for request transactions@fluojs/prismaand@fluojs/mongoose: alternate ORM/ODM integrations with the same fluo runtime model
Example Sources
packages/drizzle/src/vertical-slice.test.tspackages/drizzle/src/module.test.tspackages/drizzle/src/public-api.test.ts
