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
, creation_date TIMESTAMPTZ NOT NULL
, latest_position NUMERIC NOT NULL
, latest_in_position_order INT2 NOT NULL
, default_org_id TEXT
, iam_project_id TEXT
, console_client_id TEXT

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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