Grid
ORM

Drizzle ORM Forms

Lightweight, edge-ready form data mutations with type-safe schema validation and Drizzle ORM.

Preview

Drizzle ORM Edge SQL

Drizzle Edge API Key Form

Direct SQL-like TypeScript query building with zero overhead, instant returning statements, and native edge compatibility.

Hash stored with argon2id
Interactive responsive preview
Drag handles on right and bottom to resize

Overview

Drizzle ORM provides a thin, TypeScript-first SQL schema definition and query builder. It has zero dependencies, generates predictable SQL, and runs seamlessly in serverless, edge, and traditional Node.js environments.

Schema Definition with drizzle-zod

// db/schema.ts
import { pgTable, text, timestamp, integer, uuid } from 'drizzle-orm/pg-core';
import { createInsertSchema } from 'drizzle-zod';

export const apiKeys = pgTable('api_keys', {
  id: uuid('id').defaultRandom().primaryKey(),
  name: text('name').notNull(),
  environment: text('environment').notNull().default('live'),
  keyHash: text('key_hash').notNull(),
  rateLimit: integer('rate_limit').notNull().default(500),
  expiresAt: timestamp('expires_at'),
  createdAt: timestamp('created_at').defaultNow().notNull(),
});

// Auto-derive Zod validation schema from database columns
export const insertApiKeySchema = createInsertSchema(apiKeys);

Server Action with Drizzle Mutation

'use server';

import { db } from '@/db';
import { apiKeys, insertApiKeySchema } from '@/db/schema';
import { revalidatePath } from 'next/cache';
import crypto from 'crypto';

export async function createApiKeyAction(formData: FormData) {
  const parsed = insertApiKeySchema.omit({ id: true, keyHash: true }).safeParse(
    Object.fromEntries(formData)
  );

  if (!parsed.success) {
    return { success: false, errors: parsed.error.flatten().fieldErrors };
  }

  const rawSecret = `drz_${crypto.randomBytes(24).toString('hex')}`;
  const keyHash = crypto.createHash('sha256').update(rawSecret).digest('hex');

  const [created] = await db
    .insert(apiKeys)
    .values({
      ...parsed.data,
      keyHash,
    })
    .returning({
      id: apiKeys.id,
      name: apiKeys.name,
      createdAt: apiKeys.createdAt,
    });

  revalidatePath('/settings/api-keys');
  return { success: true, key: created, rawSecret };
}

Advantages of Drizzle ORM

  1. SQL-Like Simplicity: If you know SQL, you already know Drizzle. No abstract conceptual layer.
  2. Edge Compatibility: Perfect fit for Cloudflare Workers, Vercel Edge Functions, and Supabase Edge runtimes.
  3. drizzle-zod Pairing: Guarantees that form validation schemas and database column constraints never fall out of sync.
  4. Returning Clauses: Fetch freshly mutated rows directly with .returning() in a single round-trip.

On this page