Redis Support

BlazeKit can automatically generate a Redis controller for your models. It uses simple key-based storage where each entry is stored as a JSON string under a unique key — no setup required beyond a running Redis instance.

What BlazeKit Generates

For each model, BlazeKit creates a Redis controller with:

  • Redis Client Setup — A fully configured Redis client using createClient from the redis npm package.
  • CRUD Functions — Key-based Create, Read, Update, and Delete operations with data serialized as JSON.
  • UUID Support — Each object is automatically assigned a UUID when created.

Example Output

Suppose you have a model User:

schema.blaze

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

BlazeKit will generate the following Redis controller:

controllers/user.ts

// Redis Setup (auto-generated)
import { createClient } from "redis";
import { User } from "../types";
import { randomUUID } from "crypto";

const redis = createClient({
  url: process.env.REDIS_URL || "redis://localhost:6379",
});

await redis.connect();

// CRUD Operations for User

export async function createUser(data: User) {
  const id = randomUUID();
  const key = "mydb:user:" + id;
  await redis.set(key, JSON.stringify({ ...data, id }));
  return { ...data, id };
}

export async function getAllUsers() {
  const keys = await redis.keys("mydb:user:*");
  const values = await Promise.all(keys.map((key) => redis.get(key)));
  return values.map((v) => v ? JSON.parse(v) : null).filter(Boolean);
}

export async function getUserById(id: string) {
  const key = "mydb:user:" + id;
  const value = await redis.get(key);
  return value ? JSON.parse(value) : null;
}

export async function updateUser(id: string, data: Partial<User>) {
  const existing = await getUserById(id);
  if (!existing) return null;
  const updated = { ...existing, ...data };
  const key = "mydb:user:" + id;
  await redis.set(key, JSON.stringify(updated));
  return updated;
}

export async function deleteUser(id: string) {
  const key = "mydb:user:" + id;
  return redis.del(key);
}

Notes

  • BlazeKit uses key patterns like database:model:id (e.g., mydb:user:abc123) to store each entry.
  • Redis values are stored as JSON strings and deserialized on access.
  • This setup uses Node’s native crypto.randomUUID() to generate unique IDs.
  • This Redis integration is ideal for lightweight, fast-access use cases such as caching, queues, or quick prototyping.