Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data service example #14

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions README-DATASERVICE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# TiDB Cloud Data Service

[About TiDB Cloud Data Service (Beta)](https://docs.pingcap.com/tidbcloud/data-service-overview)

> TiDB Cloud provides a Data Service (beta) feature that enables you to access TiDB Cloud data via an HTTPS request using a custom API endpoint. This feature uses a serverless architecture to handle computing resources and elastic scaling, so you can focus on the query logic in endpoints without worrying about infrastructure or maintenance costs.

We will introduce how to add some Data Service API examples to the existing Bookshop demo.

## Prerequisites

- Create a [TiDB Cloud](https://tidbcloud.com/) account and get your free trial cluster.

- Deploy a Bookshop demo on Vercel. You can follow the [README](./README.md) to deploy the demo.

- All the Data Service configurations are under the `data-service` folder. You can follow the official [documentation](https://docs.pingcap.com/tidbcloud/data-service-manage-github-connection#import-configurations-of-an-existing-data-app) to set up the environment.

## Data Service Examples

### Before you start

Setup the environment variables:

```bash
TIDB_CLOUD_DS_PUB_KEY= # The public key of the Data Service
TIDB_CLOUD_DS_PRI_KEY= # The private key of the Data Service
TIDB_CLOUD_DS_ENDPOINT= # The endpoint of the Data Service
```

### Example 1: Query the data of a table

SQL is defined in [`data-service/http_endpoints/sql/GET-v1-book.sql`](data-service/http_endpoints/sql/GET-v1-book.sql):

```sql
USE bookshop;

SELECT
b.id,
b.title,
b.type,
b.published_at,
b.stock,
b.price,
r.score,
a.name
FROM
`books` AS b
JOIN `ratings` AS r ON b.id = r.book_id
JOIN `book_authors` AS ba ON b.id = ba.book_id
JOIN `authors` AS a ON ba.author_id = a.id
WHERE b.id = ${book_id}
LIMIT 1
```

The `book_id` is a parameter in the URL. The Data Service will replace `${book_id}` with the value in the URL.

API is defined in [`pages/api/data-service/book/[id].ts`](pages/api/data-service/book/[id].ts):

```typescript
// handle GET request
if (req.method === 'GET') {
const url = process.env?.TIDB_CLOUD_DS_ENDPOINT + `/v1/book?book_id=${id}`;
const data = await client.fetch(url).then((res) => res.json());
res.status(200).json(data);
return;
}
```
5 changes: 5 additions & 0 deletions data-service/data_sources/cluster.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{
"cluster_id": 1379661944634374075
}
]
7 changes: 7 additions & 0 deletions data-service/dataapp_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"app_id": "dataapp-SdpjBpyj",
"app_name": "bookshop",
"app_type": "dataapp",
"description": "",
"app_version": "1.0.0"
}
199 changes: 199 additions & 0 deletions data-service/http_endpoints/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
[
{
"name": "book/rating",
"description": "",
"method": "DELETE",
"endpoint": "/v1/book/rating",
"data_source": {
"cluster_id": 1379661944634374075
},
"params": [
{
"name": "user_id",
"type": "number",
"required": 0,
"default": "",
"description": ""
},
{
"name": "book_id",
"type": "number",
"required": 0,
"default": "",
"description": ""
}
],
"settings": {
"timeout": 5000,
"row_limit": 50
},
"tag": "Default",
"batch_operation": 0,
"sql_file": "sql/DELETE-v1-book-rating.sql",
"type": "sql_endpoint",
"return_type": "json"
},
{
"name": "book/rating",
"description": "",
"method": "POST",
"endpoint": "/v1/book/rating",
"data_source": {
"cluster_id": 1379661944634374075
},
"params": [
{
"name": "score",
"type": "number",
"required": 1,
"default": "",
"description": ""
},
{
"name": "user_id",
"type": "number",
"required": 1,
"default": "",
"description": ""
},
{
"name": "book_id",
"type": "number",
"required": 1,
"default": "",
"description": ""
}
],
"settings": {
"timeout": 5000,
"row_limit": 50
},
"tag": "Default",
"batch_operation": 0,
"sql_file": "sql/POST-v1-book-rating.sql",
"type": "sql_endpoint",
"return_type": "json"
},
{
"name": "book/rating",
"description": "",
"method": "GET",
"endpoint": "/v1/book/rating",
"data_source": {
"cluster_id": 1379661944634374075
},
"params": [
{
"name": "book_id",
"type": "number",
"required": 1,
"default": "",
"description": ""
}
],
"settings": {
"timeout": 5000,
"row_limit": 50
},
"tag": "Default",
"batch_operation": 0,
"sql_file": "sql/GET-v1-book-rating.sql",
"type": "sql_endpoint",
"return_type": "json"
},
{
"name": "book/[id]",
"description": "",
"method": "PUT",
"endpoint": "/v1/book",
"data_source": {
"cluster_id": 1379661944634374075
},
"params": [
{
"name": "stock",
"type": "number",
"required": 1,
"default": "",
"description": ""
},
{
"name": "book_id",
"type": "number",
"required": 1,
"default": "",
"description": ""
}
],
"settings": {
"timeout": 5000,
"row_limit": 50
},
"tag": "Default",
"batch_operation": 0,
"sql_file": "sql/PUT-v1-book.sql",
"type": "sql_endpoint",
"return_type": "json"
},
{
"name": "book/[id]",
"description": "",
"method": "GET",
"endpoint": "/v1/book",
"data_source": {
"cluster_id": 1379661944634374075
},
"params": [
{
"name": "book_id",
"type": "string",
"required": 1,
"default": "",
"description": ""
}
],
"settings": {
"timeout": 5000,
"row_limit": 50
},
"tag": "Default",
"batch_operation": 0,
"sql_file": "sql/GET-v1-book.sql",
"type": "sql_endpoint",
"return_type": "json"
},
{
"name": "books",
"description": "",
"method": "GET",
"endpoint": "/v1/books",
"data_source": {
"cluster_id": 1379661944634374075
},
"params": [
{
"name": "start_id",
"type": "number",
"required": 0,
"default": "0",
"description": ""
},
{
"name": "size",
"type": "number",
"required": 0,
"default": "8",
"description": ""
}
],
"settings": {
"timeout": 5000,
"row_limit": 50
},
"tag": "Default",
"batch_operation": 0,
"sql_file": "sql/GET-v1-books.sql",
"type": "sql_endpoint",
"return_type": "json"
}
]
11 changes: 11 additions & 0 deletions data-service/http_endpoints/sql/DELETE-v1-book-rating.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* Getting Started:
Enter "USE {database};" before entering your SQL statements.
Type "--your question" + Enter to try out AI-generated SQL queries
Declare a parameter like "Where id = ${arg}".
*/
USE bookshop;

DELETE FROM `ratings`
WHERE
`user_id` = ${user_id}
AND `book_id` = ${book_id};
21 changes: 21 additions & 0 deletions data-service/http_endpoints/sql/GET-v1-book-rating.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* Getting Started:
Enter "USE {database};" before entering your SQL statements.
Type "--your question" + Enter to try out AI-generated SQL queries
Declare a parameter like "Where id = ${arg}".
*/
USE bookshop;

-- get all ratings by variable book.id, and user details
SELECT
ratings.book_id,
ratings.user_id,
ratings.score,
ratings.rated_at,
users.id,
users.balance,
users.nickname
FROM
ratings
INNER JOIN users ON ratings.user_id = users.id
WHERE
ratings.book_id = ${book_id};
23 changes: 23 additions & 0 deletions data-service/http_endpoints/sql/GET-v1-book.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Getting Started:
Enter "USE {database};" before entering your SQL statements.
Type "--your question" + Enter to try out AI-generated SQL queries
Declare a parameter like "Where id = ${arg}".
*/
USE bookshop;

SELECT
b.id,
b.title,
b.type,
b.published_at,
b.stock,
b.price,
r.score,
a.name
FROM
`books` AS b
JOIN `ratings` AS r ON b.id = r.book_id
JOIN `book_authors` AS ba ON b.id = ba.book_id
JOIN `authors` AS a ON ba.author_id = a.id
WHERE b.id = ${book_id}
LIMIT 1
25 changes: 25 additions & 0 deletions data-service/http_endpoints/sql/GET-v1-books.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* Getting Started:
Enter "USE {database};" before entering your SQL statements.
Type "--your question" + Enter to try out AI-generated SQL queries
Declare a parameter like "Where id = ${arg}".
*/
USE bookshop;

SELECT
b.id,
b.title,
b.type,
b.published_at,
b.stock,
b.price,
r.score,
a.name
FROM
`books` AS b
JOIN `ratings` AS r ON b.id = r.book_id
JOIN `book_authors` AS ba ON b.id = ba.book_id
JOIN `authors` AS a ON ba.author_id = a.id
WHERE b.id > ${start_id}
ORDER BY
b.id ASC
LIMIT ${size}
20 changes: 20 additions & 0 deletions data-service/http_endpoints/sql/POST-v1-book-rating.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* Getting Started:
Enter "USE {database};" before entering your SQL statements.
Type "--your question" + Enter to try out AI-generated SQL queries
Declare a parameter like "Where id = ${arg}".
*/
USE bookshop;

-- check if user ID in users.id, and insert a score to ratings by variable book.id
INSERT INTO
`ratings` (`book_id`, `user_id`, `score`)
SELECT
`book`.`id`,
`users`.`id`,
${score}
FROM
`books` AS `book`
CROSS JOIN `users`
WHERE
`users`.`id` = ${user_id}
AND `book`.`id` = ${book_id};
Loading