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

Pragmatic intro to React part 1 — Basics

NodeJS with Typescript & TypeORM - Custom Pagination, Search & Sorting using MySQL

Rethinked guide to testing React & Redux applications

A Quick Guide to Redux

Buefy — Loading Indicator and Menu

Start to Finish — Write & Deploy a Vue App, setup Auto-deploy, Enable Authentication, link a…

React Micro Frontend Components in a Monorepo

Super simplified copy of React

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

Event-Driven Architecture with TypeScript and RabbitMQ

NestJS: Use a solo @injectable class as a singleton Provider

Using an interface as a class in typescript

Frameworkifying Express API with Typescript decorators