An ORM simplifies working with Hydra. It turns tables into objects, queries into code. Less SQL, more logic. ORMs unlock efficient data modeling and transaction management. Hydra is Postgres so all existing ORM integrations with Postgres are compatible. In this guide we will focus on Drizzle ORM, but feel free to connect with your preferred ORM by navigating to their Postgres documentation.

Drizzle ORM

This guide assumes familiarity with:

Drizzle has native support for PostgreSQL connections with the node-postgres and postgres.js drivers.

There are a few differences between the node-postgres and postgres.js drivers that we discovered while using both and integrating them with the Drizzle ORM. For example:

  • With node-postgres, you can install pg-native to boost the speed of both node-postgres and Drizzle by approximately 10%.

  • node-postgres supports providing type parsers on a per-query basis without globally patching things. For more details, see Types Docs.

  • postgres.js uses prepared statements by default, which you may need to opt out of.

node-postgres

Step 1 - Install packages

npm i drizzle-orm pg
npm i -D drizzle-kit @types/pg

Step 2 - Initialize the driver and make a query

// Make sure to install the 'pg' package 
import { drizzle } from 'drizzle-orm/node-postgres';

const db = drizzle(process.env.DATABASE_URL);
 
const result = await db.execute('select 1');

If you need to provide your existing driver:

// Make sure to install the 'pg' package 
import { pgTable, serial, text, varchar } from "drizzle-orm/pg-core";
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";

const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
});
const db = drizzle({ client: pool });
 
const result = await db.execute('select 1');

postgres.js

Step 1 - Install packages

npm i drizzle-orm postgres
npm i -D drizzle-kit

Step 2 - Initialize the driver and make a query

import { drizzle } from 'drizzle-orm/postgres-js';

const db = drizzle(process.env.DATABASE_URL);

const result = await db.execute('select 1');

If you need to provide your existing driver:

// Make sure to install the 'postgres' package
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';

const queryClient = postgres(process.env.DATABASE_URL);
const db = drizzle({ client: queryClient });

const result = await db.execute('select 1');

What’s Next?

We recommend