new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Drizzle ORM · all subjects

batch operations & advanced features

22 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Insert rows with all default values in v0.28.0

Starting in v0.28.0, you can insert rows with all default column values by passing an empty object {} to the values method, or an array of empty objects [{}, {}] to insert multiple rows with defaults.

Insert multiple rows with defaults example

To insert multiple rows with all column defaults in Drizzle v0.28.0: await db.insert(usersTable).values([{}, {}]);

OpenTelemetry disabled by default in Drizzle

OpenTelemetry logic in Drizzle ORM is disabled by default and does nothing in the current version. It was an experimental feature that was disabled before release. Drizzle does not collect or send any statistics using OpenTelemetry.

OpenTelemetry design purpose in Drizzle

OpenTelemetry in drizzle-orm was designed to allow ORM users to collect query statistics and send them to their own telemetry consumers. The ORM itself does not send statistics anywhere.

v0.28.5 fix - OpenTelemetry import syntax error

Version 0.28.5 fixed an incorrect OpenTelemetry type import that caused a runtime error. The bug was caused by using `import { type ... }` syntax instead of `import type { ... }` in tracing.ts, which caused the `import '@opentelemetry/api'` statement to leak to the runtime.

LibSQL batch API support in Drizzle

LibSQL now supports batch API through db.batch() which accepts multiple queries in an array. All builders that can be used inside db.batch are: db.all(), db.get(), db.values(), db.run(), db.query.<table>.findMany(), db.query.<table>.findFirst(), db.select()..., db.update()..., db.delete()..., and db.insert()...

LibSQL batch API example with insert, update, and query

const batchResponse = await db.batch([ db.insert(usersTable).values({ id: 1, name: 'John' }).returning({ id: usersTable.id, }), db.update(usersTable).set({ name: 'Dan' }).where(eq(usersTable.id, 1)), db.query.usersTable.findMany({}), db.select().from(usersTable).where(eq(usersTable.id, 1)), db.select({ id: usersTable.id, invitedBy: usersTable.invitedBy }).from( usersTable, ), ]);

D1 Batch API support in v0.29.0

Drizzle v0.29.0 adds support for Cloudflare D1 Batch API, allowing you to batch multiple queries together for execution.

D1 Batch API usage example

Example of using D1 Batch API: ```ts const batchResponse = await db.batch([ db.insert(usersTable).values({ id: 1, name: 'John' }).returning({ id: usersTable.id, }), db.update(usersTable).set({ name: 'Dan' }).where(eq(usersTable.id, 1)), db.query.usersTable.findMany({}), db.select().from(usersTable).where(eq(usersTable.id, 1)), db.select({ id: usersTable.id, invitedBy: usersTable.invitedBy }).from( usersTable, ), ]); type BatchResponse = [ { id: number; }[], D1Result, { id: number; name: string; verified: number; invitedBy: number | null; }[], { id: number; name: string; verified: number; invitedBy: number | null; }[], { id: number; invitedBy: number | null; }[], ]; ```

D1 Batch API supported query builders

The following query builders can be used inside db.batch(): db.all(), db.get(), db.values(), db.run(), db.query.<table>.findMany(), db.query.<table>.findFirst(), db.select()..., db.update()..., db.delete()..., db.insert()...

Drizzle ESLint Plugin

Drizzle ORM v0.29.1 introduces a new ESLint plugin package called eslint-plugin-drizzle. This plugin provides recommended rules for scenarios where type checking is difficult or error messages would be unclear. It aims to help developers handle crucial scenarios during development.

Install Drizzle ESLint Plugin

To install the Drizzle ESLint plugin, run: npm install eslint eslint-plugin-drizzle. For TypeScript support in the IDE, also install: npm install @typescript-eslint/eslint-plugin @typescript-eslint/parser

ESLint Drizzle plugin configuration

To configure the Drizzle ESLint plugin, create a .eslintrc.yml file. Add 'drizzle' to the plugins array. In the parser options, set project to './tsconfig.json'. Use '@typescript-eslint/parser' as the parser. Example configuration: root: true, parser: '@typescript-eslint/parser', parserOptions: { project: './tsconfig.json' }, plugins: [ drizzle ].

ESLint Drizzle enforce-delete-with-where rule

The 'drizzle/enforce-delete-with-where' ESLint rule enforces that delete operations include a .where() clause to prevent accidentally deleting all rows in a table. When violated, it produces the error: 'Without `.where(...)` you will delete all the rows in a table. If you didn't want to do it, please use `db.delete(...).where(...)` instead. Otherwise you can ignore this rule here'. You can optionally define a 'drizzleObjectName' option as a string or string[] to limit the rule to specific Drizzle object names (like 'db'), which prevents false positives from other classes with delete methods.

ESLint Drizzle enforce-update-with-where rule

The 'drizzle/enforce-update-with-where' ESLint rule enforces that update operations include a .where() clause to prevent accidentally updating all rows in a table. When violated, it produces the error: 'Without `.where(...)` you will update all the rows in a table. If you didn't want to do it, please use `db.update(...).set(...).where(...)` instead. Otherwise you can ignore this rule here'. You can optionally define a 'drizzleObjectName' option as a string or string[] to limit the rule to specific Drizzle object names (like 'db'), which prevents false positives from other classes with update methods.

ESLint Drizzle recommended config

The Drizzle ESLint plugin exports a 'recommended' config that includes recommended rules. To use it, extend 'plugin:drizzle/recommended' in .eslintrc.yml. There is also an 'all' config that is currently equivalent to 'recommended' but includes all rules except deprecated ones.

Neon HTTP Batch support

Drizzle ORM v0.29.4 added support for Neon HTTP Batch, allowing multiple queries to be executed in a single batch request. Use `db.batch()` with an array of queries, and it returns a typed tuple matching each query's result type.

Neon HTTP Batch example

To use Neon HTTP Batch: import neon from '@neondatabase/serverless' and drizzle from 'drizzle-orm/neon-http'. Create the database client with `const db = drizzle(sql)` where sql is the neon instance. Call `db.batch([...queries])` with an array of insert, query, and other operations. The batch returns a typed array tuple with results matching each query's type signature.

WITH clause support for INSERT, UPDATE, and DELETE statements

Drizzle ORM supports using WITH statements (Common Table Expressions) with INSERT, UPDATE, and DELETE statements. WITH INSERT, WITH UPDATE, and WITH DELETE clauses allow you to create temporary result sets that can be referenced in your insert, update, or delete operations.

WITH DELETE example with CTE

Example showing how to use a WITH clause with DELETE: first define a CTE using db.$with('average_amount').as() with a select query, then use db.with(averageAmount).delete(orders).where() to delete rows. The SQL output is: with "average_amount" as (select avg("amount") as "value" from "orders") delete from "orders" where "orders"."amount" > (select * from "average_amount") returning "id";

Fixed $onUpdate not handling SQL values

Drizzle v1.0.0-beta.2 fixes the issue where `$onUpdate` was not handling SQL values correctly, addressing GitHub issue #2388.

Drizzle to GraphQL conversion feature

Drizzle ORM supports converting Drizzle Schema to GraphQL schema with a single line of code. The conversion is fully customizable.

Give your agent this brain