npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@nready/nestjs-shared

v0.0.3

Published

NestJs common Lib for NestJs project

Readme

@nready/nestjs-shared

_____   ___________________________________  __   _____________________ 
___  | / /__  __ \__  ____/__    |__  __ \ \/ /   ___  /___    |__  __ )
__   |/ /__  /_/ /_  __/  __  /| |_  / / /_  /    __  / __  /| |_  __  |
_  /|  / _  _, _/_  /___  _  ___ |  /_/ /_  /     _  /___  ___ |  /_/ / 
/_/ |_/  /_/ |_| /_____/  /_/  |_/_____/ /_/      /_____/_/  |_/_____/  

Description

The lib @nready/nestjs-shared is used for Nest.js v10.0.0 project with pre-built some utilities function:

  • Message Queue
  • Redis Cache
  • Abstract Model
  • Search and paging: Support Paging/Search with TypeORM
  • UtilsService
    • .generateHash(password: string)
    • .validateHash(password: string, hash: string)
    • .generateRandomInteger(min: number, max: number)
    • .generateRandomString(length: number)
    • .getAge(d1: Date, d2?: Date)
    • .capitalizeName(name: string)
    • .encodeString(text: string)
    • .mergeObject(A: any, B: any)
    • .cleanNullObject(obj: any)
    • .getLocaleDate(isoString: string, timeZone?: string)
    • .isNullOrUndefined(value: any)
    • .isFutureDate(value: any)
    • .isActiveByDate(effectDate: Date, inactiveDate: Date)
    • .base64Encode(text: string)
    • .base64Decode(text: string)
    • .randomString()
    • .transformEndOfDate(date: Date | string)

(tobe deprecated later)

  • extractKey(path: string)
  • transformEndOfDate(date: Date | string)
  • randomString(length = 60)
  • isDate(value: any)
  • getTodayFormatYYYYDDMM()
  • hasCommonItemInArrays(arr1: [], arr2: [])
  • convertToAscii(str: string)
  • cleanNullObject(obj: any)
  • isNullable(value: any)
  • isFutureDate(value: any)
  • isActiveByDate(effectDate: Date, inactiveDate: Date)

Usage

First install the library with command:

npm i @nready/nestjs-shared

Message Queue

The pre-built Message Queue using RabbitMq, supported re-connect if RabbitMq is down, supported queue survives RabbitMQ restart, Message will persist until it is consumed

  • emit: Use AMQP library to send persistent messages
  • overwriteQueue: Overwrite an existed key with another value.

Install AMQP library in project:

npm i amqp-connection-manager
npm i amqplib

Config the .env:

RABBITMQ_URL=amqp://localhost:5672
RABBITMQ_QUEUE_NAME=USER
RABBITMQ_TTL=3600000
RABBITMQ_ACK=false

Register the @nready/nestjs-shared in app.module.ts.

import { MessagingModule } from '@nready/nestjs-shared';

@Module({
  controllers: [AppController],
  providers: [],
  imports: [
    MessagingModule,
    ...

Import in Service:

import { MessagingService } from '@nready/nestjs-shared';
...
@Injectable()
export class MyService {
  constructor(
    private rabbitClient: MessagingService,
  ) {}
  ...
  public async myFunction(): Promise {
    await this.rabbitClient.emit(key, value);
  }

Redis

The pre-built Cache using Redis, supported re-connect if the Redis server is down and more.

  • setKey(key: string, value: string, ttlMinutes?: number)
  • getKey(key: string)
  • deleteKey(key: string)
  • increaseValue(key: string)
  • reset()

Install ioredis lib in project:

npm i ioredis

Config .env:

REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PREFIX=NRD
REDIS_TTL=5

Register the @nready/nestjs-shared in app.module.ts.

import { RedisModule } from '@nready/nestjs-shared';

@Module({
  controllers: [AppController],
  providers: [],
  imports: [
    RedisModule,
    ...

Import in Service:

import { RedisService } from '@nready/nestjs-shared';
...
@Injectable()
export class MyService {
  constructor(
    private readonly redisService: RedisService,
  ) {}
  ...
  public async myFunction(): Promise {
    await this.redisService.setKey(key, value);
  }

Abstract Model

With pre-built properties that inheritance from Abstract Class, we will have common field and no need to duplicate these properties.

List of these properties: createDate, effectDate, inactiveDate, dateLastMaint, version, editedBy, approvedBy, note.

In *.entity.ts, inherit from abstract Model:

export class MyEntity extends AbstractEntity {
  // rest of properties
}

Search and paging:

Dto:

// Search
import { AbstractSearchDto } from '@nready/nestjs-shared';
export class MyEntitySearch extends AbstractSearchDto<MyEntity> {}

//
@Exclude()
export class MyEntityResponseDto extends MyEntity {
  @Expose()
  id: string;
  ...

In Controller:

@Get()
async get(@Query(new QueryTransformPipe()) searchModel: MyEntitySearch) {
  const res = await this.service.query(searchModel);
  return res;
}

In Service:

import { AbstractSearchService, SearchResultDto } from '@nready/nestjs-shared';
import { plainToInstance } from 'class-transformer';

@Injectable()
export class MyService extends AbstractSearchService<MyEntity, MyEntitySearch> {
  constructor(
    @InjectRepository(MyEntitySearch) private readonly repository: Repository<MyEntitySearch>
  ) {
    super(repository);
  }

  public async query(model: MyEntitySearch): Promise<any> {
    const res = await this.paginate(model);
    const data = plainToInstance(MyEntityResponseDto, res.data, { excludeExtraneousValues: true });
    return new SearchResultDto<MyEntityResponseDto>(res.pageMeta, data);
  }
  ...

Curl with paging and search by field name:

curl --location '/?page=0&take=5&someFieldName=blahblah' \
--header 'Content-Type: application/json'

Development

Check Published Package:

npm pack

use in project with zip:

npm install ../@nready/shared-0.0.1.tgz

or using locally:

# package.json
"@nready/shared": "file:../libs/nestjs-shared",

Use in project

npm i @nready/shared

Publish

npm publish --access public

License

Author

Le Quoc Nam, <[email protected] ([email protected])>

https://nready.net/