ores.cpp.nats-event-registrar.nats_event_registrar_implementation
Table of Contents
This page is the node for the ores.cpp.nats-event-registrar.nats_event_registrar_implementation archetype in the codegen
physical-space address graph. Its title is its MASD address; it generates one
output artefact and owns the mustache source below, tangled to the sibling
.mustache consumed by generator.py. Edit this document, then run the
tangle (compass build --direct tangle_codegen_templates).
1. Summary
Implements register_<entity>_event_mapping(): registers the entity's
Postgres NOTIFY channel with the postgres_event_source, then subscribes
the resulting in-process event_bus event and republishes it to NATS via
the shared ores.eventing.core publish_entity_event helper. The channel
name (sql_name_base, e.g. ores_refdata_purpose_types) matches
exactly what the notify_trigger SQL archetype emits in its
pg_notify() call — both derive from the same sql_name_base, the
:tablename:-derived base that keeps every object name within
PostgreSQL's 63-byte identifier limit, so they can never drift apart.
2. The ores.cpp.nats-event-registrar.nats_event_registrar_implementation archetype
{{! GENERATED FILE — tangled from projects/ores.codegen/library/templates/ores.cpp.nats-event-registrar.nats_event_registrar_implementation.org. Edit the org source. }}
{{{cpp_license}}}
{{#domain_entity}}
#include "ores.{{component_service}}/messaging/{{entity_singular}}_event_registrar.hpp"
#include "ores.eventing.api/domain/entity_event_traits.hpp"
#include "ores.eventing.core/service/entity_event_publisher.hpp"
#include "ores.eventing.api/service/event_bus.hpp"
#include "ores.eventing.core/service/postgres_event_source.hpp"
#include "ores.nats/service/client.hpp"
// The event header specialises entity_event_traits, which the mapping and the
// subject below instantiate.
#include "ores.{{component_include}}/eventing/{{entity_singular}}_event.hpp" // IWYU pragma: keep.
#include "ores.{{component_include}}/messaging/{{entity_singular}}_protocol.hpp"
namespace ores::{{component}}::service::messaging {
namespace {
namespace ev = ores::eventing;
}
[[nodiscard]] ev::service::subscription
register_{{entity_singular}}_event_mapping(ev::service::postgres_event_source& event_source,
ev::service::event_bus& event_bus,
ores::nats::service::client& nats) {
// The trigger publishes on the table's own channel, and the mapping turns
// what it says into this entity's event.
event_source.register_entity_event_mapping<{{component}}::messaging::{{entity_singular}}_event>(
"{{sql_name_base}}");
return event_bus.subscribe<ev::domain::published_entity_event<
{{component}}::messaging::{{entity_singular}}_event>>(
[&nats](const ev::domain::published_entity_event<
{{component}}::messaging::{{entity_singular}}_event>& published) {
// One payload is addressed by three subjects, so the subject is
// the collection's prefix and the action the event reports. The
// tenant and the party travel in the headers, not in the payload.
ev::service::publish_entity_event(
nats,
ev::domain::event_subject<{{component}}::messaging::{{entity_singular}}_event>(
published.event.action),
published);
});
}
}
{{/domain_entity}}
{{#junction}}
#include "ores.{{component_service}}/messaging/{{name_singular}}_event_registrar.hpp"
#include "ores.eventing.api/domain/entity_event_traits.hpp"
#include "ores.eventing.core/service/entity_event_publisher.hpp"
#include "ores.eventing.api/service/event_bus.hpp"
#include "ores.eventing.core/service/postgres_event_source.hpp"
#include "ores.nats/service/client.hpp"
// The event header specialises entity_event_traits, which the mapping and the
// subject below instantiate.
#include "ores.{{component_include}}/eventing/{{name_singular}}_event.hpp" // IWYU pragma: keep.
#include "ores.{{component_include}}/messaging/{{name_singular}}_protocol.hpp"
namespace ores::{{component}}::service::messaging {
namespace {
namespace ev = ores::eventing;
}
[[nodiscard]] ev::service::subscription
register_{{name_singular}}_event_mapping(ev::service::postgres_event_source& event_source,
ev::service::event_bus& event_bus,
ores::nats::service::client& nats) {
// The trigger publishes on the table's own channel, and the mapping turns
// what it says into this link's event.
event_source.register_entity_event_mapping<{{component}}::messaging::{{name_singular}}_event>(
"{{events_channel}}");
return event_bus.subscribe<ev::domain::published_entity_event<
{{component}}::messaging::{{name_singular}}_event>>(
[&nats](const ev::domain::published_entity_event<
{{component}}::messaging::{{name_singular}}_event>& published) {
ev::service::publish_entity_event(
nats,
ev::domain::event_subject<{{component}}::messaging::{{name_singular}}_event>(
published.event.action),
published);
});
}
}
{{/junction}}
3. See also
- Parent facet: ores.cpp.nats-event-registrar
- Template variable reference
- ores.cpp.nats-event-registrar.nats_event_registrar_header — the header this implements.
- ores.sql.schema.notify_trigger — the SQL side of this pipeline; channel name must match.