Drizzle Seed

PostgreSQL
SQLite
MySQL
SingleStore
重要提示

drizzle-seed 只能与 [email protected] 或更高版本一起使用。低于此版本可能在运行时正常工作,但可能存在类型问题和标识列问题,因为此补丁是在 [email protected] 中引入的。

drizzle-seed 是一个 TypeScript 库,可帮助您生成确定性但逼真的假数据以填充您的数据库。通过利用可播种的伪随机数生成器 (pRNG),它确保您生成的数据在不同运行中保持一致和可重现。这对于测试、开发和调试特别有用。

什么是确定性数据生成?

确定性数据生成意味着相同的输入将始终产生相同的输出。在 drizzle-seed 的上下文中,当您使用相同的种子数初始化库时,它每次都会生成相同的假数据序列。这使得数据集可预测和可重复。

伪随机数生成器 (pRNG)

伪随机数生成器是一种算法,它生成一系列近似于随机数属性的数字。然而,由于它基于一个称为种子的初始值,您可以控制其随机性。通过使用相同的种子,pRNG 将生成相同的数字序列,从而使您的数据生成过程可重现。

使用 pRNG 的好处:

使用 drizzle-seed,您可以两全其美:生成逼真假数据的能力,以及在需要时重现它的控制能力。

安装

npm
yarn
pnpm
bun
npm i drizzle-seed

基本用法

在此示例中,我们将创建 10 个具有随机名称和 ID 的用户

import { pgTable, integer, text } from "drizzle-orm/pg-core";
import { drizzle } from "drizzle-orm/node-postgres";
import { seed } from "drizzle-seed";

const users = pgTable("users", {
  id: integer().primaryKey(),
  name: text().notNull(),
});

async function main() {
  const db = drizzle(process.env.DATABASE_URL!);
  await seed(db, { users });
}

main();

选项

count

默认情况下,seed 函数将创建 10 个实体。但是,如果您的测试需要更多,您可以在种子选项对象中指定。

await seed(db, schema, { count: 1000 });

seed

如果您需要一个种子来为所有后续运行生成不同的值集,您可以在 seed 选项中定义一个不同的数字。任何新数字都将生成一组唯一的值。

await seed(db, schema, { seed: 12345 });

重置数据库

使用 drizzle-seed,您可以轻松重置数据库并使用新值播种,例如,在您的测试套件中。

// path to a file with schema you want to reset
import * as schema from "./schema.ts";
import { reset } from "drizzle-seed";

async function main() {
  const db = drizzle(process.env.DATABASE_URL!);
  await reset(db, schema);
}

main();

不同的数据库方言将有不同的数据库重置策略。

PostgreSQL
MySQL
SQLite

对于 PostgreSQL,drizzle-seed 包将生成带有 CASCADE 选项的 TRUNCATE 语句,以确保在运行重置函数后所有表都为空。

TRUNCATE tableName1, tableName2, ... CASCADE;

细化

如果您需要更改 drizzle-seed 默认使用的种子生成器函数的行为,您可以指定自己的实现,甚至可以使用自己的值列表进行播种过程。

.refine 是一个回调,它接收来自 drizzle-seed 的所有可用生成器函数的列表。它应该返回一个对象,其中键表示您要细化的表,并根据需要定义它们的行为。每个表都可以指定几个属性以简化数据库播种。

信息

您还可以为要创建的引用值的数量指定加权随机分布。有关此 API 的详细信息,请参阅 加权随机文档 部分。

API

await seed(db, schema).refine((f) => ({
  users: {
    columns: {},
    count: 10,
    with: {
        posts: 10
    }
  },
}));

让我们通过一些示例和解释来了解会发生什么。

schema.ts
import { pgTable, integer, text } from "drizzle-orm/pg-core";

export const users = pgTable("users", {
  id: integer().primaryKey(),
  name: text().notNull(),
});

export const posts = pgTable("posts", {
  id: integer().primaryKey(),
  description: text(),
  userId: integer().references(() => users.id),
});

示例 1:仅使用 20 个实体和细化的 name 列种子逻辑来播种 users 表。

index.ts
import { drizzle } from "drizzle-orm/node-postgres";
import { seed } from "drizzle-seed";
import * as schema from './schema.ts'

async function main() {
  const db = drizzle(process.env.DATABASE_URL!);

  await seed(db, { users: schema.users }).refine((f) => ({
    users: {
        columns: {
            name: f.fullName(),
        },
        count: 20
    }
  }));
}

main();

示例 2:用 20 个实体播种 users 表,并通过播种 posts 表并从 posts 创建对 users 的引用,为每个 user 添加 10 个 posts

index.ts
import { drizzle } from "drizzle-orm/node-postgres";
import { seed } from "drizzle-seed";
import * as schema from './schema.ts'

async function main() {
  const db = drizzle(process.env.DATABASE_URL!);

  await seed(db, schema).refine((f) => ({
    users: {
        count: 20,
        with: {
            posts: 10
        }
    }
  }));
}

main();

示例 3:用 5 个实体播种 users 表,并用 100 个 posts 填充数据库,而无需将它们连接到 users 实体。细化 usersid 生成,使其将给出从 1000020000 的任何整数并保持唯一,并细化 posts 以从自定义数组中检索值。

index.ts
import { drizzle } from "drizzle-orm/node-postgres";
import { seed } from "drizzle-seed";
import * as schema from './schema.ts'

async function main() {
  const db = drizzle(process.env.DATABASE_URL!);

  await seed(db, schema).refine((f) => ({
    users: {
        count: 5,
        columns: {
            id: f.int({
              minValue: 10000,
              maxValue: 20000,
              isUnique: true,
            }),
        }
    },
    posts: {
        count: 100,
        columns: {
            description: f.valuesFromArray({
            values: [
                "The sun set behind the mountains, painting the sky in hues of orange and purple", 
                "I can't believe how good this homemade pizza turned out!", 
                "Sometimes, all you need is a good book and a quiet corner.", 
                "Who else thinks rainy days are perfect for binge-watching old movies?", 
                "Tried a new hiking trail today and found the most amazing waterfall!",
                // ...
            ],
          })
        }
    }
  }));
}

main();
重要提示

我们将在此文档中定义更多可能性,但目前,您可以浏览本文档中的一些部分。查看 生成器 部分,熟悉所有可用的生成器函数。

一个特别棒的功能是能够使用加权随机化,既可以用于为列创建的生成器值,也可以用于确定 drizzle-seed 可以生成的相关实体数量。

有关更多信息,请查看 加权随机文档

加权随机

在某些情况下,您可能需要使用多个数据集,并在播种阶段以不同的优先级插入到数据库中。对于这种情况,drizzle-seed 提供了加权随机 API。

Drizzle Seed 包有几个地方可以使用加权随机。

我们来看两个例子。

schema.ts
import { pgTable, integer, text, varchar, doublePrecision } from "drizzle-orm/pg-core";

export const orders = pgTable(
  "orders",
  {
    id: integer().primaryKey(),
    name: text().notNull(),
    quantityPerUnit: varchar().notNull(),
    unitPrice: doublePrecision().notNull(),
    unitsInStock: integer().notNull(),
    unitsOnOrder: integer().notNull(),
    reorderLevel: integer().notNull(),
    discontinued: integer().notNull(),
  }
);

export const details = pgTable(
  "details",
  {
    unitPrice: doublePrecision().notNull(),
    quantity: integer().notNull(),
    discount: doublePrecision().notNull(),

    orderId: integer()
      .notNull()
      .references(() => orders.id, { onDelete: "cascade" }),
  }
);

示例 1:细化 unitPrice 生成逻辑以生成 5000 个随机价格,其中 30% 的价格在 10-100 之间,70% 的价格在 100-300 之间。

index.ts
import { drizzle } from "drizzle-orm/node-postgres";
import { seed } from "drizzle-seed";
import * as schema from './schema.ts'

async function main() {
  const db = drizzle(process.env.DATABASE_URL!);

  await seed(db, schema).refine((f) => ({
    orders: {
       count: 5000,
       columns: {
           unitPrice: f.weightedRandom(
               [
                   {
                       weight: 0.3,
                       value: funcs.int({ minValue: 10, maxValue: 100 })
                   },
                   {
                       weight: 0.7,
                       value: funcs.number({ minValue: 100, maxValue: 300, precision: 100 })
                   }
               ]
           ),
       }
    }
  }));
}

main();

示例 2:对于每个订单,生成 1 到 3 个详细信息,有 60% 的几率;5 到 7 个详细信息,有 30% 的几率;8 到 10 个详细信息,有 10% 的几率。

index.ts
import { drizzle } from "drizzle-orm/node-postgres";
import { seed } from "drizzle-seed";
import * as schema from './schema.ts'

async function main() {
  const db = drizzle(process.env.DATABASE_URL!);

  await seed(db, schema).refine((f) => ({
    orders: {
       with: {
           details:
               [
                   { weight: 0.6, count: [1, 2, 3] },
                   { weight: 0.3, count: [5, 6, 7] },
                   { weight: 0.1, count: [8, 9, 10] },
               ]
       }
    }
  }));
}

main();

复杂示例

main.ts
schema.ts
import { seed } from "drizzle-seed";
import * as schema from "./schema.ts";

const main = async () => {
    const titlesOfCourtesy = ["Ms.", "Mrs.", "Dr."];
    const unitsOnOrders = [0, 10, 20, 30, 50, 60, 70, 80, 100];
    const reorderLevels = [0, 5, 10, 15, 20, 25, 30];
    const quantityPerUnit = [
        "100 - 100 g pieces",
        "100 - 250 g bags",
        "10 - 200 g glasses",
        "10 - 4 oz boxes",
        "10 - 500 g pkgs.",
        "10 - 500 g pkgs."
    ];
    const discounts = [0.05, 0.15, 0.2, 0.25];

    await seed(db, schema).refine((funcs) => ({
        customers: {
            count: 10000,
            columns: {
                companyName: funcs.companyName(),
                contactName: funcs.fullName(),
                contactTitle: funcs.jobTitle(),
                address: funcs.streetAddress(),
                city: funcs.city(),
                postalCode: funcs.postcode(),
                region: funcs.state(),
                country: funcs.country(),
                phone: funcs.phoneNumber({ template: "(###) ###-####" }),
                fax: funcs.phoneNumber({ template: "(###) ###-####" })
            }
        },
        employees: {
            count: 200,
            columns: {
                firstName: funcs.firstName(),
                lastName: funcs.lastName(),
                title: funcs.jobTitle(),
                titleOfCourtesy: funcs.valuesFromArray({ values: titlesOfCourtesy }),
                birthDate: funcs.date({ minDate: "2010-12-31", maxDate: "2010-12-31" }),
                hireDate: funcs.date({ minDate: "2010-12-31", maxDate: "2024-08-26" }),
                address: funcs.streetAddress(),
                city: funcs.city(),
                postalCode: funcs.postcode(),
                country: funcs.country(),
                homePhone: funcs.phoneNumber({ template: "(###) ###-####" }),
                extension: funcs.int({ minValue: 428, maxValue: 5467 }),
                notes: funcs.loremIpsum()
            }
        },
        orders: {
            count: 50000,
            columns: {
                shipVia: funcs.int({ minValue: 1, maxValue: 3 }),
                freight: funcs.number({ minValue: 0, maxValue: 1000, precision: 100 }),
                shipName: funcs.streetAddress(),
                shipCity: funcs.city(),
                shipRegion: funcs.state(),
                shipPostalCode: funcs.postcode(),
                shipCountry: funcs.country()
            },
            with: {
                details:
                    [
                        { weight: 0.6, count: [1, 2, 3, 4] },
                        { weight: 0.2, count: [5, 6, 7, 8, 9, 10] },
                        { weight: 0.15, count: [11, 12, 13, 14, 15, 16, 17] },
                        { weight: 0.05, count: [18, 19, 20, 21, 22, 23, 24, 25] },
                    ]
            }
        },
        suppliers: {
            count: 1000,
            columns: {
                companyName: funcs.companyName(),
                contactName: funcs.fullName(),
                contactTitle: funcs.jobTitle(),
                address: funcs.streetAddress(),
                city: funcs.city(),
                postalCode: funcs.postcode(),
                region: funcs.state(),
                country: funcs.country(),
                phone: funcs.phoneNumber({ template: "(###) ###-####" })
            }
        },
        products: {
            count: 5000,
            columns: {
                name: funcs.companyName(),
                quantityPerUnit: funcs.valuesFromArray({ values: quantityPerUnit }),
                unitPrice: funcs.weightedRandom(
                    [
                        {
                            weight: 0.5,
                            value: funcs.int({ minValue: 3, maxValue: 300 })
                        },
                        {
                            weight: 0.5,
                            value: funcs.number({ minValue: 3, maxValue: 300, precision: 100 })
                        }
                    ]
                ),
                unitsInStock: funcs.int({ minValue: 0, maxValue: 125 }),
                unitsOnOrder: funcs.valuesFromArray({ values: unitsOnOrders }),
                reorderLevel: funcs.valuesFromArray({ values: reorderLevels }),
                discontinued: funcs.int({ minValue: 0, maxValue: 1 })
            }
        },
        details: {
            columns: {
                unitPrice: funcs.number({ minValue: 10, maxValue: 130 }),
                quantity: funcs.int({ minValue: 1, maxValue: 130 }),
                discount: funcs.weightedRandom(
                    [
                        { weight: 0.5, value: funcs.valuesFromArray({ values: discounts }) },
                        { weight: 0.5, value: funcs.default({ defaultValue: 0 }) }
                    ]
                )
            }
        }
    }));
}

main();

限制

with 的类型限制

由于某些 TypeScript 限制和 Drizzle 中的当前 API,无法正确推断表之间的引用,尤其是在表之间存在循环依赖的情况下。

这意味着 with 选项将显示模式中的所有表,您需要手动选择具有一对多关系的表。

警告

with 选项适用于一对多关系。例如,如果您有一个 user 和许多 posts,您可以使用 users with posts,但不能使用 posts with users

Drizzle 表中第三个参数的类型限制:

目前,我们不支持 Drizzle 表中第三个参数的类型。虽然它在运行时会起作用,但在类型层面将无法正常工作。