Code Icon

Build an app / Node.js + Drizzle

How to connect a Node.js app to SingleStore using Drizzle ORM

on-this-guideOn this guide

This guide walks you through the steps to connect a Node.js application to a SingleStore database using Drizzle ORM. You’ll start by setting up a SingleStore deployment and workspace, then create a database and retrieve your connection credentials. Finally, you’ll configure your Node.js project by installing the drizzle-orm and drizzle-kit packages, loading environment variables, and establishing a connection to handle queries against your SingleStore instance. By the end, you’ll have a working example that you can use in your application.

prerequisitesPrerequisites

  • SingleStore account
  • Node.js

Don’t have a SingleStore account yet?

Sign up nowArrow Up Right Icon

create-deploymentCreate deployment

1. Log in to your SingleStore Portal account.

2. In the left-hand menu, click Create NewDeployment.

3. In the Create Workspace form, follow the on‑screen instructions to complete the form.

4. Click Create Workspace.

5. Wait for the workspace to finish deploying.

create-workspaceCreate workspace

Note: If the required workspace already exists in the target deployment, you can skip this step.

1. Log in to your SingleStore Portal account.

2. In the left-hand menu, click Deployments.

3. From the deployments list, select the deployment where you want to create a workspace.

4. In the left‑hand pane, click + Create Workspace.

5. In the Create Workspace form, follow the on‑screen instructions to complete the form.

6. Сlick Create Workspace.

7. Wait for the workspace to finish deploying.

create-databaseCreate database

1. Log in to your SingleStore Portal account.

2. In the left-hand menu, click Deployments.

3. From the deployments list, select the deployment where you want to create a database.

4. In the right‑hand pane, click + Create Database.

5. In the Create Database form, enter a new database name and select the workspace to attach it to.

6. Click Create Database.

retrieve-database-credentialsRetrieve database credentials

1. Log in to your SingleStore Portal account.

2. In the left-hand menu, click Deployments.

3. From the deployments list, select the deployment that contains your database.

4. From the workspaces list, select the workspace to which your database is attached.

5. In the selected workspace, click Connect.

6. In the Connect dropdown, choose SQL IDE.

7. In the SQL IDE tab, copy the connection parameters.

If you don’t know the password, click Reset Password, then copy the new password.

prepare-environmentPrepare environment

1. Create a .env file in the root of your project.

2. In this file, define your connection string by adding a DB_URL variable, for example:

1

DB_URL="singlestore://<USER>:<PASSWORD>@<HOST>:<PORT>/<DATABASE_NAME>"

If you don’t know your connection string, see the Retrieve database credentials section above.

3. Now, in a terminal pointing to the root directory of your project, install the following NPM packages:

1

npm install drizzle-orm mysql2

2

npm install -D drizzle-kit

4. In the same terminal, install and configure dotenv to load your environment variables by running the following command:

1

npm install dotenv

5. Then, you’ll need to import dotenv into your application so you can retrieve the environment variables stored in your .env file. To do this, at the following import statement at the very top of your application entry point (e.g., index.ts):

1

import 'dotenv/config'

establish-database-connectionEstablish database connection

In your application entry point, add the following code to establish a database connection:

1

import { drizzle } from "drizzle-orm/singlestore";

2

3

const db = drizzle(process.env.DB_URL ?? "");

create-database-schema-using-drizzle-ormCreate database schema using Drizzle ORM

1. Create a drizzle.config.ts file in the root of your project with the following content:

1

import "dotenv/config";

2

import { defineConfig } from "drizzle-kit";

3

4

export default defineConfig({

5

out: "./drizzle",

6

schema: "./schema.ts",

7

dialect: "singlestore",

8

dbCredentials: {

9

url: process.env.DB_URL ?? "",

10

},

11

});

2. Create a schema.ts file in the root of your project and describe your database schema. For example:

1

import { bigint, singlestoreTable, varchar } from "drizzle-orm/singlestore-core";

2

3

export const usersTable = singlestoreTable("users", {

4

id: bigint({ mode: "number" }).autoincrement().primaryKey(),

5

name: varchar({ length: 255 }).notNull(),

6

});

3. Push the schema into the database by running the following command:

1

npx drizzle-kit push

Once this is done, you should have a database with a table of users ready to use.

query-databaseQuery database

Let’s test to make sure that the connection is working as anticipated. Here is an example of a simple code snippet you can add to your app's main function at the entry point.

1

import "dotenv/config";

2

import { drizzle } from "drizzle-orm/singlestore";

3

import { usersTable } from "./schema";

4

import { eq } from "drizzle-orm";

5

6

const db = drizzle(process.env.DB_URL ?? "");

7

8

async function main() {

9

try {

10

console.log("Insert user");

11

const insertUserResult = await db.insert(usersTable).values({ name: "John" });

12

const insertedUserId = insertUserResult[0].insertId;

13

console.log(insertUserResult);

14

15

console.log("Select all users");

16

const selectUsersResult = await db.select().from(usersTable);

17

console.log(selectUsersResult);

18

19

console.log("Select user");

20

const [selectUserResult] = await db.select().from(usersTable).where(eq(usersTable.id, insertedUserId));

21

console.log(selectUserResult);

22

23

console.log("Update user");

24

const updateUserResult = await db.update(usersTable).set({ name: "John Doe" }).where(eq(usersTable.id, insertedUserId));

25

console.log(updateUserResult);

26

27

console.log("Delete user");

28

const [deleteUserResult] = await db.delete(usersTable).where(eq(usersTable.id, insertedUserId));

29

console.log(deleteUserResult);

30

} catch (error) {

31

console.error(error);

32

}

33

}

34

35

main();

Once you’ve saved your changes, open a terminal, navigate to your project’s root directory, and run the test script:

1

npx tsx ./index.ts

This will issue a query against your SingleStore instance, allowing you to see if connectivity was successful or if further troubleshooting is required.

error-handling-and-troubleshootingError handling and troubleshooting

auth-failedAUTH_FAILED

If you receive an AUTH_FAILED exception when trying to establish a connection to database, try the following steps to remedy the issue: