# Connect with Drizzle ORM

Drizzle ORM is a modern, lightweight ORM designed for TypeScript and JavaScript. You can connect to your SingleStore databases from Drizzle ORM using the SingleStore Drizzle ORM driver (`singlestore`).

## SingleStore Drizzle ORM Driver

Drizzle ORM natively supports `mysql2` with `drizzle-orm/singlestore` package for connectivity with SingleStore.

## Prerequisites

* [Install npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm).
* Install the `mysql2` package.
* [Install Drizzle Kit](https://www.npmjs.com/package/drizzle-kit) to manage database migrations using Drizzle.
* (Optional) [Install dotenv](https://www.npmjs.com/package/dotenv) package to manage environment variables.
* (Optional) [Install tsx](https://tsx.is/) to run TypeScript.

To install using `npm`:

```shell
npm i drizzle-orm mysql2 dotenv
npm i -D drizzle-kit tsx
```

## Connect to SingleStore using Drizzle ORM

To connect your Drizzle ORM application (built with Node.js) to SingleStore, initialize the Drizzle client from the SingleStore driver and pass the connection configuration to a `mysql2` connection.

Required connection configurations:

* `<hostname>`: Hostname or IP address of the SingleStore deployment.

  Refer to [SingleStore Helios Endpoints](https://docs.singlestore.com/cloud/connect-to-singlestore/singlestore-helios-endpoints.md) to determine the endpoint (`host:port`) of your deployment.
* `<port>`: Port of the SingleStore deployment.
* `<username>`: Name of the SingleStore database user with which to access the database.
* `<password>`: Password for the SingleStore database user.

## Using an Environment Variable

1. Create a `.env` file at the root of your project and add the connection URL in the `"mysql://<username>:<password>@<hostname>:<port>/<database>[?connection_options]"` format. For example:
   ```
   DATABASE_URL="mysql://s2user:pa55w04d@svchost:3306/dbTest"
   ```

2. Use either of the following code snippets to establish the connection:
   ```TypeScript
   import 'dotenv/config';
   import { drizzle } from "drizzle-orm/singlestore";
   const db = drizzle(process.env.DATABASE_URL);
   ```
   ```TypeScript
   // To specify additional mysql2 connection options
   import 'dotenv/config';
   import { drizzle } from "drizzle-orm/singlestore";
   const db = drizzle({ connection: { uri: process.env.DATABASE_URL }});
   ```

## Using `mysql.createConnection` Function

* ```TypeScript
  import { drizzle } from "drizzle-orm/singlestore";
  import mysql from "mysql2/promise";

  const connection = await mysql.createConnection("mysql://<username>:<password>@<hostname>:<port>/<database>?ssl={}");
  const db = drizzle({ client: connection });
  ```
* ```TypeScript
  import { drizzle } from "drizzle-orm/singlestore";
  import mysql from "mysql2/promise";

  const connection = await mysql.createConnection({
    host: "<hostname>",
    user: "<username>",
    database: "<database>",
    password: "<password>",
    ssl: {}
  });
  const db = drizzle({ client: connection });
  ```

## Example

The following example connects to a SingleStore database named `dbTest`, creates a table named `Stock`, and inserts data into the table.

1. Install the required packages.
   ```shell
   npm i drizzle-orm mysql2 dotenv
   npm i -D drizzle-kit tsx
   ```

2. Create a new project folder with the following structure:
   ```
   Drizzle
   ├── .env
   ├── app.ts
   ├── drizzle.config.ts
   └── db
       └── schema.ts
   ```

3. Assign the connection string to the `DATABASE_URL` variable in the `.env` file.

   **Note**: Update the connection string with the connection configuration of your SingleStore deployment.
   ```
   DATABASE_URL="mysql://s2user:pa55w04d@svchost:3306/dbTest"
   ```

4. Define the schema of the `Stock` table. Add the following code to `db/schema.ts` file:
   ```TypeScript
   import { int, bigint, text, singlestoreTable } from "drizzle-orm/singlestore-core";

   export const stock = singlestoreTable("Stock", {
     id: bigint("ID", { mode: "bigint" }).primaryKey().autoincrement(),
     code: text("Code"),
     quantity: int("Quantity"),
   });
   ```

5. Add the following configuration to the `drizzle.config.ts` file:
   ```TypeScript
   import { defineConfig } from "drizzle-kit";
   import 'dotenv/config';

   export default defineConfig({
     out: './drizzle',
     dialect: 'singlestore',
     schema: './db/schema.ts',
     dbCredentials: {
       url: process.env.DATABASE_URL!}
   })
   ```

6. Generate the Drizzle migrations.

   **Note**: To directly apply the schema changes without generating the migrations, run the `npx drizzle-kit push` command, and skip to step 8.
   ```shell
   npx drizzle-kit generate

   ```
   ```output

   Reading config file '/Drizzle/drizzle.config.ts'
   Reading schema files:
   /Drizzle/db/schema.ts
   1 tables
   Stock 3 columns 0 indexes 0 fks
   [✓] Your SQL migration file ➜ drizzle/0000_deep_the_call.sql
   ```
   This command creates a migration file (SQL file) that contains the SQL statements required to create the `Stock` table. For example:
   ```shell
   cat drizzle/0000_deep_the_call.sql

   ```
   ```output

   CREATE TABLE `Stock` (
   	`ID` bigint AUTO_INCREMENT NOT NULL,
   	`Code` text,
   	`Quantity` int,
   	CONSTRAINT `Stock_ID` PRIMARY KEY(`ID`)
   );
   ```

7. Apply this migration to the target SingleStore database.
   ```shell
   npx drizzle-kit migrate

   ```
   ```output

   migrations applied successfully!
   ```
   To verify that the migration is successfully applied, log in to your SingleStore deployment, and run the following commands:
   ```sql
   USE dbTest;
   DESC Stock;

   ```
   ```output

   +----------+------------+------+------+---------+----------------+
   | Field    | Type       | Null | Key  | Default | Extra          |
   +----------+------------+------+------+---------+----------------+
   | ID       | bigint(20) | NO   | PRI  | NULL    | auto_increment |
   | Code     | text       | YES  |      | NULL    |                |
   | Quantity | int(11)    | YES  |      | NULL    |                |
   +----------+------------+------+------+---------+----------------+
   ```

8. To insert data into the `Stock` table, update `app.ts` to the following:
   ```TypeScript
   import { stock } from "./db/schema.ts"
   import 'dotenv/config';
   import { drizzle } from "drizzle-orm/singlestore";

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

   async function main() {
       const newStock = [
         { code: 'xvf1', quantity: 40 },
         { code: 'gwl2', quantity: 15 },
         { code: 'cdq3', quantity: 25 }]
       
       await db.insert(stock).values(newStock);
       const rows = await db.select().from(stock);
       console.log(rows);
   }
   main();
   ```

9. Run the `app.ts` file.
   ```shell
   npx tsx app.ts

   ```
   ```output

   [
     { id: 3n, code: 'cdq3', quantity: 25 },
     { id: 1n, code: 'xvf1', quantity: 40 },
     { id: 2n, code: 'gwl2', quantity: 15 }
   ]
   ```
   Run the following command to verify that the rows are inserted:
   ```sql
   SELECT * FROM Stock;

   ```
   ```output

   +----+------+----------+
   | ID | Code | Quantity |
   +----+------+----------+
   |  3 | cdq3 |       25 |
   |  1 | xvf1 |       40 |
   |  2 | gwl2 |       15 |
   +----+------+----------+
   ```

***

Modified at: September 26, 2025

Source: [/cloud/developer-resources/connect-with-application-development-tools/connect-with-node-js/connect-with-drizzle-orm/](https://docs.singlestore.com/cloud/developer-resources/connect-with-application-development-tools/connect-with-node-js/connect-with-drizzle-orm/)

(An index of the documentation is available at /llms.txt)
