[英]How to get a list of Postgres types from Supabase table?
I am trying to add this in an RPC function in Supabase but I'm not sure why it's only returning the first column.我试图在 Supabase 的 RPC function 中添加它,但我不确定为什么它只返回第一列。
SELECT
column_name,
data_type
FROM
information_schema.columns
WHERE
table_name = 'frappe';
It returns this:它返回这个:
{ column_name: 'id', data_type: 'bigint' }
How can I get all the results?我怎样才能得到所有的结果? Is it possible to call this function using the Javascript Libraries?
是否可以使用 Javascript 库调用此 function?
This is how you can get a list of columns and their types from Postgres using a Supabase function. To achieve this, you can get the data from Postgres' information_schema.columns
table.这就是如何使用 Supabase function 从 Postgres 获取列及其类型的列表。为此,您可以从 Postgres 的
information_schema.columns
表中获取数据。
CREATE OR REPLACE FUNCTION get_types(tname text)
RETURNS TABLE ( column_name text,data_type text ) AS
$BODY$
BEGIN
RETURN QUERY EXECUTE format($$SELECT column_name::text,data_type::text FROM information_schema.columns WHERE table_name ='$$|| '%I' ||$$';$$,tname);
END;
$BODY$
LANGUAGE plpgsql;
Please note that this is a more advanced function and you should deploy it using the SQL Editor or PSQL and not using the Supabase UI (return types of table
/ setof
is not available there).请注意,这是一个更高级的 function,您应该使用 SQL 编辑器或 PSQL 部署它,而不是使用 Supabase UI(返回类型的
table
/ setof
在那里不可用)。 Here are the results from calling this function in SQL:以下是在 SQL 中调用这个 function 的结果:
Since this is a Postgres function, you can call it using Supabase Javascript API for RPC:由于这是一个 Postgres function,您可以使用 Supabase Javascript API 为 RPC 调用它:
const { data, error } = await supabase
.rpc('get_types', {tname: 'frappe'})
[
{ column_name: 'cnt', data_type: 'bigint' },
{ column_name: 'item', data_type: 'text' },
{ column_name: 'daytime', data_type: 'text' },
{ column_name: 'weekday', data_type: 'text' },
{ column_name: 'isweekend', data_type: 'text' },
{ column_name: 'homework', data_type: 'text' },
{ column_name: 'cost', data_type: 'text' },
{ column_name: 'weather', data_type: 'text' },
{ column_name: 'country', data_type: 'text' },
{ column_name: 'user', data_type: 'text' },
{ column_name: 'city', data_type: 'text' }
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.