|
| 1 | +--- |
| 2 | +updated: 2025-03-04 |
| 3 | +difficulty: Beginner |
| 4 | +content_type: Tutorial |
| 5 | +pcx_content_type: tutorial |
| 6 | +title: Test D1 read replication |
| 7 | +products: |
| 8 | + - D1 |
| 9 | +languages: |
| 10 | + - JavaScript |
| 11 | + - TypeScript |
| 12 | + - SQL |
| 13 | +--- |
| 14 | + |
| 15 | +import { Render, Steps, Tabs, TabItem, FileTree } from "~/components"; |
| 16 | + |
| 17 | +In this tutorial, you will create a basic Worker script to test the effect of [D1 read replication](/d1/concepts/read-replication/), and see the difference in the request latency. |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +This tutorial assumes you have completed and understood the [D1 get started](/d1/get-started/) tutorial. |
| 22 | + |
| 23 | +## 1. Create a Worker |
| 24 | + |
| 25 | +Create a new Worker as the means to query your database. |
| 26 | + |
| 27 | +```sh |
| 28 | +npm create cloudflare@latest -- <DATABASE_NAME> |
| 29 | +``` |
| 30 | + |
| 31 | +## 2. Create a database located far away |
| 32 | + |
| 33 | +Create a database located far away by specifying a [location hint](/d1/configuration-data-location/) which is far away from the region you are located in. |
| 34 | + |
| 35 | +```sh |
| 36 | +npx wrangler d1 create <DATABASE_NAME> --location=apac |
| 37 | +``` |
| 38 | +```sh output |
| 39 | +✅ Successfully created DB '<DATABASE_NAME>' in region APAC |
| 40 | +Created your new D1 database. |
| 41 | + |
| 42 | +{ |
| 43 | + "d1_databases": [ |
| 44 | + { |
| 45 | + "binding": "DB", |
| 46 | + "database_name": "<DATABASE_NAME>", |
| 47 | + "database_id": "<DATABASE_ID>" |
| 48 | + } |
| 49 | + ] |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +This creates a new D1 database and outputs the [binding](/workers/runtime-apis/bindings/) configuration needed in the next step. |
| 54 | + |
| 55 | +## 3. Bind your D1 database to your Worker |
| 56 | + |
| 57 | +Modify your `wrangler.jsonc` file to include the output of the CLI to bind your D1 database to your Worker. |
| 58 | + |
| 59 | +## 4. Populate the D1 database |
| 60 | + |
| 61 | +Populate your database with the table from the [D1 get started](/d1/get-started/) tutorial. |
| 62 | + |
| 63 | +<Steps> |
| 64 | +1. Copy the following code and save it as a `schema.sql` file in the Worker directory you created in step 1: |
| 65 | + |
| 66 | + ```sql |
| 67 | + DROP TABLE IF EXISTS Customers; |
| 68 | + CREATE TABLE IF NOT EXISTS Customers (CustomerId INTEGER PRIMARY KEY, CompanyName TEXT, ContactName TEXT); |
| 69 | + INSERT INTO Customers (CustomerID, CompanyName, ContactName) VALUES (1, 'Alfreds Futterkiste', 'Maria Anders'), (4, 'Around the Horn', 'Thomas Hardy'), (11, 'Bs Beverages', 'Victoria Ashworth'), (13, 'Bs Beverages', 'Random Name'); |
| 70 | + ``` |
| 71 | +2. Initialize your database to run remotely. |
| 72 | + |
| 73 | + ```sh |
| 74 | + npx wrangler d1 execute <DATABASE_NAME> --remote --file=./schema.sql |
| 75 | + ``` |
| 76 | +</Steps> |
| 77 | + |
| 78 | +## 5. Write a Worker file which queries the table |
| 79 | + |
| 80 | +Write a Worker file which queries the table and outputs both the results with the query latency. |
| 81 | + |
| 82 | +```js |
| 83 | +// TBC |
| 84 | +``` |
| 85 | + |
0 commit comments