mongoose-crud-generator

1.0.5 • Public • Published

CRUD API Generator with Role-Based Permissions

A highly customizable CRUD API generator for Mongoose models with built-in filtering, pagination, search, population and much more.

Features

  • 🚀 Auto-generate CRUD APIs for any Mongoose model.
  • 🔐 Role-based permissions with middleware support.
  • 🔎 Advanced query support (filters, search, sort, pagination, field selection, and population).
  • 🪝 Pre and post hooks for customization.
  • 📦 Fully compatible with Express.js.

Installation

Install the package using npm:

npm install mongoose-crud-generator

Or with yarn:

yarn add mongoose-crud-generator

Setup

1. Import and Use in Express

const express = require("express");
const mongoose = require("mongoose");
const { generateCrudRoutes } = require("mongoose-crud-generator");
const User = require("./models/User"); // Example Mongoose model

const app = express();
app.use(express.json());

// Define API options
const options = {
  authOptions: { authenticate: true, authMiddleware: require("./middlewares/auth") },
  preHooksOptions: { getAll: async (req, res) => console.log("Pre Hook Triggered") },
  postHooksOptions: { getAll: async (req, res, response) => response },
  middlewareOptions: { customMiddleware: require("./middlewares/customMiddleware") },
};

// Generate routes
const userRoutes = generateCrudRoutes({ model: User, modelName: "User", ...options });
app.use("/users", userRoutes);

// Start server
app.listen(3000, () => console.log("Server running on port 3000"));

API Endpoints

1. Get All Items

GET /users?page=1&limit=10&search=John&filter={"age":{"$gte":25}}

Query Parameters:

  • page: Pagination (default: 1)
  • limit: Items per page (default: 10)
  • search: Search by name or custom fields
  • filter: JSON object for advanced filtering
  • sort: Sort the output in order (default: -updatedAt)
  • join: Populate related fields (field(field1[field2,field3]) format) // () for nested joins and [] for selecting specific fields after join
  • select: Select specific fields (field1,field2 format)

2. Get Item by ID

GET /users/:id

3. Create New Item

POST /users
Content-Type: application/json
{
  "name": "John Doe",
  "email": "johndoe@example.com",
  "age": 30
}

4. Update Item

PUT /users/:id
Content-Type: application/json
{
  "name": "John Updated",
  "age": 31
}

5. Soft Delete Item

DELETE /users/:id

6. Hard Delete Item

DELETE /users/hard/:id

Authentication & Role-Based Access Control

You can add authentication and role-based permissions with middleware:

const options = {
  authOptions: { authenticate: true, authMiddleware: require("./middlewares/auth") }
};

Hooks for Custom Logic

Customize API logic using pre and post hooks:

const options = {
  preHooksOptions: { getAll: async (req, res) => console.log("Pre Hook") },
  postHooksOptions: { getAll: async (req, res, response) => response },
};

Versioning

Use semantic versioning:

npm version patch # Bug fixes
npm version minor # New features
npm version major # Breaking changes

Contributing

Pull requests are welcome! Open an issue for discussions.

License

MIT

Package Sidebar

Install

npm i mongoose-crud-generator

Weekly Downloads

12

Version

1.0.5

License

ISC

Unpacked Size

124 kB

Total Files

26

Last publish

Collaborators

  • biswajitpanda-iboon