@aaroncadillac/crudify-mongo
v1.4.0
Published
An automatic CRUD generator for fastify
Downloads
4
Readme
Crudify Mongo
An automatic CRUD generator for fastify and MongoDB, using mongoose,a nd adding basic features like pagination, filtering, and sorting.
Table of contents
Pre-requisites
- Fastify v4 or higher
- Mongoose v8 or higher
- mongoose-paginate-v2 v1.8 or higher
Features
Automatic CRUD generation
You can automatically enjoy the following CRUD operations:
| Route | Description | Additional built-in benefits |
|-------|-------------|----|
| GET /
| Get all documents | Pagination
Filtering
Sorting
|
| GET /:id
| Get a document by id | Not-Found validation
|
| POST /
| Create a document | Schema validation
|
| PUT /:id
| Update a document by id | Not-Found validation
Schema validation
|
| DELETE /:id
| Delete a document by id | Not-Found validation
|
Custom routes
You can add custom routes to the CRUD, to increase the functionality of your API, you only need to pass the route configuration to the plugin.
const routes = ( fastify, opts, done ) => {
fastify.register(Crudify, {
url: '/users',
Model: UserModel,
additionalRoutes: [
{
method: 'GET',
url: '/:id/light-details',
handler: LightDetailsUserHandler
},
...
]
})
Getting started
Installation
yarn add @aaroncadillac/crudify-mongo
Usage
First, you need to create a mongoose model, and use the mongoose-paginate-v2
plugin to enable pagination.
// models/user.js
import mongoose from 'mongoose';
import paginate from 'mongoose-paginate-v2'; // Required for pagination
const Schema = mongoose.Schema;
const userSchema = new Schema({
name: {
type: String,
required: true,
},
age: {
type: Number,
required: true,
},
email: {
type: String,
required: true,
},
}, {
timestamps: true,
});
userSchema.plugin(paginate);
const UserModel = mongoose.model('Users', userSchema);
export {
UserModel
}
Then, I suggest to separate DB connection into a module, and connect to the database, this will be an async fastify plugin.
// db-connection.js
import mongoose from 'mongoose';
const dbConnection = async ( fastify ) => {
try {
await mongoose.connect(`${process.env.MONGODB_URL}/${process.env.MONGO_DATABASE}${process.env.MONGO_OPTIONS && `?${process.env.MONGO_OPTIONS}`}`);
fastify.log.info('Connected to database');
}
catch (error) {
fastify.log.error('Error connecting to database');
}
}
export {
dbConnection
}
Then, you can create the server file, and register the CRUD plugin.
// server.js
import fastify from 'fastify';
import Crudify from 'crudify-mongo';
import { dbConnection } from './db-connection';
import { UserModel } from './models/user';
const fastify = Fastify({
logger: true
})
const routes = ( fastify, opts, done ) => {
fastify.register(Crudify, {
url: '/users',
Model: UserModel
})
}
fastify.register(await dbConnection)
fastify.register(routes)
fastify.listen({ port: process.env.PORT || 3000, host: process.env.HOST || 'localhost' }, function (err, address) {
if (err) {
fastify.log.error(err)
process.exit(1)
}
})
Simple as that, you have a CRUD for the User
model and could be accessed through the /users
route.
Aknowledgements
This plugin is inspired by fastify-mongoose-crud