@@ -68,11 +68,12 @@ Populate your database with the table from the [D1 get started](/d1/get-started/
6868 CREATE TABLE IF NOT EXISTS Customers (CustomerId INTEGER PRIMARY KEY , CompanyName TEXT , ContactName TEXT );
6969 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' );
7070 ```
71+
71722 . Initialize your database to run remotely.
7273
73- ``` sh
74- npx wrangler d1 execute < DATABASE_NAME> --remote --file=./schema.sql
75- ```
74+ ``` sh
75+ npx wrangler d1 execute < DATABASE_NAME> --remote --file=./schema.sql
76+ ```
7677</Steps >
7778
7879## 5. Write a Worker file which queries the table
@@ -81,36 +82,36 @@ Write a Worker file which queries the table and outputs both the results with th
8182
8283``` js
8384export default {
84- async fetch (request , env ) {
85- const { pathname } = new URL (request .url );
86- const companyName1 = ` Bs Beverages` ;
87- const stmt = env .DB .prepare (` SELECT * FROM Customers WHERE CompanyName = ?` );
88- const session = env .DB .withSession (" first-unconstrained" );
89-
90- if (pathname === ` /run` ) {
91- const tsStart1 = Date .now ();
92- const { results , meta } = await stmt .bind (companyName1).run ();
93- const d1Duration1 = Date .now () - tsStart1;
94- return Response .json ({ results, meta, d1Duration1 });
95-
96- } else if (pathname === ` /withsession` ) {
97- const tsStart2 = Date .now ();
98- const { results , meta } = await session .prepare (` SELECT * FROM Customers WHERE CompanyName = ?` ).bind (companyName1).run ();
99- const d1Duration2 = Date .now () - tsStart2;
100- return Response .json ({ results, meta, d1Duration2 });
101- }
102- return new Response (
103- ` Welcome to the D1 read replication demo!
85+ async fetch (request , env ) {
86+ const { pathname } = new URL (request .url );
87+ const companyName1 = ` Bs Beverages` ;
88+ const stmt = env .DB .prepare (` SELECT * FROM Customers WHERE CompanyName = ?` );
89+ const session = env .DB .withSession (" first-unconstrained" );
90+
91+ if (pathname === ` /run` ) {
92+ const tsStart1 = Date .now ();
93+ const { results , meta } = await stmt .bind (companyName1).run ();
94+ const d1Duration1 = Date .now () - tsStart1;
95+ return Response .json ({ results, meta, d1Duration1 });
96+
97+ } else if (pathname === ` /withsession` ) {
98+ const tsStart2 = Date .now ();
99+ const { results , meta } = await session .prepare (` SELECT * FROM Customers WHERE CompanyName = ?` ).bind (companyName1).run ();
100+ const d1Duration2 = Date .now () - tsStart2;
101+ return Response .json ({ results, meta, d1Duration2 });
102+ }
103+ return new Response (
104+ ` Welcome to the D1 read replication demo!
104105
105106 Add one of the following slugs below to see the effects of using D1 read replication.
106107
107108 \n /run - Queries the table without using read replication
108109
109110 \n /withsession - Queries the table using read replication (using "first-unconstrained")
110111
111- \n Use the two options to compare the difference in query latency.`
112- );
113- }
112+ \n Use the two options to compare the difference in query latency.`
113+ );
114+ }
114115 };
115116```
116117
0 commit comments