@elijahjcobb/sql-cmd

Documentation coming soon...

Usage no npm install needed!

<script type="module">
  import elijahjcobbSqlCmd from 'https://cdn.skypack.dev/@elijahjcobb/sql-cmd';
</script>

README

SQL-CMD

A SQL command builder using the 'modifier' paradigm.

Import

import { ECSQLCMD, ECSQLCMDQuery } from "@elijahjcobb/sql-cmd";

SELECT

Basic Query

const cmd: ECSQLCMD = ECSQLCMD
    .select("tab")
    .where("key", "<=", 10);

cmd.generate() // => SELECT * FROM tab WHERE (key<=10);

Nested Conditions

const cmd: ECSQLCMD = ECSQLCMD
    .select("tab")
    .whereThese(ECSQLCMDQuery
        .or()
        .whereThese(ECSQLCMDQuery
            .and()
            .where("age", ">=", 18)
            .where("age", "<=", 21)
            .whereThese(ECSQLCMDQuery
                .or()
                .where("gender", "=", true)
                .where("isCool", "!=", false)
            )
        )
    .whereThese(ECSQLCMDQuery
        .and()
        .where("age", ">=", 13)
        .where("age", "<=", 16)
    )
);
cmd.generate() // string => SELECT * FROM tab WHERE ((age>=18 AND age<=21 AND (gender=true OR isCool!=false)) OR (age>=13 AND age<=16));

Delete

const cmd: ECSQLCMD = ECSQLCMD
    .delete("tab")
    .where("x", "=", "John");

cmd.generate() // => DELETE FROM tab WHERE (x='John');;

Insert

const data: Buffer = crypto.randomBytes(2);

const cmd: ECSQLCMD = ECSQLCMD
    .insert("tab")
    .set("name", "Elijah")
    .set("age", 20)
    .set("isMale", true)
    .set("buff", data);

cmd.generate() // => INSERT INTO tab (name, age, isMale, buff) VALUES ('Elijah', 20, true, <data as hex string>);

Update

const cmd: ECSQLCMD = ECSQLCMD
    .update("tab")
    .set("name", "Elijah")
    .where("id", "=", "xxx");

cmd.generate() // => UPDATE tab SET name='Elijah' WHERE (id='xxx');

Documentation

You can view the declaration files or even the source code on GitHub it is all written in TypeScript.

Bugs

If you find any bugs please create an issue on GitHub or if you are old fashioned email me at elijah@elijahcobb.com.