Open3
SupabaseのTable取得時にTypeScriptの型情報を使用する
ここの丸写し
supabase-cliを使う。でもドキュメント読んでも見当たらなかった。メンテしてるんか?
supabase-cliをインストールして(別ディレクトリでもOK)Dashboardからtoken生成してloginしてプロジェクトにlinkしてtypesを生成して保存する。
### Install and Setup
$ mkdir -p Nodejs/supabase
$ cd Nodejs/supabse
$ npm i supabase --save-dev
$ npx supabase login
You can generate an access token from https://supabase.com/dashboard/account/tokens
Enter your access token:
Finished supabase login.
$ npx supabase link --project-ref xxxxxxxxxxxxxxxxxxxx
Enter your database password (or leave blank to skip):
Finished supabase link.
Local config differs from linked project. Try updating supabase/config.toml
["db.pooler"]
enabled = false
port = 54329
pool_mode = "transaction"
default_pool_size = 15
max_client_conn = 100
$ npx supabase gen types typescript --linked > schema.ts
Enter your database password:
あとはコードに読み込んで使用すると型が取得できるようになっている。ビューティフォー
import { createClient } from "$supabase-js";
import { Database } from "./schema.ts";
const supabaseKey = Deno.env.get("SUPABASE_KEY");
const supabaseUrl = Deno.env.get("SUPABASE_URL");
supabase = createClient<Database>(supabaseUrl, supabaseKey, {
auth: {
autoRefreshToken: false,
persistSession: false,
detectSessionInUrl: false,
},
});
/*
* function SelectArticles(): Promise<PostgrestSingleResponse<{
* content: string | null;
* created_at: string | null;
* id: string;
* is_deleted: boolean;
* title: string | null;
* updated_at: string | null;
* }[]>>
*/
export async function SelectArticles() {
init();
return await supabase
.from("articles")
.select("*");
}