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