Scott P
07/30/2021, 3:41 PMsql
CREATE OR REPLACE FUNCTION public.some_function()
RETURNS boolean
LANGUAGE plpgsql AS $$
BEGIN
-- Your query code goes here
END;
$$;
The open and saved queries on the dashboard are saved in your browser, and only really exist to make it easier for you to run common queries. Take a look at https://www.postgresqltutorial.com/postgresql-create-function/ for more details on creating functions.
Gonna open a thread from here if you want to discuss further 🙂a d
07/30/2021, 3:42 PMa d
07/30/2021, 3:42 PMa d
07/30/2021, 3:42 PMScott P
07/30/2021, 3:44 PMa d
07/30/2021, 3:44 PMa d
07/30/2021, 3:45 PMScott P
07/30/2021, 3:50 PMRETURN BOOLEAN
in the example above) needs to match the output from the query. As a simple example, if I wanted to find a user by their ID using a stored function, I could do something like this:
sql
CREATE OR REPLACE FUNCTION public.get_user_by_id(user_id uuid)
RETURNS SETOF public.users
LANGUAGE plpgsql AS $$
BEGIN
RETURN QUERY
SELECT * FROM public.users users
WHERE users.id = user_id
RETURNING *;
END;
$$;
a d
07/30/2021, 3:52 PMScott P
07/30/2021, 3:53 PMauth.users
and public.users
), the function might not know which table you're referring toa d
07/30/2021, 3:55 PMa d
07/30/2021, 3:55 PMScott P
07/30/2021, 3:59 PMScott P
07/30/2021, 4:00 PMa d
07/30/2021, 4:01 PMa d
07/30/2021, 4:03 PMScott P
07/30/2021, 4:04 PMa d
07/30/2021, 4:04 PMa d
07/30/2021, 4:05 PMScott P
07/30/2021, 4:06 PMa d
07/30/2021, 4:08 PMScott P
07/30/2021, 4:09 PMa d
07/30/2021, 4:10 PMsql
CREATE TABLE ID_OF_GUILD(
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
inserted_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
updated_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
data jsonb,
name text
);
a d
07/30/2021, 4:11 PMa d
07/30/2021, 4:11 PMa d
07/30/2021, 4:11 PMScott P
07/30/2021, 4:11 PMa d
07/30/2021, 4:12 PMa d
07/30/2021, 4:12 PMScott P
07/30/2021, 4:33 PMsql
-- If you wanted to create the table with the name 'ID_OF_GUILD'
CREATE OR REPLACE FUNCTION public.create_table()
RETURNS void
LANGUAGE plpgsql AS $$
BEGIN
CREATE TABLE ID_OF_GUILD(
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
inserted_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
updated_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL,
data jsonb,
name text
);
END;
$$;
-- SQL usage
SELECT * FROM create_table()
-- If you want to create the table with a custom name
CREATE OR REPLACE FUNCTION public.create_table_with_name(in_table_name text)
RETURNS void
LANGUAGE plpgsql AS $$
BEGIN
EXECUTE FORMAT('
CREATE TABLE IF NOT EXISTS %I (
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
inserted_at timestamp with time zone DEFAULT timezone(''utc''::text, now()) NOT NULL,
updated_at timestamp with time zone DEFAULT timezone(''utc''::text, now()) NOT NULL,
data jsonb,
name text
);', in_table_name
);
END;
$$;
-- SQL usage
SELECT * FROM create_table_with_name('this_is_a_test_name');
In the first example, you'd call it like this from the Supabase-js library:
rpc("create_table")
In the second example:
rpc("create_table_with_name", { in_table_name: "this_is_a_test_name" })
Scott P
07/30/2021, 4:34 PMsql
CREATE OR REPLACE FUNCTION public.create_table_with_default_name(in_table_name text DEFAULT 'ID_OF_GUILD')
RETURNS void
LANGUAGE plpgsql AS $$
BEGIN
EXECUTE FORMAT('
CREATE TABLE IF NOT EXISTS %I (
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
inserted_at timestamp with time zone DEFAULT timezone(''utc''::text, now()) NOT NULL,
updated_at timestamp with time zone DEFAULT timezone(''utc''::text, now()) NOT NULL,
data jsonb,
name text
);', in_table_name
);
END;
$$;
-- SQL usage
SELECT * FROM create_table_with_default_name();
-- OR
SELECT * FROM create_table_with_default_name('this_is_a_test_name_2');
a d
07/31/2021, 3:19 AMScott P
07/31/2021, 2:37 PMa d
07/31/2021, 4:06 PMa d
07/31/2021, 4:06 PMa d
07/31/2021, 4:06 PMa d
07/31/2021, 4:07 PMsql
CREATE OR REPLACE FUNCTION public.create_table_with_name(in_table_name text)
RETURNS void
LANGUAGE plpgsql AS $$
BEGIN
EXECUTE FORMAT('
CREATE TABLE IF NOT EXISTS %I (
id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
inserted_at timestamp with time zone DEFAULT timezone(''utc''::text, now()) NOT NULL,
updated_at timestamp with time zone DEFAULT timezone(''utc''::text, now()) NOT NULL,
data jsonb,
name text
);', in_table_name
);
END;
$$;
stored in a new query named as create_tablea d
07/31/2021, 4:07 PMa d
07/31/2021, 4:07 PMrpc('create_table_with_name')
a d
07/31/2021, 4:08 PMa d
07/31/2021, 4:08 PMa d
07/31/2021, 4:08 PMa d
07/31/2021, 4:09 PMa d
07/31/2021, 4:10 PMa d
07/31/2021, 4:10 PMa d
07/31/2021, 4:10 PMa d
07/31/2021, 4:10 PM