Class: UnitOfWork<TContext>
Defined in: src/uow/UnitOfWork.ts:46
Enterprise Unit of Work pattern implementation for EntityTS.
Collects entity mutations across multiple DbSet collections, performs topological
dependency resolution to sequence parent-before-child inserts and child-before-parent deletes,
and commits all operations atomically within a single database transaction across PostgreSQL, MySQL, SQLite, and MSSQL.
Usecase
Coordinate multi-aggregate transactions where multiple entities across different tables must be persisted together or fail together.
Example
PostgreSQL / MySQL / SQLite / MSSQL:
const uow = context.createUnitOfWork();
const customer = new Customer({ name: 'Acme Corp' });
const order = new Order({ totalAmount: 1500 });
const invoice = new Invoice({ status: 'PENDING' });
uow.registerNew(context.customers, customer);
uow.registerNew(context.orders, order);
uow.registerNew(context.invoices, invoice);
const result = await uow.commit();
console.log(`Committed ${result.insertedCount} entities in atomic transaction.`);
Type Parameters
| Type Parameter | Default type |
|---|---|
TContext extends DbContext | DbContext |
Accessors
pendingCount
Get Signature
get pendingCount():
number
Defined in: src/uow/UnitOfWork.ts:54
The count of uncommitted operations currently queued in this Unit of Work.
Returns
number
Constructors
Constructor
new UnitOfWork<
TContext>(context):UnitOfWork<TContext>
Defined in: src/uow/UnitOfWork.ts:49
Parameters
| Parameter | Type |
|---|---|
context | TContext |
Returns
UnitOfWork<TContext>
Methods
hasChanges()
hasChanges():
boolean
Defined in: src/uow/UnitOfWork.ts:61
Returns true if there are uncommitted operations waiting in the Unit of Work.
Returns
boolean
registerNew()
registerNew<
T>(set,entity):this
Defined in: src/uow/UnitOfWork.ts:73
Registers a newly created entity to be inserted during commit().
Type Parameters
| Type Parameter |
|---|
T extends object |
Parameters
| Parameter | Type | Description |
|---|---|---|
set | DbSet<T> | Target DbSet for the entity. |
entity | T | The entity object to insert. |
Returns
this
this instance for chaining.
Usecase
Queue an entity insertion in the Unit of Work.
registerDirty()
registerDirty<
T>(set,entity,patch?):this
Defined in: src/uow/UnitOfWork.ts:86
Registers an existing entity as modified to be updated during commit().
Type Parameters
| Type Parameter |
|---|
T extends object |
Parameters
| Parameter | Type | Description |
|---|---|---|
set | DbSet<T> | Target DbSet for the entity. |
entity | T | The entity object being modified. |
patch? | Partial<T> | Optional partial update payload. |
Returns
this
this instance for chaining.
Usecase
Queue an entity modification with optional patch payload.
registerDeleted()
registerDeleted<
T>(set,entity):this
Defined in: src/uow/UnitOfWork.ts:93
Registers an entity to be deleted during commit().
Type Parameters
| Type Parameter |
|---|
T extends object |
Parameters
| Parameter | Type |
|---|---|
set | DbSet<T> |
entity | T |
Returns
this
register()
register<
T>(set,entity,state,patch?):this
Defined in: src/uow/UnitOfWork.ts:100
Registers an entity with a specific EntityState lifecycle.
Type Parameters
| Type Parameter |
|---|
T extends object |
Parameters
| Parameter | Type |
|---|---|
set | DbSet<T> |
entity | T |
state | EntityState |
patch? | Partial<T> |
Returns
this
rollback()
rollback():
void
Defined in: src/uow/UnitOfWork.ts:153
Clears all pending operations without committing them to the database.
Returns
void
commit()
commit():
Promise<UnitOfWorkCommitResult>
Defined in: src/uow/UnitOfWork.ts:162
Commits all queued operations atomically within a single database transaction with topological dependency ordering (parents before children on insert, children before parents on delete).
Returns
Promise<UnitOfWorkCommitResult>
Properties
context
readonlycontext:TContext
Defined in: src/uow/UnitOfWork.ts:49