Skip to content

Commit ec1f012

Browse files
authored
PCX review - basic syntax
1 parent d752ce0 commit ec1f012

File tree

1 file changed

+33
-14
lines changed
  • src/content/docs/d1/tutorials/using-read-replication-for-e-com

1 file changed

+33
-14
lines changed

src/content/docs/d1/tutorials/using-read-replication-for-e-com/index.mdx

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import {
2020
Details,
2121
} from "~/components";
2222

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.
2424

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, 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.
2626

2727
## Prerequisites
2828

@@ -443,15 +443,15 @@ Create a new `public/product-details.html` file to display a single product.
443443
```
444444
</Details>
445445

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.
447447

448448
## Step 3: Create a D1 database and enable read replication
449449

450450
Create a new D1 database by running the following command:
451451

452452
```sh
453453
npx wrangler d1 create fast-commerce
454-
````
454+
```
455455

456456
Add the D1 bindings returned in the terminal to the `wrangler` file:
457457

@@ -519,7 +519,7 @@ The above code creates three API routes:
519519
- `GET /api/products/:id`: Returns a single product.
520520
- `POST /api/product`: Creates or updates a product.
521521

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.
523523

524524
## Step 5: Create the D1 database schema
525525

@@ -614,13 +614,20 @@ app.post('/api/product', async (c) => {
614614

615615
```
616616

617-
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.
618625

619626
Since you are writing the data to the database, you use the `first-primary` constraint.
620627

621628
The bookmark set in the cookie is used to get the latest data from the database.
622629

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.
624631

625632
### 2. GET /api/products
626633

@@ -653,7 +660,14 @@ app.get('/api/products', async (c) => {
653660

654661
```
655662

656-
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.
669+
- You then set this bookmark in the cookie.
670+
- Finally, you return the results.
657671

658672
### 3. GET /api/products/:id
659673

@@ -694,7 +708,12 @@ app.get('/api/products/:id', async (c) => {
694708

695709
```
696710

697-
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.
698717

699718

700719
## 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
705724
npm run dev
706725
```
707726

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.
709728

710-
To insert a new product, you can use the following command:
729+
To insert a new product, use the following command:
711730

712731
```sh
713732
curl -X POST http://localhost:8787/api/product \
714733
-H "Content-Type: application/json" \
715734
-d '{"id": 6, "name": "Fast Computer", "description": "A computer for your home or office", "price": 1000.00, "inventory": 10, "category": "Electronics"}'
716735
```
717736

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.
719738

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.
721740

722741
```sh
723742
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
759778

760779
You then created the products table in the remote database and deployed the application.
761780

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.
763782

764783
You can find the complete code for this tutorial in the [GitHub repository](https://github.com/harshil1712/e-com-d1-hono).
765784

0 commit comments

Comments
 (0)