swiss
02/14/2022, 7:14 PMsilentworks
02/14/2022, 7:19 PMswiss
02/14/2022, 7:23 PMsilentworks
02/14/2022, 7:24 PMswiss
02/14/2022, 7:24 PMswiss
02/14/2022, 7:25 PMsilentworks
02/14/2022, 7:26 PMsilentworks
02/14/2022, 7:29 PMswiss
02/14/2022, 7:34 PMsilentworks
02/14/2022, 7:37 PMsilentworks
02/14/2022, 7:43 PMsql
-- Table Definition
CREATE TABLE "public"."sessions" (
"id" int8 NOT NULL,
"created_at" timestamptz DEFAULT now(),
"end_at" timestamptz,
"task_id" int8,
PRIMARY KEY ("id")
);
and from this I create a View called sessions_difference
, you can call it whatever you please
sql
CREATE VIEW sessions_difference AS
SELECT
id,
task_id,
created_at,
end_at,
end_at - created_at AS difference
FROM sessions;
In this view I am creating a select query against the sessions table with a column that is calculated during the query which is the end_at - created_at AS difference
part of the script. Now in javascript you can just call this view as you would a normal table
js
const { data, error } = await supabase.from('sessions_difference')
.select('*')
.eq('task_id', taskId);
In the instance above taskId
would be the variable name in your code of the task id.silentworks
02/14/2022, 7:47 PMswiss
02/14/2022, 8:42 PMswiss
02/14/2022, 8:43 PMswiss
02/14/2022, 8:52 PMsilentworks
02/14/2022, 9:21 PMsql
CREATE TABLE "public"."sessions" (
"id" BIGSERIAL NOT NULL,
"created_at" timestamptz DEFAULT now(),
"end_at" timestamptz,
"task_id" int8,
"difference" interval GENERATED ALWAYS AS (end_at - created_at) STORED,
PRIMARY KEY ("id")
);
In this example the computed property will always generate the difference once both values are in place, if you don't have a end_at the difference column will be NULLsilentworks
02/14/2022, 9:26 PMsilentworks
02/14/2022, 9:44 PMsql
-- create table without the total_time column
CREATE TABLE "public"."sessions" (
"id" BIGSERIAL NOT NULL,
"created_at" timestamptz DEFAULT now(),
"end_at" timestamptz,
"task_id" int8,
PRIMARY KEY ("id")
);
-- create the total_time computed column using a function
CREATE FUNCTION total_time(sessions)
RETURNS interval
LANGUAGE sql AS
$$
SELECT SUM(end_at - created_at)
FROM public.sessions ps
WHERE ps.id = $1.id
$$;
-- querying the database whilst referring that column
SELECT id, created_at, end_at, task_id, sessions.total_time
FROM public.sessions
You should be able to call this the same in JS now
js
const { data, error } = await supabase.from('session')
.select(`id, created_at, end_at, task_id, total_time`);
swiss
02/14/2022, 9:47 PMYeah there may be times where end_at isn't defined so that doesn't work. I'm trying to piece together a function by reading the pgpsql docs because I found a video about it from a supabase team member and I honestly didn't even know this is a thing (I try to avoid db stuff which is why I'm using supabase in the first place lol). Could you help me understand why I'm getting date/time field value out of range: "1644782218932" when I run the following?
create or replace function getTotalTimeSpentOnAction(action_id int) returns integer as $$
declare
sesh sessions;
time_spent int default 0;
begin
for sesh in
select start, stop
from sessions where sessions.action = action_id
loop
time_spent = time_spent + (sesh.stop - sesh.start);
end loop;
return time_spent;
end;
$$ language plpgsql;
swiss
02/14/2022, 9:47 PMsilentworks
02/14/2022, 9:48 PMswiss
02/14/2022, 9:49 PMswiss
02/14/2022, 9:49 PMsilentworks
02/14/2022, 9:50 PM.rpc
https://supabase.com/docs/reference/javascript/rpcswiss
02/14/2022, 9:52 PMsilentworks
02/14/2022, 9:53 PMswiss
02/14/2022, 9:54 PM