重大更改
移除了对嵌套关系过滤的支持
当前示例在 0.28.0
中将无法工作:
const usersWithPosts = await db.query.users.findMany({
where: (table, { sql }) => (sql`json_array_length(${table.posts}) > 0`),
with: {
posts: true,
},
});
在 where
回调中的 table
对象将不再包含 with
和 extras
中的字段。我们移除了这些字段,以便构建更高效的关系查询,从而提高行读取和性能。
如果您之前在 where
回调中使用了这些字段,有几种解决方法:
- 在行获取后手动在代码层面应用这些过滤条件;
- 使用核心 API。
为 mysql2
驱动添加了关系查询 mode
配置
Drizzle 关系查询始终生成恰好一条 SQL 语句以便在数据库上运行,这存在某些注意事项。为了在市面上的每种数据库中提供最佳支持,我们引入了模式。
Drizzle 关系查询在内部使用子查询的侧连接,目前 PlanetScale 不支持它们。
在使用常规 MySQL 数据库的 mysql2
驱动时,您应指定模式: “default”。
在使用 PlanetScale 的 mysql2
驱动时,您需要指定模式:“planetscale”。
import { drizzle } from 'drizzle-orm/mysql2';
import mysql from 'mysql2/promise';
import * as schema from './schema';
const client = await mysql.createConnection({
uri: process.env.PLANETSCALE_DATABASE_URL,
});
const db = drizzle({ client, schema, mode: 'planetscale' });
改进了大型模式的 IntelliSense 性能
我们对一个包含 85 个表、666 列、26 个枚举、172 个索引和 133 个外键的数据库模式进行了诊断。我们优化了内部类型,从而实现了 430% 的 IntelliSense 性能提升。
改进的关系查询性能和读取使用
在本次发布中,我们完全更改了关系查询 API 的查询生成方式。
总的来说,我们对查询生成策略进行了一系列更改:
-
侧连接:在新版本中,我们利用侧连接,通过 “LEFT JOIN LATERAL” 子句高效地从相关表中检索特定数据。对于 PlanetScale 和 SQLite 的 MySQL,我们使用了简单的子查询选择,从而改善了查询计划和整体性能。
-
选择性数据检索:在新版本中,我们只从表中检索必要的数据。这种有针对性的数据检索减少了获取的不必要信息,从而产生了更小的数据集以进行处理,执行速度更快。
-
减少聚合:在新版本中,我们减少了聚合函数的数量(例如,COUNT, json_agg)。通过在侧连接中直接使用 json_build_array,drizzle 以更简化的方式聚合数据,提升了查询性能。
-
简化分组:在新版本中,GROUP BY 子句被移除,因为侧连接和子查询已经更加高效地处理数据聚合。
对于这个 drizzle 查询
const items = await db.query.comments.findMany({
limit,
orderBy: comments.id,
with: {
user: {
columns: { name: true },
},
post: {
columns: { title: true },
with: {
user: {
columns: { name: true },
},
},
},
},
});
-- 现在生成的查询
select "comments"."id",
"comments"."user_id",
"comments"."post_id",
"comments"."content",
"comments_user"."data" as "user",
"comments_post"."data" as "post"
from "comments"
left join lateral (select json_build_array("comments_user"."name") as "data"
from (select *
from "users" "comments_user"
where "comments_user"."id" = "comments"."user_id"
limit 1) "comments_user") "comments_user" on true
left join lateral (select json_build_array("comments_post"."title", "comments_post_user"."data") as "data"
from (select *
from "posts" "comments_post"
where "comments_post"."id" = "comments"."post_id"
limit 1) "comments_post"
left join lateral (select json_build_array("comments_post_user"."name") as "data"
from (select *
from "users" "comments_post_user"
where "comments_post_user"."id" = "comments_post"."user_id"
limit 1) "comments_post_user") "comments_post_user"
on true) "comments_post" on true
order by "comments"."id"
limit 1
-- 之前生成的查询
SELECT "id",
"user_id",
"post_id",
"content",
"user"::JSON,
"post"::JSON
FROM
(SELECT "comments".*,
CASE
WHEN count("comments_post"."id") = 0 THEN '[]'
ELSE json_agg(json_build_array("comments_post"."title", "comments_post"."user"::JSON))::text
END AS "post"
FROM
(SELECT "comments".*,
CASE
WHEN count("comments_user"."id") = 0 THEN '[]'
ELSE json_agg(json_build_array("comments_user"."name"))::text
END AS "user"
FROM "comments"
LEFT JOIN
(SELECT "comments_user".*
FROM "users" "comments_user") "comments_user" ON "comments"."user_id" = "comments_user"."id"
GROUP BY "comments"."id",
"comments"."user_id",
"comments"."post_id",
"comments"."content") "comments"
LEFT JOIN
(SELECT "comments_post".*
FROM
(SELECT "comments_post".*,
CASE
WHEN count("comments_post_user"."id") = 0 THEN '[]'
ELSE json_agg(json_build_array("comments_post_user"."name"))
END AS "user"
FROM "posts" "comments_post"
LEFT JOIN
(SELECT "comments_post_user".*
FROM "users" "comments_post_user") "comments_post_user" ON "comments_post"."user_id" = "comments_post_user"."id"
GROUP BY "comments_post"."id") "comments_post") "comments_post" ON "comments"."post_id" = "comments_post"."id"
GROUP BY "comments"."id",
"comments"."user_id",
"comments"."post_id",
"comments"."content",
"comments"."user") "comments"
LIMIT 1
更多信息请查看文档中的 关系查询。
以默认值插入所有列行的可能性
现在您可以提供一个空对象或空对象数组,Drizzle 会将所有默认值插入数据库。
// 插入 1 行所有默认值
await db.insert(usersTable).values({});
// 插入 2 行所有默认值
await db.insert(usersTable).values([{}, {}]);