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

Function makePluginStorageFacade

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

Source from the content-addressed store, hash-verified

1125};
1126
1127const makePluginStorageFacade = (input: {
1128 readonly core: CoreDb;
1129 readonly pluginId: string;
1130 readonly owner: OwnerBinding;
1131}): PluginStorageFacade => {
1132 // Owner partitions: org always, plus this subject's user partition.
1133 const readOwners: readonly Owner[] = input.owner.subject == null ? ["org"] : ["user", "org"];
1134
1135 const ownerSubject = (owner: Owner): { owner: Owner; subject: string } | null => {
1136 if (owner === "org") return { owner: "org", subject: ORG_SUBJECT };
1137 if (input.owner.subject == null) return null;
1138 return { owner: "user", subject: String(input.owner.subject) };
1139 };
1140
1141 const tenant = String(input.owner.tenant);
1142
1143 const whereFor =
1144 (collection: string, key?: string): CoreWhere =>
1145 (b: AnyCb) =>
1146 b.and(
1147 b("plugin_id", "=", input.pluginId),
1148 b("collection", "=", collection),
1149 key === undefined ? true : b("key", "=", key),
1150 );
1151
1152 const whereOwner = (owner: Owner, collection: string, key: string): CoreWhere => {
1153 const os = ownerSubject(owner);
1154 return (b: AnyCb) =>
1155 b.and(
1156 b("plugin_id", "=", input.pluginId),
1157 b("collection", "=", collection),
1158 b("key", "=", key),
1159 b("owner", "=", owner),
1160 b("subject", "=", os ? os.subject : ORG_SUBJECT),
1161 );
1162 };
1163
1164 const ownerRank = (owner: Owner): number => readOwners.indexOf(owner);
1165
1166 const sortByOwnerPrecedence = (rows: readonly CoreRow<"plugin_storage">[]) =>
1167 [...rows].sort((left, right) => {
1168 const l = ownerRank(left.owner as Owner);
1169 const r = ownerRank(right.owner as Owner);
1170 return l - r || left.key.localeCompare(right.key);
1171 });
1172
1173 const getVisible = <T>(collection: string, key: string) =>
1174 input.core.findMany("plugin_storage", { where: whereFor(collection, key) }).pipe(
1175 Effect.map((rows) => sortByOwnerPrecedence(rows)[0] ?? null),
1176 Effect.map((row) => (row ? pluginStorageEntryFromRow<T>(row) : null)),
1177 );
1178
1179 const getForOwnerImpl = <T>(owner: Owner, collection: string, key: string) =>
1180 input.core
1181 .findFirst("plugin_storage", {
1182 where: whereOwner(owner, collection, key),
1183 })
1184 .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