1.2.3 • Published 4 months ago

crud-api-express v1.2.3

Weekly downloads
-
License
ISC
Repository
-
Last release
4 months ago

CRUD API Controller

npm downloads license made-with-node made-with-typescript express MongoDB

Installation

Install the package using npm:

npm install crud-api-express

This project provides a flexible and reusable CRUD (Create, Read, Update, Delete) API controller for MongoDB using Express.js and Mongoose.

📌 Table of Contents


Introduction

The CrudController class simplifies the creation of RESTful APIs in Node.js applications using MongoDB. It abstracts away common CRUD operations, error handling, middleware integration, and supports custom routes and aggregation pipelines.


Usage

Here's a basic example of how to use in Es module CrudController:

import express from 'express';
import mongoose from 'mongoose';
import CrudController from 'crud-api-express';

const Schema = mongoose.Schema;
const ExampleSchema = new Schema(
  {
    type: { type: String, default: 'Percentage', enum: ['Percentage', 'Flat'] },
    status: { type: String, default: 'Active', trim: true },
    expiry_date: { type: Date, index: true, trim: true },
  },
  { timestamps: true, versionKey: false }
);

const ExampleModel = mongoose.model('Example', ExampleSchema);

const options = {
  middleware: [
    (req, res, next) => {
      const authToken = req.headers.authorization;
      if (!authToken) {
        return res.status(401).json({ message: 'Unauthorized' });
      }
      next();
    },
    (req, res, next) => {
      console.log(`Request received at ${new Date()}`);
      next();
    },
  ],
  onSuccess: (res, method, result) => {
    console.log(`Successful ${method} operation:`, result);
    res.status(200).json({ success: true, data: result });
  },
  onError: (res, method, error) => {
    console.error(`Error in ${method} operation:`, error);
    res.status(500).json({ error: error.message });
  },
  methods: ['GET', 'POST', 'PUT', 'DELETE'],
  aggregatePipeline: [
    { $match: { status: 'Active' } },
    { $sort: { createdAt: -1 } },
  ],
  customRoutes: [
    {
      method: 'get',
      path: '/custom-route',
      handler: (req, res) => {
        res.json({ message: 'Custom route handler executed' });
      },
    },
    {
      method: 'post',
      path: '/custom-action',
      handler: (req, res) => {
        res.json({ message: 'Custom action executed' });
      },
    },
  ],
};

const exampleController = new CrudController(ExampleModel, 'examples', options);

const mongoURI = 'mongodb://localhost:27017/mydatabase';

mongoose
  .connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => {
    console.log('Connected to MongoDB');

    const app = express();
    app.use(express.json());
    app.use('/api', exampleController.getRouter());

    console.log(exampleController.getRoutes());

    const PORT = process.env.PORT || 3000;
    app.listen(PORT, () => {
      console.log(`Server is running on port ${PORT}`);
    });
  })
  .catch((err) => {
    console.error('Error connecting to MongoDB:', err.message);
    process.exit(1);
  });

Here's a basic example of how to use in cjs module CrudController:

const express = require('express');
const mongoose = require('mongoose');
const CrudController = require('crud-api-express');

const Schema = mongoose.Schema;
const ExampleSchema = new Schema(
  {
    type: { type: String, default: 'Percentage', enum: ['Percentage', 'Flat'] },
    status: { type: String, default: 'Active', trim: true },
    expiry_date: { type: Date, index: true, trim: true },
  },
  { timestamps: true, versionKey: false }
);

const ExampleModel = mongoose.model('Example', ExampleSchema);

const options = {
  middleware: [
    (req, res, next) => {
      const authToken = req.headers.authorization;
      if (!authToken) {
        return res.status(401).json({ message: 'Unauthorized' });
      }
      next();
    },
    (req, res, next) => {
      console.log(`Request received at ${new Date()}`);
      next();
    },
  ],
  onSuccess: (res, method, result) => {
    console.log(`Successful ${method} operation:`, result);
    res.status(200).json({ success: true, data: result });
  },
  onError: (res, method, error) => {
    console.error(`Error in ${method} operation:`, error);
    res.status(500).json({ error: error.message });
  },
  methods: ['GET', 'POST', 'PUT', 'DELETE'],
  aggregatePipeline: [
    { $match: { status: 'Active' } },
    { $sort: { createdAt: -1 } },
  ],
  customRoutes: [
    {
      method: 'get',
      path: '/custom-route',
      handler: (req, res) => {
        res.json({ message: 'Custom route handler executed' });
      },
    },
    {
      method: 'post',
      path: '/custom-action',
      handler: (req, res) => {
        res.json({ message: 'Custom action executed' });
      },
    },
  ],
};

const exampleController = new CrudController(ExampleModel, 'examples', options);

const mongoURI = 'mongodb://localhost:27017/mydatabase';

mongoose
  .connect(mongoURI, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => {
    console.log('Connected to MongoDB');

    const app = express();
    app.use(express.json());
    app.use('/api', exampleController.getRouter());

    console.log(exampleController.getRoutes());

    const PORT = process.env.PORT || 3000;
    app.listen(PORT, () => {
      console.log(`Server is running on port ${PORT}`);
    });
  })
  .catch((err) => {
    console.error('Error connecting to MongoDB:', err.message);
    process.exit(1);
  });

API

getRouter(): Router

Returns the Express Router instance configured with CRUD routes.

[
  { "method": "POST", "path": "/examples", "params": null },
  { "method": "GET", "path": "/examples", "params": ["filter", "sort", "page", "limit"] },
  { "method": "GET", "path": "/examples/:id", "params": ["id"] },
  { "method": "PUT", "path": "/examples/:id", "params": ["id"] },
  { "method": "DELETE", "path": "/examples", "params": ["filter"] },
  { "method": "DELETE", "path": "/examples/:id", "params": ["id"] },
  { "method": "GET", "path": "/examples/aggregate", "params": null },
  { "method": "GET", "path": "/examples/custom-route", "params": null },
  { "method": "POST", "path": "/examples/custom-action", "params": null }
]

Options

CrudOptions<T>

OptionTypeDescription
middleware((req: Request, res: Response, next: NextFunction) => void)[]Array of middleware functions
onSuccess(res: Response, method: string, result: T \| T[]) => voidSuccess handler function
onError(res: Response, method: string, error: Error) => voidError handler function
methods('POST' \| 'GET' \| 'PUT' \| 'DELETE')[]Array of HTTP methods to support
relatedModelModel<any>Related Mongoose model for relational operations
relatedFieldstringField name for related models
relatedMethods('POST' \| 'GET' \| 'PUT' \| 'DELETE')[]Methods to apply on related models
aggregatePipelineobject[]MongoDB aggregation pipeline stages
customRoutes{ method: 'post' \| 'get' \| 'put' \| 'delete', path: string, handler: (req: Request, res: Response) => void }[]Array of custom route definitions

📖 Fetch All Records with Query Params (GET)

🛠️ URL:
GET http://localhost:3000/api/examples?filter={"status":"Active"}&sort={"expiry_date":1}&page=1&limit=10

🔍 Query Params Explanation:

  • filter → Filter results (e.g., { "status": "Active" }).
  • sort → Sort order (e.g., { "expiry_date": 1 } for ascending).
  • page → Pagination (e.g., page=1).
  • limit → Number of results per page.

License

This project is licensed under the ISC License.

Support Me! ❤️

If you find this package useful, consider supporting me:
Buy Me a Coffee ☕

1.2.0

4 months ago

1.1.9

4 months ago

1.1.8

4 months ago

1.1.7

4 months ago

1.1.6

4 months ago

1.1.5

4 months ago

1.2.3

4 months ago

1.1.4

4 months ago

1.2.2

4 months ago

1.1.3

4 months ago

1.2.1

4 months ago

1.1.2

4 months ago

1.1.1

12 months ago

1.1.0

12 months ago

1.0.9

12 months ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago