Skip to content

xendarboh/kwildb-query-builder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

KwilDB-Query-Builder

  • A SQL query builder for KwilDB.
  • Built on top of Knex.js.
  • Built-in typescript support

Installation

npm install kwildb-query-builder

Initialization

import { readFileSync } from "fs";
import { KwilDB } from "kwildb-query-builder";

let privateKey = JSON.parse(readFileSync("./privateKey.json").toString());
let secret = "LT|UllzAI~.V-10!ITC&nf#IV;KJV6Tk";

const kwildb = new KwilDB({
  host: "test-db.kwil.xyz",
  protocol: "https",
  moat: "testdemo",
  privateKey: privateKey,
  secret,
});

Documentation

browse Documentation

Schema and Table builder

// create and drop schema

kwildb.createSchema(schemaName: string, sync = true)

kwildb.dropSchema(schemaName: string, cascade?: boolean, sync = true)

kwildb.dropSchemaIfExists(
    schemaName: string,
    cascade?: boolean,
    sync = true
  )


// create and drop table

kwildb.createTable(
    tableName: string,
    builder: (tableBuilder: Knex.CreateTableBuilder) => any,
    schemaName?: string,
    sync = true
  )

kwildb.dropTable(tableName: string, sync = true)

kwildb.dropTableIfExists(tableName: string, sync = true)

Example

const kwildb = new KwilDB({
  host: "test-db.kwil.xyz",
  protocol: "https",
  moat: "testdemo",
  privateKey: privateKey,
  secret,
});

// create a table
// second argument is a callback function to modify the table's structure using the knex.js schema-building commands.
await kwildb.createTable(
  "users",
  (builder) => {
    builder.increments("id"), builder.string("name"), builder.integer("age");
  },
  "testSchema"
);

// drop a schema
await kwildb.dropSchema("testSchema");

look at knex.js Schema Builder page for more info

QueryBuilder

kwildb.query(tableName: string, schemaName?: string, sync = false)

Example

const kwildb = new KwilDB({
  host: "test-db.kwil.xyz",
  protocol: "https",
  moat: "testdemo",
  privateKey: privateKey,
  secret,
});

// For read queries except execute() and first() methods you have to end the method chain with execute() method to run it.
const result = await kwildb
  .query("users", "testSchema")
  .select("name")
  .whereNull("age")
  .execute();

await kwildb.query("users").insert(
  [
    {
      name: "test1",
    },
    {
      name: "test2",
    },
  ],
  true
);

Supported methods

list of all available methods except execute() and first() methods are available at query builder documentation page

Syntax

Syntax of all available methods is the same as knex.js query builder syntax.

you can find knex.js query builder documentation page at http://knexjs.org/guide/query-builder

execute and first Query builder methods

execute

execute(sync?: boolean)

For read queries except first() , you have to end the method chain with execute() method to run it.

const result = await kwildb.query("users").max("age as a").execute();

first

The select queries always return an array of objects, even when the query is intended to fetch a single row. However, using the first method will give you the first row or null (when there are no rows).

const user = await kwildb.query("users").where("id", 1).first();
if (user) {
  console.log(user);
}

Raw Query

kwildb.rawQuery(query: string, sync = false)
kwildb.preparedStatement(query: string, values: any[], sync = false)

License

Available under the MIT license. See the LICENSE file for more info.

About

SQL query builder for KwilDB

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published