Zum Inhalt springen
Aurabase-Logo
Aurabasis

Ihre erste Edge-Funktion

Sie schreiben eine Funktion, stellen sie in Ihrem Projekt bereit, rufen sie über Node.js auf und lesen das Ergebnis. Jeder folgende Befehl wurde tatsächlich ausgeführt.

Tutorial10 Min. gelesenEbene Anfänger

Die englische Quelle hat sich seit dieser Übersetzung geändert. Einige Informationen sind möglicherweise veraltet.
Diese Seite wurde automatisch übersetzt. Maßgeblich ist die englische Version.

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↗ Ein Problem melden