Giter Club home page Giter Club logo

sksql's Introduction

SKSQL is a SQL database for the web and node.js written in Typescript.

It uses as storage on the client, Shared Array Buffers. Allowing for fast communication between the main web page and web workers.

It can be used stand-alone as a SQL engine or with a server allowing for persistence and replication to other connected clients.

T-SQL inspired syntax with support for functions and procedures. Execute javascript functions in SQL statements and procedures.

Quick Example

SQL_STRING =

CREATE TABLE pirates (
	id uint32 IDENTITY(1,1),
	name VARCHAR(255),
	country VARCHAR(50),
	dob date,
	death date,
	bounty numeric(12,0)
);
INSERT INTO pirates (name, country, dob, death, bounty) VALUES
('Calico Jack John Rackham', 'England', '1682-12-26', '1720-11-18', 125000),
('Anne Bonny', 'Ireland', '1697-03-08', '1721-04-00', 80000),
('Bartholomew Roberts', 'Wales', '1682-05-17', '1722-02-10', 800000),
('Blackbeard (Edward Teach)', 'England', '1680-00-00', '1718-11-22', 900000);

Typescript

import * as sksql from "sksql";
let db = new sksql.SKSQL();
let pirates = new sksql.SQLStatement(db, SQL_STRING);
pirates.runSync();

let st = new sksql.SQLStatement(db, "SELECT name, country FROM pirates WHERE country IN ('Wales', 'Ireland')");
let res = st.runSync();

interface IPirateInfo {
	name: string;
	country: string;
}
let result = res.getRows<IPirateInfo>();
// result = [{name: "Anne Bonny", country: "Ireland"}, {name: "Bartholomew Roberts", country: "Wales"}]

Webworker

import {SKSQL, SQLStatement} from "sksql";
import * as fs from "fs";

let db = new SKSQL();
let sksqlData = fs.readFileSync("../dist/sksql.min.js").toString();
db.initWorkerPool(4, sksqlData);

let st = new SQLStatement(db, "SELECT name, country FROM pirates WHERE country IN ('Wales', 'Ireland')");
st.runOnWebWorker().then((result: SQLResult) => {
	let rows = result.getRows();
	
});

Connect to server:

import {SKSQL, SQLStatement} from "sksql";

// node-js only
import {WebSocket} from 'ws';
//@ts-ignore
global["WebSocket"] = WebSocket;

let db = new SKSQL();
let token = "";
let ok = await db.connectAsync("ws://localhost:32000", token, "ClientRW");
let sql = new SQLStatement(db, "SELECT * from table");
let result = sql.runSync();	

// this command will be executed locally and then sent to the server and to all connected clients
let sql2 = new SQLStatement(db, "INSERT INTO table(a) VALUES(@a);");
sql2.setParameter("@a", "Hello");
sql2.runSync();

Installation

NodeJS:

npm install sksql

Building from source:

git clone https://github.com/alexraffy/sksql.git sksql
cd sksql
npm run sk-00-runall

COLUMN TYPES SUPPORTED

Integers: INT, UINT8, UINT16, UINT32, UINT64, INT8, INT16, INT32, INT64 Numeric: NUMERIC(PRECISION, SCALE) Boolean: BOOL UTF-8 Strings: VARCHAR(DIMENSION)

SUPPORTED SQL STATEMENTS

SQL statements supported

  • CREATE TABLE: COLUMN_DEF IDENTITY (SEED, INCREMENT), COLUMN_DEF NOT NULL/NULL COLUMN_DEF DEFAULT EXPRESSION PRIMARY KEY [CONSTRAINT_NAME] (COLUMN,...) FOREIGN KEY [CONSTRAINT_NAME] (COLUMN ASC|DESC, ...) REFERENCES {TABLENAME} (FOREIGN_COLUMN,...) CHECK [CONSTRAINT_NAME] (BOOLEAN EXPRESSION)

  • INSERT: INSERT INTO {TABLE} (COLUMNS) VALUES (...),... INSERT INTO {TABLE} VALUES(COLUMN...),...

  • UPDATE: UPDATE [TOP(EXPRESSION)] SET COLUMN = EXPRESSION FROM {TABLE} [WHERE CLAUSE]

  • DELETE: DELETE [TOP(EXPRESSION)] FROM {TABLE} [WHERE CLAUSE]

  • SELECT: SELECT
    COLUMN | EXPRESSION [AS ALIAS],... FROM TABLE1, [JOIN | LEFT JOIN | RIGHT JOIN] TABLE2... [WHERE BOOLEAN_EXPRESSION] [ORDER BY COLUMN_A ASC | DESC, ...] [GROUP BY COLUMN_A ASC | DESC, ...] [HAVING EXPRESSION]

T-SQL Statements

  • CREATE PROCEDURE
  • CREATE FUNCTION
  • DECLARE
  • IF, WHILE
  • SET
  • EXECUTE
  • RETURN

sksql's People

Contributors

alexraffy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.