From 5bd66349c0a30ca3b5f717c2ced3c7bdbad86e83 Mon Sep 17 00:00:00 2001 From: keyan Date: Mon, 8 May 2023 15:27:04 -0500 Subject: [PATCH] add user data to comments query when logged out --- .../migration.sql | 2 +- .../migration.sql | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 prisma/migrations/20230508202036_item_comments_users/migration.sql diff --git a/prisma/migrations/20230507205613_update_job_priority/migration.sql b/prisma/migrations/20230507205613_update_job_priority/migration.sql index 4164e221..121b1986 100644 --- a/prisma/migrations/20230507205613_update_job_priority/migration.sql +++ b/prisma/migrations/20230507205613_update_job_priority/migration.sql @@ -30,4 +30,4 @@ $$ LANGUAGE plpgsql; -- we can drop these triggers because item_acts denormalize into item and hit the item trigger DROP TRIGGER IF EXISTS index_item_after_act ON "ItemAct"; -DROP FUNCTION index_item_after_act; \ No newline at end of file +DROP FUNCTION IF EXISTS index_item_after_act; \ No newline at end of file diff --git a/prisma/migrations/20230508202036_item_comments_users/migration.sql b/prisma/migrations/20230508202036_item_comments_users/migration.sql new file mode 100644 index 00000000..3e79e051 --- /dev/null +++ b/prisma/migrations/20230508202036_item_comments_users/migration.sql @@ -0,0 +1,27 @@ +CREATE OR REPLACE FUNCTION item_comments(_item_id int, _level int, _where text, _order_by text) + RETURNS jsonb + LANGUAGE plpgsql STABLE PARALLEL SAFE AS +$$ +DECLARE + result jsonb; +BEGIN + IF _level < 1 THEN + RETURN '[]'::jsonb; + END IF; + + EXECUTE '' + || 'SELECT COALESCE(jsonb_agg(sub), ''[]''::jsonb) AS comments ' + || 'FROM ( ' + || ' SELECT "Item".*, "Item".created_at at time zone ''UTC'' AS "createdAt", "Item".updated_at at time zone ''UTC'' AS "updatedAt", ' + || ' item_comments("Item".id, $2 - 1, $3, $4) AS comments, to_jsonb(users.*) as user ' + || ' FROM "Item" p ' + || ' JOIN "Item" ON "Item"."parentId" = p.id ' + || ' JOIN users ON users.id = "Item"."userId" ' + || ' WHERE p.id = $1 ' + || _where || ' ' + || _order_by + || ' ) sub' + INTO result USING _item_id, _level, _where, _order_by; + RETURN result; +END +$$; \ No newline at end of file