mcpField
S
supabase-js

supabase-js

github.com/fcakyon开发1.2k49 次安装今天更新
代码仓库

关于 supabase-js

面向使用 `@supabase/supabase-js` 构建应用的开发者技能指南,涵盖认证、数据库、存储、实时功能和 Edge Functions,并提供类型安全查询等常见模式。

主要功能

  1. 01认证与用户会话管理
  2. 02PostgREST 数据查询与变更
  3. 03文件存储操作
  4. 04实时频道与状态同步
  5. 05调用 Edge Functions

适用场景

  1. 01为应用接入用户注册与登录
  2. 02查询并更新 Supabase 数据
  3. 03上传文件并监听实时变更

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/fcakyon/claude-codex-settings --skill supabase-js

安装后在 Claude Code 中输入/supabase-js确认已加载。

包含的文件

除 SKILL.md 外,安装时一起下载的参考资料和脚本。

references/
overview.md
references/auth-js/
README.md
references/functions-js/
README.md
references/postgrest-js/
README.md
references/realtime-js/
README.md
references/storage-js/
README.md
references/supabase-js/
README.md

详细介绍

Supabase JavaScript SDK 技能

用于使用 @supabase/supabase-js SDK 构建应用的技能。涵盖 Auth、Database(PostgREST)、Storage、Realtime 和 Edge Functions。

https://supabase.com/docs/reference/javascript 上的 SDK 文档是权威信息来源。此技能附带的参考文件包含从 monorepo 中提取的源代码和 README,便于快速查阅。

设置

npm install @supabase/supabase-js
import { createClient } from '@supabase/supabase-js'

const supabase = createClient('https://xyzcompany.supabase.co', 'public-anon-key')

如需类型安全的查询,请根据数据库架构生成类型:

supabase gen types typescript --project-id your-project-id > database.types.ts
import { createClient } from '@supabase/supabase-js'
import type { Database } from './database.types'

const supabase = createClient<Database>(SUPABASE_URL, SUPABASE_ANON_KEY)

快速决策树

“我需要查询数据”
Database query?
├─ Select rows → supabase.from('table').select('*')
├─ Filter rows → .select().eq('col', val) / .gt() / .lt() / .in() / .like()
├─ Join tables → .select('*, other_table(*)') or .select('*, other_table!fk(*)')
├─ Insert → supabase.from('table').insert({ col: val })
├─ Upsert → supabase.from('table').upsert({ id: 1, col: val })
├─ Update → supabase.from('table').update({ col: val }).eq('id', 1)
├─ Delete → supabase.from('table').delete().eq('id', 1)
├─ Call RPC function → supabase.rpc('function_name', { arg: val })
├─ Count rows → .select('*', { count: 'exact', head: true })
├─ Pagination → .range(0, 9) or .limit(10).offset(20)
└─ Order → .order('created_at', { ascending: false })
“我需要身份验证”
Auth?
├─ Email/password sign up → supabase.auth.signUp({ email, password })
├─ Email/password sign in → supabase.auth.signInWithPassword({ email, password })
├─ OAuth (Google, GitHub, etc.) → supabase.auth.signInWithOAuth({ provider: 'google' })
├─ Magic link → supabase.auth.signInWithOtp({ email })
├─ Phone OTP → supabase.auth.signInWithOtp({ phone })
├─ Sign out → supabase.auth.signOut()
├─ Get current user → supabase.auth.getUser()
├─ Get session → supabase.auth.getSession()
├─ Listen to auth changes → supabase.auth.onAuthStateChange((event, session) => {})
├─ Reset password → supabase.auth.resetPasswordForEmail(email)
├─ Update user → supabase.auth.updateUser({ data: { name: 'New' } })
└─ Admin operations → supabase.auth.admin.listUsers() / .deleteUser(id)
“我需要文件存储”
Storage?
├─ Upload file → supabase.storage.from('bucket').upload('path/file.png', file)
├─ Download file → supabase.storage.from('bucket').download('path/file.png')
├─ Get public URL → supabase.storage.from('bucket').getPublicUrl('path/file.png')
├─ Create signed URL → supabase.storage.from('bucket').createSignedUrl('path', 3600)
├─ List files → supabase.storage.from('bucket').list('folder')
├─ Move file → supabase.storage.from('bucket').move('old/path', 'new/path')
├─ Remove file → supabase.storage.from('bucket').remove(['path/file.png'])
├─ Create bucket → supabase.storage.createBucket('name', { public: false })
└─ List buckets → supabase.storage.listBuckets()
“我需要实时功能”
Realtime?
├─ Listen to DB changes → supabase.channel('name')
│    .on('postgres_changes', { event: 'INSERT', schema: 'public', table: 'messages' }, handler)
│    .subscribe()
├─ Broadcast messages → channel.send({ type: 'broadcast', event: 'cursor', payload: { x, y } })
├─ Listen to broadcasts → .on('broadcast', { event: 'cursor' }, handler)
├─ Presence (who's online) → channel.track({ user_id, online_at })
│    .on('presence', { event: 'sync' }, () => channel.presenceState())
├─ Unsubscribe → supabase.removeChannel(channel)
└─ Unsubscribe all → supabase.removeAllChannels()
“我需要边缘函数”
Edge Functions?
├─ Invoke function → supabase.functions.invoke('function-name', { body: { key: 'val' } })
├─ With custom headers → .invoke('fn', { headers: { 'x-custom': 'val' }, body })
└─ Set region → .invoke('fn', { body, region: 'us-east-1' })

常见模式

使用 service role key 的服务端模式
// Server-side only - bypasses RLS
const supabase = createClient(SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY, {
  auth: { persistSession: false }
})
React/Next.js 身份验证
import { createClient } from '@supabase/supabase-js'
import { useEffect, useState } from 'react'

const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY)

function useUser() {
  const [user, setUser] = useState(null)
  useEffect(() => {
    supabase.auth.getUser().then(({ data }) => setUser(data.user))
    const { data: { subscription } } = supabase.auth.onAuthStateChange(
      (_, session) => setUser(session?.user ?? null)
    )
    return () => subscription.unsubscribe()
  }, [])
  return user
}
类型化数据库查询
// Generated types give autocomplete for table names, column names, and return types
const { data, error } = await supabase
  .from('profiles')        // autocompleted table name
  .select('id, username')  // autocompleted columns
  .eq('id', userId)        // type-safe filter
  .single()                // returns single row or error
// data is typed as { id: string; username: string } | null

软件包索引

软件包子客户端参考资料
@supabase/supabase-jscreateClient()references/supabase-js/
@supabase/auth-js.authreferences/auth-js/
@supabase/postgrest-js.from()、.rpc()references/postgrest-js/
@supabase/realtime-js.channel()、.realtimereferences/realtime-js/
@supabase/storage-js.storagereferences/storage-js/
@supabase/functions-js.functionsreferences/functions-js/

评论

支持 Markdown