Boilerplate for quick start on Cloudflare pages and Cloudflare D1.
- Cloudflare account
- Node.js v16+
npm i
npm run create-db -- YOUR_DB_NAME
Append the previous command output to wrangler.toml.
[[ d1_databases ]]
binding = "DB" # i.e. available in your Worker on env.DB
database_name = "YOUR_DB_NAME"
database_id = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
schema.sql
is the database definition.
To reflect this, run the following command.
npm run create-table:local
check the contents of SQLite.
npm run exec-sql -- "select * from memo"
The sample is a memo application. Feel free to edit it.
CRUD API to access D1.
npm run dev
Access: http://localhost:8788/
npm run create-table:prod
npm run deploy
Enter the name of your new project: YOUR_PROJECT_NAME
Enter the production branch name: production
- Access: https://dash.cloudflare.com/
- Pages > YOUR_PROJECT_NAME
- Settings > Functions > D1 database bindings > [Add binding]
- Valiable name =
DB
/ D1 database =YOUR_DB_NAME
> [Save]