@nullplatform/ingest
Version:
Client SDK for the nullplatform ingest API: push snapshots and delta events from a collector into the catalog and other targets, follow runs, replay and drift checks
129 lines (118 loc) • 4.66 kB
JavaScript
/**
* 02-events — forward delta events (webhooks, watches, CDC) as they happen.
*
* SCENARIO: a watch on a cluster delivers ADDED / MODIFIED / DELETED
* notifications. The handler turns each into an event: an upsert or an
* explicit delete, observed at the time the cluster reports. An event batcher
* ships them in batches as they come and is closed on shutdown.
*
* Order is the collector's clock: the API applies the newest `observedAt` per
* entity, whatever order batches arrive in, and drops an older one as `stale`.
* Events never infer deletions — only a delete event deletes, and only an
* entity this source owns.
*
* Run it like 01-snapshot.cjs: NULLPLATFORM_API_KEY=… node examples/02-events.cjs
*/
const { IngestClient } = require('@nullplatform/ingest');
// ── client: the SDK reads no environment; this example reads the API key and ─
// passes it in. Everything else is the SDK's default: the public API.
function connect() {
const apiKey = process.env.NULLPLATFORM_API_KEY ?? process.env.NP_API_KEY;
if (!apiKey) {
console.error('Set NULLPLATFORM_API_KEY (or NP_API_KEY) to run this example.');
process.exit(1);
}
return new IngestClient({ apiKey });
}
/** A Deployment as the Kubernetes API answers it (trimmed): the shipped mapping reads this shape. */
function deployment({ namespace, name, uid, replicas, image }) {
return {
apiVersion: 'apps/v1',
kind: 'Deployment',
metadata: { namespace, name, uid },
spec: {
replicas,
selector: { matchLabels: { app: name } },
template: { spec: { containers: [{ name, image }] } },
},
};
}
// ── stubbed watch stream: what the cluster client already delivers ─────────────
const notifications = [
{
type: 'ADDED',
time: '2026-03-01T10:00:00.000Z',
object: deployment({
namespace: 'shop',
name: 'search',
uid: '0b5e6a2f-3c7d-4e8f-9a1b-2c3d4e5f6a04',
replicas: 1,
image: 'registry.example.com/search:3.2.0',
}),
},
{
type: 'MODIFIED',
time: '2026-03-01T10:05:00.000Z',
object: deployment({
namespace: 'shop',
name: 'search',
uid: '0b5e6a2f-3c7d-4e8f-9a1b-2c3d4e5f6a04',
replicas: 4,
image: 'registry.example.com/search:3.2.0',
}),
},
{
type: 'DELETED',
time: '2026-03-01T10:30:00.000Z',
object: {
metadata: { namespace: 'shop', name: 'search', uid: '0b5e6a2f-3c7d-4e8f-9a1b-2c3d4e5f6a04' },
},
},
];
const SOURCE = 'cluster-prod-eu-watch';
/** The watch handler: one notification in, one event out. Returns the input id. */
function onNotification(events, notification) {
const options = { observedAt: notification.time };
return notification.type === 'DELETED'
? events.delete('deployment', notification.object, options)
: events.upsert('deployment', notification.object, options);
}
/**
* The organization's setup, done once (by its administrators, not by every
* collector): `kubernetes` is nullplatform's global source type, so its
* collectors define no mappings. The organization binds the type's mapping of
* deployments, the one nullplatform ships, to a destination once, for every
* source of the type. It keys each entity by the object's uid, so two clusters
* never claim one.
*/
async function bindOnce(ingest) {
const type = await ingest.sourceTypes.find('kubernetes');
const [mapping] = (await ingest.mappings.listForSourceType(type.id, { recordType: 'deployment' }))
.results;
// An upsert: run it again and nothing changes.
await ingest.destinations.upsertForSourceType(type.id, [
{ mappingId: mapping.id, target: 'catalog', endpoint: 'k8s-deployment' },
]);
}
async function main() {
const ingest = connect();
await bindOnce(ingest);
const [source] = await ingest.sources.upsert([
{ slug: SOURCE, name: 'Production cluster (eu) watch', type: 'kubernetes' },
]);
// Flushed every second, or as soon as a batch fills up.
const events = ingest.events.batcher(source.id, { flushIntervalMs: 1000 });
const ids = notifications.map((notification) => onNotification(events, notification));
// On shutdown: send what is buffered and refuse anything later.
await events.close();
console.log('sent:', events.stats);
// Optional: the API applies inputs asynchronously; wait to see what became of them.
const settled = await ingest.inputs.wait(ids, { intervalMs: 200, timeoutMs: 30_000 });
settled.forEach((input) => {
console.log(input.op, input.status, input.result && input.result.outcome);
});
}
main().catch((error) => {
console.error(error);
process.exit(1);
});