2025-04-22

supabase的AI, functions和API

再探了一下 supabase.

之前我想好了新app的 data tables. 當然只是草稿了 table name 和相關欄位名. 面對 supabase 介面, 我當然不想再用 GUI 慢慢建立數據表. 我看到了它內置的 AI 命令介面: 右上角綠色菱形. 

AI

那我就說

follow tables please create them in effortapp schema: i will list the table name, followed by a colon, then fields. you try to define the type for them. and also you can see the FK between them.  
user: id, email, name
team: id, name 
project: id, name, team id, start date, end date, ....


然後它就生成了相關的 create table SQL, 一鍵完成. 看了一下, 連 foreign key 也是正確的. 當然我用的約定俗成的命名慣例應該有幫助.

我再來:

also create two many-to-many tables to link up relevant tables.

user_in_team: link up user and team  

task_by_user: link up task and user.

當然也完美運行, 2 個多對多的關係表也完成了. 甚至連 on delete cascade 也寫好.

我還打開 entity diagram 看了一下, 應該沒問題.

Database Functions

然後我看到 supabase function. 也許是相當於 oracle 的 stored procedures. 問了AI確認, 我就叫它為幾個表都寫上 CRUD:

create CRUD functions for user, team....  tables. execute the code directly.

似乎它不會立即運行, 我還是要手動按一下運行鍵. 一下子20個 function 就出現了. 

API

我想到, 這麼好的東西, 也許可以經 HTTP 使用吧, 那豈不美妙?

當然是可以的:

POST https://<your-supabase-url>/rest/v1/rpc/create_user
Authorization: Bearer <your-jwt-token>
Content-Type: application/json

{
  "p_email": "user@example.com",
  "p_name": "User Name"
}

好吧, 似乎連 Vercel 都可以省去了...


沒有留言:

發佈留言