본문으로 건너뛰기
아우라베이스 로고
아우라베이스

Quickstart

You will create a PostgreSQL table, see that the public key cannot read it, add a policy, and read and write your own rows from Node.js. Every output on this page was captured on a real project.

튜토리얼10분 읽기레벨 초보자

이 페이지는 아직 귀하의 언어로 제공되지 않습니다. 영어로 표시됩니다.

What you will build#

A notes table where each signed-in user reads and writes only their own notes. At the end you will have:

  • a project linked to your working folder;
  • a table with row level security, which Aurabase turns on for every table you create;
  • an owner policy written in SQL;
  • a Node.js script that signs a user in, inserts a note and reads it back with @aurabase/aurabase-js.

Prerequisites#

  • The aura CLI, signed in to your account: aura login.
  • Node.js 20 or later, and a folder with the SDK installed: npm install @aurabase/aurabase-js. The scripts below use top-level await: run them as ES modules ("type": "module" in package.json) with npx tsx <file>.ts.

This tutorial does not use the example schema: it creates its own table and removes it at the end.

1. Create and link a project#

Create a project, then link the current folder to it. The second command writes aurabase.toml, which the aura db commands read to know which project to work on.

aura projects create my-app
aura link <project-id>

Output of aura projects create (the id, the slug and the keys of your project differ):

» Creating project 'my-app'...
  ✔ Project created: my-app (slug: my-app-utsrqfcx)

✔ Project provisioned and ready to use!
  Project ID       : 41fd6f07-3acf-48cf-9eb5-d665ce99a849
  Slug             : my-app-utsrqfcx
  Project URL      : http://my-app-utsrqfcx.localhost:8080

  ⚠ Project API keys (shown only once):
    • anon (front-end)  : aura_anon_…
    • service_role (backend)    : aura_sk_…

Copy the two keys now: they are shown only once. If you lose them, create new ones in the Studio under Settings › API keys or with aura db api-keys create <name> --scope anon. Then export the project URL and the keys in your shell; the commands and scripts below read them from the environment.

export AURABASE_URL=http://my-app-utsrqfcx.localhost:8080
export AURABASE_ANON_KEY=aura_anon_…
export AURABASE_SERVICE_ROLE_KEY=aura_sk_…
  • The anon key is public: it can go in a browser or a mobile app. Row level security decides what it can read.
  • The service_role key is secret: it bypasses row level security. Keep it on a server or in your terminal. The aura db commands that read or change data use it.

2. Create a table#

aura db raw runs SQL on the linked project. user_id takes the id of the signed-in user by default: auth.uid() reads it from the request.

aura db raw "create table notes (
  id bigint generated always as identity primary key,
  user_id uuid not null default auth.uid(),
  body text not null,
  created_at timestamptz not null default now()
)"
  ℹ No results (0 rows).

Aurabase enabled row level security on notes when the table was created. With RLS on and no policy, PostgreSQL returns no row to the public roles instead of an error.

3. Read it with the public key#

Save this script as read-public.ts and run it with npx tsx read-public.ts. Each SDK call returns an object with data (the rows), error (null on success), meta and count.

import { createClient } from '@aurabase/aurabase-js'

const aura = createClient(process.env.AURABASE_URL!, process.env.AURABASE_ANON_KEY!)

const { data, error, meta } = await aura.from('notes').select('body')
console.log(data, error)
console.log(meta?.hint)
[] null
0 rows returned. The table "notes" may be empty, or it has row-level security and no SELECT policy lets the role "aura_anon" read its rows (PostgreSQL returns nothing instead of an error). To check what this role can see: aura db raw "select * from notes" --as anon. To open rows to it, add a policy, for example: create policy "notes_read" on notes for select to aura_anon using (true); — guide: /docs/database/rls

The request succeeded, but no row came back. The public key reaches PostgreSQL as the role aura_anon, and no policy lets that role read notes.

4. Add a policy#

Let signed-in users (aura_authenticated) read, insert, update and delete the rows whose user_id is their own id:

aura db raw "create policy notes_owner on notes
  for all to aura_authenticated
  using (auth.uid() = user_id)
  with check (auth.uid() = user_id)"
  ℹ No results (0 rows).

using filters the rows a user can read, update or delete; with check rejects an insert or update that would write a row for another user.

5. Write and read as a user#

A new project requires users to confirm their email address before they can sign in. For this tutorial, turn the confirmation off so that a test user can sign in at once:

aura auth settings set email_confirmation_required=false
  ✔ 1 setting(s) updated.

Save this script as notes.ts and run it. It signs a new user up, signs it in, inserts a note without user_id (the default fills it), reads the notes back, then reads them again with a client that has no user.

import { createClient } from '@aurabase/aurabase-js'

const aura = createClient(process.env.AURABASE_URL!, process.env.AURABASE_ANON_KEY!)

const email = `quickstart-${Date.now()}@example.com`
const password = 'correct-horse-battery-9'
const signUp = await aura.auth.signUp({ email, password })
if (signUp.error) throw signUp.error
const signIn = await aura.auth.signInWithPassword({ email, password })
if (signIn.error) throw signIn.error

const inserted = await aura.from('notes').insert({ body: 'My first note' }).select('body')
console.log('insert:', inserted.data, inserted.error)

const mine = await aura.from('notes').select('body')
console.log('as the user:', mine.data, mine.error)

const visitor = createClient(process.env.AURABASE_URL!, process.env.AURABASE_ANON_KEY!)
const others = await visitor.from('notes').select('body')
console.log('without a user:', others.data, others.error)
insert: [ { body: 'My first note' } ] null
as the user: [ { body: 'My first note' } ] null
without a user: [] null

The same anon key now returns rows, because the request carries the access token of a signed-in user: the gateway hands it to PostgreSQL as the role aura_authenticated with the user id in the claims that auth.uid() reads.

Result#

You have a table that a browser can use with the public key, and that shows each user only their own rows. Check it from the CLI as the service role, which bypasses the policy and sees every row:

aura db raw "select body from notes"
  body
  ─────────────
  My first note

  ℹ 1 row(s).

Clean up#

Drop the table; its policy goes with it.

aura db raw "drop table notes"
  ℹ No results (0 rows).

Before real users sign up, turn email confirmation back on (it is the default of every project):

aura auth settings set email_confirmation_required=true

The test users stay in the project. Delete them in the Studio under Auth, or with aura auth users list then aura auth users delete <user-id>.

Next steps#

@aurabase/aurabase-js@0.7.5 · aura@0.3.4↗ 문제 신고