Skip to content
This repository was archived by the owner on Jan 17, 2022. It is now read-only.

NateLevin1/JSQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

7df6a46 Β· Nov 24, 2020

History

78 Commits
Nov 23, 2020
Oct 31, 2020
Nov 24, 2020
Nov 24, 2020
Nov 24, 2020
Oct 24, 2020
Nov 23, 2020
Nov 10, 2020
Nov 24, 2020
Sep 26, 2020
Oct 24, 2020
Oct 9, 2020
Nov 23, 2020
Nov 23, 2020
Oct 24, 2020
Oct 27, 2020

Repository files navigation

The JSQL Logo

SQL for the Browser

JSQL allows developers to write SQL queries that run in the browser.
It exposes a modern, promise-based API built on DexieJS.

Features

  • Custom Databases and Tables
  • Unlimited database size and permanent changes through IndexedDB
  • ES Module based consumption allows for tree shaking and small bundle sizes
  • 100% test coverage in all metrics
  • Promise-based API which takes advantage of async/await

Examples

const tbl = new Table(`CREATE TABLE tbl (
  id AUTO_INCREMENT,
  firstName,
  lastName,
  email
)`);
await tbl.create();
if(await tbl.isEmpty()) {
  await tbl.query(`INSERT INTO ${tbl.name} VALUES
  ('John', 'Doe', '[email protected]'),
  ('Jane', 'Doe', '[email protected]'),
  ('Joe', 'Schmoe', '[email protected]'),
  ('Jill', 'Schmoe', '[email protected]')`);
}
const result = await tbl.query(`SELECT * FROM ${tbl.name} WHERE lastName = 'Doe'`);
console.log(result);
/* 
[
  { id:1, firstName: "John", lastName:"Doe", email:"johndoe@example.com"},
  { id:2, firstName: "Jane", lastName:"Doe", email:"janedoe@example.com"}
] 
*/
// create the database
const library = await new Database(`CREATE DATABASE library`).create();

// create the tables
const books = await new Table(`CREATE TABLE books (
  id AUTO_INCREMENT,
  title,
  author,
  isCheckedOut
)`, library).create();
const users = await new Table(`CREATE TABLE users (
  id AUTO_INCREMENT,
  firstName,
  lastName,
  hasCheckedOutBook,
  checkedOutBookTitle
)`, library).create();

// add data to the tables if they are empty
if(await books.isEmpty()) {
  await books.query(`INSERT INTO ${books.name} VALUES 
  ("Moby Dick", "Herman Melville",  false),
  ("War and Peace", "Leo Tolstoy",  false),
  ("Hamlet", "William Shakespeare", false)
  `)
}
if(await users.isEmpty()) {
  await users.query(`INSERT INTO ${users.name} VALUES
  ("Jane", "Doe", false, ""),
  ("John", "Doe", false, "")
  `);
}

// do something with that data
if(!(await users.query(`SELECT hasCheckedOutBook FROM ${users.name} WHERE id = 1`))[0]) {
  // if Jane hasn't checked out a book, check out Moby Dick for her
  users.query(`UPDATE ${users.name} SET hasCheckedOutBook = true, checkedOutBookTitle = "Moby Dick" WHERE id = 1`);
}

// log out the new user data
console.log(await users.query(`SELECT * FROM ${users.name}`));

Click here to visit the JSQL language docs.

Click here to see the API references.

Click here for more info on JSQL.