Add TS typed publication node sync
This commit is contained in:
parent
719ea7950a
commit
7a335b1a11
3 changed files with 103 additions and 0 deletions
|
|
@ -364,6 +364,10 @@ When the caller wants source selection to be configuration, use
|
|||
`CultMesh.documentFromPublication(...)` with a `single-file`, `store`, or
|
||||
`peer-snapshot` source; it returns the same typed handle while hiding whether
|
||||
sync came from local persistence or a remote CultNet snapshot.
|
||||
Use `node.syncDocumentFromPublication(...)` or
|
||||
`CultMesh.syncDocumentFromPublication(...)` for the same source-agnostic shape
|
||||
when the publication should hydrate a local node before the caller continues
|
||||
through `node.document(...)` or `node.reactiveDocument(...)`.
|
||||
|
||||
## Streaming Mode
|
||||
|
||||
|
|
|
|||
|
|
@ -2756,6 +2756,37 @@ export function cultMeshDocumentFromPublication(
|
|||
}
|
||||
}
|
||||
|
||||
export async function cultMeshSyncDocumentFromPublication<TDefinition extends AnyCultCacheDocumentDefinition>(
|
||||
node: CultMeshNode,
|
||||
source: CultMeshDocumentPublicationSource,
|
||||
definition: TDefinition,
|
||||
recordKey: string,
|
||||
options: {
|
||||
timeoutMs?: number;
|
||||
messageIdPrefix?: string;
|
||||
} = {},
|
||||
): Promise<CultCacheDocumentValue<TDefinition>> {
|
||||
if (!source) {
|
||||
throw new Error("CultMesh publication source is required.");
|
||||
}
|
||||
requireNonEmpty(recordKey, "recordKey");
|
||||
|
||||
if (source.kind === "peer-snapshot") {
|
||||
return node.syncDocumentFromPeerSnapshot(source.peer, definition, recordKey, options);
|
||||
}
|
||||
|
||||
const value = await cultMeshDocumentFromPublication(
|
||||
source,
|
||||
definition,
|
||||
recordKey,
|
||||
{
|
||||
timeoutMs: options.timeoutMs,
|
||||
messageIdPrefix: options.messageIdPrefix,
|
||||
},
|
||||
).latest();
|
||||
return node.put(definition, recordKey, value);
|
||||
}
|
||||
|
||||
export function cultMeshGlobalDocumentFromCache<TDefinition extends AnyCultCacheDocumentDefinition>(
|
||||
cache: CultCache,
|
||||
definition: TDefinition,
|
||||
|
|
@ -3855,6 +3886,18 @@ export class CultMeshNode {
|
|||
return this.parseDocumentValue(definition, this.cache.getRequired(registered, key));
|
||||
}
|
||||
|
||||
public syncDocumentFromPublication<TDefinition extends AnyCultCacheDocumentDefinition>(
|
||||
source: CultMeshDocumentPublicationSource,
|
||||
definition: TDefinition,
|
||||
key: string,
|
||||
options: {
|
||||
timeoutMs?: number;
|
||||
messageIdPrefix?: string;
|
||||
} = {},
|
||||
): Promise<CultCacheDocumentValue<TDefinition>> {
|
||||
return cultMeshSyncDocumentFromPublication(this, source, definition, key, options);
|
||||
}
|
||||
|
||||
public globalDocument<TDefinition extends AnyCultCacheDocumentDefinition>(
|
||||
definition: TDefinition,
|
||||
options: {
|
||||
|
|
@ -4845,6 +4888,19 @@ export class CultMesh {
|
|||
return node.syncDocumentFromPeerSnapshot(peer, definition, key, options);
|
||||
}
|
||||
|
||||
public static syncDocumentFromPublication<TDefinition extends AnyCultCacheDocumentDefinition>(
|
||||
node: CultMeshNode,
|
||||
source: CultMeshDocumentPublicationSource,
|
||||
definition: TDefinition,
|
||||
key: string,
|
||||
options: {
|
||||
timeoutMs?: number;
|
||||
messageIdPrefix?: string;
|
||||
} = {},
|
||||
): Promise<CultCacheDocumentValue<TDefinition>> {
|
||||
return node.syncDocumentFromPublication(source, definition, key, options);
|
||||
}
|
||||
|
||||
public static documents(
|
||||
...documents: readonly CultMeshDocumentHandle<any>[]
|
||||
): CultMeshDocumentCatalog {
|
||||
|
|
|
|||
|
|
@ -679,6 +679,49 @@ test("CultMesh TS binds publication document catalogs from source resolvers", as
|
|||
assert.equal(catalog.document(noteDocument).routeHint.description, "publication catalog");
|
||||
});
|
||||
|
||||
test("CultMesh TS syncs configured publications into local node aliases", async () => {
|
||||
const sourcePath = join(await mkdtemp(join(tmpdir(), "cultmesh-ts-publication-sync-source-")), "source.ccmp");
|
||||
const targetPath = join(await mkdtemp(join(tmpdir(), "cultmesh-ts-publication-sync-target-")), "target.ccmp");
|
||||
const source = await CultMesh.startNode(sourcePath, {
|
||||
documents: [noteDocument],
|
||||
});
|
||||
await source.put(noteDocument, "note:published", {
|
||||
noteId: "note:published",
|
||||
body: "publication source hydrates local alias",
|
||||
});
|
||||
await source.flush();
|
||||
const target = await CultMesh.startNode(targetPath, {
|
||||
documents: [noteDocument],
|
||||
});
|
||||
|
||||
const synced = await target.syncDocumentFromPublication(
|
||||
{
|
||||
kind: "single-file",
|
||||
path: sourcePath,
|
||||
},
|
||||
noteAliasDocument,
|
||||
"note:published",
|
||||
);
|
||||
const facadeSynced = await CultMesh.syncDocumentFromPublication(
|
||||
target,
|
||||
{
|
||||
kind: "single-file",
|
||||
path: sourcePath,
|
||||
},
|
||||
noteAliasDocument,
|
||||
"note:published",
|
||||
);
|
||||
|
||||
assert.deepEqual(synced, {
|
||||
noteId: "note:published",
|
||||
body: "publication source hydrates local alias",
|
||||
});
|
||||
assert.deepEqual(facadeSynced, synced);
|
||||
assert.equal(target.getRequired(noteDocument, "note:published").body, "publication source hydrates local alias");
|
||||
assert.equal(target.getRequired(noteAliasDocument, "note:published").body, "publication source hydrates local alias");
|
||||
assert.equal((await target.reactiveDocument(noteAliasDocument, "note:published", { watch: false }).ready).body, "publication source hydrates local alias");
|
||||
});
|
||||
|
||||
test("CultMesh TS document catalogs resolve semantic schema versions passed as schema ids", async () => {
|
||||
const current = {
|
||||
noteId: "note:semantic",
|
||||
|
|
|
|||
Loading…
Reference in a new issue