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

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

PostgreSQL connection setup

Connect Drizzle ORM to a PostgreSQL database using node-postgres by establishing a connection in your application.

PostgreSQL local testing with Docker

If you do not have a PostgreSQL database yet and want to create one for testing, Drizzle provides a guide on how to set up PostgreSQL in Docker at /docs/guides/postgresql-local-setup.

PostgreSQL introspection workflow

When working with an existing PostgreSQL database, use Drizzle's introspect command to generate schema files from your database structure.

PostgreSQL DATABASE_URL environment variable

Set up a DATABASE_URL environment variable to store your PostgreSQL connection string.

PostgreSQL node-postgres installation

Install the node-postgres package with npm install pg and its type definitions with npm install -D @types/pg.

PostgreSQL setup prerequisites

To set up Drizzle ORM with PostgreSQL in an existing project, you need three prerequisite packages: dotenv for managing environment variables, tsx for running TypeScript files, and node-postgres for querying your PostgreSQL database.

PGLite environment variable

When setting up Drizzle ORM with PGLite, use the DATABASE_URL environment variable to store the connection string.

PGLite Drizzle config dialect

When configuring Drizzle ORM for PGLite, set the dialect to 'postgresql' in the Drizzle config file.

PGLite integration steps overview

The steps to integrate Drizzle ORM with PGLite in an existing project are: install packages, setup connection variables, setup Drizzle config file with postgresql dialect, introspect the database, transfer code to schema file, connect Drizzle ORM to the database, query the database, run the index.ts file, optionally update table schema, optionally apply changes to the database, and optionally query with new fields.

PGLite setup prerequisites

To set up Drizzle ORM with PGLite in an existing project, you need dotenv for managing environment variables, tsx for running TypeScript files, ElectricSQL, and the pglite driver.

SingleStore environment variable setup

The primary environment variable needed for SingleStore connection in Drizzle ORM is DATABASE_URL, which should be configured in your environment.

Install @planetscale/database package

Install the @planetscale/database package to connect Drizzle ORM to PlanetScale.

PlanetScale connection environment variables

To connect to PlanetScale, set these environment variables in a .env file: DATABASE_HOST, DATABASE_USERNAME, and DATABASE_PASSWORD. These values can be obtained from the PlanetScale docs for the serverless driver.

PlanetScale also supports PostgreSQL

PlanetScale offers a PostgreSQL option in addition to MySQL. The PostgreSQL setup is documented in a separate PlanetScale Postgres guide.

PlanetScale HTTP connection with database-js driver

PlanetScale can be connected via HTTP calls using the database-js driver for serverless connections. For TCP connections to PlanetScale, refer to the MySQL Get Started guide instead.

SQLite existing project setup workflow

The workflow for setting up Drizzle ORM with an existing SQLite project consists of: install @libsql/client package, setup connection variables in environment, setup Drizzle config file with sqlite dialect, introspect the existing database, transfer introspected code to schema file, connect Drizzle ORM to the database, and query the database. Optional steps include updating table schema and applying changes to the database.

SQLite existing project setup prerequisites

To set up Drizzle ORM with SQLite in an existing project, you need three packages installed: dotenv for managing environment variables, tsx for running TypeScript files, and libsql which is a fork of SQLite optimized for low query latency suitable for global applications.

Get Started with SQLite prerequisites

To follow the SQLite get started guide, you need dotenv for managing environment variables, tsx for running TypeScript files, and libsql which is a fork of SQLite optimized for low query latency suitable for global applications.

LibSQL database file format for local development

When using LibSQL for local SQLite database files, the connection string must be prefixed with 'file:' before the filename. For example, to create a local database file named 'local.db' in the project root, use the format: file:local.db

SQLite drivers supported by Drizzle

Drizzle ORM has native support for SQLite connections with three drivers: libsql, node:sqlite, and better-sqlite3.

DATABASE_URL environment variable for Supabase

Supabase connections use the DATABASE_URL environment variable to store the connection string.

Supabase setup steps overview

Setting up Drizzle with Supabase involves 11 steps: install the postgres package, setup connection variables, setup the Drizzle config file, introspect the database, transfer introspected code to the schema file, connect Drizzle ORM to the database, query the database, run the index.ts file, optionally update the table schema, optionally apply changes to the database, and optionally query with the new field.

Postgres driver package for Supabase

To connect to Supabase (which uses PostgreSQL), install the postgres package as the database driver.

SQLite Cloud connection setup with Drizzle ORM

To connect Drizzle ORM to SQLite Cloud, install drizzle-orm, @sqlitecloud/drivers, and dotenv. Set up a SQLITE_CLOUD_CONNECTION_STRING environment variable, configure Drizzle with sqlite dialect, and import drizzle from 'drizzle-orm/sqlite-cloud'. Call drizzle() without arguments to create a database instance that reads from the environment variable.

Supabase DATABASE_URL environment variable

Supabase projects require a DATABASE_URL environment variable to be set up for Drizzle ORM connection configuration.

Supabase setup prerequisites

To set up Drizzle ORM with Supabase, you need dotenv for managing environment variables, tsx for running TypeScript files, and Supabase as the database provider.

Supabase postgres connection package

When connecting Drizzle ORM to Supabase, install the postgres package for database connectivity.

SingleStore connection setup steps

Setting up Drizzle with SingleStore involves eight steps: install the mysql2 package, set up connection variables in environment configuration, connect Drizzle ORM to the database, create a table, set up the Drizzle config file with singlestore dialect, apply changes to the database, seed and query the database, and run the TypeScript file.

Prerequisites for SingleStore and Drizzle setup

To set up Drizzle with SingleStore, you need three packages: dotenv for managing environment variables, tsx for running TypeScript files, and mysql2 for querying your SingleStore database.

mysql2 is the required package for SingleStore

The mysql2 package is a MySQL client for Node.js with a focus on performance. It is used with Drizzle ORM to connect to SingleStore databases.

SQLite Cloud connection environment variable

The environment variable for SQLite Cloud connection is named SQLITE_CLOUD_CONNECTION_STRING and should be configured during the setup process.

SQLite Cloud package installation

Install the following packages for SQLite Cloud with Drizzle ORM: drizzle-orm@rc, @sqlitecloud/drivers, and dotenv as regular dependencies, plus drizzle-kit@rc and tsx as dev dependencies.

SQLite Cloud prerequisites

To get started with Drizzle ORM and SQLite Cloud, you need: dotenv package for managing environment variables, tsx package for running TypeScript files, a SQLite Cloud database, and the SQLite Cloud driver (available at https://docs.sqlitecloud.io/docs/sdk-js-introduction and https://github.com/sqlitecloud/sqlitecloud-js).

Get started with SQLite Cloud setup steps

The complete setup process for Drizzle ORM with SQLite Cloud involves 8 steps: install required packages (drizzle-orm, @sqlitecloud/drivers, dotenv, and dev dependencies drizzle-kit and tsx), setup connection variables via environment variables using SQLITE_CLOUD_CONNECTION_STRING, connect Drizzle ORM to the database, create a table in the schema, setup the Drizzle config file with sqlite dialect, apply changes to the database using migrations, seed and query the database, and finally run the index.ts file.

drizzle-orm/sqlite-cloud import

Import Drizzle ORM for SQLite Cloud using `import { drizzle } from 'drizzle-orm/sqlite-cloud'` and instantiate without parameters as `const db = drizzle()` when the connection string is set via environment variable.

TiDB serverless driver package

Install the @tidbcloud/serverless package to connect Drizzle ORM to TiDB using HTTP external connections.

TiDB Drizzle setup steps overview

To set up Drizzle with TiDB: install @tidbcloud/serverless, setup DATABASE_URL environment variable, connect Drizzle ORM to the database, create a table, setup the Drizzle config file with mysql dialect, apply changes using drizzle-kit, seed and query the database, then run the index.ts file.

TiDB HTTP vs TCP connection

Drizzle ORM provides the @tidbcloud/serverless driver for making HTTP calls to TiDB. If you need to connect to TiDB through TCP instead of HTTP, refer to the MySQL Get Started documentation.

TiDB setup step 5 transfer code to schema

The fifth step is to transfer code to your actual schema file.

TiDB setup step 9 update schema optional

The ninth step is to update your table schema, which is optional.

TiDB setup step 8 run index file

The eighth step is to run the index.ts file.

TiDB setup step 6 connect drizzle to database

The sixth step is to connect Drizzle ORM to the TiDB database.

TiDB setup step 4 introspect database

The fourth step is to introspect your TiDB database.

TiDB setup step 2 connection variables

The second step is to setup connection variables using the DATABASE_URL environment variable.

TiDB setup step 1 install serverless package

The first step to get started with Drizzle and TiDB in an existing project is to install the @tidbcloud/serverless package.

TiDB get started prerequisites

To get started with Drizzle and TiDB in an existing project, you need: dotenv package for managing environment variables, tsx package for running TypeScript files, TiDB (The Distributed SQL Database by PingCAP), and serverless-js package for serverless and edge compute platforms that require HTTP external connections.

Turso database connection import path

Import the Drizzle ORM connection for Turso database from 'drizzle-orm/tursodatabase/database' using: import { drizzle } from 'drizzle-orm/tursodatabase/database'

Turso database connection initialization

Initialize a Turso database connection by calling drizzle() with no arguments: const db = drizzle();

Required packages for Turso database setup

To use Drizzle ORM with Turso database in an existing project, install: drizzle-orm (rc version), @tursodatabase/database, dotenv (for environment variables), and as dev dependencies: drizzle-kit (rc version) and tsx (for running TypeScript files).

PlanetScale existing project setup workflow

The steps to set up Drizzle ORM with PlanetScale on an existing project are: 1) Install @planetscale/database package, 2) Setup connection variables in environment, 3) Setup Drizzle config file with mysql dialect, 4) Introspect the database, 5) Transfer introspected code to schema file, 6) Connect Drizzle ORM to the database, 7) Query the database, 8) Run the TypeScript file, 9-11) Optionally update table schema and apply changes.

PlanetScale HTTP connection setup with database-js driver

To connect to PlanetScale using HTTP calls, install the @planetscale/database package. Create a drizzle instance by calling drizzle({ connection: { host, username, password } }) from the 'drizzle-orm/planetscale-serverless' module. The host, username, and password are passed as environment variables.

PlanetScale MySQL setup prerequisites

Setting up Drizzle ORM with PlanetScale MySQL in an existing project requires: dotenv package for managing environment variables, tsx package for running TypeScript files, a PlanetScale MySQL database platform account, and the database-js PlanetScale serverless driver.

PlanetScale TCP connection alternative

The @planetscale/database driver makes HTTP calls to PlanetScale. If you need to connect to PlanetScale through TCP instead, refer to the MySQL Get Started documentation.

Turso Database import path

Import Drizzle client for Turso using 'drizzle-orm/tursodatabase/database'.

Turso Database setup with Drizzle ORM

To set up Drizzle ORM with Turso Database, install the packages drizzle-orm@rc, @tursodatabase/database, dotenv, and as dev dependencies drizzle-kit@rc and tsx. Set up environment variables for database configuration, create a Drizzle schema with tables, configure drizzle.config.ts with sqlite dialect, and run migrations using drizzle-kit.

Turso Database required packages

Install drizzle-orm@rc, @tursodatabase/database, and dotenv as dependencies. Install drizzle-kit@rc and tsx as dev dependencies.

Initialize Turso Database connection without arguments

When calling drizzle() for Turso Database, no arguments are required. The function reads database connection details from environment variables.

Turso connection with Drizzle ORM - direct initialization

To connect Drizzle ORM to a Turso database, import drizzle from 'drizzle-orm/libsql' and initialize it with connection options. Pass the TURSO_DATABASE_URL and TURSO_AUTH_TOKEN as properties in the connection object. Example: const db = drizzle({ connection: { url: process.env.TURSO_DATABASE_URL!, authToken: process.env.TURSO_AUTH_TOKEN! } });

Turso environment variables required

Two environment variables must be configured for Turso connections: TURSO_DATABASE_URL and TURSO_AUTH_TOKEN. These should be stored in a .env file in the project root.

Turso connection code example with existing libsql client

import 'dotenv/config'; import { drizzle } from 'drizzle-orm/libsql'; import { createClient } from '@libsql/client'; const client = createClient({ url: process.env.TURSO_DATABASE_URL!, authToken: process.env.TURSO_AUTH_TOKEN! }); const db = drizzle({ client });

Give your agent this brain