This is a template for the Cloudflare C3 CLI that initializes a new SvelteKit project with a D1 database.
Key Features:
pnpm create cloudflare@latest --template [email protected]:ViggieM/sveltekit-d1.git [DIRECTORY] --git
cd [DIRECTORY]
pre-commit install
pnpx wrangler d1 create --binding 'DB' [DATABASE_NAME]
# update types after db creation
pnpm wrangler types ./src/worker-configuration.d.ts
Set the "migrations_dir" of your recently created database in wrangler.jsonc
to "drizzle".
// wrangler.jsonc
"d1_databases": [
{
// ...
"migrations_dir": "drizzle"
}
]
And generate the migrations for the tables 'user' and 'session':
pnpm run db:generate
.env
with the following values (see .env.example
)Name-D1-Import-API-Token
..env
file as CLOUDFLARE_D1_TOKEN
)npx wrangler secret put CLOUDFLARE_ACCOUNT_ID
npx wrangler secret put CLOUDFLARE_DATABASE_ID
npx wrangler secret put CLOUDFLARE_D1_TOKEN
# update types after secrets or env variables update
pnpm wrangler types ./src/worker-configuration.d.ts
pnpm drizzle-kit push
# generate migrations in the /drizzle folder
pnpm drizzle-kit generate
pnpm drizzle-kit migrate
# apply migrations locally
pnpm wrangler d1 migrations apply [DATABASE_NAME] --local
pnpm build
pnpm wrangler deploy
# follow logs
pnpm wrangler tail
In case you encounter any issues with the worker, you can use
pnpm wrangler tail
to inspect logs.
Here are some errors I encountered and their solutions:
Note that these reports were written by Claude Code, so they might contain false informations and hints. :)
No, since the access to the D1 database is configured in the drizzle.config.ts
file as:
export default defineConfig({
// ...
dbCredentials: {
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
databaseId: process.env.CLOUDFLARE_DATABASE_ID!,
token: process.env.CLOUDFLARE_D1_TOKEN!
}
// ...
});