GELLIFY.dev

Get started

Drizzle

Drizzle is a headless Typescript ORM with relational↗ and SQL-like↗ query APIs. It can handle database migrations and schemas, and provides a type safe database client. It also comes with Drizzle-Kit↗, a set of companion tools that help with querying your database.

Drizzle Client

The Drizzle Client is located at src/server/db/index.ts. In this file, you can define your database connection url and connect your schema to the database object.

import { neon, neonConfig, Pool } from "@neondatabase/serverless";
import { drizzle as drizzleHttp } from "drizzle-orm/neon-http";
import { drizzle as drizzleWs } from "drizzle-orm/neon-serverless";
import ws from "ws";

import { env } from "~/env";
import * as post from "./schema/posts";

let connectionString = env.DATABASE_URL;

const sql = neon(connectionString);
const pool = new Pool({ connectionString });

export const schema = { ...post };

// Drizzle supports both HTTP and WebSocket clients. Choose the one that fits your needs:
// HTTP Client:
// - Best for serverless functions and Lambda environments
// - Ideal for stateless operations and quick queries
// - Lower overhead for single queries
// - Better for applications with sporadic database access
export const drizzleClientHttp = drizzleHttp({
  client: sql,
  schema,
  logger: false, // env.NODE_ENV !== "production",
  casing: "snake_case",
});

// WebSocket Client:
// - Best for long-running applications (like servers)
// - Maintains a persistent connection
// - More efficient for multiple sequential queries
// - Better for high-frequency database operations
export const drizzleClientWs = drizzleWs({
  client: pool,
  schema,
  logger: env.NODE_ENV !== "production",
  casing: "snake_case",
});

export const db = drizzleClientWs;

Schema

The Drizzle schema file can be found at src/server/db/schema/**/*.ts. This file is where you can define your database schema and models, and connects to the Drizzle Client.

Drizzle Kit

Drizzle Kit is a collection of command line tools designed to help you manage your database. GELLIFY Stack automatically includes drizzle kit.

"scripts": {
  ...
  "db:generate": "drizzle-kit generate",
  "db:migrate": "drizzle-kit migrate",
  "db:push": "drizzle-kit push",
  "db:studio": "drizzle-kit studio",
  ...
},

Script Explanations

  • db:generate Generates TypeScript types and models from your database schema, ensuring type safety and easy integration with Drizzle ORM.
  • db:migrate Applies pending migrations to your database, keeping your schema in sync with changes and updates in your project.
  • db:push Pushes local schema changes directly to the database without needing explicit migration files. This can be useful for quick syncing in development.
  • db:studio Opens a visual interface for managing and inspecting your database tables, data, and relationships.

Useful Resources