我来自 Firebase,我正在尝试获取数据库的类型。例如,我习惯于像这样获取时间戳的类型:
import type { Timestamp } from 'firebase/firestore';
export type User = {
//..
createdAt: Timestamp;
updatedAt: Timestamp | null;
};
如何获取 Supabase 数据库的类型?
您可以查看 Supabase 官方文档以了解生成类型。还有一些例子可以参考。
https://supabase.com/docs/guides/api/generate-types
import { NextApiRequest, NextApiResponse } from 'next'
import { createClient } from '@supabase/supabase-js'
import { Database } from '../types/supabase'
const supabase = createClient<Database>(
process.env.NEXT_PUBLIC_SUPABASE_URL,
process.env.SUPABASE_SECRET_KEY
)
export default async (req: NextApiRequest, res: NextApiResponse) => {
const allOnlineUsers = await supabase.from('users').select('*').eq('status', 'ONLINE')
res.status(200).json(allOnlineUsers)
}
您应该为各个表定义自己的类型,例如
import supabase from '~/lib/supabase'
import type { Database } from '~/lib/database.types'
async function getMovies() {
return await supabase.from('movies').select('id, title, actors(\*)')
}
type Actors = Database['public']['tables']['actors']['row']
type MoviesResponse = Awaited<ReturnType<typeof getMovies>>
type MoviesResponseSuccess = MoviesResponse['data'] & {
actors: Actors[]
}
https://supabase.com/dashboard/project/_/api?page=tables-intro
您可以转到此链接并打开您的项目,然后使用右侧的按钮下载类型