DrizzleORM v0.29.5 发布
Mar 6, 2024

新功能

🎉 WITH UPDATE, WITH DELETE, WITH INSERT

你现在可以将 WITH 语句与 INSERTUPDATEDELETE 语句一起使用。

使用示例

const averageAmount = db.$with('average_amount').as(
	db.select({ value: sql`avg(${orders.amount})`.as('value') }).from(orders),
);

const result = await db
	.with(averageAmount)
	.delete(orders)
	.where(gt(orders.amount, sql`(select * from ${averageAmount})`))
	.returning({
		id: orders.id,
	});
with "average_amount" as (select avg("amount") as "value" from "orders") 
delete from "orders" 
where "orders"."amount" > (select * from "average_amount") 
returning "id";

有关所有语句的更多示例,请查看文档:

🎉 可以为迁移表指定自定义模式和自定义名称

默认情况下,所有已执行迁移的信息都将存储在数据库中的 __drizzle_migrations 表中;对于 PostgreSQL,则存储在 drizzle 模式中。但是,你可以配置这些记录的存储位置。

要为存储在数据库中的迁移添加自定义表名称,你应该使用 migrationsTable 选项

使用示例

await migrate(db, {
	migrationsFolder: './drizzle',
	migrationsTable: 'my_migrations',
});

仅适用于 PostgreSQL 数据库

要为存储在数据库中的迁移添加自定义架构名称,你应该使用 migrationsSchema 选项

使用示例

await migrate(db, {
	migrationsFolder: './drizzle',
	migrationsSchema: 'custom',
});

🎉 SQLite 代理查询和关系查询支持

你可以在 docs 中找到更多关于 SQLite 代理的信息。

import { drizzle } from 'drizzle-orm/sqlite-proxy';

type ResponseType = { rows: any[][] | any[] }[];

const db = drizzle(
	async (sql, params, method) => {
		// single query logic
	},
	// new batch callback
	async (
		queries: {
			sql: string;
			params: any[];
			method: 'all' | 'run' | 'get' | 'values';
		}[],
	) => {
		try {
			const result: ResponseType = await axios.post(
				'http://localhost:3000/batch',
				{ queries },
			);

			return result;
		} catch (e: any) {
			console.error('Error from sqlite proxy server:', e);
			throw e;
		}
	},
);

然后,你可以使用 db.batch([]) 方法,它将代理所有查询

批处理响应应为原始值数组(数组中的数组),其顺序与发送到代理服务器的顺序相同。