Package Exports
- nestjs-cls-neverthrow
- nestjs-cls-neverthrow/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nestjs-cls-neverthrow) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
nestjs-cls-neverthrow
A @TransactionalResult decorator for @nestjs-cls/transactional that works with neverthrow's ResultAsync. When the decorated method returns an Err, the transaction automatically rolls back.
For methods that don't use neverthrow, use the original @Transactional from @nestjs-cls/transactional.
Installation
npm install nestjs-cls-neverthrowUsage
import { Transactional } from '@nestjs-cls/transactional'; // plain returns
import { TransactionalResult } from 'nestjs-cls-neverthrow'; // ResultAsync returns@TransactionalResult — for ResultAsync returns
@Injectable()
export class UserService {
constructor(
private readonly txHost: TransactionHost<TransactionalAdapterTypeOrm>,
) {}
@TransactionalResult()
createUser(name: string): ResultAsync<User, Error> {
const repo = this.txHost.tx.getRepository(User);
return ResultAsync.fromSafePromise(repo.save({ name }));
// Ok → transaction commits
}
@TransactionalResult()
failingMethod(): ResultAsync<void, Error> {
const repo = this.txHost.tx.getRepository(User);
return ResultAsync.fromSafePromise(repo.save({ name: 'will be rolled back' }))
.andThen(() => errAsync(new Error('oops')));
// Err → transaction rolls back
}
}@Transactional — for plain returns
For methods that don't return ResultAsync, use the original decorator from @nestjs-cls/transactional:
import { Transactional } from '@nestjs-cls/transactional';
@Injectable()
export class PlainService {
constructor(
private readonly txHost: TransactionHost<TransactionalAdapterTypeOrm>,
) {}
@Transactional()
async createUser(name: string): Promise<User> {
const repo = this.txHost.tx.getRepository(User);
return repo.save({ name });
}
}Behavior
| Return Value | Transaction |
|---|---|
okAsync(value) / ok(value) via ResultAsync |
Commits |
errAsync(error) / err(error) via ResultAsync |
Rolls back, returns Err to caller |
Peer Dependencies
@nestjs-cls/transactional>=3.0.0neverthrow>=6.0.0