跳到内容
光环标志
光环基地

您的第一个边缘函数

您将编写一个函数,将其部署到您的项目中,从 Node.js 调用它并读取其结果。下面的每个命令都是真实运行的。

教程10分钟 读取级别 初学者

自此翻译以来,英文来源已发生变化。某些信息可能已过时。
该页面已自动翻译。英文版具有权威性。

Before you start#

You need:

  • Node.js 20 or later.
  • The aura CLI. Install it with npm install -g @aurabase/cli (see the CLI reference for other platforms).
  • An Aurabase account.

1. Create and link a project#

Sign in, create a project, and link your working folder to it. Replace <org-slug> with the slug shown by aura orgs list.

aura login --email [email protected] --password '<your-password>'
aura orgs list
aura projects create my-app --org <org-slug>

The last command prints the project URL and your first API keys. Copy them now: the initial keys are shown only once. If you miss them, create new ones in the Studio under Settings, API keys.

  ✔ Project created: my-app (e99f2c4f-43e2-4d08-b023-b4f9e687d17c)
    import { createClient } from '@aurabase/aurabase-js'
    const client = createClient('http://my-app-ajap5iw2.localhost:8080', 'aura_anon_…')

Link the current folder to the project:

aura link --project-id e99f2c4f-43e2-4d08-b023-b4f9e687d17c
  ✔ Project e99f2c4f-43e2-4d08-b023-b4f9e687d17c linked in aurabase.toml.
  • Project URL: http://my-app-ajap5iw2.localhost:8080

Then tell the CLI where the data plane is and which key to use. A service_role key is required to deploy, because deploying code is an administrative action.

NEXT_PUBLIC_AURABASE_URL=http://my-app-ajap5iw2.localhost:8080
AURABASE_SERVICE_ROLE_KEY=<your service_role key>
Keep the service_role key on your machine

A service_role key bypasses row-level security and can deploy code. Never put it in a browser bundle or commit .env.

2. Write the function#

aura functions new hello
✔ New function created in aurabase/functions/hello (TypeScript Deno template)

Open aurabase/functions/hello/index.ts and replace its content:

Deno.serve(async (req: Request) => {
  const { name = 'world' } = await req.json().catch(() => ({}))

  return new Response(JSON.stringify({ greeting: `Hello, ${name}!` }), {
    headers: { 'content-type': 'application/json' },
  })
})

A function must call Deno.serve. An export default is never called.

3. Deploy it#

aura functions deploy hello
  ✔ Detected TypeScript script (runtime: deno)...
  • Uploading to service (runtime: deno)...
✔ Function 'hello' deployed successfully!

Call it once from the CLI to check that it answers:

aura functions invoke hello --data '{"name":"Ada"}'
  ✔ Response (200):
{
  "data": {
    "body": { "greeting": "Hello, Ada!" },
    "duration_ms": 74,
    …

4. Call it from the SDK#

mkdir hello-client && cd hello-client
npm init -y
npm install @aurabase/aurabase-js
import { createClient } from '@aurabase/aurabase-js'

const aura = createClient(process.env.NEXT_PUBLIC_AURABASE_URL, process.env.NEXT_PUBLIC_AURABASE_ANON_KEY)

const { data, error } = await aura.functions.invoke('hello', { body: { name: 'Ada' } })
if (error) throw error

console.log(data.greeting)
NEXT_PUBLIC_AURABASE_URL=http://my-app-ajap5iw2.localhost:8080 NEXT_PUBLIC_AURABASE_ANON_KEY=aura_anon_… node hello.mjs
Hello, Ada!

You called a function with the public anon key. That is the only call a public key can make: see who can call what.

Where to go next#

@aurabase/aurabase-js@0.7.5 · aura@0.3.4↗ 报告问题