Tutorial Tutorial Tutorial Cloudflare One Cloudflare One Cloudflare One ~88 phút ~88 min ~88 នាទី Đồng bộ 2026-06-10 Synced 2026-06-10 ធ្វើសមកាល 2026-06-10

Tạo API thành access D1 bằng cách sử dụng Proxy Worker Build an API to access D1 using a proxy Worker បង្កើត API ទៅ access D1 ដោយប្រើប្រូកស៊ីកម្មករ

Hướng dẫn chi tiết đồng bộ từ docs Cloudflare — mỗi section có backlink tới đúng vị trí trên trang gốc. Detailed guide synced from Cloudflare docs — each section links to the matching anchor on the official page. មគ្គុទ្ទេសក៍លម្អិតធ្វើសមកាលពី docs Cloudflare — ផ្នែកនីមួយៗមានតំណទៅទីតាំងត្រូវគ្នានៅទំព័រផ្លូវការ។

← Danh mục ← Catalog ← បញ្ជី

Giải thích nhanh Quick context បរិបទរហ័ស

Thuộc Cloudflare One — Zero Trust, truy cập an toàn và kiểm soát traffic người dùng/thiết bị. Tutorial «Xây dựng API to access D1 using proxy Worker» giúp bạn làm quen luồng triển khai thật — phù hợp đọc trước khi mở tài liệu gốc tiếng Anh. Docs gốc chia khoảng 11 bước chính; bản tóm tắt dưới đây giúp bạn nắm khung trước khi làm theo từng lệnh.

This tutorial shows how to create an API that allows you to securely run queries against a D1 database. The API can be used to customize access controls and/or limit what tables can be queried.

ការបង្រៀននេះបង្ហាញពីរបៀបបង្កើត API ដែលអនុញ្ញាតឱ្យអ្នកដំណើរការសំណួរដោយសុវត្ថិភាពប្រឆាំងនឹងមូលដ្ឋានទិន្នន័យ D1 ។ API អាចត្រូវបានប្រើដើម្បីប្ដូរតាមបំណង access គ្រប់គ្រង និង/ឬកំណត់តារាងដែលអាចសួរបាន។

Lưu ý Note ចំណាំ

Lưu ý trước khi làm Notes before you start ចំណាំមុនពេលចាប់ផ្តើម

  • Đây là bản tóm tắt trên Orange Cloud Learning Hub — không thay thế tài liệu chính thức.
  • Luôn mở liên kết «Tài liệu gốc» bên dưới khi cần lệnh CLI, snippet code và ảnh minh họa đầy đủ.
  • Cần tài khoản Cloudflare, Wrangler CLI và (thường) hoàn thành hướng dẫn Get started của Workers/Pages.
  • Zero Trust thường cần quyền admin trên tenant Cloudflare One và IdP đã kết nối.
  • Docs Cloudflare cập nhật thường xuyên — đối chiếu ngày «Rà soát lần cuối» trên trang gốc khi triển khai production.
  • This is a summary on Orange Cloud Learning Hub — it does not replace the official documentation.
  • Open the Official docs link below for CLI commands, code snippets, and full screenshots.
  • Requires a Cloudflare account, Wrangler CLI, and (typically) completing the Workers/Pages Get started guide.
  • Zero Trust typically requires Cloudflare One tenant admin access and a connected IdP.
  • Cloudflare docs change frequently — verify the Last reviewed date on the official page before production use.
  • នេះគឺជាការសង្ខេបនៅលើ Orange Cloud Learning Hub — វាមិនជំនួសឯកសារផ្លូវការទេ។
  • បើកតំណឯកសារផ្លូវការខាងក្រោមសម្រាប់ពាក្យបញ្ជា CLI កូដ snippets និងរូបថតអេក្រង់ពេញ។
  • ទាមទារគណនី Cloudflare, Wrangler CLI និង (ជាធម្មតា) បំពេញការណែនាំ Workers/Pages Get started ។
  • Zero Trust ជាធម្មតាទាមទារ Cloudflare One អ្នកគ្រប់គ្រងភតិកៈ access និង IdP ដែលបានតភ្ជាប់។
  • Cloudflare ឯកសារផ្លាស់ប្តូរជាញឹកញាប់ — ផ្ទៀងផ្ទាត់កាលបរិច្ឆេទដែលបានពិនិត្យចុងក្រោយនៅលើទំព័រផ្លូវការមុនពេលប្រើប្រាស់ផលិតកម្ម។

Tài liệu gốc — rà soát lần cuối: over 1 year ago Official docs — last reviewed: over 1 year ago ឯកសារផ្លូវការ — ពិនិត្យចុងក្រោយ: over 1 year ago

Overview Overview ទិដ្ឋភាពទូទៅ

Trong tutorial này, bạn sẽ create an API that allows you to securely run queries against a cơ sở dữ liệu D1.

Read the "Overview" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «ទិដ្ឋភាពទូទៅ» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
Trong hướng dẫn này, bạn sẽ học cách tạo một API cho phép bạn chạy truy vấn an toàn chống lại một cơ sở dữ liệu D1.
In this tutorial, you will learn how to create an API that allows you to securely run queries against a D1 database.
នៅក្នុងមេរៀននេះ អ្នកនឹងរៀនពីរបៀបបង្កើត API ដែលអនុញ្ញាតឱ្យអ្នកដំណើរការសំណួរដោយសុវត្ថិភាពប្រឆាំងនឹងមូលដ្ឋានទិន្នន័យ D1 ។
Điều này hữu ích nếu bạn muốn access một cơ sở dữ liệu D1 bên ngoài một dự án Công nhân hoặc Pages, tùy chỉnh các điều khiển access và/hoặc hạn chế những bảng có thể được truy vấn.
This is useful if you want to access a D1 database outside of a Worker or Pages project, customize access controls and/or limit what tables can be queried.
វាមានប្រយោជន៍ប្រសិនបើអ្នកចង់ access មូលដ្ឋានទិន្នន័យ D1 នៅខាងក្រៅគម្រោង Worker ឬ Pages ប្ដូរតាមបំណង access គ្រប់គ្រង និង/ឬកំណត់តារាងអ្វីដែលអាចសួរបាន។
D1 tích hợp REST API là tốt nhất cho việc sử dụng hành chính vì Cloudflare API rate limit toàn cầu áp dụng.
D1's built-in REST API is best suited for administrative use as the global Cloudflare API rate limit applies.
ភ្ជាប់មកជាមួយរបស់ D1 API គឺសក្តិសមបំផុតសម្រាប់ការប្រើប្រាស់ផ្នែករដ្ឋបាលជាសកល ដែនកំណត់អត្រា API ត្រូវបានអនុវត្ត។
Để access một cơ sở dữ liệu D1 bên ngoài một dự án Worker, bạn cần tạo một API bằng cách sử dụng một Worker. Ứng dụng của bạn sau đó có thể tương tác an toàn với API này để chạy các truy vấn D1.
To access a D1 database outside of a Worker project, you need to create an API using a Worker. Your application can then securely interact with this API to run D1 queries.
ដើម្បី access មូលដ្ឋានទិន្នន័យ D1 នៅខាងក្រៅគម្រោងកម្មករ អ្នកត្រូវបង្កើត API ដោយប្រើកម្មករ។ បន្ទាប់មកកម្មវិធីរបស់អ្នកអាចធ្វើអន្តរកម្មដោយសុវត្ថិភាពជាមួយ API ដើម្បីដំណើរការសំណួរ D1 ។
D1 sử dụng các truy vấn tham số. Điều này ngăn chặn SQL Injection. Để làm cho API của bạn an toàn hơn, hãy xác nhận input bằng cách sử dụng thư viện như zod ↗.
D1 uses parameterized queries. This prevents SQL injection. To make your API more secure, validate the input using a library like zod ↗.
D1 ប្រើសំណួរដែលកំណត់ប៉ារ៉ាម៉ែត្រ។ នេះរារាំងការចាក់ SQL ។ ដើម្បីធ្វើឱ្យ API របស់អ្នកកាន់តែមានសុវត្ថិភាព សូមធ្វើសុពលភាពការបញ្ចូលដោយប្រើបណ្ណាល័យដូចជា zod ↗។

Liên kết liên quan (docs Cloudflare) Related links (Cloudflare docs) តំណពាក់ព័ន្ធ (docs Cloudflare)

Điều kiện Prerequisites តម្រូវការជាមុន

Phần «Yêu cầu trước» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "Prerequisites" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «តម្រូវការជាមុន» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
  1. Đăng ký cho một Cloudflare account ↗.
  2. Cài đặt Node.js ↗
  3. Có một cơ sở dữ liệu hiện có D1. Nhắc đến Get started tutorial for D1
  1. Sign up for a Cloudflare account ↗.
  2. Install Node.js ↗.
  3. Have an existing D1 database. Refer to Get started tutorial for D1.
  1. ចុះឈ្មោះសម្រាប់ Cloudflare គណនី>។
  2. ដំឡើង Node.js ↗។
  3. មានមូលដ្ឋានទិន្នន័យ D1 ដែលមានស្រាប់។ សូមមើល Get started ការបង្រៀនសម្រាប់ __TERM61a_5__
Node.js Trình quản lý phiên bản
Node.js version manager
កម្មវិធីគ្រប់គ្រងកំណែ Node.js
Sử dụng trình quản lý phiên bản nút như Volta ↗ hoặc nvm ↗ để tránh các vấn đề quyền và thay đổi các phiên bản Node.js. Wrangler, được thảo luận sau trong hướng dẫn này, yêu cầu một phiên bản nút của 16.17.0 hoặc mới hơn.
Use a Node version manager like Volta ↗ ornvm ↗ to avoid permission issues and change Node.js versions. Wrangler, discussed later in this guide, requires a Node version of 16.17.0 or later.
ប្រើកម្មវិធីគ្រប់គ្រងកំណែ Node ដូចជា Volta ↗ ឬvm" class="linker""> ↗ ដើម្បីជៀសវាងបញ្ហាការអនុញ្ញាត និងផ្លាស់ប្តូរកំណែ Node.js ។ __TERM27_2_code ទាមទារកំណែក្រោយនេះ ការណែនាំ class="tutorial-inline-code">16.17.0 ឬក្រោយ។

1.Điều Tạo một dự án mới 1. Create a new project 1. បង្កើតគម្រោងថ្មីមួយ

Tạo new Worker to create and deploy your API.

Read the "1. Create a new project" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «1. បង្កើតគម្រោងថ្មីមួយ» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
Tạo một công nhân mới để tạo và triển khai API của bạn.
Create a new Worker to create and deploy your API.
បង្កើតអ្នកធ្វើការថ្មីដើម្បីបង្កើត និងប្រើប្រាស់ API របស់អ្នក។
  1. Tạo một Worker có tên d1-http bằng cách chạy:
  1. Create a Worker named d1-http by running:
  1. បង្កើត​កម្មករ​ម្នាក់​ឈ្មោះ d1-http ដោយ​ដំណើរការ៖
Mã sản phẩm: npm yarn pnpm
npm yarn pnpm
npm yarn pnpm
text
npm create cloudflare@latest -- d1-http
text
yarn create cloudflare d1-http
text
pnpm create cloudflare@latest d1-http
Để cài đặt, hãy chọn các tùy chọn sau: Đối với Bạn muốn bắt đầu với gì?, chọn Hello World example. Đối với Mẫu nào bạn muốn sử dụng?, chọn Worker only. Đối với Ngôn ngữ nào bạn muốn sử dụng?, chọn TypeScript. Đối với Bạn muốn sử dụng git để kiểm soát phiên bản?, chọn Yes. * Đối với Bạn muốn triển khai ứng dụng của mình?, chọn No (chúng tôi sẽ thực hiện một số thay đổi trước khi triển khai).
For setup, select the following options: For What would you like to start with?, choose Hello World example. For Which template would you like to use?, choose Worker only. For Which language do you want to use?, choose TypeScript. For Do you want to use git for version control?, choose Yes. * For Do you want to deploy your application?, choose No (we will be making some changes before deploying).
សម្រាប់ការដំឡើង សូមជ្រើសរើសជម្រើសខាងក្រោម៖ សម្រាប់ តើអ្នកចង់ចាប់ផ្តើមជាមួយអ្វី? សូមជ្រើសរើស Hello World example។ សម្រាប់ តើគំរូមួយណាដែលអ្នកចង់ប្រើ? សូមជ្រើសរើស Worker only។ សម្រាប់ តើភាសាមួយណាដែលអ្នកចង់ប្រើ? សូមជ្រើសរើស TypeScript។ សម្រាប់ តើអ្នកចង់ប្រើ git សម្រាប់ការគ្រប់គ្រងកំណែទេ? សូមជ្រើសរើស Yes។ * សម្រាប់ តើអ្នកចង់ដាក់ឱ្យប្រើប្រាស់កម្មវិធីរបស់អ្នកទេ? សូមជ្រើសរើស No (យើងនឹងធ្វើការផ្លាស់ប្តូរមួយចំនួនមុនពេលដាក់ឱ្យប្រើប្រាស់)។
  1. Thay đổi vào thư mục dự án mới của bạn để bắt đầu phát triển:
  1. Change into your new project directory to start developing:
  1. ផ្លាស់ប្តូរទៅក្នុងថតគម្រោងថ្មីរបស់អ្នក ដើម្បីចាប់ផ្តើមអភិវឌ្ឍ៖
text
cd d1-http

2.Đối với Cài đặt HONO 2. Install Hono 2. ដំឡើង Hono

Phần «Cài đặt Hono» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "2. Install Hono" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «2. ដំឡើង Hono» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
Trong hướng dẫn này, bạn sẽ sử dụng Hono ↗, một framework kiểu Express.js, để xây dựng API.
In this tutorial, you will use Hono ↗, an Express.js-style framework, to build the API.
នៅក្នុងមេរៀននេះ អ្នកនឹងប្រើ Hono ↗ ដែលជាក្របខ័ណ្ឌរចនាប័ទ្ម Express.js ដើម្បីបង្កើត API ។
  1. Để sử dụng Hono trong dự án này, hãy cài đặt nó bằng cách sử dụng npm:
  1. To use Hono in this project, install it using npm:
  1. ដើម្បីប្រើ Hono ក្នុងគម្រោងនេះ សូមដំឡើងវាដោយប្រើ npm៖
npm yarn pnpm bun
npm yarn pnpm bun
npm yarn pnpm ប៊ុន
text
npm i hono
text
yarn add hono
text
pnpm add hono
text
bun add hono

3 Cái Thêm một API.KEY 3. Add an API.KEY 3. បន្ថែម API.KEY

Phần «Thêm API.KEY» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "3. Add an API.KEY" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «3. បន្ថែម API.KEY» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
Bạn cần một phím API để thực hiện các cuộc gọi xác thực đến API. Để đảm bảo rằng khóa API là an toàn, hãy thêm nó dưới dạng secret.
You need an API key to make authenticated calls to the API. To ensure that the API key is secure, add it as a secret.
អ្នកត្រូវការសោ API ដើម្បីធ្វើការហៅទូរសព្ទដែលផ្ទៀងផ្ទាត់ទៅកាន់ API ។ ដើម្បីធានាថាសោ API មានសុវត្ថិភាព សូមបន្ថែមវាជា
  1. Để phát triển cục bộ, hãy tạo tệp .dev.vars trong thư mục gốc của d1-http.
  2. Thêm phím API vào file như sau:
  1. For local development, create a .dev.vars file in the root directory of d1-http.
  2. Add your API key in the file as follows.
  1. សម្រាប់ការអភិវឌ្ឍន៍មូលដ្ឋាន បង្កើតឯកសារ .dev.vars នៅក្នុងថត root នៃ d1-http។
  2. បន្ថែមសោ API របស់អ្នកនៅក្នុងឯកសារដូចខាងក្រោម។
.công ty.vars
.dev.vars
.dev.vars
text
API_KEY="YOUR_API_KEY"
Thay thế YOUR<em>API</em>KEY bằng giá trị chuỗi hợp lệ. Bạn cũng có thể tạo giá trị này bằng cách sử dụng lệnh sau. Cửa sổ Terminal
Replace YOUR<em>API</em>KEY with a valid string value. You can also generate this value using the following command. Terminal window
ជំនួស YOUR<em>API</em>KEY ជាមួយនឹងតម្លៃខ្សែអក្សរត្រឹមត្រូវ។ អ្នកក៏អាចបង្កើតតម្លៃនេះដោយប្រើពាក្យបញ្ជាខាងក្រោម។ បង្អួចស្ថានីយ
text
openssl rand -base64 32
Trong bước này, chúng tôi đã xác định tên của khóa API là API_KEY.
In this step, we have defined the name of the API key to be API_KEY.
ក្នុង​ជំហាន​នេះ យើង​បាន​កំណត់​ឈ្មោះ​សោ API ជា API_KEY។

Bốn Initialize ứng dụng 4. Initialize the application 4. ចាប់ផ្តើមកម្មវិធី

Phần «Khởi tạo ứng dụng» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "4. Initialize the application" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «4. ចាប់ផ្តើមកម្មវិធី» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
Để khởi tạo ứng dụng, bạn cần nhập các gói cần thiết, khởi tạo một ứng dụng Hono mới và cấu hình middleware sau:
To initialize the application, you need to import the required packages, initialize a new Hono application, and configure the following middleware:
ដើម្បីចាប់ផ្តើមកម្មវិធី អ្នកត្រូវនាំចូលកញ្ចប់ដែលត្រូវការ ចាប់ផ្តើមកម្មវិធី Hono ថ្មី និងកំណត់រចនាសម្ព័ន្ធឧបករណ៍កណ្តាលខាងក្រោម៖
  • Bearer Auth ↗: Thêm xác thực vào API.
  • Logger ↗: Cho phép theo dõi dòng chảy của yêu cầu và phản hồi.
  • Pretty JSON ↗: Cho phép "JSON pretty print" cho các cơ quan phản hồi JSON.
  • Bearer Auth ↗៖ បន្ថែមការផ្ទៀងផ្ទាត់ទៅ API។
  • អ្នកកាប់ឈើ ↗៖ អនុញ្ញាតឱ្យត្រួតពិនិត្យលំហូរនៃសំណើ និងការឆ្លើយតប។
  • ស្អាត JSON ↗៖ បើកដំណើរការ "JSON pretty print" សម្រាប់តួការឆ្លើយតប JSON ។
  1. Thay thế nội dung của tập tin src/index.ts bằng mã bên dưới.
  1. Replace the contents of the src/index.ts file with the code below.
  1. ជំនួសមាតិកានៃឯកសារ src/index.ts ដោយលេខកូដខាងក្រោម។
Trang chủ / index.ts
src/index.ts
src/index.ts
text
import { Hono } from "hono";  
import { bearerAuth } from "hono/bearer-auth";  
import { logger } from "hono/logger";  
import { prettyJSON } from "hono/pretty-json";  
type Bindings = {  
  API_KEY: string;  
};  
const app = new Hono<{ Bindings: Bindings }>();  
app.use("*", prettyJSON(), logger(), async (c, next) => {  
  const auth = bearerAuth({ token: c.env.API_KEY });  
  return auth(c, next);  
});

5 Thêm API điểm cuối 5. Add API endpoints 5. បន្ថែម API ចំណុចបញ្ចប់

Phần «Thêm API endpoints» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "5. Add API endpoints" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «5. បន្ថែម API ចំណុចបញ្ចប់» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
  1. Thêm đoạn trích sau vào src/index.ts của bạn.
  1. Add the following snippet into your src/index.ts.
  1. បន្ថែម snippet ខាងក្រោមទៅក្នុង src/index.ts របស់អ្នក។
Trang chủ / index.ts
src/index.ts
src/index.ts
text
// Paste this code at the end of the src/index.ts file  
app.post("/api/all", async (c) => {  
  return c.text("/api/all endpoint");  
});  
app.post("/api/exec", async (c) => {  
  return c.text("/api/exec endpoint");  
});  
app.post("/api/batch", async (c) => {  
  return c.text("/api/batch endpoint");  
});  
export default app;
Điều này thêm các điểm cuối sau: POST /api/all POST /api/exec * POST /api/batch
This adds the following endpoints: POST /api/all POST /api/exec * POST /api/batch
វាបន្ថែមចំណុចបញ្ចប់ខាងក្រោម៖ POST /api/all POST /api/exec * POST /api/batch
  1. Khởi động máy chủ phát triển bằng cách chạy lệnh sau:
  1. Start the development server by running the following command:
  1. ចាប់ផ្តើមម៉ាស៊ីនមេអភិវឌ្ឍន៍ដោយដំណើរការពាក្យបញ្ជាខាងក្រោម៖
Mã sản phẩm: npm yarn pnpm
npm yarn pnpm
npm yarn pnpm
text
npm run dev
text
yarn run dev
text
pnpm run dev
  1. Để kiểm tra API tại địa phương, mở một thiết bị đầu cuối thứ hai.
  2. Trong terminal thứ hai, thực hiện lệnh cURL bên dưới. Thay thế YOUR<em>API</em>KEY bằng giá trị bạn đã đặt trong tệp .dev.vars.
  1. To test the API locally, open a second terminal.
  2. In the second terminal, execute the below cURL command. Replace YOUR<em>API</em>KEY with the value you set in the .dev.vars file.
  1. ដើម្បីសាកល្បង API ក្នុងមូលដ្ឋាន សូមបើកស្ថានីយទីពីរ។
  2. នៅក្នុងស្ថានីយទីពីរ ប្រតិបត្តិពាក្យបញ្ជា cURL ខាងក្រោម។ ជំនួស YOUR<em>API</em>KEY ជាមួយនឹងតម្លៃដែលអ្នកបានកំណត់ក្នុងឯកសារ .dev.vars។
text
curl -H "Authorization: Bearer YOUR_API_KEY" "http://localhost:8787/api/all" --data '{}'
Bạn sẽ nhận được kết quả sau:
You should get the following output:
អ្នកគួរតែទទួលបានលទ្ធផលដូចខាងក្រោមៈ
text
/api/all endpoint
  1. Ngăn chặn máy chủ cục bộ chạy bằng cách nhấn x trong thiết bị đầu tiên.
  1. Stop the local server from running by pressing x in the first terminal.
  1. បញ្ឈប់ម៉ាស៊ីនមេក្នុងស្រុកពីការដំណើរការដោយចុច x នៅក្នុងស្ថានីយទីមួយ។
Ứng dụng Hono hiện đã được thiết lập. Bạn có thể kiểm tra các điểm cuối khác và thêm nhiều điểm cuối nếu cần. API chưa trả về bất kỳ thông tin nào từ cơ sở dữ liệu của bạn. Trong các bước tiếp theo, bạn sẽ tạo một cơ sở dữ liệu, thêm liên kết của nó, và cập nhật các điểm cuối để tương tác với cơ sở dữ liệu.
The Hono application is now set up. You can test the other endpoints and add more endpoints if needed. The API does not yet return any information from your database. In the next steps, you will create a database, add its bindings, and update the endpoints to interact with the database.
កម្មវិធី Hono ឥឡូវនេះត្រូវបានតំឡើង។ អ្នកអាចសាកល្បងចំណុចបញ្ចប់ផ្សេងទៀត និងបន្ថែមចំណុចបញ្ចប់បន្ថែមទៀតប្រសិនបើចាំបាច់។ API មិនទាន់ត្រឡប់ព័ត៌មានណាមួយពីមូលដ្ឋានទិន្នន័យរបស់អ្នកទេ។ នៅជំហានបន្ទាប់ អ្នកនឹងបង្កើតមូលដ្ឋានទិន្នន័យ បន្ថែមការចងរបស់វា និងធ្វើបច្ចុប្បន្នភាពចំណុចបញ្ចប់ដើម្បីធ្វើអន្តរកម្មជាមួយមូលដ្ឋានទិន្នន័យ។

Sáu Tạo cơ sở dữ liệu 6. Create a database 6. បង្កើតមូលដ្ឋានទិន្នន័យ

If you do not have a cơ sở dữ liệu D1 already, you can create a new database with wrangler d1 create .

Read the "6. Create a database" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «6. បង្កើតមូលដ្ឋានទិន្នន័យ» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
Nếu bạn chưa có cơ sở dữ liệu D1, bạn có thể tạo cơ sở dữ liệu mới với wrangler d1 create.
If you do not have a D1 database already, you can create a new database with wrangler d1 create.
ប្រសិនបើអ្នកមិនមានមូលដ្ឋានទិន្នន័យ D1 រួចហើយទេ អ្នកអាចបង្កើតមូលដ្ឋានទិន្នន័យថ្មីជាមួយ wrangler d1 create។
  1. Trong terminal của bạn, chạy:
  1. In your terminal, run:
  1. នៅក្នុងស្ថានីយរបស់អ្នក សូមដំណើរការ៖
text
npx wrangler d1 create d1-http-example
Bạn có thể được yêu cầu đăng nhập vào tài khoản Cloudflare của bạn. Sau khi đăng nhập, lệnh sẽ tạo một cơ sở dữ liệu D1 mới. Bạn nên thấy một đầu ra tương tự trong thiết bị đầu cuối của bạn.
You may be asked to login to your Cloudflare account. Once logged in, the command will create a new D1 database. You should see a similar output in your terminal.
អ្នកអាចត្រូវបានស្នើសុំឱ្យចូលគណនី Cloudflare របស់អ្នក។ នៅពេលដែលបានចូល នោះពាក្យបញ្ជានឹងបង្កើតមូលដ្ឋានទិន្នន័យ D1 ថ្មី។ អ្នកគួរតែឃើញលទ្ធផលស្រដៀងគ្នានៅក្នុងស្ថានីយរបស់អ្នក។
text
✅ Successfully created DB 'd1-http-example' in region EEUR  
Created your new D1 database.  
[[d1_databases]]  
binding = "DB" # i.e. available in your Worker on env.DB  
database_name = "d1-http-example"  
database_id = "1234567890"
Hãy ghi chú database<em>name và database</em>id được hiển thị. Bạn sẽ sử dụng điều này để tham khảo cơ sở dữ liệu bằng cách tạo binding.
Make a note of the displayed database<em>name and database</em>id. You will use this to reference the database by creating a binding.
កំណត់ចំណាំអំពី database<em>name និង database</em>id ដែលបានបង្ហាញ។ អ្នកនឹងប្រើវាដើម្បីយោងមូលដ្ឋានទិន្នន័យដោយបង្កើត

Bảy Thêm một ràng buộc 7. Add a binding 7. បន្ថែមការចង

Phần «Thêm binding» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "7. Add a binding" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «7. បន្ថែមការចង» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
  1. Từ thư mục d1-http của bạn, mở tệp Wrangler, tệp cấu hình của Wrangler.
  2. Thêm liên kết sau vào file. Hãy chắc chắn rằng database<em>name và database</em>id là chính xác.
  1. From your d1-http folder, open the Wrangler file, Wrangler's configuration file.
  2. Add the following binding in the file. Make sure that the database<em>name and the database</em>id are correct.
  1. ពីថត d1-http របស់អ្នក បើកឯកសារ Wrangler ឯកសារកំណត់រចនាសម្ព័ន្ធរបស់ Wrangler។
  2. បន្ថែមការចងខាងក្រោមនៅក្នុងឯកសារ។ សូមប្រាកដថា database<em>name និង database</em>id ត្រឹមត្រូវ។
text
{  
  "d1_databases": [  
    {  
      "binding": "DB", // i.e. available in your Worker on env.DB  
      "database_name": "d1-http-example",  
      "database_id": "1234567890"  
    }  
  ]  
}
text
[[d1_databases]]  
binding = "DB"  
database_name = "d1-http-example"  
database_id = "1234567890"
  1. Trong tệp src/index.ts của bạn, hãy cập nhật kiểu Bindings bằng cách thêm DB: D1Database.
  1. In your src/index.ts file, update the Bindings type by adding DB: D1Database.
  1. នៅក្នុងឯកសារ src/index.ts របស់អ្នក ធ្វើបច្ចុប្បន្នភាពប្រភេទ Bindings ដោយបន្ថែម DB: D1Database។
text
type Bindings = {  
  DB: D1Database;  
  API_KEY: string;  
};
Bây giờ bạn có thể access cơ sở dữ liệu trong ứng dụng Hono.
You can now access the database in the Hono application.
ឥឡូវនេះអ្នកអាច access មូលដ្ឋានទិន្នន័យនៅក្នុងកម្មវិធី Hono ។
Để tạo một bảng trong cơ sở dữ liệu mới được tạo:
To create a table in your newly created database:
ដើម្បីបង្កើតតារាងក្នុងមូលដ្ឋានទិន្នន័យដែលទើបបង្កើតថ្មីរបស់អ្នក៖
  1. Tạo một thư mục mới có tên schemas bên trong thư mục d1-http của bạn.
  2. Tạo một tệp mới có tên schema.sql, và dán câu lệnh SQL sau vào tệp.
  1. Create a new folder called schemas inside your d1-http folder.
  2. Create a new file called schema.sql, and paste the following SQL statement into the file.
  1. បង្កើតថតថ្មីមួយដែលមានឈ្មោះថា schemas នៅក្នុងថត d1-http របស់អ្នក។
  2. បង្កើតឯកសារថ្មីមួយដែលមានឈ្មោះថា schema.sql ហើយបិទភ្ជាប់សេចក្តីថ្លែងការណ៍ SQL ខាងក្រោមទៅក្នុងឯកសារ។
Lời bài hát: Sql
schema.sql
schema.sql
text
DROP TABLE IF EXISTS posts;  
CREATE TABLE IF NOT EXISTS posts (  
  id integer PRIMARY KEY AUTOINCREMENT,  
  author text NOT NULL,  
  title text NOT NULL,  
  body text NOT NULL,  
  post_slug text NOT NULL  
);  
INSERT INTO posts (author, title, body, post_slug) VALUES ('Harshil', 'D1 HTTP API', 'Learn to create an API to query your D1 database.','d1-http-api');
Mã thả bất kỳ bảng nào có tên posts nếu nó tồn tại, sau đó tạo một bảng mới posts với trường id, author, title, body và post_slug. Sau đó, nó sử dụng một câu lệnh INSERT để chiếm bảng.
The code drops any table named posts if it exists, then creates a new table posts with the field id, author, title, body, and post_slug. It then uses an INSERT statement to populate the table.
លេខកូដទម្លាក់តារាងណាមួយដែលមានឈ្មោះ posts ប្រសិនបើមាន នោះនឹងបង្កើតតារាង posts ជាមួយនឹងវាល id, author ial title និង body។ បន្ទាប់មកវាប្រើសេចក្តីថ្លែងការណ៍ INSERT ដើម្បីបំពេញតារាង។
  1. Trong thiết bị đầu cuối của bạn, hãy thực hiện lệnh sau để tạo bảng này:
  1. In your terminal, execute the following command to create this table:
  1. នៅក្នុងស្ថានីយរបស់អ្នក ប្រតិបត្តិពាក្យបញ្ជាខាងក្រោមដើម្បីបង្កើតតារាងនេះ៖
text
npx wrangler d1 execute d1-http-example --file=./schemas/schema.sql
Sau khi thực hiện thành công, một bảng mới sẽ được thêm vào cơ sở dữ liệu của bạn.
Upon successful execution, a new table will be added to your database.
នៅពេលដំណើរការដោយជោគជ័យ តារាងថ្មីមួយនឹងត្រូវបានបន្ថែមទៅក្នុងមូលដ្ឋានទិន្នន័យរបស់អ្នក។
Bảng sẽ được tạo trong trường hợp cục bộ của cơ sở dữ liệu. Nếu bạn muốn thêm bảng này vào cơ sở dữ liệu sản xuất của bạn đặt "remote" : true trong cấu hình ràng buộc D1. Xem remote bindings documentation để biết thêm thông tin.
The table will be created in the local instance of the database. If you want to add this table to your production database set "remote" : true in the D1 binding configuration. Refer to the remote bindings documentation for more information.
តារាងនឹងត្រូវបានបង្កើតនៅក្នុងឧទាហរណ៍មូលដ្ឋាននៃមូលដ្ឋានទិន្នន័យ។ ប្រសិនបើអ្នកចង់បន្ថែមតារាងនេះទៅក្នុងសំណុំទិន្នន័យផលិតកម្មរបស់អ្នក "remote" : true នៅក្នុងការកំណត់រចនាសម្ព័ន្ធចង D1។ សូមមើល ឯកសារភ្ជាប់ពីចម្ងាយ> សម្រាប់ព័ត៌មានបន្ថែម។

Chín Muốn cơ sở dữ liệu 9. Query the database 9. សាកសួរមូលដ្ឋានទិន្នន័យ

Your application can now access the cơ sở dữ liệu D1. In this step, you will update the API endpoints to query the database and return the result.

Read the "9. Query the database" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «9. សាកសួរមូលដ្ឋានទិន្នន័យ» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
Ứng dụng của bạn bây giờ có thể access cơ sở dữ liệu D1. Trong bước này, bạn sẽ cập nhật các điểm cuối API để truy vấn cơ sở dữ liệu và trả về kết quả.
Your application can now access the D1 database. In this step, you will update the API endpoints to query the database and return the result.
កម្មវិធីរបស់អ្នកឥឡូវនេះអាច access មូលដ្ឋានទិន្នន័យ D1 ។ នៅក្នុងជំហាននេះ អ្នកនឹងធ្វើបច្ចុប្បន្នភាពចំណុចបញ្ចប់ API ដើម្បីសាកសួរមូលដ្ឋានទិន្នន័យ និងត្រឡប់លទ្ធផល។
  1. Trong tập tin src/index.ts của bạn, hãy cập nhật mã như sau.
  1. In your src/index.ts file, update the code as follow.
  1. នៅក្នុងឯកសារ src/index.ts របស់អ្នក ធ្វើបច្ចុប្បន្នភាពកូដដូចខាងក្រោម។
Trang chủ / index.ts
src/index.ts
src/index.ts
text
// Update the API routes  
/**  
* Executes the `stmt.run()` method.  
* https://developers.cloudflare.com/d1/worker-api/prepared-statements/#run  
*/  
app.post('/api/all', async (c) => {  
    return c.text("/api/all endpoint");  
  try {  
    let { query, params } = await c.req.json();  
    let stmt = c.env.DB.prepare(query);  
    if (params) {  
      stmt = stmt.bind(params);  
    }  
    const result = await stmt.run();  
    return c.json(result);  
  } catch (err) {  
    return c.json({ error: `Failed to run query: ${err}` }, 500);  
  }  
});  
/**  
* Executes the `db.exec()` method.  
* https://developers.cloudflare.com/d1/worker-api/d1-database/#exec  
*/  
app.post('/api/exec', async (c) => {  
    return c.text("/api/exec endpoint");  
  try {  
    let { query } = await c.req.json();  
    let result = await c.env.DB.exec(query);  
    return c.json(result);  
  } catch (err) {  
    return c.json({ error: `Failed to run query: ${err}` }, 500);  
  }  
});  
/**  
* Executes the `db.batch()` method.  
* https://developers.cloudflare.com/d1/worker-api/d1-database/#batch  
*/  
app.post('/api/batch', async (c) => {  
    return c.text("/api/batch endpoint");  
  try {  
    let { batch } = await c.req.json();  
    let stmts = [];  
    for (let query of batch) {  
      let stmt = c.env.DB.prepare(query.query);  
      if (query.params) {  
        stmts.push(stmt.bind(query.params));  
      } else {  
        stmts.push(stmt);  
      }  
    }  
    const results = await c.env.DB.batch(stmts);  
    return c.json(results);  
  } catch (err) {  
    return c.json({ error: `Failed to run query: ${err}` }, 500);  
  }  
});  
...
Trong mã trên, các điểm cuối được cập nhật để nhận query và params. Những truy vấn và tham số này được truyền đến các hàm tương ứng để tương tác với cơ sở dữ liệu.
In the above code, the endpoints are updated to receive query and params. These queries and parameters are passed to the respective functions to interact with the database.
នៅក្នុងកូដខាងលើ ចំណុចបញ្ចប់ត្រូវបានធ្វើបច្ចុប្បន្នភាពដើម្បីទទួលបាន query និង params។ សំណួរ និងប៉ារ៉ាម៉ែត្រទាំងនេះត្រូវបានបញ្ជូនទៅមុខងាររៀងៗខ្លួនដើម្បីធ្វើអន្តរកម្មជាមួយមូលដ្ឋានទិន្នន័យ។
  • Nếu truy vấn thành công, bạn sẽ nhận được kết quả từ cơ sở dữ liệu.
  • Nếu có lỗi, thông báo lỗi sẽ được trả về.
  • If the query is successful, you receive the result from the database.
  • If there is an error, the error message is returned.
  • ប្រសិនបើសំណួរជោគជ័យ អ្នកទទួលបានលទ្ធផលពីមូលដ្ឋានទិន្នន័យ។
  • បើ​មាន​កំហុស សារ​កំហុស​ត្រូវ​បាន​ត្រឡប់​មក​វិញ។

Mười Kiểm tra API 10. Test the API 10. សាកល្បង API

Phần «Test API» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "10. Test the API" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «10. សាកល្បង API» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
Bây giờ API có thể truy vấn cơ sở dữ liệu, bạn có thể kiểm tra nó cục bộ.
Now that the API can query the database, you can test it locally.
ឥឡូវនេះ API អាចសួរមូលដ្ឋានទិន្នន័យ អ្នកអាចសាកល្បងវានៅក្នុងមូលដ្ឋាន។
  1. Khởi động máy chủ phát triển bằng cách thực hiện lệnh sau:
  1. Start the development server by executing the following command:
  1. ចាប់ផ្តើមម៉ាស៊ីនមេអភិវឌ្ឍន៍ដោយប្រតិបត្តិពាក្យបញ្ជាខាងក្រោម៖
Mã sản phẩm: npm yarn pnpm
npm yarn pnpm
npm yarn pnpm
text
npm run dev
text
yarn run dev
text
pnpm run dev
  1. Trong cửa sổ Terminal mới, thực hiện các lệnh cURL sau. Hãy chắc chắn thay thế YOUR<em>API</em>KEY bằng giá trị chính xác.
  1. In a new terminal window, execute the following cURL commands. Make sure to replace YOUR<em>API</em>KEY with the correct value.
  1. នៅក្នុងបង្អួចស្ថានីយថ្មី ប្រតិបត្តិពាក្យបញ្ជា cURL ខាងក្រោម។ សូមប្រាកដថាបានជំនួស YOUR<em>API</em>KEY ជាមួយនឹងតម្លៃត្រឹមត្រូវ។
/api/tất cả
/api/all
/api/ទាំងអស់។
text
curl -H "Authorization: Bearer YOUR_API_KEY" "http://localhost:8787/api/all" --data '{"query": "SELECT title FROM posts WHERE id=?", "params":1}'
/api/số lượng
/api/batch
/api/បាច់
text
curl -H "Authorization: Bearer YOUR_API_KEY" "http://localhost:8787/api/batch" --data '{"batch": [ {"query": "SELECT title FROM posts WHERE id=?", "params":1},{"query": "SELECT id FROM posts"}]}'
/api / Exec
/api/exec
/api/exec
text
curl -H "Authorization: Bearer YOUR_API_KEY" "localhost:8787/api/exec" --data '{"query": "INSERT INTO posts (author, title, body, post_slug) VALUES ('\''Harshil'\'', '\''D1 HTTP API'\'', '\''Learn to create an API to query your D1 database.'\'','\''d1-http-api'\'')" }'
Nếu mọi thứ được thực hiện đúng cách, các lệnh trên sẽ dẫn đến kết quả thành công.
If everything is implemented correctly, the above commands should result successful outputs.
ប្រសិនបើអ្វីៗត្រូវបានអនុវត្តយ៉ាងត្រឹមត្រូវ នោះពាក្យបញ្ជាខាងលើគួរតែទទួលបានលទ្ធផលជោគជ័យ។

Mười một Cài đặt API 11. Deploy the API 11. ដាក់ពង្រាយ API

Phần «Triển khai API» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "11. Deploy the API" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «11. ដាក់ពង្រាយ API» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
Bây giờ mọi thứ đang hoạt động như mong đợi, bước cuối cùng là triển khai nó vào mạng Cloudflare. Bạn sẽ sử dụng Wrangler để triển khai API.
Now that everything is working as expected, the last step is to deploy it to the Cloudflare network. You will use Wrangler to deploy the API.
ឥឡូវនេះអ្វីៗដំណើរការដូចការរំពឹងទុក ជំហានចុងក្រោយគឺត្រូវដាក់ពង្រាយវាទៅបណ្តាញ Cloudflare។ អ្នកនឹងប្រើ Wrangler ដើម្បីដាក់ពង្រាយ API ។
  1. Để sử dụng API trong sản xuất thay vì sử dụng nó cục bộ, bạn cần thêm bảng vào cơ sở dữ liệu từ xa (sản xuất). Để thêm bảng vào cơ sở dữ liệu sản xuất của bạn, hãy chạy lệnh sau:
  1. To use the API in production instead of using it locally, you need to add the table to your remote (production) database. To add the table to your production database, run the following command:
  1. ដើម្បីប្រើ API នៅក្នុងការផលិតជំនួសឱ្យការប្រើវាក្នុងមូលដ្ឋាន អ្នកត្រូវបន្ថែមតារាងទៅមូលដ្ឋានទិន្នន័យពីចម្ងាយ (ផលិតកម្ម) របស់អ្នក។ ដើម្បីបន្ថែមតារាងទៅក្នុងមូលដ្ឋានទិន្នន័យផលិតកម្មរបស់អ្នក សូមដំណើរការពាក្យបញ្ជាខាងក្រោម៖
text
npx wrangler d1 execute d1-http-example --file=./schemas/schema.sql --remote
Bây giờ bạn có thể xem bảng trên Cloudflare dashboard > Storage & Databases \> D1. ↗
You should now be able to view the table on the Cloudflare dashboard > Storage & Databases \> D1. ↗
ឥឡូវនេះ អ្នកគួរតែអាចមើលតារាងនៅលើ _TER មូលដ្ឋានទិន្នន័យ \> Storage & Databases។ ↗
  1. Để triển khai ứng dụng vào mạng Cloudflare, hãy chạy lệnh sau:
  1. To deploy the application to the Cloudflare network, run the following command:
  1. ដើម្បីដាក់ពង្រាយកម្មវិធីទៅបណ្តាញ Cloudflare សូមដំណើរការពាក្យបញ្ជាខាងក្រោម៖
text
npx wrangler deploy
text
⛅️ wrangler 3.78.4 (update available 3.78.5)  
-------------------------------------------------------  
Total Upload: 53.00 KiB / gzip: 13.16 KiB  
Your worker has access to the following bindings:  
- D1 Databases:  
  - DB: d1-http-example (DATABASE_ID)  
Uploaded d1-http (4.29 sec)  
Deployed d1-http triggers (5.57 sec)  
  [DEPLOYED_APP_LINK]  
Current Version ID: [BINDING_ID]
Sau khi triển khai thành công, bạn sẽ nhận được liên kết của ứng dụng được triển khai trong thiết bị đầu cuối (DEPLOYED<em>APP</em>LINK). Làm một ghi chú về nó.
Upon successful deployment, you will get the link of the deployed app in the terminal (DEPLOYED<em>APP</em>LINK). Make a note of it.
នៅពេលដាក់ឱ្យប្រើប្រាស់ដោយជោគជ័យ អ្នកនឹងទទួលបានតំណភ្ជាប់នៃកម្មវិធីដែលបានដាក់ឱ្យប្រើប្រាស់នៅក្នុងស្ថានីយ (DEPLOYED<em>APP</em>LINK)។ កំណត់ចំណាំរបស់វា។
  1. Tạo một khóa API mới để sử dụng trong sản xuất.
  1. Generate a new API key to use in production.
  1. បង្កើតសោ API ថ្មី ដើម្បីប្រើក្នុងផលិតកម្ម។
Cửa sổ Terminal
Terminal window
បង្អួចស្ថានីយ
text
openssl rand -base64 32
text
[YOUR_API_KEY]
  1. Chạy lệnh wrangler secret put để thêm API vào dự án được triển khai.
  1. Execute the wrangler secret put command to add an API to the deployed project.
  1. ប្រតិបត្តិពាក្យបញ្ជា wrangler secret put ដើម្បីបន្ថែម API ទៅគម្រោងដែលបានដាក់ពង្រាយ។
text
npx wrangler secret put API_KEY
text
✔ Enter a secret value:
Terminal sẽ yêu cầu bạn nhập một giá trị bí mật.
The terminal will prompt you to enter a secret value.
ស្ថានីយនឹងប្រាប់អ្នកឱ្យបញ្ចូលតម្លៃសម្ងាត់។
  1. Nhập giá trị của phím API của bạn (YOUR<em>API</em>KEY). Khóa API của bạn bây giờ sẽ được thêm vào dự án của bạn. Sử dụng giá trị này, bạn có thể thực hiện các cuộc gọi API an toàn cho API được triển khai của bạn.
  1. Enter the value of your API key (YOUR<em>API</em>KEY). Your API key will now be added to your project. Using this value you can make secure API calls to your deployed API.
  1. បញ្ចូលតម្លៃនៃសោ API របស់អ្នក (YOUR<em>API</em>KEY)។ ឥឡូវនេះ សោ API របស់អ្នកនឹងត្រូវបានបញ្ចូលទៅក្នុងគម្រោងរបស់អ្នក។ ដោយប្រើតម្លៃនេះ អ្នកអាចធ្វើការហៅទូរស័ព្ទ API ទៅកាន់ API ដែលអ្នកបានដាក់ពង្រាយដោយសុវត្ថិភាព។
Cửa sổ Terminal
Terminal window
បង្អួចស្ថានីយ
text
✔ Enter a secret value: [YOUR_API_KEY]
text
🌀 Creating the secret for the Worker "d1-http"  
✨ Success! Uploaded secret API_KEY
  1. Để kiểm tra nó, hãy chạy lệnh cURL sau với YOUR<em>API</em>KEY và DEPLOYED<em>APP</em>LINK chính xác.
  1. To test it, run the following cURL command with the correct YOUR<em>API</em>KEY and DEPLOYED<em>APP</em>LINK.
  1. ដើម្បីសាកល្បងវា ដំណើរការពាក្យបញ្ជា cURL ខាងក្រោមជាមួយនឹង YOUR<em>API</em>KEY និង DEPLOYED<em>APP</em>LINK។
Use the YOUR<em>API</em>KEY you have generated as the secret API key. You can also find your DEPLOYED<em>APP</em>LINK from the Cloudflare dashboard > Workers & Pages \> d1-http \> Settings \> Domains & Routes.
Use the YOUR<em>API</em>KEY you have generated as the secret API key. You can also find your DEPLOYED<em>APP</em>LINK from the Cloudflare dashboard > Workers & Pages \> d1-http \> Settings \> Domains & Routes.
ប្រើ YOUR<em>API</em>KEY ដែលអ្នកបានបង្កើតជាសោសម្ងាត់ API។ អ្នកក៏អាចស្វែងរក DEPLOYED<em>APP</em>LINK របស់អ្នកពីផ្ទាំងគ្រប់គ្រង Cloudflare > Workers & Pages \> d1-http \> Settings \> Domains & Routes។
text
curl -H "Authorization: Bearer YOUR_API_KEY" "https://DEPLOYED_APP_LINK/api/exec" --data '{"query": "SELECT 1"}'

Summary Summary សង្ខេប

Phần «Tóm tắt» — đọc hướng dẫn bên dưới, dùng liên kết docs gốc để xem ảnh minh họa và tab cấu hình đầy đủ.

Read the "Summary" section below — open the official docs link for full screenshots and configuration tabs.

អានផ្នែក «សង្ខេប» ខាងក្រោម — បើកតំណ docs ផ្លូវការសម្រាប់រូបភាព និង tab កំណត់។

Mở section docs gốc ↗ Open source section ↗ បើកផ្នែក docs ផ្លូវការ ↗
Trong hướng dẫn này, bạn có:
In this tutorial, you have:
នៅក្នុងមេរៀននេះ អ្នកមាន៖
  1. Tạo API tương tác với cơ sở dữ liệu D1 của bạn.
  2. Phát triển API này thành Workers. Bạn có thể sử dụng API này trong ứng dụng bên ngoài của bạn để thực hiện truy vấn chống lại cơ sở dữ liệu D1 của bạn. Mã đầy đủ cho hướng dẫn này có thể được tìm thấy trên GitHub ↗.
  1. Created an API that interacts with your D1 database.
  2. Deployed this API to the Workers. You can use this API in your external application to execute queries against your D1 database. The full code for this tutorial can be found on GitHub ↗.
  1. បានបង្កើត API ដែលធ្វើអន្តរកម្មជាមួយមូលដ្ឋានទិន្នន័យ D1 របស់អ្នក។
  2. បានដាក់ពង្រាយ API នេះទៅ Workers ។ អ្នកអាចប្រើ API នេះនៅក្នុងកម្មវិធីខាងក្រៅរបស់អ្នក ដើម្បីដំណើរការសំណួរប្រឆាំងនឹងមូលដ្ឋានទិន្នន័យ D1 របស់អ្នក។ កូដពេញលេញសម្រាប់ការបង្រៀននេះអាចត្រូវបានរកឃើញនៅលើ GitHub>
Bạn có thể kiểm tra một triển khai tương tự sử dụng Zod để xác thực trong this GitHub repository ↗. Nếu bạn muốn xây dựng một OpenAPI tuân thủ API cho cơ sở dữ liệu của bạn D1, bạn nên sử dụng Cloudflare Workers OpenAPI 3.1 template ↗.
You can check out a similar implementation that uses Zod for validation in this GitHub repository ↗. If you want to build an OpenAPI compliant API for your D1 database, you should use the Cloudflare Workers OpenAPI 3.1 template ↗.
អ្នកអាចពិនិត្យមើលការអនុវត្តស្រដៀងគ្នាដែលប្រើ Zod សម្រាប់សុពលភាពនៅក្នុង ↗។ ប្រសិនបើអ្នកចង់បង្កើត OpenAPI អនុលោមតាម API សម្រាប់មូលដ្ឋានទិន្នន័យ D1 របស់អ្នក អ្នកគួរតែប្រើ __TER_9 OpenAPI គំរូ 3.1 ↗។
json
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"item":{"@id":"/directory/","name":"Directory"}},{"@type":"ListItem","position":2,"item":{"@id":"/d1/","name":"D1"}},{"@type":"ListItem","position":3,"item":{"@id":"/d1/tutorials/","name":"Tutorials"}},{"@type":"ListItem","position":4,"item":{"@id":"/d1/tutorials/build-an-api-to-access-d1/","name":"Build an API to access D1 using a proxy Worker"}}]}

Xem bản đầy đủ trên developers.cloudflare.com (ảnh, tab cấu hình). View the full guide on developers.cloudflare.com (images, config tabs). មើលមគ្គុទ្ទេសក៍ពេញលើ developers.cloudflare.com (រូបភាព, tab កំណត់)។

Tài liệu gốc ↗ Official docs ↗ ឯកសារផ្លូវការ ↗