Node.js - Drizzle support

Introduction

Drizzle is a lightweight, TypeScript-first ORM built around plain SQL-like, fluent query builders, for PostgreSQL, MySQL, SQLite, Microsoft SQL Server, SingleStore and CockroachDB

Objects

This extension creates the entity and entity operation objects:

  • a NodeJS Entity object when a table is defined using one of pgTable, mysqlTable, sqliteTable, mssqlTable, singlestoreTable or cockroachTable;
  • a NodeJS Entity Operation object when one of the supported Drizzle APIs is used on a Drizzle client and linked to one of the entities.
Icon Description
NodeJS Entity
NodeJS Entity Operation

Supported persistence SQL databases

Supported operations

Entity Operation Supported APIs
Insert
  • insert
Update
  • update
Delete
  • delete
Select
  • from
  • leftJoin
  • rightJoin
  • innerJoin
  • fullJoin
  • refreshMaterializedView

The table argument of these APIs is resolved back to its table definition even when it is a variable, a namespace/barrel-exported reference (e.g. import * as schema from './schema'), or a table wrapped with alias() - and regardless of whether the table definition or the API call is analyzed first.

Link Type Caller type Callee type Comment
callLink
  • TypeScript Function
  • TypeScript Method
  • TypeScript Module
  • NodeJS Entity Operation
useInsertLink
  • NodeJS Entity Operation: Insert
  • Table
  • Missing Table
Created by SQL Analyzer when DDL source files are analyzed or by Missing tables and procedures for Node.js when the object is not analyzed.
useUpdateLink
  • NodeJS Entity Operation: Update
  • Table
  • Missing Table
Created by SQL Analyzer when DDL source files are analyzed or by Missing tables and procedures for Node.js when the object is not analyzed.
useDeleteLink
  • NodeJS Entity Operation: Delete
  • Table
  • Missing Table
Created by SQL Analyzer when DDL source files are analyzed or by Missing tables and procedures for Node.js when the object is not analyzed.
useSelectLink
  • NodeJS Entity Operation: Select
  • Table
  • Missing Table
Created by SQL Analyzer when DDL source files are analyzed or by Missing tables and procedures for Node.js when the object is not analyzed.

Example

Take the following code:

import { drizzle } from 'drizzle-orm/libsql';
import { createClient } from '@libsql/client';
import { sqliteTable, text, integer } from 'drizzle-orm/sqlite-core';
import { eq } from 'drizzle-orm';

const usersTable = sqliteTable('users_table', {
  id: integer('id').primaryKey(),
  name: text('name').notNull(),
});

const client = createClient({ url: 'file:local.db' });
const db = drizzle(client);

async function main() {
  await db.insert(usersTable).values({ name: 'Some Name' });
  await db.select().from(usersTable);
  await db.update(usersTable).set({ name: 'A name' }).where(eq(usersTable.id, 1));
  await db.delete(usersTable).where(eq(usersTable.name, 'Another name'));
}

In this example, one ‘NodeJS Entity’ object (‘users_table’) and four ‘NodeJS Entity Operation’ objects (Insert, Select, Update, Delete) are created, each with a callLink from the ‘main’ function. The SQL Analyzer or Missing tables and procedures for Node.js then links these entity operations with the corresponding missing table ‘users_table’:

Known limitations

The following features are not supported:

  • raw SQL queries (e.g. db.execute(sql\…`)`) do not create ‘TypeScript SQL Query’ objects
  • resolving the table used inside a db.transaction(async (tx) => { ... }) callback