NestJS: Clean Code With Decorators

What is a Decorator?

@Min(18)
public age: number;

Premise

  1. Call the acquireLock() at the very beginning of the method.
  2. Wrap the entire existing method code in try/catch
  3. Call the releaseLock() in finally block

Disadvantage Of Above Code Style

End Goal

Implementation Of @ConcurrencyLock() decorator

Explanation

Line 12: const LockServiceinjector = Inject(LockService);
Line 19: LockServiceinjector(target, 'lockService');
Line 22: descriptor.value = async function wrapper(...args: any[])...

Conclusion

--

--

--

We are business accelerator working with startups / entrepreneurs in building the product & launching the companies.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Building a ready for production microservice in NestJS: Writing an authorization microservice —…

Using React + Google Sheets as your CMS

Angular DI: Getting to know the Ivy NodeInjector

How I created a door bell with AWS Serverless

New and Hybrid Approaches to Rendering Web Apps with Next.js

Singly Linked List in JavaScript

JavaScript: Use a Recursive Function to Determine If a Given Number is Even

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
LiftOff LLC

LiftOff LLC

We are business accelerator working with startups / entrepreneurs in building the product & launching the companies.

More from Medium

A better way to do the validation in NestJS

Why you should start using Nest JS in 2022?

Event-Driven Architecture with TypeScript and RabbitMQ

How to run Commands in NestJS app like a Pro