Skip to content

A JavaScript library to convert a json filter to SQL Where Clause

License

Notifications You must be signed in to change notification settings

jugnuagrawal/where-in-json

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

where-in-json

A JavaScript library to convert a json filter to SQL Where Clause.

How to use

const parser = require('where-in-json');

Example 1

const filter = {
    name: 'John',
    email: 'John@doe.com',
    contactNo: 1231231230
};

console.log(parser.toWhereClause(filter));

//OUTPUT: name='John' AND email='John@doe.com' AND contactNo=1231231230

Example 2

const filter = {
    $and: [
        { name: 'John' },
        {
            $or: [
                { email: 'John@doe.com' },
                { contactNo: 1231231230 }
            ]
        }
    ]
};

console.log(parser.toWhereClause(filter));

//OUTPUT: (name='John' AND (email='John@doe.com' OR contactNo=1231231230))

Example 3

const filter = {
    name: 'John',
    $or: [
        {
            email: {
                $ne: 'John@doe.com'
            }
        },
        { contactNo: 1231231230 }
    ]
};

console.log(parser.toWhereClause(filter));

//OUTPUT: name='John' AND (email <> 'John@doe.com' OR contactNo=1231231230)

Example 4

const filter = {
    name: 'John',
    age: {
        $and: [
            { $gte: 18 },
            { $lte: 25 }
        ]
    }
};

console.log(parser.toWhereClause(filter));

//OUTPUT: name='John' AND (age >= 18 AND age <= 25)

Example 5

const filter = {
    $and: [
        { name: 'John' },
        {
            age: { $gte: 18 }
        },
        {
            age: { $lte: 25 }
        }
    ]
};

console.log(parser.toWhereClause(filter));

//OUTPUT: (name='John' AND age >= 18 AND age <= 25)

About

A JavaScript library to convert a json filter to SQL Where Clause

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published