Add CultMesh TS publication document catalogs
This commit is contained in:
parent
07d4c4164a
commit
4189ee0811
2 changed files with 171 additions and 0 deletions
|
|
@ -82,6 +82,18 @@ export type CultMeshDocumentPublicationSource =
|
|||
readonly endpoint?: string;
|
||||
};
|
||||
|
||||
export interface CultMeshPublicationDocumentBinding {
|
||||
readonly schema: string | AnyCultCacheDocumentDefinition;
|
||||
readonly recordKey: string;
|
||||
readonly documentId?: string;
|
||||
readonly sourceId?: string;
|
||||
readonly source?: CultMeshDocumentPublicationSource;
|
||||
}
|
||||
|
||||
export type CultMeshDocumentPublicationSourceResolver =
|
||||
| CultMeshDocumentPublicationSource
|
||||
| ((binding: CultMeshPublicationDocumentBinding) => CultMeshDocumentPublicationSource);
|
||||
|
||||
export interface CultMeshAuthorityClaim {
|
||||
readonly role: string;
|
||||
readonly shardId?: string;
|
||||
|
|
@ -2275,6 +2287,80 @@ export function cultMeshDocuments(
|
|||
return new CultMeshDocumentCatalog(documents);
|
||||
}
|
||||
|
||||
export function cultMeshPublicationDocument(
|
||||
schema: string | AnyCultCacheDocumentDefinition,
|
||||
recordKey: string,
|
||||
options: {
|
||||
documentId?: string;
|
||||
sourceId?: string;
|
||||
source?: CultMeshDocumentPublicationSource;
|
||||
} = {},
|
||||
): CultMeshPublicationDocumentBinding {
|
||||
requireNonEmpty(recordKey, "recordKey");
|
||||
return {
|
||||
schema,
|
||||
recordKey,
|
||||
documentId: options.documentId,
|
||||
sourceId: options.sourceId,
|
||||
source: options.source,
|
||||
};
|
||||
}
|
||||
|
||||
export function cultMeshDocumentsFromPublication(
|
||||
source: CultMeshDocumentPublicationSourceResolver,
|
||||
bindings: readonly CultMeshPublicationDocumentBinding[],
|
||||
options: {
|
||||
routeHint?: CultMeshRouteHint;
|
||||
timeoutMs?: number;
|
||||
pollMs?: number;
|
||||
messageIdPrefix?: string;
|
||||
} = {},
|
||||
): CultMeshDocumentCatalog {
|
||||
if (!source) {
|
||||
throw new Error("CultMesh publication source is required.");
|
||||
}
|
||||
if (!bindings) {
|
||||
throw new Error("CultMesh publication bindings are required.");
|
||||
}
|
||||
|
||||
return cultMeshDocuments(
|
||||
...bindings.map(binding => {
|
||||
if (!binding) {
|
||||
throw new Error("CultMesh publication binding is required.");
|
||||
}
|
||||
const resolvedSource = binding.source ??
|
||||
(typeof source === "function" ? source(binding) : source);
|
||||
return typeof binding.schema === "string"
|
||||
? cultMeshDocumentFromPublication(
|
||||
resolvedSource,
|
||||
binding.schema,
|
||||
binding.recordKey,
|
||||
{
|
||||
documentId: binding.documentId ?? binding.recordKey,
|
||||
sourceId: binding.sourceId ?? binding.documentId ?? binding.recordKey,
|
||||
routeHint: options.routeHint,
|
||||
timeoutMs: options.timeoutMs,
|
||||
pollMs: options.pollMs,
|
||||
messageIdPrefix: options.messageIdPrefix,
|
||||
},
|
||||
)
|
||||
: cultMeshDocumentFromPublication(
|
||||
resolvedSource,
|
||||
binding.schema,
|
||||
binding.recordKey,
|
||||
{
|
||||
documentId: binding.documentId ?? binding.recordKey,
|
||||
sourceId: binding.sourceId ?? binding.documentId ?? binding.recordKey,
|
||||
routeHint: options.routeHint,
|
||||
timeoutMs: options.timeoutMs,
|
||||
pollMs: options.pollMs,
|
||||
messageIdPrefix: options.messageIdPrefix,
|
||||
},
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function cultMeshCollection<TDocument>(
|
||||
collectionId: string,
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
|
|
@ -4098,6 +4184,31 @@ export class CultMesh {
|
|||
return cultMeshDocuments(...documents);
|
||||
}
|
||||
|
||||
public static publicationDocument(
|
||||
schema: string | AnyCultCacheDocumentDefinition,
|
||||
recordKey: string,
|
||||
options: {
|
||||
documentId?: string;
|
||||
sourceId?: string;
|
||||
source?: CultMeshDocumentPublicationSource;
|
||||
} = {},
|
||||
): CultMeshPublicationDocumentBinding {
|
||||
return cultMeshPublicationDocument(schema, recordKey, options);
|
||||
}
|
||||
|
||||
public static documentsFromPublication(
|
||||
source: CultMeshDocumentPublicationSourceResolver,
|
||||
bindings: readonly CultMeshPublicationDocumentBinding[],
|
||||
options: {
|
||||
routeHint?: CultMeshRouteHint;
|
||||
timeoutMs?: number;
|
||||
pollMs?: number;
|
||||
messageIdPrefix?: string;
|
||||
} = {},
|
||||
): CultMeshDocumentCatalog {
|
||||
return cultMeshDocumentsFromPublication(source, bindings, options);
|
||||
}
|
||||
|
||||
public static bindDocument<TDocument>(
|
||||
verse: CultMeshVerseContext | CultMeshVerse,
|
||||
document: CultMeshDocumentHandle<TDocument>,
|
||||
|
|
|
|||
|
|
@ -232,6 +232,66 @@ test("CultMesh TS document handles read schema publications from single-file sto
|
|||
]);
|
||||
});
|
||||
|
||||
test("CultMesh TS binds publication document catalogs from source resolvers", async () => {
|
||||
const root = await mkdtemp(join(tmpdir(), "cultmesh-ts-publication-catalog-"));
|
||||
const firstPath = join(root, "first.ccmp");
|
||||
const secondPath = join(root, "second.ccmp");
|
||||
const first = await CultMesh.startNode(firstPath, {
|
||||
documents: [noteDocument],
|
||||
});
|
||||
const second = await CultMesh.startNode(secondPath, {
|
||||
documents: [noteDocument],
|
||||
});
|
||||
await first.put(noteDocument, "note:first", {
|
||||
noteId: "note:first",
|
||||
body: "first source",
|
||||
});
|
||||
await second.put(noteDocument, "note:second", {
|
||||
noteId: "note:second",
|
||||
body: "second source",
|
||||
});
|
||||
await first.flush();
|
||||
await second.flush();
|
||||
|
||||
const bindings = [
|
||||
CultMesh.publicationDocument(noteDocument, "note:first", {
|
||||
documentId: "daemon:first",
|
||||
sourceId: "daemon:first.latest",
|
||||
}),
|
||||
CultMesh.publicationDocument(noteDocument, "note:second", {
|
||||
documentId: "daemon:second",
|
||||
sourceId: "daemon:second.latest",
|
||||
}),
|
||||
];
|
||||
const paths = new Map([
|
||||
["note:first", firstPath],
|
||||
["note:second", secondPath],
|
||||
]);
|
||||
const catalog = CultMesh.documentsFromPublication(
|
||||
binding => ({
|
||||
kind: "single-file",
|
||||
path: paths.get(binding.recordKey) ?? firstPath,
|
||||
}),
|
||||
bindings,
|
||||
{
|
||||
routeHint: CultMesh.routeHint("shared-memory", "publication catalog"),
|
||||
pollMs: 50,
|
||||
},
|
||||
);
|
||||
|
||||
assert.deepEqual(
|
||||
catalog.documents.map(document => document.documentId),
|
||||
["daemon:first", "daemon:second"],
|
||||
);
|
||||
assert.equal(
|
||||
(await catalog.document(noteAliasDocument, {
|
||||
parse: value => noteAliasDocument.schema.parse(value),
|
||||
}).latest()).body,
|
||||
"second source",
|
||||
);
|
||||
assert.equal(catalog.document(noteDocument).routeHint.description, "publication catalog");
|
||||
});
|
||||
|
||||
test("CultMesh TS collection handles expose typed snapshots and reset watches", async () => {
|
||||
const filePath = join(await mkdtemp(join(tmpdir(), "cultmesh-ts-coll-")), "node.ccmp");
|
||||
const node = await CultMesh.startNode(filePath, {
|
||||
|
|
|
|||
Loading…
Reference in a new issue