Express Sequelize AutoCRUD: Simplify API development with automatic CRUD routes for Sequelize models in Express.js.
npm install express-sequelize-autocrudExpress 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.






- 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.
Using npm:
``bash`
npm i express-sequelize-autocrud
Using yarn:
`bash`
yarn add express-sequelize-autocrud
Using pnpm:
`bash`
pnpm i express-sequelize-autocrud
Using Express Sequelize AutoCRUD is straightforward:
`typescript
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
| Operation | URL | Sequelize Operation | Comment |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| getList | Clean:
_GET_
With pagination:
_GET_
With sort & filters:
_GET_ | findAll()findAndCountAll()
or | - Enable pagination with pagination: true in the config.Content-Range
- Pagination automatically adds header.filterableFields
- Can control which fields can be filterable using in the config.sortableFieldsFields
- Can control which fields can be sortable using in the config. |
| getOne | _GET_ | findByPk()findOne()
or | - Can set custom key (not primary key) using byField in the config. |
| create | _POST_ | create() | - Can control which fields can be added to body request using creatableFields in the config. |
| bulkCreate | _POST_ | bulkCreate() | - Can control which fields can be added to body request using creatableFields in the config.path
- Can custom the url path with in the config (default: /bulk) |
| update | _PUT_ | 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_ | 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.
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.
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!