Resolve TS store document aliases by payload
This commit is contained in:
parent
dcf5f73003
commit
12f819bffe
2 changed files with 108 additions and 1 deletions
|
|
@ -6,6 +6,7 @@ import {
|
|||
SingleFileMessagePackBackingStore,
|
||||
type AnyCultCacheDocumentDefinition,
|
||||
type CacheBackingStore,
|
||||
type CultCacheEnvelope,
|
||||
type CultCacheDocumentValue,
|
||||
} from "cultcache-ts";
|
||||
import {
|
||||
|
|
@ -2425,7 +2426,7 @@ export function cultMeshDocumentFromStore(
|
|||
const documentId = options.documentId ?? resolvedSchemaId;
|
||||
const read = async () => {
|
||||
const records = await store.pullAll();
|
||||
const record = records.find(candidate => candidate.schemaId === resolvedSchemaId);
|
||||
const record = resolveCultMeshStoreDocumentRecord(records, schema, schemaOrDefinition);
|
||||
if (!record) {
|
||||
throw new Error(`CultMesh store document '${documentId}' did not contain schema ${resolvedSchemaId}.`);
|
||||
}
|
||||
|
|
@ -5681,6 +5682,59 @@ function cultMeshSchemasAreCompatible(
|
|||
return Boolean(left.type && right.type && left.type === right.type);
|
||||
}
|
||||
|
||||
function resolveCultMeshStoreDocumentRecord(
|
||||
records: readonly CultCacheEnvelope[],
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
schemaOrDefinition: string | AnyCultCacheDocumentDefinition,
|
||||
): CultCacheEnvelope | undefined {
|
||||
const schemaId = schema.schemaId ?? schema.type;
|
||||
return (
|
||||
records.find(candidate => schemaId !== undefined && candidate.schemaId === schemaId) ??
|
||||
records.find(candidate =>
|
||||
cultMeshEnvelopeMatchesSchema(candidate, schema) &&
|
||||
(typeof schemaOrDefinition === "string" ||
|
||||
cultMeshEnvelopeParsesAsDefinition(candidate, schemaOrDefinition))) ??
|
||||
(typeof schemaOrDefinition === "string"
|
||||
? undefined
|
||||
: records.find(candidate => cultMeshEnvelopeParsesAsDefinition(candidate, schemaOrDefinition)))
|
||||
);
|
||||
}
|
||||
|
||||
function cultMeshEnvelopeMatchesSchema(
|
||||
envelope: CultCacheEnvelope,
|
||||
schema: CultMeshDocumentSchemaDescriptor,
|
||||
): boolean {
|
||||
const entry = envelope.catalogEntry;
|
||||
if (!entry) {
|
||||
return false;
|
||||
}
|
||||
if (
|
||||
schema.schemaId &&
|
||||
(entry.schemaId === schema.schemaId ||
|
||||
envelope.schemaId === schema.schemaId ||
|
||||
entry.compatibleSchemaIds?.includes(schema.schemaId))
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
return Boolean(
|
||||
schema.schemaName &&
|
||||
entry.schemaName === schema.schemaName &&
|
||||
(schema.schemaVersion ?? "") === (entry.schemaVersion ?? ""),
|
||||
);
|
||||
}
|
||||
|
||||
function cultMeshEnvelopeParsesAsDefinition(
|
||||
envelope: CultCacheEnvelope,
|
||||
definition: AnyCultCacheDocumentDefinition,
|
||||
): boolean {
|
||||
try {
|
||||
definition.schema.parse(decode(envelope.payload));
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function cultMeshSchemaLabel(schema: CultMeshDocumentSchemaDescriptor): string {
|
||||
return schema.schemaId ??
|
||||
cultMeshSchemaNameVersionKey(schema) ??
|
||||
|
|
|
|||
|
|
@ -220,6 +220,59 @@ test("CultMesh TS document handles submit predictions through configured authori
|
|||
);
|
||||
});
|
||||
|
||||
test("CultMesh TS store document handles choose compatible foreign schema payloads", async () => {
|
||||
const store = {
|
||||
async pullAll() {
|
||||
return [
|
||||
{
|
||||
key: "note:foreign",
|
||||
type: "runtime.generated.incompatible-note",
|
||||
schemaId: "runtime.generated.incompatible-note.v1",
|
||||
storedAt: "2026-06-27T00:00:00Z",
|
||||
payload: encode({ nope: "not the requested shape" }),
|
||||
catalogEntry: {
|
||||
schemaId: "runtime.generated.incompatible-note.v1",
|
||||
schemaName: "runtime.generated.incompatible-note",
|
||||
schemaVersion: "runtime.generated.incompatible-note.v1",
|
||||
contentHash: "runtime.generated.incompatible-note.v1",
|
||||
canonicalSchemaJson: "{}",
|
||||
compatibleSchemaIds: ["runtime.generated.incompatible-note.v1"],
|
||||
},
|
||||
},
|
||||
{
|
||||
key: "note:foreign",
|
||||
type: "runtime.generated.cultmesh.note",
|
||||
schemaId: "runtime.generated.cultmesh.note.ui.42",
|
||||
storedAt: "2026-06-27T00:00:01Z",
|
||||
payload: encode({
|
||||
noteId: "note:foreign",
|
||||
body: "foreign schema id, local store shape",
|
||||
}),
|
||||
catalogEntry: {
|
||||
schemaId: "runtime.generated.cultmesh.note.ui.42",
|
||||
schemaName: "cultmesh.note",
|
||||
schemaVersion: "cultmesh.note.v1",
|
||||
contentHash: "runtime.generated.cultmesh.note.ui.42",
|
||||
canonicalSchemaJson: "{}",
|
||||
compatibleSchemaIds: ["runtime.generated.cultmesh.note.ui.42"],
|
||||
},
|
||||
},
|
||||
];
|
||||
},
|
||||
async push() {},
|
||||
async delete() {},
|
||||
};
|
||||
|
||||
const document = CultMesh.documentFromStore(store, noteAliasDocument, {
|
||||
documentId: "note:foreign.store",
|
||||
});
|
||||
|
||||
assert.deepEqual(await document.latest(), {
|
||||
noteId: "note:foreign",
|
||||
body: "foreign schema id, local store shape",
|
||||
});
|
||||
});
|
||||
|
||||
test("CultMesh TS reactive documents submit predictions from member writes", async () => {
|
||||
let current = {
|
||||
noteId: "note:reactive",
|
||||
|
|
|
|||
Loading…
Reference in a new issue