@nestplatform/transactional-mongoose
v1.1.1
Published
Mongoose adapter for `@nestplatform/transactional`.
Maintainers
Readme
@nestplatform/transactional-mongoose
Mongoose adapter for @nestplatform/transactional.
Installation
npm install @nestplatform/transactional @nestplatform/transactional-mongooseBasic Usage
1. Register the module
import { getConnectionToken } from "@nestjs/mongoose";
import { Connection } from "mongoose";
import { TransactionalModule } from "@nestplatform/transactional";
import { MongooseTransactionAdapter } from "@nestplatform/transactional-mongoose";
@Module({
imports: [
TransactionalModule.registerAsync({
inject: [getConnectionToken()],
useFactory: (connection: Connection) => ({
adapters: new MongooseTransactionAdapter(connection),
}),
}),
],
})
export class AppModule {}2. Use @Transactional in your services
@Injectable()
export class OrderService {
constructor(@InjectModel(Order.name) private readonly orderModel: Model<Order>) {}
@Transactional()
async createOrder(productName: string, amount: number) {
const order = new this.orderModel({ productName, amount });
return order.save();
}
}Features
- Propagation Support: Supports
REQUIREDandREQUIRES_NEW.NESTEDfalls back toREQUIRED(not natively supported by MongoDB). - Auto-session binding: Automatically attaches the active session to Mongoose model queries within the transaction context.
- Synchronizations: Supports
afterCommit,afterRollback, andafterCompletionhooks.
Prerequisites
- MongoDB replica set or sharded cluster (required for transactions).
- NestJS with
@nestjs/mongoose.
Changelog
1.1.1
- adjust to allow multi layers transactional (hexagonal, clean architecture)
1.0.0
- release first version supporting for mongoose
