#!/bin/sh
set -eu

STREAM="${STREAM:-DebeziumStream}"
COUNT="${COUNT:-20}"
CONSUMER="viewer-$(date +%s)"

echo "Stream subjects:"
nats --server "$NATS_URL" stream subjects "$STREAM"

echo
echo "Change events:"
nats --server "$NATS_URL" consumer add "$STREAM" "$CONSUMER" --ephemeral --pull --defaults >/dev/null

if command -v jq >/dev/null 2>&1; then
  nats --server "$NATS_URL" consumer next --raw --count "$COUNT" "$STREAM" "$CONSUMER" \
    | jq -c '{op, table: .source.table, before, after}'
else
  nats --server "$NATS_URL" consumer next --raw --count "$COUNT" "$STREAM" "$CONSUMER"
fi
