generate
migrate
push
pull
export
check
up
studio
drizzle-seed 使用版本控制来管理静态和动态数据的输出。为确保真正的确定性,请确保在使用相同的 seed 编号时值保持不变。如果对静态数据源或动态数据生成逻辑进行更改,版本将会更新,这样您可以选择坚持使用以前的版本或使用最新版本。
drizzle-seed
seed
您可以升级到最新的 drizzle-seed 版本以获得新功能,例如额外的生成器,同时在需要时保持确定性输出的旧版本。这在您需要依赖现有确定性数据同时访问新功能时特别有用。
await seed(db, schema, { version: '2' });
v1
0.1.1
v2 (LTS)
0.2.1
string()
interval({ isUnique: true })
底层工作原理?
这不是实际的 API 更改;这只是我们如何进行 drizzle-seed 版本控制的一个示例。
例如,lastName 生成器发生了变化,新的版本 V2 的此生成器可用。
lastName
V2
后来,firstName 生成器发生了改变,生成了此生成器的 V3 版本。
firstName
V3
V1
V3(最新)
LastNameGenV1
LastNameGenV2
FirstNameGenV1
FirstNameGenV3
await seed(db, schema);
如果您尚未准备好立即使用最新的生成器版本,可以指定最大使用版本
await seed(db, schema, { version: '1' });
interval
较旧版本的生成器可能生成类似 1 minute 60 seconds 和 2 minutes 0 seconds 的间隔,将它们视为不同的间隔。 然而,当将 1 minute 60 seconds 间隔插入到 PostgreSQL 数据库时,它会自动转换为 2 minutes 0 seconds。因此,在之后尝试将 2 minutes 0 seconds 间隔插入唯一列将会导致错误。
1 minute 60 seconds
2 minutes 0 seconds
如果您的表包含类型为 interval 的唯一列,则会受到影响:
import { drizzle } from "drizzle-orm/node-postgres"; import { pgTable, interval } from "drizzle-orm/pg-core"; import { seed } from "drizzle-seed"; const intervals = pgTable("intervals", { interval: interval().unique() }); async function main() { const db = drizzle(process.env.DATABASE_URL!); await seed(db, { intervals }); } main();
如果您在种子脚本中使用唯一的 interval 生成器,如下面的脚本所示,也会受到影响:
import { drizzle } from "drizzle-orm/node-postgres"; import { pgTable, interval, char, varchar, text } from "drizzle-orm/pg-core"; import { seed } from "drizzle-seed"; const intervals = pgTable("intervals", { interval: interval().unique(), interval1: interval(), interval2: char({ length: 256 }).unique(), interval3: char({ length: 256 }), interval4: varchar().unique(), interval5: varchar(), interval6: text().unique(), interval7: text(), }); async function main() { const db = drizzle(process.env.DATABASE_URL!); await seed(db, { intervals }).refine((f) => ({ intervals: { columns: { interval: f.interval({ isUnique: true }), interval1: f.interval({ isUnique: true }), interval2: f.interval({ isUnique: true }), interval3: f.interval({ isUnique: true }), interval4: f.interval({ isUnique: true }), interval5: f.interval({ isUnique: true }), interval6: f.interval({ isUnique: true }), interval7: f.interval({ isUnique: true }), } } })); } main();
import { binary, char, mysqlTable, text, varbinary, varchar } from 'drizzle-orm/mysql-core'; import { drizzle } from 'drizzle-orm/mysql2'; import { seed } from "drizzle-seed"; const intervals = mysqlTable('intervals', { interval1: char({ length: 255 }).unique(), interval2: char({ length: 255 }), interval3: varchar({ length: 255 }).unique(), interval4: varchar({ length: 255 }), interval5: binary({ length: 255 }).unique(), interval6: binary({ length: 255 }), interval7: varbinary({ length: 255 }).unique(), interval8: varbinary({ length: 255 }), interval9: text(), }); async function main() { const db = drizzle(process.env.DATABASE_URL!); await seed(db, { intervals }, { version: '2' }).refine((f) => ({ intervals: { columns: { interval: f.interval({ isUnique: true }), interval1: f.interval({ isUnique: true }), interval2: f.interval({ isUnique: true }), interval3: f.interval({ isUnique: true }), interval4: f.interval({ isUnique: true }), interval5: f.interval({ isUnique: true }), interval6: f.interval({ isUnique: true }), interval7: f.interval({ isUnique: true }), interval8: f.interval({ isUnique: true }), interval9: f.interval({ isUnique: true }), }, }, })); } main();
import { blob, sqliteTable, text } from 'drizzle-orm/sqlite-core'; import { drizzle } from 'drizzle-orm/better-sqlite3'; import { seed } from 'drizzle-seed'; const intervals = sqliteTable('intervals', { interval1: text().unique(), interval2: text(), interval3: blob().unique(), interval4: blob(), }); async function main() { const db = drizzle(process.env.DATABASE_URL!); await seed(db, { intervals }).refine((f) => ({ intervals: { columns: { interval1: f.interval({ isUnique: true }), interval2: f.interval({ isUnique: true }), interval3: f.interval({ isUnique: true }), interval4: f.interval({ isUnique: true }), }, }, })); } main();
string
能够根据文本列的长度生成唯一字符串(例如,varchar(20))
varchar(20)
如果您的表包含具有最大长度参数的文本类型列或唯一文本类型列,则会受到影响:
import { drizzle } from "drizzle-orm/node-postgres"; import { pgTable, char, varchar, text } from "drizzle-orm/pg-core"; import { seed } from "drizzle-seed"; const strings = pgTable("strings", { string2: char({ length: 256 }).unique(), string3: char({ length: 256 }), string4: varchar().unique(), string5: varchar({ length: 256 }).unique(), string6: varchar({ length: 256 }), string7: text().unique(), }); async function main() { const db = drizzle(process.env.DATABASE_URL!); await seed(db, { strings }); } main();
import { binary, char, mysqlTable, varbinary, varchar } from 'drizzle-orm/mysql-core'; import { drizzle } from 'drizzle-orm/mysql2'; import { seed } from "drizzle-seed"; const strings = mysqlTable('strings', { string1: char({ length: 255 }).unique(), string2: char({ length: 255 }), string3: varchar({ length: 255 }).unique(), string4: varchar({ length: 255 }), string5: binary({ length: 255 }).unique(), string6: binary({ length: 255 }), string7: varbinary({ length: 255 }).unique(), string8: varbinary({ length: 255 }), }); async function main() { const db = drizzle(process.env.DATABASE_URL!); await seed(db, { strings }); } main();
import { drizzle } from 'drizzle-orm/better-sqlite3'; import { blob, sqliteTable, text } from 'drizzle-orm/sqlite-core'; import { seed } from "drizzle-seed"; const strings = sqliteTable('strings', { string1: text().unique(), string2: text({ length: 256 }), string3: text({ length: 256 }).unique(), string4: blob().unique(), }); async function main() { const db = drizzle(process.env.DATABASE_URL!); await seed(db, { strings }); } main();
如果您在种子脚本中使用 string 生成器,如下面的脚本所示,也会受到影响:
import { drizzle } from "drizzle-orm/node-postgres"; import { pgTable, char, varchar, text } from "drizzle-orm/pg-core"; import { seed } from "drizzle-seed"; const strings = pgTable("strings", { string1: char({ length: 256 }).unique(), string2: char({ length: 256 }), string3: char({ length: 256 }), string4: varchar(), string5: varchar().unique(), string6: varchar({ length: 256 }).unique(), string7: varchar({ length: 256 }), string8: varchar({ length: 256 }), string9: text().unique(), string10: text(), }); async function main() { const db = drizzle(process.env.DATABASE_URL!); await seed(db, { strings }).refine((f) => ({ strings: { columns: { string1: f.string({ isUnique: true }), string2: f.string(), string3: f.string({ isUnique: true }), string4: f.string({ isUnique: true }), string5: f.string({ isUnique: true }), string6: f.string({ isUnique: true }), string7: f.string(), string8: f.string({ isUnique: true }), string9: f.string({ isUnique: true }), string10: f.string({ isUnique: true }), } } })); } main();
import { binary, char, mysqlTable, text, varbinary, varchar } from 'drizzle-orm/mysql-core'; import { drizzle } from 'drizzle-orm/mysql2'; import { seed } from "drizzle-seed"; const strings = mysqlTable('strings', { string1: char({ length: 255 }).unique(), string2: char({ length: 255 }), string3: char({ length: 255 }), string4: varchar({ length: 255 }).unique(), string5: varchar({ length: 255 }), string6: varchar({ length: 255 }), string7: binary({ length: 255 }).unique(), string8: binary({ length: 255 }), string9: binary({ length: 255 }), string10: varbinary({ length: 255 }).unique(), string11: varbinary({ length: 255 }), string12: varbinary({ length: 255 }), string13: text(), }); async function main() { const db = drizzle(process.env.DATABASE_URL!); await seed(db, { strings }).refine((f) => ({ strings: { columns: { string1: f.string({ isUnique: true }), string2: f.string({ isUnique: true }), string3: f.string(), string4: f.string({ isUnique: true }), string5: f.string({ isUnique: true }), string6: f.string(), string7: f.string({ isUnique: true }), string8: f.string({ isUnique: true }), string9: f.string(), string10: f.string({ isUnique: true }), string11: f.string({ isUnique: true }), string12: f.string(), string13: f.string({ isUnique: true }), }, }, })); } main();
import { blob, sqliteTable, text } from 'drizzle-orm/sqlite-core'; import { drizzle } from 'drizzle-orm/better-sqlite3'; import { seed } from "drizzle-seed"; const strings = sqliteTable("strings", { string1: text().unique(), string2: text(), string3: text({ length: 256 }).unique(), string4: text({ length: 256 }), string5: text({ length: 256 }), string6: blob().unique(), string7: blob(), }); async function main() { const db = drizzle(process.env.DATABASE_URL!); await seed(db, { strings }).refine((f) => ({ strings: { columns: { string1: f.string({ isUnique: true }), string2: f.string({ isUnique: true }), string3: f.string({ isUnique: true }), string4: f.string({ isUnique: true }), string5: f.string(), string6: f.string({ isUnique: true }), string7: f.string({ isUnique: true }), }, }, })); } main();