有没有更好的方法来访问Supabase生成的TS类型?

huangapple go评论71阅读模式
英文:

Is there a better way to access Supabase generated TS types?

问题

我已在我的 Supabase 数据库中创建了一个名为 customer 的表格。
使用以下命令生成了我的类型:

npx supabase gen types typescript --project-id "$PROJECT_REF" --schema public > types/supabase.ts

在我的 React 应用中,这是我如何引用生成的类型的方式:

const [customerDetails, setCustomerDetails] = useState<Database["public"]["Tables"]["customer"]["Row"]>()

是否可以更简洁一些,像这样?

const [customerDetails, setCustomerDetails] = useState<customer>()
英文:

I have created a table called customer in my supabase db.
Generated my types using

npx supabase gen types typescript --project-id &quot;$PROJECT_REF&quot; --schema public &gt; types/supabase.ts

In my React app this is how I am referencing the generated types.

const [customerDetails, setCustomerDetails] = useState&lt;Database[&quot;public&quot;][&quot;Tables&quot;][&quot;customer&quot;][&quot;Row&quot;]&gt;()

Can it be a bit more cleaner, like this?

const [customerDetails, setCustomerDetails] = useState&lt;customer&gt;()

答案1

得分: 1

你可以使用类似这样的工具:

https://github.com/FroggyPanda/better-supabase-types

来后处理 Supabase 生成的类型。这将生成一个新的模式文件,内容如下:

export type Account = Database['public']['Tables']['accounts']['Row'];
export type InsertAccount = Database['public']['Tables']['accounts']['Insert'];
export type UpdateAccount = Database['public']['Tables']['accounts']['Update'];

如果你不想依赖其他库或者编写自己的后处理脚本,你还可以根据建议这里改进类型,使用一些辅助类型,例如:

export type Row<T extends keyof Database['public']['Tables']> = Database['public']['Tables'][T]['Row'];
export type InsertDto<T extends keyof Database['public']['Tables']> = Database['public']['Tables'][T]['Insert'];
export type UpdateDto<T extends keyof Database['public']['Tables']> = Database['public']['Tables'][T]['Update'];

这将导致以下结果:

import { InsertDto, Row, UpdateDto } from '@src/interfaces/database';

export type Location = Row<'location'>;
export type LocationInsertDto = InsertDto<'location'>;
export type LocationUpdateDto = UpdateDto<'location'>;
英文:

You could use something like this

https://github.com/FroggyPanda/better-supabase-types

to post-process the types that supabase generates. That will generate a new schema file with content like this

export type Account = Database[&#39;public&#39;][&#39;Tables&#39;][&#39;accounts&#39;][&#39;Row&#39;];
export type InsertAccount = Database[&#39;public&#39;][&#39;Tables&#39;][&#39;accounts&#39;][&#39;Insert&#39;];
export type UpdateAccount = Database[&#39;public&#39;][&#39;Tables&#39;][&#39;accounts&#39;][&#39;Update&#39;];

If you don't want another dependency or write your own post-processor script, you could also improve the types with some helper types as suggested here:

export type Row&lt;T extends keyof Database[&#39;public&#39;][&#39;Tables&#39;]&gt; = Database[&#39;public&#39;][&#39;Tables&#39;][T][&#39;Row&#39;];
export type InsertDto&lt;T extends keyof Database[&#39;public&#39;][&#39;Tables&#39;]&gt; = Database[&#39;public&#39;][&#39;Tables&#39;][T][&#39;Insert&#39;];
export type UpdateDto&lt;T extends keyof Database[&#39;public&#39;][&#39;Tables&#39;]&gt; = Database[&#39;public&#39;][&#39;Tables&#39;][T][&#39;Update&#39;];

Which will lead to

import { InsertDto, Row, UpdateDto } from &#39;@src/interfaces/database&#39;;

export type Location = Row&lt;&#39;location&#39;&gt;;
export type LocationInsertDto = InsertDto&lt;&#39;location&#39;&gt;;
export type LocationUpdateDto = UpdateDto&lt;&#39;location&#39;&gt;;

huangapple
  • 本文由 发表于 2023年6月22日 08:56:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定