drizzle-team / drizzle-team/drizzle-orm
[DOCS]: How do I seed my Cloudflare D1 db with `drizzle-seed`?
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Enhancement hasn't been filed before.
- [x] I have verified this enhancement I'm about to request hasn't been suggested before.
### Describe the enhancement you want to request
I have created a simple schema
```schema.ts
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
export const users = sqliteTable("users", {
id: text("id").primaryKey(),
name: text("name").notNull(),
email: text("email").notNull().unique(),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
});
```
```drizzle.config.ts
import type { Config } from "drizzle-kit";
export default {
schema: "./app/db/schema.ts",
out: "./migrations",
driver: "d1-http",
dialect: "sqlite",
} satisfies Config;
```
```wrangler.toml
"$schema" = "node_modules/wrangler/config-schema.json"
name = "hono"
compatibility_date = "2025-03-18"
pages_build_output_dir = "./dist"
compatibility_flags = ["nodejs_compat"]
[[d1_databases]]
binding = "DB"
database_name = "hono-db"
database_id = "..."
migrations_dir = "./migrations"
```
and I have been successful in migrating both the local and prod dbs
```bash
wrangler d1 migrations apply hono-db --local
wrangler d1 migrations apply hono-db
```
I have also setup drizzle-seed (correctly I think)
```seed.ts
import { seed } from "drizzle-seed";
import { users } from './schema';
import { drizzle } from "drizzle-orm/sqlite";
async function main() {
# env var only available when running with wrangler
const db = drizzle(process.env.DB!);
await seed(db, { users }).refine((f) => ({
users: {
columns: {
name: f.fullName(),
email: f.email(),
},
count: 20
}
}));
}
main();
```
How would I run the seed script against Cloudflare D1?
How would I run it against the local instance? (Its just a sqlite file inside `.wrangler`)
Is there a way to generate a SQL file from the seed script instead? I know I can run it against both local and prod with wrangler :: https://developers.cloudflare.com/d1/wrangler-commands/#d1-execute
Contributor guide
Assessment
This issue has not been assessed yet.