Tenant data export
How a module contributes to the tenant data export. The contributor handler, the archive it writes into, and the manifest that records the outcome.
Hook.TENANT_DATA_EXPORT asks every module for its part of one tenant's data, and the DMS assembles the parts into a single ZIP archive. It is the one hook whose handler produces something. A handler receives the tenant id, an archive to write into, and an AbortSignal. It contributes in either or both of two ways:
- It returns a
TenantDataExportContribution. The DMS serializes itsdatatomodules/<moduleId>.json. - It writes entries into the
archive, for data too large to hold in memory or that is not JSON.
The types on this page live in @antelopejs/interface-dms/tenant-export. @antelopejs/interface-dms/hooks re-exports them, so either import path works. Hooks covers the hook registry this contract builds on.
Register a contributor
RegisterTenantDataExportContributor registers the handler with an explicit module id. That id namespaces the contributor's archive entries, so it no longer depends on the call site the runtime attributes the registration to. Prefer it to a bare RegisterHook.
import { RegisterTenantDataExportContributor } from "@antelopejs/interface-dms/hooks";
RegisterTenantDataExportContributor(
"acme-billing",
async (tenantId, archive, signal) => {
await archive.addStream(
"invoices.ndjson",
streamInvoices(tenantId, signal),
);
return {
moduleId: "acme-billing",
data: await collectBillingSettings(tenantId),
};
},
);
The helper's signature:
import type { Hook, HookHandler } from "@antelopejs/interface-dms/hooks";
declare function RegisterTenantDataExportContributor(
moduleId: string,
handler: HookHandler<Hook.TENANT_DATA_EXPORT>,
): void;
The handler's arguments are tenantId: string, archive: TenantExportArchive and signal: AbortSignal. It returns a TenantDataExportContribution, or nothing when it only writes archive entries.
interface TenantDataExportContribution {
moduleId: string;
data: unknown;
}
Module ids in the archive
The DMS derives each contributor's directory name from the module id its registration recorded:
- Every character other than a letter, a digit,
.,_or-becomes-. A registration under@acme/dms-billingwrites undermodules/-acme-dms-billing/. - A registration with no module id gets
module-<index>, its position among the contributors. - When two contributors resolve to the same name, the second gets the suffix
-2, the third-3, and so on.
The moduleId a contribution returns does not move its entries. When it differs from the resolved name, the manifest records it as declaredModuleId. Return the same id the registration uses, written in the allowed characters, to keep the two equal.
TenantExportArchive
import type { Readable } from "node:stream";
import type { JsonValue } from "@antelopejs/interface-dms/json";
interface TenantExportArchive {
addJson(path: string, data: JsonValue): Promise<void>;
addFile(path: string, localPath: string): Promise<void>;
addStream(path: string, stream: Readable): Promise<void>;
}
| Method | Adds |
|---|---|
addJson | data, a JSON value (JsonValue from @antelopejs/interface-dms/json), serialized as a JSON entry. |
addFile | An artifact already on disk, read from localPath. The call rejects when localPath does not exist. |
addStream | A remote or generated dump, without holding it in memory. |
Paths are relative. The DMS prefixes every entry with modules/<moduleId>/, so two contributors can both write invoices.ndjson without a collision. It drops empty, . and .. segments from the path, and rejects a path left with no segment.
Each method resolves only once the archiver has consumed the entry. Awaiting each call passes the archiver's back-pressure to whatever produces the data, and keeps memory flat at any export size. A call that is not awaited loses both.
The archive accepts writes only while the handler runs. A write after the handler has settled throws Tenant export archive for '<moduleId>' is no longer writable.
Abort signal
The signal aborts when an export job runs longer than its maxExecutionTimeMs, or when the caller of buildTenantExportArchive aborts the signal it passed. Pass it down to whatever produces your data, so an aborted export stops reading. An abort fails the whole export. The DMS does not write a partial archive for it.
Manifest
Every run writes a manifest.json at the archive root. It records what each contributor produced:
interface TenantExportManifest {
tenantId: string;
exportedAt: string;
partial: boolean;
modules: TenantExportModuleReport[];
}
interface TenantExportModuleReport {
moduleId: string;
declaredModuleId?: string;
entries: string[];
failed: boolean;
error?: string;
}
Contributors run one after another, in registration order. A contributor that throws does not fail the export, unless the export was aborted or the ZIP stream broke. Its report carries failed: true and the error message, keeps the entries it wrote before it failed, and partial is true on the manifest. A consumer reads partial to tell an incomplete archive from a complete one.
declaredModuleId is present when the returned contribution named another id than the one the archive namespaced the entries under. It is a sign that the registration and the return value disagree.
buildTenantExportArchive runs the contributors and writes the ZIP, and startTenantExportJob runs it as a background export job. Both are covered in Export a tenant's data.