rembrembdocs

WARNING

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

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

Step 1 - Install packages

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

Step 2 - Initialize the driver and make a query

// 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');
// Make sure to install the 'pg' package 
import { drizzle } from 'drizzle-orm/node-mssql';

// You can specify any property from the mssql connection options
const db = drizzle({ 
  connection: { 
    connectionString: process.env.DATABASE_URL,
    ssl: true
  }
});
 
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?