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.
Using npm:
npm i express-sequelize-autocrud
Using yarn:
yarn add express-sequelize-autocrud
Using pnpm:
pnpm i express-sequelize-autocrud
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
Operation | URL | Sequelize Operation | Comment |
---|---|---|---|
getList | Clean: GET <API_URL>/<BASE_PATH> With pagination: GET <API_URL>/<BASE_PATH>?_start=30&_end=60 With sort & filters: GET <API_URL>/<BASE_PATH>?_sort=createdAt&_order=DESC&gender=female |
findAll() or findAndCountAll() |
- 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() or findOne() |
- 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. |
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!