1.0.1 • Published 8 months ago
mongoose-crud-generator v1.0.1
CRUD API Generator with Role-Based Permissions & AES-256 Encryption
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, 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-generatorOr with yarn:
yarn add mongoose-crud-generatorSetup
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: { pre: { getAll: async (req, res) => console.log("Pre Hook Triggered") } },
postHooksOptions: { post: { getAll: async (req, res, response) => response } },
middlewareOptions: { customMiddleware: require("./middlewares/customMiddleware") },
encryption: { enable: true, secretKey: "your-secret-key" },
};
// 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 fieldsfilter: JSON object for advanced filteringjoin: Populate related fields (field(subField1,subField2)format)select: Select specific fields (field1,field2format)
2. Get Item by ID
GET /users/:id3. 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/:id6. Hard Delete Item
DELETE /users/hard/:idAuthentication & 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: { pre: { getAll: async (req, res) => console.log("Pre Hook") } },
postHooksOptions: { post: { getAll: async (req, res, response) => response } },
};Versioning
Use semantic versioning:
npm version patch # Bug fixes
npm version minor # New features
npm version major # Breaking changesContributing
Pull requests are welcome! Open an issue for discussions.