try using events as param

This commit is contained in:
adlerhurst
2025-01-06 10:46:36 +01:00
parent 10acecb7a1
commit 296f8cb5f1
9 changed files with 61 additions and 119 deletions

View File

@@ -4,6 +4,7 @@ CREATE TABLE IF NOT EXISTS instances (
, change_date TIMESTAMPTZ NOT NULL , change_date TIMESTAMPTZ NOT NULL
, creation_date TIMESTAMPTZ NOT NULL , creation_date TIMESTAMPTZ NOT NULL
, latest_position NUMERIC NOT NULL , latest_position NUMERIC NOT NULL
, latest_in_position_order INT2 NOT NULL
, default_org_id TEXT , default_org_id TEXT
, iam_project_id TEXT , iam_project_id TEXT
, console_client_id TEXT , console_client_id TEXT

View File

@@ -1,11 +1,6 @@
CREATE OR REPLACE FUNCTION reduce_instance_added( CREATE OR REPLACE FUNCTION reduce_instance_added("event" RECORD)
id TEXT
, "name" TEXT
, creation_date TIMESTAMPTZ
, "position" NUMERIC
)
RETURNS VOID RETURNS VOID
LANGUAGE PLpgSQL LANGUAGE SQL
AS $$ AS $$
BEGIN BEGIN
INSERT INTO instances ( INSERT INTO instances (
@@ -14,12 +9,14 @@ BEGIN
, creation_date , creation_date
, change_date , change_date
, latest_position , latest_position
, latest_in_position_order
) VALUES ( ) VALUES (
id (event).aggregate_id
, "name" , (event).payload->>'name'
, creation_date , (event).created_at
, creation_date , (event).created_at
, "position" , (event).position
, (event).in_tx_order::INT2
) )
ON CONFLICT (id) DO NOTHING; ON CONFLICT (id) DO NOTHING;
END; END;

View File

@@ -1,19 +1,15 @@
CREATE OR REPLACE FUNCTION reduce_instance_default_language_set( CREATE OR REPLACE FUNCTION reduce_instance_default_language_set("event" eventstore.events2)
instance_id TEXT
, "language" TEXT
, change_date TIMESTAMPTZ
, "position" NUMERIC
)
RETURNS VOID RETURNS VOID
LANGUAGE PLpgSQL LANGUAGE PLpgSQL
AS $$ AS $$
BEGIN BEGIN
UPDATE instances SET UPDATE instances SET
default_language = "language" default_language = (event).payload->>'language'
, change_date = change_date , change_date = (event).created_at
, latest_position = "position" , latest_position = (event).position
, latest_in_position_order = (event).in_tx_order::INT2
WHERE WHERE
id = instance_id id = (event).aggregate_id
AND latest_position <= "position"; AND (latest_position, latest_in_position_order) < ((event).position, (event).in_tx_order::INT2);
END; END;
$$; $$;

View File

@@ -1,19 +1,15 @@
CREATE OR REPLACE FUNCTION reduce_instance_project_set( CREATE OR REPLACE FUNCTION reduce_instance_project_set("event" eventstore.events2)
instance_id TEXT
, project_id TEXT
, change_date TIMESTAMPTZ
, "position" NUMERIC
)
RETURNS VOID RETURNS VOID
LANGUAGE PLpgSQL LANGUAGE PLpgSQL
AS $$ AS $$
BEGIN BEGIN
UPDATE instances SET UPDATE instances SET
iam_project_id = project_id iam_project_id = (event).payload->>'iamProjectId'
, change_date = change_date , change_date = (event).created_at
, latest_position = "position" , latest_position = (event).position
, latest_in_position_order = (event).in_tx_order::INT2
WHERE WHERE
id = instance_id id = (event).aggregate_id
AND latest_position <= "position"; AND (latest_position, latest_in_position_order) < ((event).position, (event).in_tx_order::INT2);
END; END;
$$; $$;

View File

@@ -1,21 +1,16 @@
CREATE OR REPLACE FUNCTION reduce_instance_console_set( CREATE OR REPLACE FUNCTION reduce_instance_console_set("event" eventstore.events2)
instance_id TEXT
, app_id TEXT
, client_id TEXT
, change_date TIMESTAMPTZ
, "position" NUMERIC
)
RETURNS VOID RETURNS VOID
LANGUAGE PLpgSQL LANGUAGE PLpgSQL
AS $$ AS $$
BEGIN BEGIN
UPDATE instances SET UPDATE instances SET
console_app_id = app_id console_app_id = (event).payload->>'appId'
, console_client_id = client_id , console_client_id = (event).payload->>'clientId'
, change_date = change_date , change_date = (event).created_at
, latest_position = "position" , latest_position = (event).position
, latest_in_position_order = (event).in_tx_order::INT2
WHERE WHERE
id = instance_id id = (event).aggregate_id
AND latest_position <= "position"; AND (latest_position, latest_in_position_order) < ((event).position, (event).in_tx_order::INT2);
END; END;
$$; $$;

View File

@@ -1,19 +1,15 @@
CREATE OR REPLACE FUNCTION reduce_instance_default_org_set( CREATE OR REPLACE FUNCTION reduce_instance_default_org_set("event" eventstore.events2)
instance_id TEXT
, org_id TEXT
, change_date TIMESTAMPTZ
, "position" NUMERIC
)
RETURNS VOID RETURNS VOID
LANGUAGE PLpgSQL LANGUAGE PLpgSQL
AS $$ AS $$
BEGIN BEGIN
UPDATE instances SET UPDATE instances SET
default_org_id = org_id default_org_id = (event).payload->>'orgId'
, change_date = change_date , change_date = (event).created_at
, latest_position = "position" , latest_position = (event).position
, latest_in_position_order = (event).in_tx_order::INT2
WHERE WHERE
id = instance_id id = (event).aggregate_id
AND latest_position <= "position"; AND (latest_position, latest_in_position_order) < ((event).position, (event).in_tx_order::INT2);
END; END;
$$; $$;

View File

@@ -1,19 +1,15 @@
CREATE OR REPLACE FUNCTION reduce_instance_changed( CREATE OR REPLACE FUNCTION reduce_instance_changed("event" eventstore.events2)
instance_id TEXT
, "name" TEXT
, change_date TIMESTAMPTZ
, "position" NUMERIC
)
RETURNS VOID RETURNS VOID
LANGUAGE PLpgSQL LANGUAGE PLpgSQL
AS $$ AS $$
BEGIN BEGIN
UPDATE instances SET UPDATE instances SET
"name" = $2 "name" = (event).payload->>'name'
, change_date = change_date , change_date = (event).created_at
, latest_position = "position" , latest_position = (event).position
, latest_in_position_order = (event).in_tx_order::INT2
WHERE WHERE
id = instance_id id = (event).aggregate_id
AND latest_position <= "position"; AND (latest_position, latest_in_position_order) < ((event).position, (event).in_tx_order::INT2);
END; END;
$$; $$;

View File

@@ -1,6 +1,4 @@
CREATE OR REPLACE FUNCTION reduce_instance_removed( CREATE OR REPLACE FUNCTION reduce_instance_removed("event" eventstore.events2)
instance_id TEXT
)
RETURNS VOID RETURNS VOID
LANGUAGE PLpgSQL LANGUAGE PLpgSQL
AS $$ AS $$
@@ -8,6 +6,6 @@ BEGIN
DELETE FROM DELETE FROM
instances instances
WHERE WHERE
id = instance_id; id = (event).aggregate_id;
END; END;
$$; $$;

View File

@@ -4,52 +4,19 @@ LANGUAGE PLpgSQL
AS $$ AS $$
BEGIN BEGIN
IF (NEW).event_type = 'instance.added' THEN IF (NEW).event_type = 'instance.added' THEN
SELECT reduce_instance_added( SELECT reduce_instance_added(NEW::eventstore.events2);
(NEW).aggregate_id -- ELSIF (NEW).event_type = 'instance.changed' THEN
, (NEW).payload->>'name'::TEXT -- SELECT reduce_instance_changed(NEW::eventstore.events2);
, (NEW).created_at -- ELSIF (NEW).event_type = 'instance.removed' THEN
, (NEW).position -- SELECT reduce_instance_removed(NEW::eventstore.events2);
); -- ELSIF (NEW).event_type = 'instance.default.language.set' THEN
ELSIF (NEW).event_type = 'instance.changed' THEN -- SELECT reduce_instance_default_language_set(NEW::eventstore.events2);
SELECT reduce_instance_changed( -- ELSIF (NEW).event_type = 'instance.default.org.set' THEN
(NEW).aggregate_id -- SELECT reduce_instance_default_org_set(NEW::eventstore.events2);
, (NEW).payload->>'name'::TEXT -- ELSIF (NEW).event_type = 'instance.iam.project.set' THEN
, (NEW).created_at -- SELECT reduce_instance_project_set(NEW::eventstore.events2);
, (NEW).position -- ELSIF (NEW).event_type = 'instance.iam.console.set' THEN
); -- SELECT reduce_instance_console_set(NEW::eventstore.events2);
ELSIF (NEW).event_type = 'instance.removed' THEN
SELECT reduce_instance_removed(
(NEW).aggregate_id
);
ELSIF (NEW).event_type = 'instance.default.language.set' THEN
SELECT reduce_instance_default_language_set(
(NEW).aggregate_id
, (NEW).payload->>'language'::TEXT
, (NEW).created_at
, (NEW).position
);
ELSIF (NEW).event_type = 'instance.default.org.set' THEN
SELECT reduce_instance_default_org_set(
(NEW).aggregate_id
, (NEW).payload->>'orgId'::TEXT
, (NEW).created_at
, (NEW).position
);
ELSIF (NEW).event_type = 'instance.iam.project.set' THEN
SELECT reduce_instance_project_set(
(NEW).aggregate_id
, (NEW).payload->>'iamProjectId'::TEXT
, (NEW).created_at
, (NEW).position
);
ELSIF (NEW).event_type = 'instance.iam.console.set' THEN
SELECT reduce_instance_console_set(
(NEW).aggregate_id
, (NEW).payload->>'appId'::TEXT
, (NEW).payload->>'clientId'::TEXT
, (NEW).created_at
, (NEW).position
);
END IF; END IF;
RETURN NULL; RETURN NULL;
END END