From a1988a351efbab064062114b4b44ccc470650bbf Mon Sep 17 00:00:00 2001 From: shi yuhang <52435083+shiyuhang0@users.noreply.github.com> Date: Thu, 12 Oct 2023 15:21:27 +0800 Subject: [PATCH] Update netlify (#14964) --- .../integrate-tidbcloud-with-netlify.md | 40 ++++++++++++++++++- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/tidb-cloud/integrate-tidbcloud-with-netlify.md b/tidb-cloud/integrate-tidbcloud-with-netlify.md index 60e252ccef185..eefb18743c64e 100644 --- a/tidb-cloud/integrate-tidbcloud-with-netlify.md +++ b/tidb-cloud/integrate-tidbcloud-with-netlify.md @@ -7,7 +7,7 @@ summary: Learn how to connect your TiDB Cloud clusters to Netlify projects. [Netlify](https://netlify.com/) is an all-in-one platform for automating modern web projects. It replaces your hosting infrastructure, continuous integration, and deployment pipeline with a single workflow and integrates dynamic functionality like serverless functions, user authentication, and form handling as your projects grow. -This document describes how to deploy a fullstack app on Netlify with TiDB Cloud as the database backend. +This document describes how to deploy a fullstack app on Netlify with TiDB Cloud as the database backend. You can also learn how to use Netlify edge function with our TiDB Cloud serverless driver. ## Prerequisites @@ -222,4 +222,40 @@ For a TiDB Dedicated cluster, you can get the connection string only from the Ti netlify deploy --prod --trigger ``` - Go to your Netlify console to check the deployment state. After the deployment is done, the site for the app will have a public IP address provided by Netlify so that everyone can access it. \ No newline at end of file + Go to your Netlify console to check the deployment state. After the deployment is done, the site for the app will have a public IP address provided by Netlify so that everyone can access it. + +## Use the edge function + +The example app mentioned in the section above runs on the Netlify serverless function. This section shows you how to use the edge function with [TiDB Cloud serverless driver](/tidb-cloud/serverless-driver.md). The edge function is a feature provided by Netlify, which allows you to run serverless functions on the edge of the Netlify CDN. + +To use the edge function, take the following steps: + +1. Create a directory named `netlify/edge-functions` in the root directory of your project. + +2. Create a file named `hello.ts` in the directory and add the following code: + + ```typescript + import { connect } from 'https://esm.sh/@tidbcloud/serverless' + + export default async () => { + const conn = connect({url: Netlify.env.get('DATABASE_URL')}) + const result = await conn.execute('show databases') + return new Response(JSON.stringify(result)); + } + + export const config = { path: "/api/hello" }; + ``` + +3. Set the `DATABASE_URL` environment variables. You can get the connection information from the [TiDB Cloud console](https://tidbcloud.com/). + + ```shell + netlify env:set DATABASE_URL 'mysql://:@/' + ``` + +4. Deploy the edge function to Netlify. + + ```shell + netlify deploy --prod --trigger + ``` + +Then you can go to your Netlify console to check the state of the deployment. After the deployment is done, you can access the edge function through the `https:///api/hello` URL.