CRUD Operations & Data Queries
Executing database queries, applying filters, handling pagination, and mutating records.
Unified Data Query Syntax
Query data from client components or serverless edge functions using a fluent, type-safe API:
| Operation | SDK Method | Description |
|---|---|---|
| Select / Filter | db.from('orders').where('status', '==', 'paid') | Filters records matching predicate with automatic indexing |
| Insert / Create | db.from('orders').insert({{ amount, customer_id }}) | Inserts new record and returns validated object with UUID |
| Update / Patch | db.from('orders').where('id', id).update({{ status }}) | Surgically updates targeted fields without full record overwrite |
| Delete | db.from('orders').where('id', id).delete() | Safely deletes row subject to foreign key cascading rules |
Complete TypeScript Query Example
Here is a complete, real-world example querying customer analytics with pagination and sorting:
import { db } from '@/lib/rayvotech';
export async function getRecentTransactions(userId: string, page = 1) {
const pageSize = 15;
const offset = (page - 1) * pageSize;
const { data, count, error } = await db
.from('transactions')
.select('id, amount, currency, status, created_at', { count: 'exact' })
.where('user_id', '==', userId)
.orderBy('created_at', 'desc')
.range(offset, offset + pageSize - 1);
if (error) throw new Error(`Query failed: ${error.message}`);
return { transactions: data, total: count };
}
export async function getRecentTransactions(userId: string, page = 1) {
const pageSize = 15;
const offset = (page - 1) * pageSize;
const { data, count, error } = await db
.from('transactions')
.select('id, amount, currency, status, created_at', { count: 'exact' })
.where('user_id', '==', userId)
.orderBy('created_at', 'desc')
.range(offset, offset + pageSize - 1);
if (error) throw new Error(`Query failed: ${error.message}`);
return { transactions: data, total: count };
}