Production Database
In development, we used a local SQLite database file. For production, we will use Cloudflare D1, a serverless SQL database that runs on Cloudflare’s global network.
Creating a D1 Database
Section titled “Creating a D1 Database”-
Create the Database
Run the following command to create a new D1 database named
tm-d1:Terminal window bunx wrangler d1 create tm-d1 -
Update Configuration
The command output will look something like this:
{"binding": "DB","database_name": "tm-d1","database_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}Copy the
database_idand update yourwrangler.jsonfile. Replace the placeholder or existing ID with your new one.wrangler.json "d1_databases": [{"binding": "DB","database_name": "tm-d1","database_id": "YOUR_NEW_DATABASE_ID_HERE","migrations_dir": "drizzle"}]
Running Migrations
Section titled “Running Migrations”We have our schema defined in Drizzle, but our production database is currently empty. We need to apply our migrations to the remote D1 database.
-
Generate Migrations (If needed)
Ensure your migration files are up to date with your schema.
Terminal window bun run db:generate -
Apply Migrations to Remote
Use the
--remoteflag to tell Wrangler to apply changes to the production database instead of the local one.Terminal window bunx wrangler d1 migrations apply tm-d1 --remoteYou will be asked to confirm the action. Select
Yes.Note: If you see an error about the database not being found, ensure you saved
wrangler.jsonwith the correctdatabase_id. -
Verify Tables
You can inspect your production database directly from the command line:
Terminal window bunx wrangler d1 execute tm-d1 --remote --command "SELECT name FROM sqlite_master WHERE type='table';"You should see your tables (e.g.,
users,tasks) listed in the output.
Managing Data
Section titled “Managing Data”You can also interact with your production data using SQL commands.
# Check for usersbunx wrangler d1 execute tm-d1 --remote --command "SELECT * FROM users;"Alternatively, you can use the Cloudflare Dashboard to view and manage your D1 database tables through a graphical interface.
With our database ready, let’s deploy the application code!