Socket
Book a DemoInstallSign in
Socket

@nestjsbase/typeorm

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nestjsbase/typeorm

This is a library includes inheritable classes that can help you to save your time when create CRUD module in NestJS. This classes are supporting for typeorm. If you want to use mongoose instead, you can go [here](https://www.npmjs.com/package/@nestjsbase

1.0.2
latest
npmnpm
Version published
Maintainers
1
Created
Source

NestJS Base

This is a library includes inheritable classes that can help you to save your time when create CRUD module in NestJS. This classes are supporting for typeorm. If you want to use mongoose instead, you can go here

Installation

npm install @nestjsbase/typeorm

Usage

BaseEntity

First, you have create your own entity class and inherit BaseEntity of @nestjsbase/typeorm

import { Entity, Column } from 'typeorm';
import { BaseEntity } from '@nestjsbase/typeorm';

@Entity()
export class UserEntity extends BaseEntity {
    @Column()
    name!: string;
}

This is BaseEntity actually do in background

export class BaseEntity {
  @PrimaryGeneratedColumn("uuid")
  id!: string;

  @CreateDateColumn()
  createdAt!: Date;

  @UpdateDateColumn()
  updatedAt!: Date;

  @DeleteDateColumn()
  deletedAt!: Date;
}

BaseService

Now, make your service inherit BaseService

import { Repository } from 'typeorm';
import { BaseService } from '@nestjsbase/typeorm';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { UserEntity } from '../user.service';

@Injectable()
export class UserService extends BaseService {
    constructor(@InjectRepository(UserEntity) private userRepo: Repository<UserEntity>){
        super(userRepo);
    }
}

This BaseService already have methods:

export class IBaseService {
  create(...);
  createMany(...);

  getOne(...);
  getOneOrFail(...);

  getOneById(...);
  getOneByIdOrFail(...);

  getAll(...);
  getAllWithPagination(...);

  update(...);
  updateById(...);

  remove(...);
  removeById(...);

  softRemove(...);
  softRemoveById(...);
}

FindOptions and FindOrFailOptions

  • where: usage same as typeorm
  • relations: usage same as typeorm
  • errorMessage: This message will be thrown when entity not be found. Default is: "Entity not found"

FindWithPaginationOptions

  • limit: number of items in one page
  • page: page number

IPagination

export interface IPagination<T> {
  data: T[];
  pagination: {
    limit: number;
    page: number;
    total: number;
  };
}

FAQs

Package last updated on 09 Aug 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.