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 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-crudify-repo

v1.0.0

Published

The objective of NestJsCrudify is to create a generic layer for accessing databases through an API. This powerful library aims to simplify and streamline the development process by leveraging the Nestjs module architecture. With NestJsCrudify, you can eas

Downloads

4

Readme

NestJsCrudify

The objective of NestJsCrudify is to create a generic layer for accessing databases through an API. This powerful library aims to simplify and streamline the development process by leveraging the Nestjs module architecture. With NestJsCrudify, you can easily interact with your entity by utilizing a generic service that provides essential methods. Furthermore, to expedite the development of new API resources, there is an optional base implementation for a controller included in the library. This feature allows you to save time and effort while building your API. It's worth noting that NestJsCrudify fully supports JSON:API, providing capabilities for deserializing and serializing your API data seamlessly.

Install

npm install nestjs-crudify
// or
yarn add nestjs-crudify

Quick Start

  1. Create a mongo entity
@Schema({ timestamps: true })
export class Todo{
  @Prop()
  name?: string;

  @Prop()
  description?: string;
}

export type TodoDocument = HydratedDocument<Todo>;
export const TodoSchema = SchemaFactory.createForClass(Todo);
export const TodoFeature = {
  name: Todo.name,
  schema: TodoSchema,
};
  1. Create a Dto and Dto Factory
import {MongoDto} from 'nestjs-crudify';
import {Todo} from '../database/Todo.schema';

type TodoProperties = {
  id?: string
  name?: string
  description?: string
  createdAt?: number
  updateAt?: number
}

export class TodoDto extends MongoDto{
  name?: string
  description?: string
  createdAt?: number
  updateAt?: number

  constructor({id, name, description, createdAt, updateAt}: TodoProperties | undefined) {
    super("todo");
    this.id = id
    this.name = name;
    this.description = description
    this.createdAt = createdAt
    this.updateAt = updateAt
  }
}
  1. Create the service and use the Dto and Entity as parameters
@Injectable()
export class TodosService extends MongoService<Todo, TodoDto>{
  constructor(@InjectModel(Todo.name) readonly repository: Model<TodoDocument>, factory: TodoDtoFactory) {
    super(repository, factory);
  }
}
  1. Create the controller
import {Body, Controller, Get, Post, UseInterceptors, Query, Delete, Param, Put} from '@nestjs/common';
import {
  CommonCrudController, FilterLike, FilterMatch, FilterMatchIn,
  JsonApiDeserializerPipe,
  JsonApiSerializeInterceptor,
  MongoController,
  Page, parseObjectId, parseValues,
  SearchFilters,
  TransformToFilter,
} from 'nestjs-crudify';
import {TodosService} from './todos.service';
import {TodoDto} from './todos.dto';
import {Types} from 'mongoose';

class TodoFilters extends SearchFilters{
  @TransformToFilter<Types.ObjectId[]>(new FilterMatchIn("_id"), (v) => parseValues(v, parseObjectId))
  id: FilterMatchIn<Types.ObjectId[]>
  @TransformToFilter<string>(new FilterMatch("name"))
  name: FilterLike
  @TransformToFilter<string>(new FilterLike("description"))
  description: FilterLike
}

@Controller('todos')
@UseInterceptors(JsonApiSerializeInterceptor())
export class TodosController extends MongoController<TodoDto, TodosService> implements CommonCrudController<TodoDto>{

  constructor(private todosService: TodosService) {
    super(todosService)
  }

  @Post()
  create(@Body(new JsonApiDeserializerPipe()) body: any ): Promise<any> {
    return this._create(body)
  }

  @Put(":id")
  update(@Param('id') id: string,@Body(new JsonApiDeserializerPipe()) body: any ): Promise<any> {
    return this._update(id, body)
  }

  @Get()
  search(
    @Query('sort') sort?: string,
    @Query('search') search?: string,
    @Query('page') page?: Page,
    @Query('filter') filter?: TodoFilters,
  ){
    return this._search(sort, search, page, filter)
  }

  @Delete(":id")
  delele(@Param('id') id: string){
    return this._delete(id)
  }
}
  1. Create the module
import { Module } from '@nestjs/common';
import { TodosService } from './todos.service';
import {TodoDtoFactory} from './todos.dto';
import {MongooseModule} from '@nestjs/mongoose';
import {TodoFeature} from '../database/Todo.schema';
import { TodosController } from './todos.controller';

@Module({
  imports: [
    MongooseModule.forFeature([
      TodoFeature
    ]),
  ],
  providers: [TodosService, TodoDtoFactory],
  controllers: [TodosController]
})
export class TodosModule {}

Make sure that you have defined a proper mongodb database connection