Skip to content

Instantly share code, notes, and snippets.

View saikatdutta1991's full-sized avatar

ʇɐʞıɐs ɐʇʇnp saikatdutta1991

  • Bangalore
View GitHub Profile
interface Student {
id: number;
name: string;
dateOfBirth: Date;
}
class StudentRepository {
someDbClient: any;
constructor() {
@saikatdutta1991
saikatdutta1991 / concurrency_lock.decorator.ts
Last active March 28, 2022 03:52
NestJS: Clean Code With Decorators
import { Inject } from '@nestjs/common';
import { DEFAULT_LOCK_SECONDS } from '../common/constants';
import { UseKeyGenerator } from '../common/types';
import { LockAcquireException } from '../exceptions/lock-acquire.exception';
import { LockService } from '../lock.service';
export function ConcurrencyLock(options: {
useKey: string | UseKeyGenerator;
releaseAfterSeconds?: number;
errorMessage?: string;
@saikatdutta1991
saikatdutta1991 / some_class_with_decorator.ts
Created March 28, 2022 03:45
NestJS: Clean Code With Decorators
Class SomeClass {
ConcurrencyLock({
useKey: (input: CreateHelpSessionInput) => `create_help_session_${input.userId}`
releaseAfterSeconds: 30,
errorMessage?: `Failed to acquire lock for the create help session. Try after sometime.`
})
public createHelpSession(input: CreateHelpSessionInput) {
// Existing code.. No changes here.
}
@saikatdutta1991
saikatdutta1991 / some_class_without_decorator.ts
Last active March 27, 2022 18:46
NestJS: Clean Code With Decorators
Class SomeClass {
public createHelpSession(input: CreateHelpSessionInput) {
const key = `create_help_session_${input.userId}`
await this.lockService.acquireLock(key);
try {
// entire existing code goes here
} finally() {
await this.releaseLock(key);
}
@saikatdutta1991
saikatdutta1991 / Lock.Service.ts
Last active September 27, 2022 21:34
NestJS: Clean Code With Decorators
import { Inject, Injectable, Logger } from '@nestjs/common';
import { CustomLogger } from 'src/common/decorators/custom-logger.decorator';
import { Redis } from '../redis/redis';
import { LockAcquireException } from './exceptions/lock-acquire.exception';
@Injectable()
export class LockService {
@CustomLogger()
private readonly logger: Logger;