Skip to content

ppiyush13/json-sql-utility

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

json-sql-utility

module verion and npm link bundlephobia minified size bundlephobia minizipped size

This module is useful for buidling SQL queries from JSON objects. Currently only SELECT query is supported.

Install

yarn add json-sql-utility
or
npm i json-sql-utility

Usage

Simple example

import { select } from "json-sql-utility"

const query = select({
    fields: ['A', 'B'],
    aggregation: [ 
        {
            fn: 'count',
            args: 'D',
            alias: 'CNT'
        }
    ],
    from: 'STUDENTS',
    where: [
        {
            operator: 'equal',
            field: 'NAME',
            value: 'Piyush'
        },
        {
            operator: '=',
            field: 'ROLL',
            value: [13],
        },
        {
            operator: 'OR',
            conditions: [
                {
                    operator: 'equal',
                    field: 'A',
                    value: 5,
                },
                {
                    operator: 'equal',
                    field: 'A',
                    value: 10,
                }
            ]
        },
        {
            operator: 'dateBetween',
            field: 'BIRTH',
            value: {
                start: new Date(1975, 0, 1),
                end: new Date(1999, 11, 31),
            }
        },
    ],
    groupBy: ['A', 'B', 'C'],
    orderBy: ['A'],
    misc: [
        "EXTERNAL NAME 'TestFuncs$MyMath.pow'"
    ],
});

console.log(query);

/*
Will output:

SELECT A, B, count(D) AS CNT FROM STUDENTS
WHERE NAME = 'Piyush' AND ROLL = 13 AND (A = 5 OR A = 10) AND 
BIRTH BETWEEN '1975-01-01' AND '1999-12-31'
GROUP BY A, B, C
ORDER BY A
EXTERNAL NAME 'TestFuncs$MyMath.pow'

*/