A fluent interface implementation for creating different SQLite queries in TypeScript without having to write SQL directly.
README.md

SQLite Fluent Interface #

A fluent interface implementation for writing SQLite queries within TypeScript and generating useful types based on the created queries.

Examples #

Creating a user table #

const users_table = createTable("users")
		.ifNotExists()
		.withPrimaryKeyColumn("id", "INTEGER")
		.withRequiredColumn("name", "TEXT")
		.withRequiredColumn("username", "TEXT")
		.withOptionalColumn("age", "INTEGER");

// Generates the structure of the table as it passes through the fluent interface.

actors_table.table.primaryKey.id;
actors_table.table.alternateKeys.name;
actors_table.table.alternateKeys.username;
actors_table.table.alternateKeys.age;

Generating a SQLite table from the user table #

console.debug(users_table.build());
CREATE TABLE users IF NOT EXISTS (
 id INTEGER PRIMARY KEY,
 name TEXT NOT NULL,
 username TEXT NOT NULL,
 age INTEGER
);

Inferring types from SQLite table #

type User = InferFromTable<typeof users_table.table>;

// Shows up as

type User = {
    id: number;
    name: string;
    username: string;
    age: number | null;
}