On this page

Quickstart

Create a table, protect it with a policy and read it from a JavaScript app.

#1. Create a project

Sign up, then click New project in the dashboard, give it a name and pick a region. Your project is ready in a few seconds. Its overview shows the Project URL and the anon key.

#2. Create a table

Open the project, choose SQL Editor and run:

SQL
create table public.todos (
  id bigint generated always as identity primary key,
  user_id uuid not null default auth.uid() references auth.users on delete cascade,
  task text not null,
  is_complete boolean not null default false,
  created_at timestamptz not null default now()
);

alter table public.todos enable row level security;

create policy "Users manage their own todos" on public.todos
  for all to authenticated
  using (user_id = auth.uid())
  with check (user_id = auth.uid());

#3. Install the SDK

Terminal
npm install stitchdb-js

#4. Query your data

JavaScript
import { createClient } from 'stitchdb-js'

const stitchdb = createClient('https://<project-ref>.p.stitchdb.com', '<anon-key>')

await stitchdb.auth.signUp({ email: '[email protected]', password: 'a-strong-password' })
// New projects confirm e-mail addresses: click the link in the e-mail, then sign in
await stitchdb.auth.signInWithPassword({ email: '[email protected]', password: 'a-strong-password' })

await stitchdb.from('todos').insert({ task: 'Ship my first StitchDB app' })

const { data, error } = await stitchdb.from('todos').select('*')
console.log(data)

Because of the policy, each signed-in user only sees their own todos, and anonymous requests get an empty list.

#5. Open Studio

Open the project in the dashboard and choose Table Editor to see the rows. Studio also shows an alert for every table in public that has RLS turned off.