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

Function makePluginStorageFacade

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

Source from the content-addressed store, hash-verified

1300};
1301
1302const makePluginStorageFacade = (input: {
1303 readonly core: CoreDb;
1304 readonly pluginId: string;
1305 readonly owner: OwnerBinding;
1306}): PluginStorageFacade => {
1307 // Owner partitions: org always, plus this subject's user partition.
1308 const readOwners: readonly Owner[] = input.owner.subject == null ? ["org"] : ["user", "org"];
1309
1310 const ownerSubject = (owner: Owner): { owner: Owner; subject: string } | null => {
1311 if (owner === "org") return { owner: "org", subject: ORG_SUBJECT };
1312 if (input.owner.subject == null) return null;
1313 return { owner: "user", subject: String(input.owner.subject) };
1314 };
1315
1316 const tenant = String(input.owner.tenant);
1317
1318 const whereFor =
1319 (collection: string, key?: string): CoreWhere =>
1320 (b: AnyCb) =>
1321 b.and(
1322 b("plugin_id", "=", input.pluginId),
1323 b("collection", "=", collection),
1324 key === undefined ? true : b("key", "=", key),
1325 );
1326
1327 const whereOwner = (owner: Owner, collection: string, key: string): CoreWhere => {
1328 const os = ownerSubject(owner);
1329 return (b: AnyCb) =>
1330 b.and(
1331 b("plugin_id", "=", input.pluginId),
1332 b("collection", "=", collection),
1333 b("key", "=", key),
1334 b("owner", "=", owner),
1335 b("subject", "=", os ? os.subject : ORG_SUBJECT),
1336 );
1337 };
1338
1339 const ownerRank = (owner: Owner): number => readOwners.indexOf(owner);
1340
1341 const sortByOwnerPrecedence = (rows: readonly CoreRow<"plugin_storage">[]) =>
1342 [...rows].sort((left, right) => {
1343 const l = ownerRank(left.owner as Owner);
1344 const r = ownerRank(right.owner as Owner);
1345 return l - r || left.key.localeCompare(right.key);
1346 });
1347
1348 const getVisible = <T>(collection: string, key: string) =>
1349 input.core.findMany("plugin_storage", { where: whereFor(collection, key) }).pipe(
1350 Effect.map((rows) => sortByOwnerPrecedence(rows)[0] ?? null),
1351 Effect.map((row) => (row ? pluginStorageEntryFromRow<T>(row) : null)),
1352 );
1353
1354 const getForOwnerImpl = <T>(owner: Owner, collection: string, key: string) =>
1355 input.core
1356 .findFirst("plugin_storage", {
1357 where: whereOwner(owner, collection, key),
1358 })
1359 .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