[DOCS]: How do I seed my Cloudflare D1 db with `drizzle-seed`?
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
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()),
});
import type { Config } from "drizzle-kit";
export default {
schema: "./app/db/schema.ts",
out: "./migrations",
driver: "d1-http",
dialect: "sqlite",
} satisfies Config;
"$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
wrangler d1 migrations apply hono-db --local
wrangler d1 migrations apply hono-db
I have also setup drizzle-seed (correctly I think)
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
I do see the option to commit keys and credentials to drizzle.config.ts but (1) i would prefer not to commit these things and (2) even then I will not be able to seed the local db.
Anything helps :)
My current solution is to manually create a SQL file to seed DBs.
BEGIN TRANSACTION;
INSERT INTO users (id, name, email, created_at)
VALUES ('user-1', 'Alice Johnson', '[email protected]', strftime('%s', 'now'));
INSERT INTO users (id, name, email, created_at)
VALUES ('user-2', 'Bob Smith', '[email protected]', strftime('%s', 'now'));
INSERT INTO users (id, name, email, created_at)
VALUES ('user-3', 'Charlie Davis', '[email protected]', strftime('%s', 'now'));
COMMIT;
wrangler d1 execute hono-db --file=app/db/seed.sql --local
wrangler d1 execute hono-db --file=app/db/seed.sql --remote
I noticed that the NuxtHub guide for Drizzle ORM doesn't use drizzle-seed for seeding, conceivably because seed does not support a db argument of type DrizzleD1Database. Given that it acts like an SQLite database for local development, it would be very beneficial if drizzle-seed could be extended to support D1.
Interestingly, I had partial success by casting my DrizzleD1Database as any before passing it to seed, in that it was able to create records through a with refinement, but it failed to create the main user records with the error Error seeding database: Cannot read properties of undefined (reading 'rows'), apparently originating from SeedService.generateTablesValues.
I tried asking in the Discord and got a response from @mastondzn with 2 other possible workarounds. I haven't tried these yet but they do look promising.
(1) connect to remote d1 in a node script using d1 http api requests
This is how drizzle-kit handles it This approach could be extended to the local db instance by using an SQLite integration with drizzle.
Example Seed Script :: https://gist.github.com/mastondzn/76f42e77bf469a87b5dae76f7292ff2c
(2) add a seed endpoint
This way the seed script is triggered by an API call after wrangler dev has started.
I did some cost estimations and I think Turso ends up being more affordable in the long run and doesn't have a 10gb limit. That said I did want to share a way to securely import env vars with type safety.
import type { Config } from "drizzle-kit";
import { z } from "zod";
const tursoEnvSchema = z.object({
TURSO_URL: z.string().nonempty(),
TURSO_TOKEN: z.string().nonempty()
})
const env = tursoEnvSchema.parse(process.env)
export default {
schema: "./app/db/schema.ts",
out: "./.drizzle/migrations/",
dialect: "turso",
dbCredentials: {
url: env.TURSO_URL,
authToken: env.TURSO_TOKEN,
}
} satisfies Config;
You can changet he dbCredientals to match the provider you are using https://orm.drizzle.team/docs/drizzle-config-file#dbcredentials
For cloudflare you will have to put your env vars in .dev.vars or wrangler.toml/.json instead of .env for them to be present when using wrangler. I'm still not quite sure how this option can be used to migrate & seed the local D1 db.
For anyone else who may run into this issue in the near future, I recently created 2 mini utils that may save you sometime for this scenario.
https://www.npmjs.com/package/@nerdfolio/drizzle-d1-proxy gives you the drizzle adapter needed access D1 via a script that runs drizzle-seed. Just use it to acquire a db handler like any other drizzle adapters:
import {drizzle as drizzleD1Proxy}
const db = drizzleD1Proxy({ accountId, token, databaseId })
The other package is https://www.npmjs.com/package/@nerdfolio/drizzle-d1-helpers . It makes accessing D1 via various modes easier (via binding, via credentials, access the local wrangler sqlite file, etc.). It automates the reading of bindings and databaseId from your wrangler config file. For example, like this
D1Helper.get()
.withCfCredentials(
process.env.CLOUDFLARE_ACCOUNT_ID,
process.env.CLOUDFLARE_D1_TOKEN
)
.useProxyD1(async (db) => {
// run some code with db
});
or
console.log(D1Helper.get("MY_BINDING_NAME").withPersistTo("my-wrangler-path")).sqliteLocalFile;
// my-wrangler-path/d1/miniflare-D1DatabaseObject/a8bef33e667eba6dbefcb5090b02c4719daf1851f75b3901eda4b71e462fa5d2.sqlite
//
Hey everyone!
I've created this message to send in a batch to all opened issues we have, just because there are a lot of them and I want to update all of you with our current work, why issues are not responded to, and the amount of work that has been done by our team over ~8 months.
I saw a lot of issues with suggestions on how to fix something while we were not responding – so thanks everyone. Also, thanks to everyone patiently waiting for a response from us and continuing to use Drizzle!
We currently have 4 major branches with a lot of work done. Each branch was handled by different devs and teams to make sure we could make all the changes in parallel.
First branch is drizzle-kit rewrite
All of the work can be found on the alternation-engine branch. Here is a PR with the work done: https://github.com/drizzle-team/drizzle-orm/pull/4439
As you can see, it has 167k added lines of code and 67k removed, which means we've completely rewritten the drizzle-kit alternation engine, the way we handle diffs for each dialect, together with expanding our test suite from 600 tests to ~9k test units for all different types of actions you can do with kit. More importantly, we changed the migration folder structure and made commutative migrations, so you won't face complex conflicts on migrations when working in a team.
What's left here:
- We are finishing handling defaults for Postgres, the last being geometry (yes, we fixed the
sridissue here as well). - We are finishing commutative migrations for all dialects.
- We are finishing up the command, so the migration flow will be as simple as
drizzle-kit upfor you.
Where it brings us:
- We are getting drizzle-kit into a new good shape where we can call it
[email protected]!
Timeline:
- We need ~2 weeks to finish all of the above and send this branch to beta for testing.
Second big branch is a complex one with several HUGE updates
- Bringing Relational Queries v2 finally live. We've done a lot of work here to actually make it faster than RQBv1 and much better from a DX point of view. But in implementing it, we had to make another big rewrite, so we completely rewrote the drizzle-orm type system, which made it much simpler and improved type performance by ~21.4x:
(types instantiations for 3300 lines production drizzle schema + 990 lines relations)
TS v5.8.3: 728.8k -> 34.1k
TS v5.9.2: 553.7k -> 25.4k
You can read more about it here.
What's left here:
- We have 1 issue with TS that is already in progress of being fixed. The issue and Post about fixing.
Where it brings us:
- We are getting drizzle-orm into a new good shape where we can call it
[email protected]!
Breaking changes:
- We will have them, but we will have open channels for everyone building on top of drizzle types, so we can guide you through all the changes.
Third branch is adding support for CockroachDB and MSSQL dialects
Support for them is already in the alternation-engine branch and will be available together with the drizzle-kit rewrite.
Summary
All of the work we are doing is crucial and should be done sooner rather than later. We've received a lot of feedback and worked really hard to find the best strategies and decisions for API, DX, architecture, etc., so we can confidently mark it as v1 and be sure we can improve it and remain flexible for all the features you are asking for, while becoming even better for everyone building on top of the drizzle API as well.
We didn't want to stay with some legacy decisions and solutions we had, and instead wanted to shape Drizzle in a way that will be best looking ahead to 2025–2026 trends (v1 will get proper effect support, etc.).
We believe that all of the effort we've put in will boost Drizzle and benefit everyone using it.
Thanks everyone, as we said, we are here to stay for a long time to build a great tool together!
Timelines
We are hoping to get v1 for drizzle in beta this fall and same timeline for latest. Right after that we can go through all of the issues and PRs and resond everyone. v1 for drizzle should close ~70% of all the bug tickets we have, so on beta release we will start marking them as closed!
Thank the lord and jesus aka @AndriiSherman