D1 foundations

Create and bind local and remote databases

Keep development state separate from production while using one typed binding name in Worker code.

8 minute lesson

~~~

Create a D1 database with Wrangler, then add its identifier under a binding such as DB in wrangler.jsonc. Regenerate Worker types after the configuration changes.

Local D1 commands and wrangler dev use local state by default. A remote command changes the deployed database. Make --local and --remote choices explicit in scripts and review output before applying data changes.

Create the practice database, apply one local command, and prove the remote database remains empty.

Keep the local and remote commands visibly different:

npx wrangler d1 create notes
npx wrangler d1 execute notes --local --command 'select 1'
npx wrangler d1 execute notes --remote --command 'select 1'

Add the returned binding to the Worker configuration. Run migrations locally first. Before every remote command, read the database name and the —remote flag aloud; this small habit prevents destructive experiments against the wrong database.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →