Drizzle <> MSSQL

WARNING

This page explains concepts available on drizzle versions 1.0.0-beta.2 and higher.


This guide assumes familiarity with:

Drizzle has native support for MSSQL connections with the mssql driver.

Step 1 - Install packages

npm
yarn
pnpm
bun
npm i drizzle-orm@beta mssql
npm i -D drizzle-kit@beta

Step 2 - Initialize the driver and make a query

mssql
mssql with config
// Make sure to install the 'mssql' package 
import { drizzle } from 'drizzle-orm/node-mssql';

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

As long as the node-mssql driver requires await on Pool initialization, we need to await it before each request - unless you are providing your own Pool instance to Drizzle. In that case, when you want to access db.$client, you first need to await it, and then use it

const awaitedClient = await db.$client;
const response = awaitedClient.query...

If you need to provide your existing driver:

// Make sure to install the 'mssql' package 
import { drizzle } from "drizzle-orm/node-mssql";
import type { ConnectionPool } from 'mssql';

const pool = await mssql.connect(connectionString);
const db = drizzle({ client: pool });
 
const result = await db.execute('select 1');

What’s next?