@tsnx/data
v1.2.1
Published
TSNX Data Abstractions - EF Core inspired DbContext, Unit of Work, Repository, and DI integration for TSNX
Readme
@tsnx/data
EF Core-inspired database abstractions, Unit of Work, DbContext, and Dependency Injection lifecycle integration for TSNX.
Key Features
DbContext&IUnitOfWork: Coordinates entity changes, transactions, and repositories across requests.IDbContextFactory<T>: Creates isolatedDbContextsessions outside standard HTTP scopes (background workers, queue consumers, CLI commands).DbConcurrencyException(HTTP 409 Conflict): Built-in optimistic concurrency exception preserving entity identity and mapping directly to HTTP 409 ProblemDetails.- Scoped Lifetime: Ensures entity change tracking and transactions are strictly isolated per HTTP request / scope.
asNoTrackingQuery Support: Read-only queries bypass identity map tracking for improved throughput and memory efficiency.- Provider Agnostic: Core contracts are decoupled from specific ORMs, with
@tsnx/mikro-ormas the first-class provider integration.
Installation
pnpm add @tsnx/dataQuick Start (Inspired by EF Core)
import { DbContext, addDbContext } from '@tsnx/data';
// 1. Register DbContext in TSNX IServiceCollection
addDbContext(builder.services, AppDbContext, (options) => {
options
.useConnectionString('postgresql://localhost:5432/mydb')
.enableSensitiveDataLogging(process.env.NODE_ENV !== 'production')
.enableAutoMigrate();
});
// 2. Consume in route handlers / services with Scoped lifetime
app.mapGet('/users', async (db: AppDbContext) => {
return await db.set(User).findMany({}, { asNoTracking: true });
});