On this page

Insert, update and delete

Insert, update, upsert and delete rows with JSON bodies.

OperationRequest
InsertPOST /rest/v1/todos with a JSON body
UpdatePATCH /rest/v1/todos?id=eq.1
DeleteDELETE /rest/v1/todos?id=eq.1

#From JavaScript

JavaScript
await stitchdb.from('todos').insert({ task: 'New task' })
await stitchdb.from('todos').insert([{ task: 'One' }, { task: 'Two' }]) // many rows at once
await stitchdb.from('todos').update({ is_complete: true }).eq('id', 1)
await stitchdb.from('todos').delete().eq('id', 1)

// Insert or update by primary key (or another unique column)
await stitchdb.from('profiles').upsert({ id: uid, username: 'ada' }, { onConflict: 'id' })

// Get the changed rows back
const { data } = await stitchdb.from('todos').insert({ task: 'New task' }).select()

#With cURL

Terminal
curl -X PATCH 'https://<project-ref>.p.stitchdb.com/rest/v1/todos?id=eq.1' \
  -H "apikey: <anon-key>" \
  -H "Authorization: Bearer <user-access-token>" \
  -H "Content-Type: application/json" \
  -H "Prefer: return=representation" \
  -d '{"is_complete":true}'

# Upsert
curl -X POST 'https://<project-ref>.p.stitchdb.com/rest/v1/profiles?on_conflict=id' \
  -H "apikey: <anon-key>" -H "Authorization: Bearer <user-access-token>" \
  -H "Content-Type: application/json" \
  -H "Prefer: resolution=merge-duplicates" \
  -d '{"id":"<user-id>","username":"ada"}'

#Get changed rows back

Add the header Prefer: return=representation (or .select() in the SDK) to receive the changed rows in the response. Row Level Security applies to the returned rows too.