From 5ade072b8c4a4201e018ba84e628642caebbac61 Mon Sep 17 00:00:00 2001 From: keyan Date: Wed, 26 Jan 2022 11:03:21 -0600 Subject: [PATCH] search index triggers --- .../migration.sql | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 prisma/migrations/20220126155041_search_triggers/migration.sql diff --git a/prisma/migrations/20220126155041_search_triggers/migration.sql b/prisma/migrations/20220126155041_search_triggers/migration.sql new file mode 100644 index 00000000..8aa54f3c --- /dev/null +++ b/prisma/migrations/20220126155041_search_triggers/migration.sql @@ -0,0 +1,31 @@ +CREATE OR REPLACE FUNCTION index_item() RETURNS TRIGGER AS $$ + BEGIN + -- insert indexItem pgboss.job with id + INSERT INTO pgboss.job (name, data) VALUES ('indexItem', jsonb_build_object('id', NEW.id)); + -- insert indexItem pgboss.job from parentId if there's a parentId + IF NEW."parentId" IS NOT NULL THEN + INSERT INTO pgboss.job (name, data) VALUES ('indexItem', jsonb_build_object('id', NEW."parentId")); + END IF; + RETURN NEW; + END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS index_item ON "Item"; +CREATE TRIGGER index_item + AFTER INSERT OR UPDATE ON "Item" + FOR EACH ROW + EXECUTE PROCEDURE index_item(); + +CREATE OR REPLACE FUNCTION index_item_after_act() RETURNS TRIGGER AS $$ + BEGIN + -- insert indexItem pgboss.job with itemId + INSERT INTO pgboss.job (name, data) VALUES ('indexItem', jsonb_build_object('id', NEW."itemId")); + RETURN NEW; + END; +$$ LANGUAGE plpgsql; + +DROP TRIGGER IF EXISTS index_item_after_act ON "ItemAct"; +CREATE TRIGGER index_item_after_act + AFTER INSERT ON "ItemAct" + FOR EACH ROW + EXECUTE PROCEDURE index_item_after_act(); \ No newline at end of file