Skip to content

TalkySafe143/PlainSQLBuilder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Plain SQL Builder

This package convert a JSON object into a SQL string to be executed. SQL dialect is treated as generic.

Usage/Examples

You can use the types from dist folder.

Warning

If you have to validate a string, please type '' in the field.

Warning

Make shure to have additional security for SQL Injection.

  • TypeScript
import { generateQuery } from 'plainsqlbuilder'
import { QueryObject } from 'plainsqlbuilder/dist/types/plainsqlbuilderTypes'

const query : QueryObject = {
    FROM: ['user'],
    WHERE: [
        {
            column: 'email',
            type: '=',
            value: "'[email protected]'",
            connector: 'AND'
        },
        {
            column: 'password',
            type: '=',
            value: "'hash'"
        }
    ]
}

// SELECT * FROM user WHERE email = ('[email protected]') AND password = ('hash')   ;
console.log(generateQuery(query));
import { generateQuery } from 'plainsqlbuilder'
import { QueryObject } from 'plainsqlbuilder/dist/types/plainsqlbuilderTypes'

const query : QueryObject = {
    SELECT: [{
        rename: 'NAME_UPPERCASE',
        column: "name"
    }],
    FROM: ['user'],
    WHERE: [
        {
            column: 'email',
            type: '=',
            value: "'[email protected]'",
            connector: 'AND'
        },
        {
            column: 'password',
            type: '=',
            value: "'hash'"
        }
    ]
}
// SELECT name AS NAME_UPPERCASE FROM user WHERE email = ('[email protected]') AND password = ('hash')   ;
console.log(generateQuery(query));
import { generateQuery } from 'plainsqlbuilder'
import { QueryObject } from 'plainsqlbuilder/dist/types/plainsqlbuilderTypes'

const query : QueryObject = {
    SELECT: ["name", "code"],
    FROM: ['user'],
    WHERE: [
        {
            column: 'email',
            type: '=',
            value: "'[email protected]'",
            connector: 'AND'
        },
        {
            column: 'password',
            type: '=',
            value: "'hash'"
        }
    ]
}
// SELECT name , code FROM user WHERE email = ('[email protected]') AND password = ('hash')   ;
console.log(generateQuery(query));
  • JavaScript
const { generateQuery } = require('plainsqlbuilder')

const query = {
    FROM: ['student'],
    WHERE: [
        {
            column: 'name',
            type: "=",
            value: "'Sebastian'"
        }
    ],
}


// SELECT * FROM student WHERE name = ('Sebastian')   ;
console.log(generateQuery(query));

Contributing

Contributions are always welcome! One great feature would be: Adding bindings to avoid SQL Injection.

About

Plain text SQL builder with JSON object

Resources

Stars

Watchers

Forks

Packages

No packages published