Documentation
Goodies

Logging

To enable default query logging, just pass { logger: true } to the drizzle initialization function:

import { drizzle } from 'drizzle-orm/...'; // driver specific
 
const db = drizzle(pool, { logger: true });

You can change the logs destination by creating a DefaultLogger instance and providing a custom writer to it:

import { DefaultLogger, LogWriter } from 'drizzle-orm/logger';
import { drizzle } from 'drizzle-orm/...'; // driver specific
 
class MyLogWriter implements LogWriter {
  write(message: string) {
    // Write to file, stdout, etc.
  }
}
 
const logger = new DefaultLogger({ writer: new MyLogWriter() });
const db = drizzle(pool, { logger });

You can also create a custom logger:

import { Logger } from 'drizzle-orm/logger';
import { drizzle } from 'drizzle-orm/...'; // driver specific
 
class MyLogger implements Logger {
  logQuery(query: string, params: unknown[]): void {
    console.log({ query, params });
  }
}
 
const db = drizzle(pool, { logger: new MyLogger() });

Multi-project schema

table creator operator lets you define customise table names.
It's very useful when you need to keep schemas of different projects in one database.
For db push docs - see here

import { serial, text, pgTableCreator } from 'drizzle-orm/pg-core';
 
const pgTable = pgTableCreator((name) => `project1_${name}`);
 
const users = pgTable('users', {
  id: serial('id').primaryKey(),
  name: text('name').notNull(),
});

Printing SQL query

You can print SQL queries with db instance or by using standalone query builder

const query = db
  .select({ id: users.id, name: users.name })
  .from(users)
  .groupBy(users.id)
  .toSQL();
// query:
{
  sql: 'select 'id', 'name' from 'users' group by 'users'.'id'',
  params: [],
}

Raw SQL queries execution

If you have some complex queries to execute and drizzle-orm can't handle them yet, you can use the db.execute method to execute raw parametrized queries.

const statement = sql`select * from ${users} where ${users.id} = ${userId}`;
const res: postgres.RowList<Record<string, unknown>[]> = await db.execute(sql``)

Standalone query builder

Drizzle ORM provides a standalone query builder that allows you to build queries without creating a database instance and get generated SQL

import { QueryBuilder } from 'drizzle-orm/pg-core';
 
const qb = new QueryBuilder();
 
const query = qb.select().from(users).where(eq(users.name, 'Dan'));
const { sql, params } = query.toSQL();

Get typed table columns

You can get a typed table columns map, very useful when you need to omit certain columns upon selection

index.ts
schema.ts
import { getTableColumns } from "drizzle-orm";
import { user } from "./schema";
 
const { password, role, ...rest } = getTableColumns(user);
 
await db.select({ ...rest }).from(users);

Get table information

import { getTableConfig, pgTable } from 'drizzle-orm/pg-core';
 
export const table = pgTable(...);
 
const {
  columns,
  indexes,
  foreignKeys,
  checks,
  primaryKeys,
  name,
  schema,
} = await getTableConfig(table);

Compare objects types (instanceof alternative)

You can check if an object is of a specific Drizzle type using the is() function. You can use it with any available type in Drizzle.

💡

You should always use is() instead of instanceof

Few examples

import { Column, is } from 'drizzle-orm';
 
if (is(value, Column)) {
  // value's type is narrowed to Column
}