SQLite Support

BlazeKit can automatically generate a full SQLite controller for your models. This controller includes the database setup and all basic CRUD operations — ready to use out of the box!

What BlazeKit Generates

For each model, BlazeKit creates an SQLite controller with:

  • SQLite Database Setup — A ready-to-use connection to your SQLite database.
  • CRUD Functions — Create, Read, Update, and Delete operations for the model.

Example Output

Suppose you have a model User:

schema.blaze

model User {
  name: string
  email: string
  age: number
}

BlazeKit will generate the following SQLite controller:

controllers/user.ts

// SQLite Setup (auto-generated)
import db from "../lib/sqlite";
import { User } from "../types";

// CRUD Operations for User

export function createUser(data: User) {
  const keys = Object.keys(data);
  const values = Object.values(data);
  const placeholders = keys.map(() => '?').join(', ');
  const stmt = db.prepare(`INSERT INTO users (${keys.join(", ")}) VALUES (${placeholders})`);
  const result = stmt.run(...values);
  return { id: result.lastInsertRowid, ...data };
}

export function getAllUsers() {
  const stmt = db.prepare("SELECT * FROM users");
  return stmt.all();
}

export function getUserById(id: number) {
  const stmt = db.prepare("SELECT * FROM users WHERE id = ?");
  return stmt.get(id);
}

export function updateUser(id: number, data: Partial<User>) {
  const keys = Object.keys(data);
  const values = Object.values(data);
  const setClause = keys.map(k => `${k} = ?`).join(", ");
  const stmt = db.prepare(`UPDATE users SET ${setClause} WHERE id = ?`);
  stmt.run(...values, id);
  return getUserById(id);
}

export function deleteUser(id: number) {
  const stmt = db.prepare("DELETE FROM users WHERE id = ?");
  stmt.run(id);
  return { id };
}

Notes

  • BlazeKit assumes you have a `db` instance exported from lib/sqlite.ts (you can customize this).
  • Each model’s table name is automatically pluralized based on the model name (e.g., Userusers).
  • This setup uses the `better-sqlite3` library for synchronous queries, which is ideal for small or local deployments.