CultMesh TS: a catalog addresses each document by its id

Soul F7 (P9): a catalog holding two documents of one type answered a typed
lookup with the last one added, and nothing else could name the other. Two
documents of one type under different keys stay legal.

- CultMeshDocumentCatalog.tryDocumentById and documentById look a handle up
  by its document id, the key the catalog already holds handles under
  (it defaults to the record key).
- The publication-catalog test addresses both synced documents by id
  instead of documents[0] and documents[1].

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-30 19:49:57 +02:00
commit 091477d066
2 changed files with 25 additions and 9 deletions

View file

@ -1491,6 +1491,20 @@ export class CultMeshDocumentCatalog {
return this;
}
// A catalog may hold several documents of one type, one per document id. A lookup by schema answers with one of them;
// a lookup by document id addresses each.
public tryDocumentById<TDocument = unknown>(documentId: string): CultMeshDocumentHandle<TDocument> | undefined {
return this.#byDocumentId.get(documentId);
}
public documentById<TDocument = unknown>(documentId: string): CultMeshDocumentHandle<TDocument> {
const document = this.tryDocumentById<TDocument>(documentId);
if (!document) {
throw new Error(`Document catalog has no document with id '${documentId}'.`);
}
return document;
}
public tryDocument<TDocument = never, TSchema extends CultMeshDocumentSchemaDescriptor = CultMeshDocumentSchemaDescriptor>(
schema: TSchema,
): CultMeshDocumentHandle<CultMeshCatalogValue<TDocument, TSchema>> | undefined {

View file

@ -1146,19 +1146,21 @@ test("CultMesh TS syncs configured publication catalogs into a local node", asyn
bindings,
);
// Two documents of one type, each addressed by its own id whatever order the catalog lists them in.
assert.deepEqual(
catalog.documents.map(document => document.documentId),
catalog.documents.map(document => document.documentId).sort(),
["local:first", "local:second"],
);
const [firstSynced, secondSynced] = catalog.documents;
assert.equal((await firstSynced!.latest()).body, "first synced publication");
assert.equal((await secondSynced!.latest()).body, "second synced publication");
assert.equal(secondSynced!.routeHint.description, "publication sync catalog");
const secondSynced = catalog.documentById<Note>("local:second");
assert.equal((await catalog.documentById<Note>("local:first").latest()).body, "first synced publication");
assert.equal((await secondSynced.latest()).body, "second synced publication");
assert.equal(secondSynced.documentId, "local:second");
assert.equal(secondSynced.routeHint.description, "publication sync catalog");
assert.equal(target.getRequired(noteDocument, "note:second-sync").body, "second synced publication");
assert.equal(
(await facadeCatalog.documents[1]!.latest()).body,
"second synced publication",
);
assert.equal((await facadeCatalog.documentById<Note>("local:second").latest()).body, "second synced publication");
assert.equal((await facadeCatalog.documentById<Note>("local:first").latest()).body, "first synced publication");
assert.equal(catalog.tryDocumentById("note:second-sync"), undefined);
assert.throws(() => catalog.documentById("local:third"), /no document with id 'local:third'/);
});
test("CultMesh TS document catalogs resolve semantic schema versions passed as schema ids", async () => {