入门

使用 Supabase 与 Refine

了解如何创建一个 Supabase 项目,向你的数据库添加一些示例数据,并从 Refine 应用查询数据。


1

创建一个 Supabase 项目

前往 database.new 并创建一个新的 Supabase 项目。

或者,您可以使用管理 API 创建项目

1
# First, get your access token from https://supabase.org.cn/dashboard/account/tokens
2
export SUPABASE_ACCESS_TOKEN="your-access-token"
3
4
# List your organizations to get the organization ID
5
curl -H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN" \
6
https://api.supabase.com/v1/organizations
7
8
# Create a new project (replace <org-id> with your organization ID)
9
curl -X POST https://api.supabase.com/v1/projects \
10
-H "Authorization: Bearer $SUPABASE_ACCESS_TOKEN" \
11
-H "Content-Type: application/json" \
12
-d '{
13
"organization_id": "<org-id>",
14
"name": "My Project",
15
"region": "us-east-1",
16
"db_pass": "<your-secure-password>"
17
}'

当您的项目启动并运行时,前往 表格编辑器,创建一个新表格并插入一些数据。

或者,您可以在项目的 SQL 编辑器 中运行以下代码片段。这将创建一个包含一些示例数据的 instruments 表。

1
-- Create the table
2
create table instruments (
3
id bigint primary key generated always as identity,
4
name text not null
5
);
6
-- Insert some sample data into the table
7
insert into instruments (name)
8
values
9
('violin'),
10
('viola'),
11
('cello');
12
13
alter table instruments enable row level security;

通过添加 RLS 策略使表格中的数据可公开读取

1
create policy "public can read instruments"
2
on public.instruments
3
for select to anon
4
using (true);
2

创建 Refine 应用

使用 Refinecreate refine-app 创建一个 Refine 应用。

refine-supabase 预设添加了 @refinedev/supabase 辅助包,该包支持在 Refine 应用中使用 Supabase。@refinedev/supabase 开箱即用包含 Supabase 依赖项:supabase-js

终端
1
npm create refine-app@latest -- --preset refine-supabase my-app
3

在 VS Code 中打开你的 Refine 应用

你将开发你的应用,连接到 Supabase 后端并在 VS Code 中运行 Refine 应用。

终端
1
cd my-app
2
code .
4

启动应用

启动应用,在浏览器中访问 https://:5173,你应该看到 Refine 欢迎页面。

终端
1
npm run dev

Refine welcome page

5

更新 `supabaseClient`

现在你需要使用你的 Supabase API 的 SUPABASE_URLSUPABASE_KEY 更新 supabaseClientsupabaseClient 用于允许 Refine 应用连接到你的 Supabase 后端的身份验证提供程序和数据提供程序方法。

项目 URL
可发布密钥
匿名密钥
src/utility/supabaseClient.ts
1
import { createClient } from "@refinedev/supabase";
2
3
const SUPABASE_URL = YOUR_SUPABASE_URL;
4
const SUPABASE_KEY = YOUR_SUPABASE_KEY
5
6
export const supabaseClient = createClient(SUPABASE_URL, SUPABASE_KEY, {
7
db: {
8
schema: "public",
9
},
10
auth: {
11
persistSession: true,
12
},
13
});

你还可以从 项目的 连接 对话框 获取项目 URL 和密钥。

阅读 API 密钥文档 以全面了解所有密钥类型及其用途。

6

添加 instruments 资源和页面

你必须配置资源并为 instruments 资源定义页面。

使用以下命令自动添加资源并使用 Refine Inferencer 为 instruments 生成页面代码。

这在 src/pages/instruments/ 目录中定义了 listcreateshowedit 操作的页面,并带有 <HeadlessInferencer /> 组件。

<HeadlessInferencer /> 组件依赖于 @refinedev/react-table@refinedev/react-hook-form 包。为了避免错误,你应该使用 npm install @refinedev/react-table @refinedev/react-hook-form 将它们作为依赖项安装。

终端
1
npm run refine create-resource instruments
7

添加 instruments 页面的路由

添加 listcreateshowedit 页面的路由。

src/App.tsx
1
import { Refine } from "@refinedev/core";
2
import { RefineKbar, RefineKbarProvider } from "@refinedev/kbar";
3
import routerProvider, {
4
DocumentTitleHandler,
5
NavigateToResource,
6
UnsavedChangesNotifier,
7
} from "@refinedev/react-router";
8
import { dataProvider, liveProvider } from "@refinedev/supabase";
9
import { BrowserRouter, Route, Routes } from "react-router-dom";
10
11
import "./App.css";
12
import authProvider from "./authProvider";
13
import { supabaseClient } from "./utility";
14
import { InstrumentsCreate, InstrumentsEdit, InstrumentsList, InstrumentsShow } from "./pages/instruments";
15
16
function App() {
17
return (
18
<BrowserRouter>
19
<RefineKbarProvider>
20
<Refine
21
dataProvider={dataProvider(supabaseClient)}
22
liveProvider={liveProvider(supabaseClient)}
23
authProvider={authProvider}
24
routerProvider={routerProvider}
25
options={{
26
syncWithLocation: true,
27
warnWhenUnsavedChanges: true,
28
}}
29
resources={[{
30
name: "instruments",
31
list: "/instruments",
32
create: "/instruments/create",
33
edit: "/instruments/edit/:id",
34
show: "/instruments/show/:id"
35
}]}>
36
<Routes>
37
<Route index
38
element={<NavigateToResource resource="instruments" />}
39
/>
40
<Route path="/instruments">
41
<Route index element={<InstrumentsList />} />
42
<Route path="create" element={<InstrumentsCreate />} />
43
<Route path="edit/:id" element={<InstrumentsEdit />} />
44
<Route path="show/:id" element={<InstrumentsShow />} />
45
</Route>
46
</Routes>
47
<RefineKbar />
48
<UnsavedChangesNotifier />
49
<DocumentTitleHandler />
50
</Refine>
51
</RefineKbarProvider>
52
</BrowserRouter>
53
);
54
}
55
56
export default App;
8

查看 instruments 页面

现在你应该能够沿着 /instruments 路由看到 instruments 页面。你现在可以使用 Inferencer 生成的 UI 编辑和添加新的 instruments。

Inferencer 自动生成的代码为你提供了一个良好的起点,可以继续构建你的 listcreateshowedit 页面。可以通过点击各自页面中的 显示自动生成的代码 按钮来获取它们。