PostgreSQL Support

BlazeKit can generate a complete PostgreSQL controller for your models. The generated code uses the pg package and assumes a standard Next.js environment with your database URL stored in environment variables.

What BlazeKit Generates

For each model, BlazeKit creates a PostgreSQL controller with:

  • PostgreSQL Setup — A Pool instance from the pg package is automatically configured.
  • CRUD Functions — Create, Read, Update, and Delete operations mapped to your SQL tables.

Example Output

Suppose you have a model User:

schema.blaze

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

BlazeKit will generate the following PostgreSQL controller:

controllers/user.ts

// PostgreSQL Setup (auto-generated)
import { Pool } from "pg";
import { User } from "../types";

const pool = new Pool({
  connectionString: process.env.POSTGRES_URL,
});

// CRUD Operations for User

export async function createUser(data: User) {
  const keys = Object.keys(data);
  const values = Object.values(data);
  const placeholders = keys.map((_, i) => `$${i + 1}`).join(", ");
  const query = `INSERT INTO users (${keys.join(", ")}) VALUES (${placeholders}) RETURNING *`;

  const result = await pool.query(query, values);
  return result.rows[0];
}

export async function getAllUsers() {
  const result = await pool.query("SELECT * FROM users");
  return result.rows;
}

export async function getUserById(id: string) {
  const result = await pool.query("SELECT * FROM users WHERE id = $1", [id]);
  return result.rows[0];
}

export async function updateUser(id: string, data: Partial<User>) {
  const keys = Object.keys(data);
  const values = Object.values(data);
  const setClause = keys.map((key, i) => `${key} = $${i + 1}`).join(", ");
  const query = `UPDATE users SET ${setClause} WHERE id = $${keys.length + 1} RETURNING *`;

  const result = await pool.query(query, [...values, id]);
  return result.rows[0];
}

export async function deleteUser(id: string) {
  const result = await pool.query("DELETE FROM users WHERE id = $1 RETURNING *", [id]);
  return result.rows[0];
}

Notes

  • BlazeKit expects your PostgreSQL connection string to be available as POSTGRES_URL in your environment variables (or a custom name if configured).
  • Table names are generated by taking the model name in lowercase and adding an "s" (e.g., Userusers).
  • All queries are parameterized to prevent SQL injection.
  • Returned results include all columns from the table, including the id.