Are you a Mongoose lover but tired of writing the same CRUD code over and over again? Let Crudify do the heavy lifting for you! With this simple yet powerful NestJS library, you can instantly generate RESTful CRUD endpoints for your Mongoose models with just a few lines of code. Spend less time on boilerplate and more time building your application!
- Tired of repetitive code? Crudify automatically generates the full set of CRUD operations for your Mongoose models. No more writing the same functions every time you need a new endpoint.
- Swagger documentation out of the box! Crudify automatically generates a fully functional Swagger UI for your CRUD endpoints, making it easier for you and your team to test and document the API.
- Highly customizable? Of course! You can extend and tweak the generated endpoints to meet your specific needs.
- Error handling made easy: With an integrated logger, Crudify intercepts uncaught errors and makes debugging a breeze.
- Designed for NestJS: Seamlessly integrates with your NestJS project, no friction, no fuss.
Ready to get started? Just install Crudify and let it work its magic:
npm install ncrudify
- No more boilerplate: Generate Create, Read, Update, and Delete operations automatically.
- Swagger docs created for you: Just like that, Swagger UI will be set up automatically to interact with your API, making testing and documentation seamless.
- Custom error handling: Built-in error handling with easy logging configuration.
- Flexible: Customize the generated code or add your business logic.
- NestJS integration: Perfectly fits into your existing NestJS projects.
Once you set it up, Crudify will handle these endpoints for you:
POST /your-model:
Create a new recordPOST /your-model/bulk:
Create multiple new recordsGET /your-model:
Retrieve all recordsGET /your-model/:id:
Retrieve a specific record by IDPATCH /your-model/:id:
Update a record by IDPATCH /your-model/bulk
Update multiple records with filter in bodyPUT /your-model/:id:
Replace a record by IDDELETE /your-model/:id:
Delete a record by IDDELETE /your-model/bulk
Delete multiple records
If youβre done wasting time with repetitive CRUD code and ready to level up your NestJS game, Crudify is here for you. Letβs get started! π
If you haven't already configured MongoDB in your NestJS project, follow these steps:
- Install Mongoose:
First, you need to install the
@nestjs/mongoose
package andmongoose
to connect to your MongoDB database:
npm install @nestjs/mongoose mongoose
- Configure MongoDB in your NestJS module:
In your main application module (usually app.module.ts
), import and configure the MongooseModule
to connect to your MongoDB instance:
import { Module } from "@nestjs/common";
import { MongooseModule } from "@nestjs/mongoose";
import { UserModule } from "./user/user.module";
@Module({
imports: [MongooseModule.forRoot(process.env.MONGODB_URI)],
})
export class AppModule {}
- Define your Mongoose model:
In this example, we define a
User
model using@nestjs/mongoose
decorators and@nestjs/swagger
for automatic API documentation.
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty } from "@nestjs/swagger";
import { Document, model, Model } from "mongoose";
@Schema({ timestamps: true })
export class User extends Document {
@ApiProperty({ example: "John Doe", description: "The name of the user" })
@Prop({ required: true })
name: string;
@ApiProperty({example: "[email protected]", description: "The email of the user"})
@Prop({ required: true, unique: true })
email: string;
@ApiProperty({ example: 1, description: "The age of the user" })
@Prop({ required: false })
age: number;
}
export const UserSchema = SchemaFactory.createForClass(User);
export const UserModel: Model<User> = model <User> ("User", UserSchema);
- Create your service:
Extend CrudifyService
to benefit from the automatically generated CRUD methods. Override any methods if needed for custom logic, such as the findAll
method in this example.
import { Injectable } from '@nestjs/common';
import { User } from './entities/user.entity';
import { FilterQuery, Model } from 'mongoose';
import { InjectModel } from '@nestjs/mongoose';
import { CrudifyService } from 'ncrudify';
@Injectable()
export class UserService extends CrudifyService<User> {
constructor(@InjectModel(User.name) protected userModel: Model<User>) {
super(userModel);
}
}
- Create your controller:
Use the
@Crudify
decorator to automatically generate CRUD routes for theUser
model. You can still override specific methods likefindAll
as shown below.
import { Controller, Get } from '@nestjs/common';
import { UserService } from './user.service';
import { User, UserSchema } from './entities/user.entity';
import { Crudify, CrudifyController } from 'ncrudify';
@Crudify({
model: {
type: User,
schema: UserSchema
}
})
@Controller('users')
export class UserController extends CrudifyController<User> {
constructor(public service: UserService) {
super(service);
}
}
- Create your module:
Set up the module to import MongooseModule
and register the User
schema, while also providing the service and controller.
import { Module } from "@nestjs/common";
import { UserService } from "./user.service";
import { UserController } from "./user.controller";
import { MongooseModule } from "@nestjs/mongoose";
import { UserSchema } from "./entities/user.entity";
@Module({
imports: [MongooseModule.forFeature([{ name: "User", schema: UserSchema }])],
controllers: [UserController],
providers: [UserService],
})
export class UserModule {}
For more configurations and documentation, visit the Crudify wiki.
We love contributions! Whether youβve spotted a bug or have an awesome idea, feel free to open an issue or submit a PR.
Support Crudify by becoming a sponsor! Sponsors will be featured here with links to their projects or companies. Reach out if youβd like to sponsor the project.
We love contributions! Found a bug or have an idea? Open an issue or submit a PR.
If you like this project, give it a β on GitHub or Buy Me A Coffee!
This project is licensed under the MIT License. See the LICENSE file for details.