MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / makePluginStorageFacade

Function makePluginStorageFacade

packages/core/sdk/src/executor.ts:1507–1832  ·  view source on GitHub ↗
(input: {
  readonly core: CoreDb;
  readonly pluginId: string;
  readonly owner: OwnerBinding;
})

Source from the content-addressed store, hash-verified

1505};
1506
1507const makePluginStorageFacade = (input: {
1508 readonly core: CoreDb;
1509 readonly pluginId: string;
1510 readonly owner: OwnerBinding;
1511}): PluginStorageFacade => {
1512 // Owner partitions: org always, plus this subject's user partition.
1513 const readOwners: readonly Owner[] = input.owner.subject == null ? ["org"] : ["user", "org"];
1514
1515 const ownerSubject = (owner: Owner): { owner: Owner; subject: string } | null => {
1516 if (owner === "org") return { owner: "org", subject: ORG_SUBJECT };
1517 if (input.owner.subject == null) return null;
1518 return { owner: "user", subject: String(input.owner.subject) };
1519 };
1520
1521 const tenant = String(input.owner.tenant);
1522
1523 const whereFor =
1524 (collection: string, key?: string): CoreWhere =>
1525 (b: AnyCb) =>
1526 b.and(
1527 b("plugin_id", "=", input.pluginId),
1528 b("collection", "=", collection),
1529 key === undefined ? true : b("key", "=", key),
1530 );
1531
1532 const whereOwner = (owner: Owner, collection: string, key: string): CoreWhere => {
1533 const os = ownerSubject(owner);
1534 return (b: AnyCb) =>
1535 b.and(
1536 b("plugin_id", "=", input.pluginId),
1537 b("collection", "=", collection),
1538 b("key", "=", key),
1539 b("owner", "=", owner),
1540 b("subject", "=", os ? os.subject : ORG_SUBJECT),
1541 );
1542 };
1543
1544 const ownerRank = (owner: Owner): number => readOwners.indexOf(owner);
1545
1546 const sortByOwnerPrecedence = (rows: readonly CoreRow<"plugin_storage">[]) =>
1547 [...rows].sort((left, right) => {
1548 const l = ownerRank(left.owner as Owner);
1549 const r = ownerRank(right.owner as Owner);
1550 return l - r || left.key.localeCompare(right.key);
1551 });
1552
1553 const getVisible = <T>(collection: string, key: string) =>
1554 input.core.findMany("plugin_storage", { where: whereFor(collection, key) }).pipe(
1555 Effect.map((rows) => sortByOwnerPrecedence(rows)[0] ?? null),
1556 Effect.map((row) => (row ? pluginStorageEntryFromRow<T>(row) : null)),
1557 );
1558
1559 const getForOwnerImpl = <T>(owner: Owner, collection: string, key: string) =>
1560 input.core
1561 .findFirst("plugin_storage", {
1562 where: whereOwner(owner, collection, key),
1563 })
1564 .pipe(Effect.map((row) => (row ? pluginStorageEntryFromRow<T>(row) : null)));

Callers 1

createExecutorFunction · 0.85

Calls 10

getVisibleFunction · 0.85
getForOwnerImplFunction · 0.85
queryCollectionFunction · 0.85
putImplFunction · 0.85
removeImplFunction · 0.85
whereForFunction · 0.85
sortByOwnerPrecedenceFunction · 0.85
putManyImplFunction · 0.85
removeManyImplFunction · 0.85

Tested by

no test coverage detected