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

Function makePluginStorageFacade

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

Source from the content-addressed store, hash-verified

900};
901
902const makePluginStorageFacade = (input: {
903 readonly core: CoreDb;
904 readonly pluginId: string;
905 readonly owner: OwnerBinding;
906}): PluginStorageFacade => {
907 // Owner partitions: org always, plus this subject's user partition.
908 const readOwners: readonly Owner[] = input.owner.subject == null ? ["org"] : ["user", "org"];
909
910 const ownerSubject = (owner: Owner): { owner: Owner; subject: string } | null => {
911 if (owner === "org") return { owner: "org", subject: ORG_SUBJECT };
912 if (input.owner.subject == null) return null;
913 return { owner: "user", subject: String(input.owner.subject) };
914 };
915
916 const tenant = String(input.owner.tenant);
917
918 const whereFor =
919 (collection: string, key?: string): CoreWhere =>
920 (b: AnyCb) =>
921 b.and(
922 b("plugin_id", "=", input.pluginId),
923 b("collection", "=", collection),
924 key === undefined ? true : b("key", "=", key),
925 );
926
927 const whereOwner = (owner: Owner, collection: string, key: string): CoreWhere => {
928 const os = ownerSubject(owner);
929 return (b: AnyCb) =>
930 b.and(
931 b("plugin_id", "=", input.pluginId),
932 b("collection", "=", collection),
933 b("key", "=", key),
934 b("owner", "=", owner),
935 b("subject", "=", os ? os.subject : ORG_SUBJECT),
936 );
937 };
938
939 const ownerRank = (owner: Owner): number => readOwners.indexOf(owner);
940
941 const sortByOwnerPrecedence = (rows: readonly CoreRow<"plugin_storage">[]) =>
942 [...rows].sort((left, right) => {
943 const l = ownerRank(left.owner as Owner);
944 const r = ownerRank(right.owner as Owner);
945 return l - r || left.key.localeCompare(right.key);
946 });
947
948 const getVisible = <T>(collection: string, key: string) =>
949 input.core.findMany("plugin_storage", { where: whereFor(collection, key) }).pipe(
950 Effect.map((rows) => sortByOwnerPrecedence(rows)[0] ?? null),
951 Effect.map((row) => (row ? pluginStorageEntryFromRow<T>(row) : null)),
952 );
953
954 const getForOwnerImpl = <T>(owner: Owner, collection: string, key: string) =>
955 input.core
956 .findFirst("plugin_storage", {
957 where: whereOwner(owner, collection, key),
958 })
959 .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