Example application used in the Workers 201 video course, "Managing Data in your Workers Application".
This example application shows how to set up three routes in an application, backed by D1:
GET /movies
: get all movies from the database as JSON.POST /movies/:id
: update a movie in the database. In the tutorial, we enable just therating
field to be updated.GET /favorites
: get the three highest-rated movies from the database as JSON.
- Create a new d1 database:
$ npx wrangler d1 create movie-example
- Deploy your application:
$ npx wrangler deploy
-
Add the output of that command to your
wrangler.toml
file. -
Execute both SQL files against your D1 database:
$ npx wrangler d1 execute movie-example movie-example --file scripts/001_create_db.sql --remote
$ npx wrangler d1 execute movie-example movie-example --file scripts/002_insert_movies.sql --remote
Note that the --remote
flag will execute this against your remote database. You can remove this flag to execute against your local database.