문서›가이드›Query your database (SDK) JavaScript SDK로 데이터베이스 쿼리
aura.db의 일상적인 흐름, 안전하지 않은 호출을 거부하는 규칙, 행 수준 보안으로 테이블을 보호하는 방법.
9분 읽기·레벨 중급·2026. 10. 12.에 개정됨
이 영어 텍스트는 프랑스어 원본에서 자동으로 생성되었으며 아직 검토되지 않았습니다.
이 페이지는 자동으로 번역되었습니다. 영어 버전은 권위가 있습니다.
read.tstypescript
const { data, error } = await aura
.from('todos')
.select('id, title, done')
.eq('done', false)
.order('created_at', { ascending: false })
.limit(20)
aura.from('todos')는 aura.db.from('todos')의 단축키입니다. 모든 호출은 { data, error }를 반환하고 결코 발생하지 않습니다. 클라이언트 옵션 및 오류를 참조하세요.
필터 체인: eq, neq, gt, gte, lt, lte, like, ilike, in, is및 기타 항목에 나열된 항목 데이터베이스 참조. 행 페이지의 경우 range(from, to)을 사용하세요.
write.tstypescript
// 하나의 행 또는 행 배열을 삽입하고 다시 가져옵니다.
const { data } = await aura.from('todos').insert({ title: 'Write the docs' }).select()
// Upsert
await aura.from('todos').upsert({ id, title: 'Renamed' }).select()
// 업데이트 및 삭제에는 필터가 필요합니다.
await aura.from('todos').update({ done: true }).eq('id', id)
await aura.from('todos').delete().eq('id', id)
실수로부터 여러분을 보호하는 두 가지 규칙은 다음과 같습니다.
- 일괄 삽입에는 모든 행에 동일한 키가 필요합니다. 키가 다른 행은
All object keys must match와 함께 400를 반환합니다. 누락된 키를 null로 채웁니다.
- 필터가 없는
update() 및 delete()는 요청이 전송되기 전에 클라이언트에서 오류 코드 unfiltered_mutation로 거부됩니다. 의도적으로 모든 행을 변경하려면 서버에서 SQL을 실행하십시오.
count.tstypescript
// 정확히 하나의 행: 행이 없거나 여러 개인 경우 오류
const one = await aura.from('todos').select('*').eq('id', id).single()
// → 필터가 0개 또는 여러 행과 일치하는 경우 error.status 406
// 0개 또는 1개 행: 일치하는 항목이 없으면 데이터가 null입니다.
const maybe = await aura.from('todos').select('*').eq('id', id).maybeSingle()
// 행 및 합계
const page = await aura.from('todos').select('*', { count: 'exact' }).range(0, 9)
// page.count는 범위에 관계없이 총계입니다.
// 합계만 있고 행은 없습니다.
const { count } = await aura.from('todos').select('*', { count: 'exact', head: true })
count: 'exact'는 일치하는 모든 행을 계산합니다. 'planned' 및 'estimated'는 매우 큰 테이블과 근사치에서 더 빠릅니다.
SQL에서 생성된 테이블은 RLS를 활성화할 때까지 열려 있습니다.
새 테이블에서는 행 수준 보안이 해제되어 있습니다. 공개키가 있으면 누구나 읽고 쓸 수 있습니다. 활성화한 후 필요한 정책을 추가하세요.
SQLsql
alter table todos enable row level security;
create policy todos_own_read on todos
for select to aura_authenticated
using (auth.uid() = owner);
데이터베이스 역할은 aura_anon, aura_authenticated 및 aura_service_role입니다. auth.uid()는 공개 키의 경우 NULL이고 로그인한 사용자의 사용자 ID입니다. 행 수준 보안을 참조하세요.
service_role 키는 설계상 RLS를 우회합니다. 서버에 보관하세요.
서버에서 함수를 만든 다음 rpc()를 사용하여 어디서나 호출하세요.
server/setup.tstypescript
await admin.db.createFunction({
name: 'add',
args: [
{ name: 'a', data_type: 'int', mode: 'IN' },
{ name: 'b', data_type: 'int', mode: 'IN' },
],
returns: 'int',
language: 'sql',
body: 'SELECT a + b',
grant_execute_to: ['anon', 'authenticated'],
})
client.tstypescript
const { data } = await aura.rpc('add', { a: 1, b: 2 }) // → 3
- 호출하려면
grant_execute_to이 필요합니다. 이것이 없으면 누구도 해당 기능을 실행할 수 없습니다. 나열되지 않은 역할은 403 permission denied for function를 얻습니다. authenticated 및 aura_authenticated가 모두 허용됩니다.
- 몇 초 후에 새 함수를 호출할 수 있습니다. API가 해당 스키마를 다시 로드할 때까지
rpc()는 404 Could not find the function를 반환합니다.
- 원시 SQL(
CREATE FUNCTION)을 사용한 함수 생성은 거부됩니다. createFunction을 사용하세요.
server/migrate.tstypescript
import { createAdminClient } from '@aurabase/aurabase-js'
const admin = createAdminClient(process.env.AURA_URL!, process.env.AURA_SERVICE_ROLE_KEY!)
await admin.db.sql(`
create table if not exists todos (
id uuid primary key default gen_random_uuid(),
title text not null,
done boolean not null default false,
owner uuid,
created_at timestamptz not null default now()
)
`)
db.sql()에는 service_role 키가 필요합니다. WHERE없이 DELETE을 거부하고 pg_class 또는 pg_roles와 같은 시스템 카탈로그를 읽지 않습니다. 테이블을 나열하려면 admin.db.schema()를 사용하세요.