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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@automate-crud/mongoose

v0.1.3

Published

Auto CRUD for Express + Mongoose

Readme

@automate-crud/mongoose

Generate Express CRUD routes directly from a Mongoose model.

This package is for the repetitive part of backend work:

  • list
  • get by id
  • create
  • update
  • delete

You define the model, mount one router, and get working CRUD APIs with query support.

Install

npm i @automate-crud/mongoose express mongoose

Quick Start

import express from "express";
import mongoose from "mongoose";
import { createCrudRouter, crudErrorHandler } from "@automate-crud/mongoose";

await mongoose.connect(process.env.MONGO_URI);

const User = mongoose.model(
  "User",
  new mongoose.Schema(
    {
      name: { type: String, required: true, index: true },
      email: { type: String, required: true, unique: true, index: true },
      archived: { type: Boolean, default: false }
    },
    { timestamps: true }
  )
);

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

app.use(
  "/users",
  createCrudRouter({
    model: User,
    searchFields: ["name", "email"],
    allowedIncludes: []
  })
);

app.use(crudErrorHandler);
app.listen(4000);

Generated Routes

Mounting on /users gives:

  • GET /users
  • GET /users/:id
  • POST /users
  • PATCH /users/:id
  • DELETE /users/:id

What You Get

  • Pagination with page and limit
  • Sorting with sort
  • Field selection with select
  • Search with q
  • Filtering with query params or filter
  • Controlled populate with include
  • Hooks for create, update, and delete
  • Custom routes for model-specific actions

Minimal Usage

app.use(
  "/users",
  createCrudRouter({
    model: User
  })
);

Query Examples

GET /users?page=2&limit=10
GET /users?sort=-createdAt
GET /users?select=name,email
GET /users?q=shiv
GET /users?include=team
GET /users?name=Shiv
GET /users?filter={"archived":false}

Options

model

Required. The Mongoose model to use.

searchFields

Fields searched when q is provided.

createCrudRouter({
  model: User,
  searchFields: ["name", "email"]
});

allowedIncludes

Allowed populate paths for include.

createCrudRouter({
  model: User,
  allowedIncludes: ["team", "profile"]
});

idParam

Change the route param name from id.

createCrudRouter({
  model: User,
  idParam: "userId"
});

hooks

Supported hooks:

  • beforeCreate(req, body)
  • beforeUpdate(req, body)
  • beforeDelete(req)

Example:

createCrudRouter({
  model: User,
  hooks: {
    beforeCreate(req, body) {
      return {
        ...body,
        name: body.name.trim()
      };
    },
    beforeUpdate(req, body) {
      return {
        ...body,
        name: body.name?.toUpperCase() ?? body.name
      };
    }
  }
});

customRoutes

Use this for model-specific actions without writing full controller boilerplate.

createCrudRouter({
  model: User,
  customRoutes: [
    {
      method: "post",
      path: "/:id/archive",
      handler: async ({ params, model }) => {
        return model.findByIdAndUpdate(
          params.id,
          { archived: true },
          { new: true }
        ).lean();
      }
    }
  ]
});

Handler input:

  • req
  • params
  • query
  • body
  • model

Returned value becomes:

{
  "data": {}
}

Error Handling

Use the built-in middleware after your routes.

app.use(crudErrorHandler);

Error response shape:

{
  "error": {
    "message": "Internal server error"
  }
}

Local Testing

npm test --workspace @automate-crud/mongoose

The test setup uses:

  • supertest
  • mongodb-memory-server

Current Scope

This package currently focuses on:

  • Express
  • Mongoose
  • repetitive CRUD removal
  • hooks
  • custom routes

It does not yet include:

  • auth
  • role/permission system
  • soft delete
  • Prisma or SQL support
  • declarative action generation