@openbaseapp/js
v0.3.0
Published
OpenBase JavaScript/TypeScript client SDK — mirrors the Supabase JS API
Downloads
7
Maintainers
Readme
openbase-js
The official JavaScript/TypeScript client SDK for OpenBase — a Supabase-compatible backend powered by Telegram.
Installation
npm install openbase-jsQuick Start
import { createClient } from 'openbase-js'
const openbase = createClient(
'http://localhost:3001', // Your OpenBase API URL
'your-anon-key' // Your project's anon key
)Database
Query Rows
// Select all rows
const { data, error } = await openbase
.from('posts')
.select('*')
// Select with filters
const { data } = await openbase
.from('posts')
.select('id, title, created_at')
.eq('status', 'published')
.order('created_at', { ascending: false })
.limit(10)Insert Rows
const { data, error } = await openbase
.from('posts')
.insert({ title: 'Hello World', body: 'My first post' })Update Rows
const { data, error } = await openbase
.from('posts')
.update({ title: 'Updated Title' })
.eq('id', 'some-uuid')Delete Rows
const { data, error } = await openbase
.from('posts')
.delete()
.eq('id', 'some-uuid')Row Count
const { count } = await openbase
.from('posts')
.select('*', { count: 'exact', head: true })Authentication
Sign Up
const { data, error } = await openbase.auth.signUp({
email: '[email protected]',
password: 'securepassword',
})
// data.user — the created user
// data.session — { access_token, refresh_token, expires_at }Sign In
const { data, error } = await openbase.auth.signIn({
email: '[email protected]',
password: 'securepassword',
})Sign Out
await openbase.auth.signOut()Get Current User
const { data: user } = await openbase.auth.getUser()Refresh Session
The SDK automatically refreshes the access token using the refresh token when a request returns 401. You can also manually refresh:
const { data: session } = await openbase.auth.refreshSession()Storage
Upload File
const file = new File(['hello'], 'hello.txt', { type: 'text/plain' })
const { data, error } = await openbase.storage
.from('avatars')
.upload('public/hello.txt', file)Download File
const { data, error } = await openbase.storage
.from('avatars')
.download('public/hello.txt')Download with Image Transforms
const { data } = await openbase.storage
.from('avatars')
.download('profile.png', {
transform: { width: 200, height: 200, format: 'webp' },
})Create Signed URL
const { data, error } = await openbase.storage
.from('avatars')
.createSignedUrl('private/doc.pdf', 3600) // expires in 1 hourList Files
const { data, error } = await openbase.storage
.from('avatars')
.list('public/')Delete File
const { data, error } = await openbase.storage
.from('avatars')
.remove(['public/hello.txt'])Realtime
Subscribe to database changes in real-time via WebSockets:
const subscription = openbase
.channel('posts-changes')
.on('postgres_changes', {
event: 'INSERT',
schema: 'public',
table: 'posts',
}, (payload) => {
console.log('New post:', payload.new)
})
.subscribe()Listen to All Events
const subscription = openbase
.channel('all-changes')
.on('postgres_changes', {
event: '*',
schema: 'public',
table: 'posts',
}, (payload) => {
console.log(`${payload.eventType}:`, payload)
})
.subscribe()Unsubscribe
subscription.unsubscribe()Environment Variables
When using the SDK in a frontend framework (Next.js, Vite, etc.):
NEXT_PUBLIC_OPENBASE_URL=http://localhost:3001
NEXT_PUBLIC_OPENBASE_ANON_KEY=your-anon-keyconst openbase = createClient(
process.env.NEXT_PUBLIC_OPENBASE_URL!,
process.env.NEXT_PUBLIC_OPENBASE_ANON_KEY!
)License
MIT
