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

bobby_query

v1.2.0

Published

Easily Create [Express] APIs to serve Web Application.

Downloads

22

Readme

Bobbyjs - Automate CRUD and advanced query API with expressJS

Create Express APIs to serve CRUD APIs to your Web Application.

Usage

Start

First you need to install the package:

npm i bobby_query

To start the server:

npm start

Error Handling

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 })
})

Examples

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
     ]
    }

Standart Response

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

Custom Middlewares

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 )

Dependencies

  • No dependencies

ReqBobby Response

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"}
        }
    ]

About (/__about)

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)

Error (/__error)

This endpoint simply throws a JavaScript error, and catches all errors at the end.

Licence

This software is published under the MIT licence.

Example code

https://github.com/jonjj2016/bobby_express_app.git