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

Function makePluginStorageFacade

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

Source from the content-addressed store, hash-verified

1177};
1178
1179const makePluginStorageFacade = (input: {
1180 readonly core: CoreDb;
1181 readonly pluginId: string;
1182 readonly owner: OwnerBinding;
1183}): PluginStorageFacade => {
1184 // Owner partitions: org always, plus this subject's user partition.
1185 const readOwners: readonly Owner[] = input.owner.subject == null ? ["org"] : ["user", "org"];
1186
1187 const ownerSubject = (owner: Owner): { owner: Owner; subject: string } | null => {
1188 if (owner === "org") return { owner: "org", subject: ORG_SUBJECT };
1189 if (input.owner.subject == null) return null;
1190 return { owner: "user", subject: String(input.owner.subject) };
1191 };
1192
1193 const tenant = String(input.owner.tenant);
1194
1195 const whereFor =
1196 (collection: string, key?: string): CoreWhere =>
1197 (b: AnyCb) =>
1198 b.and(
1199 b("plugin_id", "=", input.pluginId),
1200 b("collection", "=", collection),
1201 key === undefined ? true : b("key", "=", key),
1202 );
1203
1204 const whereOwner = (owner: Owner, collection: string, key: string): CoreWhere => {
1205 const os = ownerSubject(owner);
1206 return (b: AnyCb) =>
1207 b.and(
1208 b("plugin_id", "=", input.pluginId),
1209 b("collection", "=", collection),
1210 b("key", "=", key),
1211 b("owner", "=", owner),
1212 b("subject", "=", os ? os.subject : ORG_SUBJECT),
1213 );
1214 };
1215
1216 const ownerRank = (owner: Owner): number => readOwners.indexOf(owner);
1217
1218 const sortByOwnerPrecedence = (rows: readonly CoreRow<"plugin_storage">[]) =>
1219 [...rows].sort((left, right) => {
1220 const l = ownerRank(left.owner as Owner);
1221 const r = ownerRank(right.owner as Owner);
1222 return l - r || left.key.localeCompare(right.key);
1223 });
1224
1225 const getVisible = <T>(collection: string, key: string) =>
1226 input.core.findMany("plugin_storage", { where: whereFor(collection, key) }).pipe(
1227 Effect.map((rows) => sortByOwnerPrecedence(rows)[0] ?? null),
1228 Effect.map((row) => (row ? pluginStorageEntryFromRow<T>(row) : null)),
1229 );
1230
1231 const getForOwnerImpl = <T>(owner: Owner, collection: string, key: string) =>
1232 input.core
1233 .findFirst("plugin_storage", {
1234 where: whereOwner(owner, collection, key),
1235 })
1236 .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