Skip to content
Back to home

CODING FINDS

Notes from the terminal.

Short write-ups of things I learned the hard way: the problem, the fix and the result, with the smallest snippet that shows it.

FOR CODING AGENTS

Every note is also available over MCP, so your coding agent can look up a fix before it writes the bug.

[MCP server URL]
All notes
NestJS

Let the service own the transaction

Problem. Creating an inventory item also means saving its identifiers. If the item is inserted but saving the identifiers fails, the database can be left with half the operation completed.

The transaction needs to cover every database write that belongs to that operation.

Fix. Use @Transactional() from @nestjs-cls/transactional on the service method that coordinates the work. It joins the current transaction or starts one if none exists. Repositories access that same transaction through this.txHost.tx, without passing a transaction object between methods.

For a method that must only run inside a transaction, use @Transactional(Propagation.Mandatory). It reuses the active transaction and throws if none exists. NestJS CLS documentation

With the CLS plugin and Drizzle adapter already configured:

The replacement deletes before inserting, so running it without a transaction could lose existing identifiers if the insert fails. Mandatory prevents that method from executing without an active transaction.

A repository does not need its own decorator to participate. With Drizzle, this.txHost.tx returns the active transaction when one exists, and the regular database client otherwise.

ts
@Injectable()class CreateCollectableService {  constructor(    private readonly items: CreateCollectableRepository,    private readonly identifiers: ReplaceCollectableIdentifiersRepository,  ) {}  @Transactional()  async handle(    item: CreateCollectableRecord,    identifiers: IdentifierInput[],  ) {    const created = await this.items.handle(item);    await this.identifiers.handle(created.id, identifiers);    return created;  }}@Injectable()class ReplaceCollectableIdentifiersRepository {  constructor(    private readonly txHost: TransactionHost<DrizzleAdapter>,  ) {}  @Transactional(Propagation.Mandatory)  async handle(id: string, identifiers: IdentifierInput[]) {    await this.txHost.tx      .delete(collectableIdentifiers)      .where(eq(collectableIdentifiers.collectableId, id));    if (!identifiers.length) return;    await this.txHost.tx      .insert(collectableIdentifiers)      .values(        identifiers.map((identifier) => ({          ...identifier,          collectableId: id,        })),      );  }}

Result. The item and its identifiers commit together when the outer transaction succeeds. Awaiting both calls and letting failures propagate allows both writes to roll back together. The service owns the transaction boundary; Mandatory guards methods that depend on it.

Stuck on something like this?

Tell me what you are working on. I’ll help you find it, fix it and keep it fixed.

Book a call