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

Function makePluginStorageFacade

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

Source from the content-addressed store, hash-verified

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