Create Express APIs to serve CRUD APIs to your Web Application.
First you need to install the package:
npm i bobby_query
To start the server:
npm start
In our Server we user centralized Error Handling
const { asyncHandler } = require('bobby_query')
const controller = asyncHandler(async (req, res) => {
// Your code
const condition = 'any'
const statusCode = 'any'
if (condition) {
res.status(statusCode)
throw new Error('Your Error message')
}
res.status(200).json({ ...req.Bobby })
})
In app.js file
const express = require('express');
const {CRUD, findQuery, getQuery, authorize, handleErrors} = require('bobby_query');
const db = require('./db');
db();
const {Product, Profile, Shop} = require('./models');
const app = express();
app.use(express.json());
const productPermissions = {
select: '',
dontSelect: [{ path: 'author', select: ['password','email'] }],
populate: [''],
};
const Router = express.Router();
// PROFILE Service
Router.post('/profiles/', CRUD(Profile).create);
Router.get('/profiles/',findQuery(Profile), CRUD().find);
Router.get('/profiles/:id',getQuery(Profile), CRUD().get);
Router.patch('/profiles/:id', CRUD(Profile).patch);
Router.delete('/profiles/:id', CRUD(Profile).remove);
// PRODUCT Service
Router.post('/products/', CRUD(Product).create);
Router.get('/products/', authorize(productPermissions), findQuery(Product), CRUD().find);
Router.get('/products/:id', authorize(productPermissions), getQuery(Product), CRUD().get);
Router.patch('/products/:id', CRUD(Product).patch);
Router.delete('/products/:id', CRUD(Product).remove);
// Shop Service
Router.post('/shops/', CRUD(Shop).create);
Router.get('/shops/',findQuery(Shop), CRUD().find);
Router.get('/shops/:id',getQuery(Shop), CRUD().get);
Router.patch('/shops/:id', CRUD(Shop).patch);
Router.delete('/shops/:id', CRUD(Shop).remove);
app.use(Router);
app.use(handleErrors.notFound)
app.use(handleErrors.errorHandler)
const PORT = process.env.PORT || 4000;
app.listen(PORT,()=>console.log(`App is running on PORT: ${PORT}`));
In ./db
const mongoose = require('mongoose');
module.exports =()=>{
mongoose.connect('mongodb://localhost:27017/my_bobby_test', {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false,
},()=>console.log('DB is running'));
}
In ./models
const mongoose = require('mongoose');
const ProfileSchema = new mongoose.Schema(
{
email: {
type: String,
required: true,
trim: true,
unique: true,
},
password: {
type: String,
required: true,
trim: true,
selsect: false,
},
name: {
type: String,
trim: true,
},
age: {
type: Number,
},
},
{
timestamps: true,
},
);
const ProducSchema = new mongoose.Schema(
{
brand: {
type: String,
required: true,
trim: true,
},
description: {
type: String,
trim: true,
},
category: {
type: String,
enum: ['tech', 'clothes', 'services'],
default: 'tech',
},
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Profile',
required: true,
},
price: {
type: Number,
required: true,
},
model: {
type: String,
required: true,
}
},
{
timestamps: true,
},
);
const ShopSchema = new mongoose.Schema(
{
name: {
type: String,
required: true,
trim: true,
},
description: {
type: String,
trim: true,
},
category: {
type: String,
enum: ['tech', 'clothes', 'services'],
default: 'tech',
},
manger: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Profile',
required: true,
},
ecosystem: {
type: String,
required: true,
},
products: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'product',
}]
},
{
timestamps: true,
},
);
const Profile = mongoose.model('Profile', ProfileSchema);
const Product = mongoose.model('product', ProducSchema);
const Shop = mongoose.model('shop', ShopSchema);
module.exports = {
Profile,
Product,
Shop,
};
More details on how ## ReqBobby Response
workes
What is Bobby
The result will be
{
"status": true,
"count": 10,
"pagination": {
"next": {
"page": 3,
"limit": 10
},
"prev": {
"page": 1,
"limit": 10
}
},
"data": [
// your data
]
}
All responses contain STATUS and DATA
{
status: true,
data: {
// your data
}
}
// Note data can be Array or Object depends on what route you hit
Some responses contain more
{
status: true,
count: 10,
pagination: {
next: {
page: 3,
limit: 10
},
prev: {
page: 1,
limit: 10
}
},
data: [
// your data
]
}
// Note data can be Array or Object depends on what route you hit
const {CRUD, findQuery, getQuery, authorize, handleErrors} = require('bobby_query');
const ProductModel = require('./models)
router.get('/products/:company', (req, res, next) => {
// your custom logic with query here
next();
}, findQuery(ProductModel), CRUD().find )
- No dependencies
This is a middleware which takes request Query, Model
It gives client ability to populate, select, paginate, limit, order and make basic query with the req.query example https://example_page.to/exaples?select=_id+user&page=2&limit=3&populate=user&limit=3
req.Bobby ={
"status": true,
"count": 3,
"pagination": {
"next": {
"page": 3,
"limit": 3
},
"prev": {
"page": 1,
"limit": 3
}
},
"data": [
{
"_id": "6112b1ac8954580004ceae52",
"user":{"users populated data here"}
}
]
Bobby is tool to help you create robust APIs with advanced query features, give you tools to make db secure, out of the box you get Error handling, authorization toos to controll which fields should be populated and what client can select, minimize your time on bulding server by having basic CRUD Controllers (get,find,patch,remove,create)
This endpoint simply throws a JavaScript error, and catches all errors at the end.
This software is published under the MIT licence.