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 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-sequelize-autocrud

v1.2.0

Published

Express Sequelize AutoCRUD: Simplify API development with automatic CRUD routes for Sequelize models in Express.js.

Downloads

30

Readme

Express Sequelize AutoCRUD

Express Sequelize AutoCRUD is a powerful and developer-friendly TypeScript library designed to simplify the creation of CRUD (Create, Read, Update, Delete) routes for your Sequelize models in an Express.js application. If you're building RESTful APIs with Express and using Sequelize as your ORM, this library will save you significant development time and effort.

npm License GitHub issues CI-Main Code Style: Google Commitizen friendly

Features

  • Automatic CRUD Operations: Easily generate CRUD endpoints for your Sequelize models with just a few lines of code.
  • Customizable Routes: Configure routes according to your project's specific requirements.
  • Validation and Error Handling: Built-in request validation and error handling for seamless API development.
  • Middleware Support: Integrate custom middleware functions with generated routes to extend functionality.
  • Transaction Support: Create, Update and Delete routes runs inside sequelize transaction to handle pre/post hooks and rollback automatically in case something failed in the process.
  • Secure and Efficient: Utilizes Sequelize's robust security features and optimized database queries.

Installation

Using npm:

npm i express-sequelize-autocrud

Using yarn:

yarn add express-sequelize-autocrud

Using pnpm:

pnpm i express-sequelize-autocrud

Usage

Using Express Sequelize AutoCRUD is straightforward:

import express from 'express';
import {Sequelize} from 'sequelize';
import sequelizeCrud from 'express-sequelize-autocrud';

const app = express();
const sequelize = new Sequelize('your_db', 'your_user', 'your_password', {
  host: 'localhost',
  dialect: 'mysql',
});

// Define your Sequelize models here

// Generate routes using express-sequelize-autocrud
app.use(
  '/api',
  sequelizeCrud(sequelize, {
    '/users': {
      model: sequelize.model('users'),
      operations: {
        getList: {
          filterableFields: ['id', 'gender'],
          attributes: ['id', 'fullName', 'gender'],
          limit: 100,
        },
        getOne: {
          attributes: req =>
            req.user.role === 'admin'
              ? ['id', 'fullName', 'secretStuf']
              : ['id', 'fullName'],
        },
        create: {
          creatableFields: {exclude: ['id']},
        },
        update: {
          updatableFields: {exclude: ['id']},
        },
        delete: {
          middleware: (req, res, next) => {
            if (req.user.role === 'admin') {
              next();
            } else {
              res.sendStatus(403);
            }
          },
        },
      },
    },
  })
);

// Start your Express server
const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

Check out our example project for more features and complex use-cases

CRUD Routes

| Operation | URL | Sequelize Operation | Comment | | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | getList | Clean:GET <API_URL>/<BASE_PATH>With pagination:GET <API_URL>/<BASE_PATH>?_start=30&_end=60With sort & filters:GET <API_URL>/<BASE_PATH>?_sort=createdAt&_order=DESC&gender=female | findAll()orfindAndCountAll() | - Enable pagination with pagination: true in the config.- Pagination automatically adds Content-Range header.- Can control which fields can be filterable using filterableFields in the config.- Can control which fields can be sortable using sortableFieldsFields in the config. | | getOne | GET <API_URL>/<BASE_PATH>/:resourceId | findByPk()orfindOne() | - Can set custom key (not primary key) using byField in the config. | | create | POST <API_URL>/<BASE_PATH> | create() | - Can control which fields can be added to body request using creatableFields in the config. | | bulkCreate | POST <API_URL>/<BASE_PATH>/bulk | bulkCreate() | - Can control which fields can be added to body request using creatableFields in the config.- Can custom the url path with path in the config (default: /bulk) | | update | PUT <API_URL>/<BASE_PATH>/:resourceId | update() | - Can control which fields can be added to body request using updatableFields in the config.- Can set custom key (not primary key) using byField in the config. | | delete | DELETE <API_URL>/<BASE_PATH>/:resourceId | destroy() |- Can set custom key (not primary key) using byField in the config. |

Documentation

For detailed usage instructions, examples, and customization options, please refer to our TypeDoc site or Documentation.md.

Contributing

We welcome contributions from the open-source community! Feel free to open issues, submit pull requests, or provide feedback to help us improve this library.

License

This project is licensed under the MIT License.


With Express Sequelize AutoCRUD, streamline your API development process, reduce boilerplate code, and focus on building robust applications. Happy coding!

If you have any questions, encounter issues, or want to collaborate, please don't hesitate to get in touch with us. We appreciate your support!