使用自定义模式
默认情况下,你的数据库有一个 public 架构,会自动在数据 API 上暴露。
🌐 By default, your database has a public schema which is automatically exposed on data APIs.
创建自定义模式 #
🌐 Creating custom schemas
你可以通过运行以下 SQL 创建你自己的自定义模式,只需将 myschema 替换为你想要使用的模式名称:
🌐 You can create your own custom schema/s by running the following SQL, substituting myschema with the name you want to use for your schema:
1CREATE SCHEMA myschema;公开自定义模式 #
🌐 Exposing custom schemas
你可以公开自定义数据库模式——要做到这一点,你需要按照以下步骤:
🌐 You can expose custom database schemas - to do so you need to follow these steps:
- 前往 API 设置 并将你的自定义 schema 添加到“公开的 schemas”。
- 运行以下 SQL,将
myschema替换为你的模式名:
1GRANT USAGE ON SCHEMA myschema TO anon, authenticated, service_role;2GRANT ALL ON ALL TABLES IN SCHEMA myschema TO anon, authenticated, service_role;3GRANT ALL ON ALL ROUTINES IN SCHEMA myschema TO anon, authenticated, service_role;4GRANT ALL ON ALL SEQUENCES IN SCHEMA myschema TO anon, authenticated, service_role;5ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA myschema GRANT ALL ON TABLES TO anon, authenticated, service_role;6ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA myschema GRANT ALL ON ROUTINES TO anon, authenticated, service_role;7ALTER DEFAULT PRIVILEGES FOR ROLE postgres IN SCHEMA myschema GRANT ALL ON SEQUENCES TO anon, authenticated, service_role;现在你可以通过数据 API 访问这些模式了:
🌐 Now you can access these schemas from data APIs:
1// Initialize the JS client2import { createClient } from '@supabase/supabase-js'3const supabase = createClient(SUPABASE_URL, SUPABASE_PUBLISHABLE_KEY, {4 db: { schema: 'myschema' },5})67// Make a request8const { data: todos, error } = await supabase.from('todos').select('*')910// You can also change the target schema on a per-query basis11const { data: todos, error } = await supabase.schema('myschema').from('todos').select('*')