new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Drizzle ORM · all subjects

drizzle-orm/setup

326 notes in this subject, read out of this brain and free to use. This is page 6 of 6.

PlanetScale Postgres connection credentials source

Connection credentials for PlanetScale Postgres are obtained from the PlanetScale dashboard by navigating to your database, clicking 'Connect', and creating a 'Default role'.

Prerequisites for Drizzle with PlanetScale Postgres

To use Drizzle ORM with PlanetScale Postgres, install the following packages: dotenv (for managing environment variables), tsx (for running TypeScript files), and node-postgres (for querying PostgreSQL databases). Node-postgres provides the PostgreSQL driver.

Setting up Drizzle ORM connection to Nile Database

Create a db.ts file in src/db directory. Import drizzle from 'drizzle-orm/node-postgres' and initialize it with process.env.NILEDB_URL. Use AsyncLocalStorage to manage tenant context and create a tenantDB wrapper function that uses db.transaction to execute queries with the tenant ID set via sql`set local nile.tenant_id = '${sql.raw(tenantId)}'`.

Drizzle with Nile Database tutorial overview

This tutorial demonstrates how to use Drizzle ORM with Nile Database, which is Postgres re-engineered for multi-tenant applications. The tutorial walks through building a secure, scalable multi-tenant application using Drizzle with Nile's virtual tenant databases.

Prerequisites for Drizzle with Nile

Required packages: drizzle-orm@rc, drizzle-kit@rc, dotenv for environment variables, node-postgres for connecting to the Postgres database, and express for the web framework. The guide uses AsyncLocalStorage to manage tenant context; if your framework or runtime does not support AsyncLocalStorage, refer to the Drizzle<>Nile documentation for alternative options.

Nile database connection string format

The Nile connection string format is: postgres://youruser:you••••••rd@us-west-2.db.thenile.dev:5432:5432/your_db_name. This URL should be stored in the NILEDB_URL environment variable.

Tenant ID can be passed via multiple methods

Tenant ID can be obtained from path parameters, headers such as 'x-tenant-id', or cookies. The example gets it from path parameters but other methods are also common.

Project file structure for Drizzle with Nile

Recommended structure: src/db/ contains db.ts (connection) and schema.ts (schema definitions), src/app.ts contains the Express application, drizzle/ folder contains meta/ (snapshots), relations.ts, schema.ts, and SQL migration files, with .env, drizzle.config.ts, and package.json at the root.

Running Drizzle with Nile application

Run the application with `npx tsx src/app.ts`. The server listens on the port specified in the PORT environment variable or defaults to 3001. Routes can be tested using curl commands with JSON payloads.

Example Neon Postgres table schema

Example schema for Neon Postgres: import { pgTable, serial, text } from 'drizzle-orm/pg-core'; export const usersTable = pgTable('users_table', { id: serial('id').primaryKey(), name: text('name').notNull(), age: text('age').notNull(), email: text('email').notNull().unique() })

Edge-compatible drivers for Vercel Edge Functions

When using Drizzle ORM with Vercel Edge functions, you must use edge-compatible drivers because the functions run in Edge runtime, not Node.js runtime, which has limitations on standard Node.js APIs. The available edge-compatible drivers are: Neon serverless driver for Neon Postgres (queries over HTTP or WebSockets), Vercel Postgres driver built on Neon serverless driver, PlanetScale serverless driver for MySQL (queries over HTTP), and libSQL client for Turso database.

Turso (libSQL) driver installation and setup

To use Turso with Drizzle ORM: install @libsql/client package, create schema using sqliteTable from drizzle-orm/sqlite-core, configure drizzle.config.ts with dialect 'turso' and dbCredentials containing url and authToken environment variables, then initialize drizzle from drizzle-orm/libsql with connection object containing url and authToken.

Vercel Edge Function route configuration

For Vercel Edge Functions using Drizzle ORM, set export const dynamic = 'force-dynamic' and export const runtime = 'edge' in the route handler file. This ensures the route is not statically cached and runs on edge runtime.

Turso database initialization code

In src/db/index.ts, initialize Drizzle with Turso driver using: import { drizzle } from 'drizzle-orm/libsql'; export const db = drizzle({ connection: { url: process.env.TURSO_CONNECTION_URL!, authToken: process.env.TURSO_AUTH_TOKEN! }})

Vercel Postgres driver installation and setup

To use Vercel Postgres driver with Drizzle ORM: install @vercel/postgres package, create schema using pgTable from drizzle-orm/pg-core, configure drizzle.config.ts with dialect 'postgresql' and dbCredentials pointing to POSTGRES_URL environment variable, then use drizzle() from drizzle-orm/vercel-postgres without passing any arguments.

Vercel Postgres database initialization code

In src/db/index.ts, initialize Drizzle with Vercel Postgres driver using: import { drizzle } from 'drizzle-orm/vercel-postgres'; export const db = drizzle()

Example Turso SQLite table schema

Example schema for Turso: import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core'; export const usersTable = sqliteTable('users_table', { id: integer('id').primaryKey(), name: text('name').notNull(), age: text('age').notNull(), email: text('email').notNull().unique() })

Example PlanetScale MySQL table schema

Example schema for PlanetScale: import { mysqlTable, serial, text } from 'drizzle-orm/mysql-core'; export const usersTable = mysqlTable('users_table', { id: serial('id').primaryKey(), name: text('name').notNull(), age: text('age').notNull(), email: text('email').notNull().unique() })

PlanetScale MySQL serverless driver setup

To use PlanetScale with Drizzle ORM: install @planetscale/database package, create schema using mysqlTable from drizzle-orm/mysql-core, configure drizzle.config.ts with dialect 'mysql' and dbCredentials pointing to MYSQL_URL environment variable, then use drizzle() from drizzle-orm/planetscale-serverless passing process.env.MYSQL_URL as argument.

PlanetScale database initialization code

In src/db/index.ts, initialize Drizzle with PlanetScale serverless driver using: import { drizzle } from 'drizzle-orm/planetscale-serverless'; export const db = drizzle(process.env.MYSQL_URL!)

Neon serverless driver installation and setup

To use Neon serverless driver with Drizzle ORM: install @neondatabase/serverless package, create schema using pgTable from drizzle-orm/pg-core, configure drizzle.config.ts with dialect 'postgresql' and dbCredentials pointing to POSTGRES_URL environment variable, then use drizzle() from drizzle-orm/neon-serverless passing process.env.POSTGRES_URL as argument.

Neon serverless database initialization code

In src/db/index.ts, initialize Drizzle with Neon serverless driver using: import { drizzle } from 'drizzle-orm/neon-serverless'; export const db = drizzle(process.env.POSTGRES_URL!)

Xata connection string format

Xata PostgreSQL connection strings follow the format: postgresql://postgres:<pa••••••d>@<branch-id>.<region>.xata.tech/<database>?sslmode=require. For example: postgresql://postgres:password@t56hgfp7hd2sjfeiqcn66qpo8s.us-east-1.xata.tech/app?sslmode=require.

Connect Drizzle to Xata with postgres driver

To connect Drizzle ORM to a Xata database, create a database client using the postgres driver and the DATABASE_URL environment variable, then pass it to drizzle(). Load the environment variable using dotenv's config() function. Example: import { config } from 'dotenv'; import { drizzle } from 'drizzle-orm/postgres-js'; import postgres from 'postgres'; config({ path: '.env' }); const client = postgres(process.env.DATABASE_URL!); export const db = drizzle({ client });

Xata database setup requires postgres package

To use Drizzle ORM with Xata, install the postgres package (npm install postgres) for connecting to the Postgres database. Also install dotenv (npm install dotenv) for managing environment variables.

Xata features: branch-based development

Xata provides branch-based development, allowing developers to create isolated database branches for development, staging, and production environments. Different connection strings can be used for different branches, making it easy to test schema changes before deploying to production.

Give your agent this brain