You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/d1/tutorials/using-read-replication-for-e-com/index.mdx
+33-14Lines changed: 33 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,9 +20,9 @@ import {
20
20
Details,
21
21
} from"~/components";
22
22
23
-
[D1 Read Replication](/d1/features/read-replication/) is a feature that allows you to replicate your D1 database to multiple regions. This is useful for your e-commerce website to reduce read latencies and improve availability. In this tutorial, you will learn how to use D1 Read Replication for your e-commerce website.
23
+
[D1 Read Replication](/d1/features/read-replication/) is a feature that allows you to replicate your D1 database to multiple regions. This is useful for your e-commerce website, as it reduces read latencies and improves availability. In this tutorial, you will learn how to use D1 read replication for your e-commerce website.
24
24
25
-
While this tutorial uses a fictional e-commerce website, the principles can be applied to any usecase that requires high availability and low read latencies, for example, a news website, a social media platform, or a marketing website.
25
+
While this tutorial uses a fictional e-commerce website, the principles can be applied to any use-case that requires high availability and low read latencies, such as a news website, a social media platform, or a marketing website.
26
26
27
27
## Prerequisites
28
28
@@ -443,15 +443,15 @@ Create a new `public/product-details.html` file to display a single product.
443
443
```
444
444
</Details>
445
445
446
-
You now have a frontend that lists products and displays a single product. However, the frontend is not connected to the D1 database yet. If you start the development server now, you will see no products. In the next steps, you will create a D1 database and create APIs to fetch products and display them on the frontend.
446
+
You now have a frontend that lists products and displays a single product. However, the frontend is not yet connected to the D1 database. If you start the development server now, you will see no products. In the next steps, you will create a D1 database and create APIs to fetch products and display them on the frontend.
447
447
448
448
## Step 3: Create a D1 database and enable read replication
449
449
450
450
Create a new D1 database by running the following command:
451
451
452
452
```sh
453
453
npx wrangler d1 create fast-commerce
454
-
````
454
+
```
455
455
456
456
Add the D1 bindings returned in the terminal to the `wrangler` file:
457
457
@@ -519,7 +519,7 @@ The above code creates three API routes:
519
519
-`GET /api/products/:id`: Returns a single product.
520
520
-`POST /api/product`: Creates or updates a product.
521
521
522
-
However, the API routes are not connected to the D1 database yet. In the next steps, you will create a products table in the D1 database and update the API routes to connect to the D1 database.
522
+
However, the API routes are not connected to the D1 database yet. In the next steps, you will create a products table in the D1 database, and update the API routes to connect to the D1 database.
In the above code, you get the product data from the request body. You then check if the product exists in the database. If it does, you update the product. If it doesn't, you insert the product. You then set the bookmark in the cookie. Finally, you return the response.
617
+
In the above code:
618
+
619
+
- You get the product data from the request body.
620
+
- You then check if the product exists in the database.
621
+
- If it does, you update the product.
622
+
- If it doesn't, you insert the product.
623
+
- You then set the bookmark in the cookie.
624
+
- Finally, you return the response.
618
625
619
626
Since you are writing the data to the database, you use the `first-primary` constraint.
620
627
621
628
The bookmark set in the cookie is used to get the latest data from the database.
622
629
623
-
If you are using an external platform to manage your products, you can connect this API to the external platform such that when a product is created or updated in the external platform, the product details are automatically updated in the D1 database.
630
+
If you are using an external platform to manage your products, you can connect this API to the external platform, such that, when a product is created or updated in the external platform, the D1 database automatically updates the product details.
In the above code, you get the database session bookmark from the cookie. If the bookmark is not set, you use the `first-unconstrained` constraint. You then create a database session with the bookmark. You fetch all the products from the database and get the latest bookmark. You then set this bookmark in the cookie. Finally, you return the results.
663
+
In the above code:
664
+
665
+
- You get the database session bookmark from the cookie.
666
+
- If the bookmark is not set, you use the `first-unconstrained` constraint.
667
+
- You then create a database session with the bookmark.
668
+
- You fetch all the products from the database and get the latest bookmark.
In the above code, you get the product id from the request parameters. You then create a database session with the bookmark. You fetch the product from the database and get the latest bookmark. You then set this bookmark in the cookie. Finally, you return the results.
711
+
In the above code:
712
+
- You get the product ID from the request parameters.
713
+
- You then create a database session with the bookmark.
714
+
- You fetch the product from the database and get the latest bookmark.
715
+
- You then set this bookmark in the cookie.
716
+
- Finally, you return the results.
698
717
699
718
700
719
## Step 7: Test the application
@@ -705,19 +724,19 @@ You have now updated the API routes to connect to the D1 database. You can now t
705
724
npm run dev
706
725
```
707
726
708
-
Navigate to `http://localhost:8787` and you should see the products listed. Click on a product to view the product details.
727
+
Navigate to `http://localhost:8787. You should see the products listed. Click on a product to view the product details.
709
728
710
-
To insert a new product, you can use the following command:
729
+
To insert a new product, use the following command:
711
730
712
731
```sh
713
732
curl -X POST http://localhost:8787/api/product \
714
733
-H "Content-Type: application/json" \
715
734
-d '{"id": 6, "name": "Fast Computer", "description": "A computer for your home or office", "price": 1000.00, "inventory": 10, "category": "Electronics"}'
716
735
```
717
736
718
-
Navigate to `http://localhost:8787/product-details?id=6` and you should see the new product.
737
+
Navigate to `http://localhost:8787/product-details?id=6`. You should see the new product.
719
738
720
-
Update the product, using the following command and navigate to `http://localhost:8787/product-details?id=6` again. You will see the updated product.
739
+
Update the product using the following command, and navigate to `http://localhost:8787/product-details?id=6` again. You will see the updated product.
721
740
722
741
```sh
723
742
curl -X POST http://localhost:8787/api/product \
@@ -759,7 +778,7 @@ In this tutorial, you learned how to use D1 Read Replication for your e-commerce
759
778
760
779
You then created the products table in the remote database and deployed the application.
761
780
762
-
You can use the same approach for your existing read heavy application to reduce read latencies and improve availability. If you are using an external platform to manage the content, you can connect the external platform to the D1 database so that the content is automatically updated in the database.
781
+
You can use the same approach for your existing read heavy application to reduce read latencies and improve availability. If you are using an external platform to manage the content, you can connect the external platform to the D1 database, so that the content is automatically updated in the database.
763
782
764
783
You can find the complete code for this tutorial in the [GitHub repository](https://github.com/harshil1712/e-com-d1-hono).
0 commit comments