GithubHelp home page GithubHelp logo

generic-query-builder's Introduction

Generic Query Builder

Almost all projects use api and they are have backend developers.

This project fast development and productivity provide for small and micro products.

You can use this project have you headless cms or if you have small development team.

This project generate dynamic query build at runtime


Available Features Here

  • Select
  • Insert
  • Update
  • Upsert (update or insert)
  • Delete

How To Use

// step 1: install package
npm install generic-query-builder

// step 2: include project
import express from 'express'
import { useQueryMiddleware, ErrorFn, SuccessFn } from 'generic-query-builder'

const app = express()

// required
app.use(express.json())

const db = {
  "host": process.env['DB_HOST'],
  "user": process.env['DB_USER'],
  "password": process.env['DB_PASSWORD'],
  "database": process.env['DATABASE']
}

const errorFn: ErrorFn = (err, req, res, next) => {
  res.status(400)
  res.json(err)
}

const successFn: SuccessFn = (data, req, res, next) => {
  res.json(data)
}

const genericQueryBuilderMiddleware = useQueryMiddleware({
  itemsPerRequest: 1000,
  dbConfig: db,
  onSuccess: successFn,
  onError: errorFn
})

// Terms
// - All requests must be POST request
// - Must be a route like
// - Do not changes this
// - But you can addition prefix

app.post('/:action/:collection', genericQueryBuilderMiddleware)

app.listen(3000, () => console.log('http://localhost:3000'))

Query Examples

// your server like this: http://localhost:3000
// for example queries for products

Select Query Example

// endpoint: http://localhost:3000/read/products

// request body (not required)
{
  // optional columns taking
  "Columns": ["name", "id"]
  // optional taking limit
  "Limit": 1000
  // optional and conditions
  "Where": [{"Key": "id", "Value": 1, "Operator": ">" }],
  // optional or conditions
  "OrWhere": [{"Key": "id", "Value": 10, "Operator": "<" }],
  // optional order by
  "OrderBy":{"id": "asc"},
  // optional joins
  "Joins": [
    { 
      "Type": "LEFT", // INNER - RIGHT - FULL OUTER
      "Collection": "categories", 
      "PrimaryKey": "id", 
      "ParentKey": "category_id", 
      "Columns": [{"Alias": "category", "Column": "name"}]
    }
  ]
}

Insert Query Example

// endpoint: http://localhost:3000/create/products

// request body (required)
{
  "Record": [
    { "name": "apple" },
    { "name": "orange" }
  ]
}

Update Query Example

// endpoint: http://localhost:3000/update/products

// request body (required)
{
  "Record": { "name": "banana" },
  "Where": [
    { 
      "Key": "id", 
      "Value": 1, 
      "Operator": "=" 
    }
  ]
}

Upsert Query Example

// endpoint: http://localhost:3000/upsert/products

// request body (required)
{
  "Record": { "name": "cherry" },
  "Where": [
    { 
      "Key": "id", 
      "Value": 1, 
      "Operator": "=" 
    }
  ]
}

Delete Query Example

// endpoint: http://localhost:3000/delete/products

// request body (required)
{
  "Where": [
    { 
      "Key": "id", 
      "Value": 1, 
      "Operator": "=" 
    }
  ]
}

generic-query-builder's People

Contributors

erdalugur avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.