|
| 1 | +import { |
| 2 | + EntityPrivacyPolicy, |
| 3 | + EntityQueryContext, |
| 4 | + IEntityClass, |
| 5 | + ReadonlyEntity, |
| 6 | + ViewerContext, |
| 7 | +} from '@expo/entity'; |
| 8 | + |
| 9 | +import { AuthorizationResultBasedKnexEntityLoader } from './AuthorizationResultBasedKnexEntityLoader'; |
| 10 | +import { EnforcingKnexEntityLoader } from './EnforcingKnexEntityLoader'; |
| 11 | +import { |
| 12 | + knexLoader as knexLoaderFn, |
| 13 | + knexLoaderWithAuthorizationResults as knexLoaderWithAuthorizationResultsFn, |
| 14 | +} from './knexLoader'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Abstract base class for readonly entities backed by Postgres. |
| 18 | + * Provides `knexLoader` and `knexLoaderWithAuthorizationResults` as inherited static methods. |
| 19 | + * |
| 20 | + * Entities that should not be mutated (e.g., representing SQL views or immutable tables) |
| 21 | + * can extend this class instead of `ReadonlyEntity` to get knex loader ergonomics. |
| 22 | + */ |
| 23 | +export abstract class ReadonlyPostgresEntity< |
| 24 | + TFields extends Record<string, any>, |
| 25 | + TIDField extends keyof NonNullable<Pick<TFields, TSelectedFields>>, |
| 26 | + TViewerContext extends ViewerContext, |
| 27 | + TSelectedFields extends keyof TFields = keyof TFields, |
| 28 | +> extends ReadonlyEntity<TFields, TIDField, TViewerContext, TSelectedFields> { |
| 29 | + /** |
| 30 | + * Vend knex loader for loading entities via non-data-loader methods in a given query context. |
| 31 | + * @param viewerContext - viewer context of loading user |
| 32 | + * @param queryContext - query context in which to perform the load |
| 33 | + */ |
| 34 | + static knexLoader< |
| 35 | + TMFields extends object, |
| 36 | + TMIDField extends keyof NonNullable<Pick<TMFields, TMSelectedFields>>, |
| 37 | + TMViewerContext extends ViewerContext, |
| 38 | + TMEntity extends ReadonlyEntity<TMFields, TMIDField, TMViewerContext, TMSelectedFields>, |
| 39 | + TMPrivacyPolicy extends EntityPrivacyPolicy< |
| 40 | + TMFields, |
| 41 | + TMIDField, |
| 42 | + TMViewerContext, |
| 43 | + TMEntity, |
| 44 | + TMSelectedFields |
| 45 | + >, |
| 46 | + TMSelectedFields extends keyof TMFields = keyof TMFields, |
| 47 | + >( |
| 48 | + this: IEntityClass< |
| 49 | + TMFields, |
| 50 | + TMIDField, |
| 51 | + TMViewerContext, |
| 52 | + TMEntity, |
| 53 | + TMPrivacyPolicy, |
| 54 | + TMSelectedFields |
| 55 | + >, |
| 56 | + viewerContext: TMViewerContext, |
| 57 | + queryContext: EntityQueryContext = viewerContext |
| 58 | + .getViewerScopedEntityCompanionForClass(this) |
| 59 | + .getQueryContextProvider() |
| 60 | + .getQueryContext(), |
| 61 | + ): EnforcingKnexEntityLoader< |
| 62 | + TMFields, |
| 63 | + TMIDField, |
| 64 | + TMViewerContext, |
| 65 | + TMEntity, |
| 66 | + TMPrivacyPolicy, |
| 67 | + TMSelectedFields |
| 68 | + > { |
| 69 | + return knexLoaderFn(this, viewerContext, queryContext); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Vend knex loader for loading entities via non-data-loader methods in a given query context. |
| 74 | + * Returns authorization results instead of throwing on authorization errors. |
| 75 | + * @param viewerContext - viewer context of loading user |
| 76 | + * @param queryContext - query context in which to perform the load |
| 77 | + */ |
| 78 | + static knexLoaderWithAuthorizationResults< |
| 79 | + TMFields extends object, |
| 80 | + TMIDField extends keyof NonNullable<Pick<TMFields, TMSelectedFields>>, |
| 81 | + TMViewerContext extends ViewerContext, |
| 82 | + TMEntity extends ReadonlyEntity<TMFields, TMIDField, TMViewerContext, TMSelectedFields>, |
| 83 | + TMPrivacyPolicy extends EntityPrivacyPolicy< |
| 84 | + TMFields, |
| 85 | + TMIDField, |
| 86 | + TMViewerContext, |
| 87 | + TMEntity, |
| 88 | + TMSelectedFields |
| 89 | + >, |
| 90 | + TMSelectedFields extends keyof TMFields = keyof TMFields, |
| 91 | + >( |
| 92 | + this: IEntityClass< |
| 93 | + TMFields, |
| 94 | + TMIDField, |
| 95 | + TMViewerContext, |
| 96 | + TMEntity, |
| 97 | + TMPrivacyPolicy, |
| 98 | + TMSelectedFields |
| 99 | + >, |
| 100 | + viewerContext: TMViewerContext, |
| 101 | + queryContext: EntityQueryContext = viewerContext |
| 102 | + .getViewerScopedEntityCompanionForClass(this) |
| 103 | + .getQueryContextProvider() |
| 104 | + .getQueryContext(), |
| 105 | + ): AuthorizationResultBasedKnexEntityLoader< |
| 106 | + TMFields, |
| 107 | + TMIDField, |
| 108 | + TMViewerContext, |
| 109 | + TMEntity, |
| 110 | + TMPrivacyPolicy, |
| 111 | + TMSelectedFields |
| 112 | + > { |
| 113 | + return knexLoaderWithAuthorizationResultsFn(this, viewerContext, queryContext); |
| 114 | + } |
| 115 | +} |
0 commit comments