Skip to content
/ ts-sql Public

A SQL database implemented purely in TypeScript type annotations.

Notifications You must be signed in to change notification settings

codemix/ts-sql

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

 β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„  β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„               β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„  β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„  β–„
β–β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–Œβ–β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–Œ             β–β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–Œβ–β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–Œβ–β–‘β–Œ
 β–€β–€β–€β–€β–ˆβ–‘β–ˆβ–€β–€β–€β–€ β–β–‘β–ˆβ–€β–€β–€β–€β–€β–€β–€β–€β–€              β–β–‘β–ˆβ–€β–€β–€β–€β–€β–€β–€β–€β–€ β–β–‘β–ˆβ–€β–€β–€β–€β–€β–€β–€β–ˆβ–‘β–Œβ–β–‘β–Œ
     β–β–‘β–Œ     β–β–‘β–Œ                       β–β–‘β–Œ          β–β–‘β–Œ       β–β–‘β–Œβ–β–‘β–Œ
     β–β–‘β–Œ     β–β–‘β–ˆβ–„β–„β–„β–„β–„β–„β–„β–„β–„  β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„β–„ β–β–‘β–ˆβ–„β–„β–„β–„β–„β–„β–„β–„β–„ β–β–‘β–Œ       β–β–‘β–Œβ–β–‘β–Œ
     β–β–‘β–Œ     β–β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–Œβ–β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–Œβ–β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–Œβ–β–‘β–Œ       β–β–‘β–Œβ–β–‘β–Œ
     β–β–‘β–Œ      β–€β–€β–€β–€β–€β–€β–€β–€β–€β–ˆβ–‘β–Œ β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€  β–€β–€β–€β–€β–€β–€β–€β–€β–€β–ˆβ–‘β–Œβ–β–‘β–ˆβ–„β–„β–„β–„β–„β–„β–„β–ˆβ–‘β–Œβ–β–‘β–Œ
     β–β–‘β–Œ               β–β–‘β–Œ                       β–β–‘β–Œβ–β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–Œβ–β–‘β–Œ
     β–β–‘β–Œ      β–„β–„β–„β–„β–„β–„β–„β–„β–„β–ˆβ–‘β–Œ              β–„β–„β–„β–„β–„β–„β–„β–„β–„β–ˆβ–‘β–Œ β–€β–€β–€β–€β–€β–€β–ˆβ–‘β–ˆβ–€β–€ β–β–‘β–ˆβ–„β–„β–„β–„β–„β–„β–„β–„β–„
     β–β–‘β–Œ     β–β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–Œ             β–β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–Œ        β–β–‘β–Œ  β–β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–‘β–Œ
      β–€       β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€               β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€          β–€    β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€β–€

This is a SQL database implemented purely in TypeScript type annotations.
This means that it operates solely on types - you define a "database" (just a type annotation) and then query it using some more type annotations.

It supports a subset of SQL, including SELECT (with conditions and joins), INSERT, UPDATE and DELETE statements.

You can install ts-sql in your own project with npm install @codemix/ts-sql or yarn add @codemix/ts-sql (TypeScript 4.1 is required).

An example query looks like this:

import { Query } from "@codemix/ts-sql";

const db = {
  things: [
    { id: 1, name: "a", active: true },
    { id: 2, name: "b", active: false },
    { id: 3, name: "c", active: true },
  ],
} as const;

type ActiveThings = Query<
  "SELECT id, name AS nom FROM things WHERE active = true",
  typeof db
>;

// ActiveThings is now equal to the following type:
type Expected = [{ id: 1; nom: "a" }, { id: 3; nom: "c" }];

See the full demo on the TypeScript playground!