Simplifying SingleStore Integration Through Drizzle ORM

Clock Icon

5 min read

Pencil Icon

Dec 10, 2024

At SingleStore, we continuously explore ways to provide the best development experience through integrations with popular tools and frameworks. Today, we're excited to announce the release of the SingleStore driver for Drizzle ORM.

Simplifying SingleStore Integration Through Drizzle ORM

By using the SingleStore Drizzle ORM driver, you can take full control of your database schema with the flexibility of a modern, typed Object Relational Mapping (ORM). This integration combines the power of SingleStore's high-performance database with Drizzle ORM’s schema-first approach, offering seamless flexibility and type safety when developing your SingleStore applications.

To explore the full range of features and get started, visit the Drizzle website with the official documentation here, where you'll find detailed documentation and examples.

from-hackathon-to-drizzle-integrationFrom hackathon to Drizzle integration

Drizzle ORM is a modern, lightweight, schema-first TypeScript ORM with +24k stars on GitHub. It’s the only ORM with both relational and SQL-like query APIs, providing you with the best of both worlds when it comes to accessing your relational data. Several modern databases already support Drizzle, making it a natural choice for SingleStore to join the party and offer its own integration.

The SingleStore Drizzle ORM driver started as a “lab project” for our internal summer hackathon. Each summer, SingleStore encourages everyone to collaborate and discover creative applications for its technology. After just four days, we had a prototype that was able to define SingleStore schemas and perform CRUD operations through TypeScript code.

A successful proof of concept in hand was all we needed to partner with Drizzle to deliver this driver to everyone.

supporting-drizzle-ormSupporting Drizzle ORM

Maintaining an open-source project — especially one as essential as an ORM — isn’t easy. As developers, how many of you, who likely rely on an ORM, would be willing to pay for one? Building great software takes resources, and there are two ways to sustain it:

  1. Develop a business model that supports the project
  2. Rely on passionate volunteers

The Drizzle team has earned plenty of praise, but praise alone doesn’t pay the bills. The team is based in Ukraine, where they continue to deliver a top-notch product despite the ongoing challenges. We could’ve simply asked for SingleStore support (which we did) or contributed code ourselves (which we also did).

However, sustainability matters. Because we want to continue partnering with Drizzle and ensure their continued growth, we’re proud to become Drizzle sponsors and invite you to join us and do the same!

getting-startedGetting started

before-you-startBefore you start

The SingleStore Drizzle driver is a TypeScript package that allows us to connect to our SingleStore database, using your favorite runtime tools like NodeJS or Bun. To use it, you need a TypeScript project. You can follow this quick tutorial to create a new one.

We will also need a SingleStore database, which we can start for free here.

install-the-single-store-driverInstall the SingleStore driver

Using NPM

npm i drizzle-orm mysql2
npm i -D drizzle-kit

Using Yarn

yarn add drizzle-orm mysql2
yarn add -D drizzle-kit

Using PNPM

pnpm add drizzle-orm mysql2
pnpm add -D drizzle-kit

Using Bun

bun add drizzle-orm mysql2
bun add -D drizzle-kit

connect-to-single-storeConnect to SingleStore

To connect to our SingleStore database, create a new file called app.ts in the root directory. It is inside this file that we are going to create a new connection to your SingleStore database. To create a new DB connection, we initialize the Drizzle client from the SingleStore driver and pass the credentials to a mysql2 connection.

import { drizzle } from "drizzle-orm/singlestore";
import mysql from "mysql2/promise";
const connection = await mysql.createConnection({
host: "host",
user: "user",
database: "database",
...
});
const db = drizzle({ client: connection });

Since we are using the SingleStore Portal, we can get our database credentials by selecting Deployments > Connect > “SQL IDE”.

define-your-database-schemaDefine your database schema

The schema is the blueprint of our database. It contains information about our tables and their columns, such as their name and type. To define our database schema, we can create the  schema.ts file inside the db directory.

📦 <project root>
└ 📜 app.ts
└ 📂 db
└ 📜 schema.ts

It is possible to define the table schemas through TypeScript code with Drizzle ORM. In this example, we create the users_table table, that contains the information about the users in our application. The users are defined by their ID (primary key), name and age.

import { int, text, singlestoreTable } from
"drizzle-orm/singlestore-core";
export const users = singlestoreTable("users_table", {
id: int("id").primaryKey().autoincrement(),
name: text("name"),
age: int("age"),
});

To apply our schema on our SingleStore database, Drizzle needs a configuration file named drizzle.config.ts. In this file, we specify the path to the schema.ts file, and the dialect, which in our case is singlestore. You can find more information on how to customize the drizzle.config.ts file here.

import { defineConfig } from "drizzle-kit";
export default defineConfig({
dialect: 'singlestore',
schema: './db/schema.ts'
})

For more information about the drizzle.config.ts file and migrations with Drizzle, please check this link.

create-the-users-tableCreate the users table

To create the users_table table on our SingleStore database, we need to first generate and then apply the Drizzle migrations. Migrations are snapshots of the current state of your database schema and it’s how Drizzle ORM tracks when and how the database schema changes.

To generate the migration files, you can run the following command.

npx drizzle-kit generate

This creates the migration file, which is an SQL file that contains the SQL statements required to create our users_table table. Feel free to inspect the migration file generated by the Drizzle ORM.

📦 <project root>
└ 📂 drizzle
└ 📂 drizzle
└ 📜 0000_<first-migration>.sql
└ 📜 app.ts
└ 📂 db
└ 📜 schema.ts

After generating the migration files, we need to apply the migration to our database. To apply this migration, run the following command.

npx drizzle-kit migrate

If we go back to the SingleStore Portal and inspect our database, we will find the users_table table with the schema defined before.

populate-the-databasePopulate the database

Now that we created the users_table table, we can create our records. We can insert them into our table using the functions insert and values. In the following example, we are importing the users table to the app.ts file, and inserting five new records.

// app.ts
import { users } from ./db/schema.ts
...
const newUsers = [{
name: 'John',
age: 28,
},
{
name: 'Jane',
age: 32,
},
{
name: 'Jack',
age: 25,
},
{
name: 'Jill',
age: 24,
},
{
name: 'James',
age: 30,
}
]
await db.insert(users).values(newUsers);

query-the-databaseQuery the database

To query the database, use the select method as if you were writing a SQL query.

// Fetch all users
const rows = await db.select().from(usersTable)
// Print all users
console.log(rows)

For a full guide, check out this example. We also encourage you to browse through the official documentation.

whats-nextWhat's next?

The current version of the SingleStore Drizzle ORM driver offers limited features. You can define your database schema and perform queries but we also want to support more features in the future, such as column types that SingleStore supports which are not supported by other databases and row level security.

Nevertheless, these examples provide a glimpse of what’s possible with the SingleStore Drizzle ORM driver. If you want to see it in action, explore and run a basic TypeScript application example that bootstraps SingleStore and Drizzle integration, click here!

Are you ready to power your next TypeScript application with SingleStore’s unmatched speed and flexibility for OLTP and OLAP workloads? Start your free SingleStore trial today, and use the new Drizzle integration to seamlessly connect your application to the world’s fastest unified database platform.

Try SingleStore free today.


Share