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

Function makePluginStorageFacade

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

Source from the content-addressed store, hash-verified

1486};
1487
1488const makePluginStorageFacade = (input: {
1489 readonly core: CoreDb;
1490 readonly pluginId: string;
1491 readonly owner: OwnerBinding;
1492}): PluginStorageFacade => {
1493 // Owner partitions: org always, plus this subject's user partition.
1494 const readOwners: readonly Owner[] = input.owner.subject == null ? ["org"] : ["user", "org"];
1495
1496 const ownerSubject = (owner: Owner): { owner: Owner; subject: string } | null => {
1497 if (owner === "org") return { owner: "org", subject: ORG_SUBJECT };
1498 if (input.owner.subject == null) return null;
1499 return { owner: "user", subject: String(input.owner.subject) };
1500 };
1501
1502 const tenant = String(input.owner.tenant);
1503
1504 const whereFor =
1505 (collection: string, key?: string): CoreWhere =>
1506 (b: AnyCb) =>
1507 b.and(
1508 b("plugin_id", "=", input.pluginId),
1509 b("collection", "=", collection),
1510 key === undefined ? true : b("key", "=", key),
1511 );
1512
1513 const whereOwner = (owner: Owner, collection: string, key: string): CoreWhere => {
1514 const os = ownerSubject(owner);
1515 return (b: AnyCb) =>
1516 b.and(
1517 b("plugin_id", "=", input.pluginId),
1518 b("collection", "=", collection),
1519 b("key", "=", key),
1520 b("owner", "=", owner),
1521 b("subject", "=", os ? os.subject : ORG_SUBJECT),
1522 );
1523 };
1524
1525 const ownerRank = (owner: Owner): number => readOwners.indexOf(owner);
1526
1527 const sortByOwnerPrecedence = (rows: readonly CoreRow<"plugin_storage">[]) =>
1528 [...rows].sort((left, right) => {
1529 const l = ownerRank(left.owner as Owner);
1530 const r = ownerRank(right.owner as Owner);
1531 return l - r || left.key.localeCompare(right.key);
1532 });
1533
1534 const getVisible = <T>(collection: string, key: string) =>
1535 input.core.findMany("plugin_storage", { where: whereFor(collection, key) }).pipe(
1536 Effect.map((rows) => sortByOwnerPrecedence(rows)[0] ?? null),
1537 Effect.map((row) => (row ? pluginStorageEntryFromRow<T>(row) : null)),
1538 );
1539
1540 const getForOwnerImpl = <T>(owner: Owner, collection: string, key: string) =>
1541 input.core
1542 .findFirst("plugin_storage", {
1543 where: whereOwner(owner, collection, key),
1544 })
1545 .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