DrizzleORM v0.28.0 版本发布
2023年8月6日

重大变更

移除了对嵌套关系过滤的支持

当前示例在 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 对象将不再包含来自 withextras 的字段。我们移除了它们,以便能够构建更高效的关系查询,这改善了行读取和性能。

如果您之前在 where 回调中使用了这些字段,有几种变通方法:

  1. 在获取行后,在代码级别手动应用这些过滤器;
  2. 使用核心 API。

mysql2 驱动添加了关系查询 mode 配置

Drizzle 关系查询总是生成一个 SQL 语句在数据库上运行,这有一些需要注意的地方。为了对每个数据库提供一流的支持,我们引入了模式。

Drizzle 关系查询在底层使用横向连接子查询,目前 PlanetScale 不支持它们。

当使用 mysql2 驱动程序与常规 MySQL 数据库时,您应该指定模式:“default”。当使用 mysql2 驱动程序与 PlanetScale 时,您需要指定模式:“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个外键的数据库模式进行了诊断。我们优化了内部类型,使 IntelliSense 速度提升了 430%

改进了关系查询的性能和读取使用率

在此版本中,我们完全改变了关系查询 API 的查询生成方式。

总结一下,我们对查询生成策略进行了以下更改:

  1. 横向连接:在新版本中,我们利用横向连接(由“LEFT JOIN LATERAL”子句表示)来高效地从相关表中检索特定数据。对于 PlanetScale 中的 MySQL 和 SQLite,我们使用了简单的子查询选择,这改善了查询计划和整体性能。

  2. 选择性数据检索:在新版本中,我们只从表中检索必要的数据。这种有针对性的数据检索减少了获取不必要信息的量,从而导致要处理的数据集更小,执行速度更快。

  3. 减少聚合:在新版本中,我们减少了聚合函数(例如,COUNT,json_agg)的数量。通过在横向连接中直接使用 json_build_array,drizzle 以更简化的方式聚合数据,从而提高了查询性能。

  4. 简化分组:在新版本中,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 },
        },
      },
    },
  },
});
-- Query generated now
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
-- Query generated before
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 将把所有默认值插入到数据库中。

// Insert 1 row with all defaults
await db.insert(usersTable).values({});

// Insert 2 rows with all defaults
await db.insert(usersTable).values([{}, {}]);