-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor BucketParameterQuerier implementation.
- Loading branch information
Showing
5 changed files
with
105 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { BucketDescription } from './BucketDescription.js'; | ||
import { RequestParameters, SqliteJsonRow, SqliteJsonValue } from './types.js'; | ||
|
||
/** | ||
* Represents a set of parameter queries for a specific request. | ||
*/ | ||
export interface BucketParameterQuerier { | ||
/** | ||
* These buckets do not change for the lifetime of the connection. | ||
* | ||
* This includes parameter queries such as: | ||
* | ||
* select request.user_id() as user_id() | ||
* select value as project_id from json_each(request.jwt() -> 'project_ids') | ||
*/ | ||
readonly staticBuckets: BucketDescription[]; | ||
|
||
/** | ||
* True if there are dynamic buckets, meaning queryDynamicBucketDescriptions() should be used. | ||
* | ||
* If this is false, queryDynamicBucketDescriptions() will always return an empty array. | ||
*/ | ||
readonly hasDynamicBuckets: boolean; | ||
|
||
/** | ||
* These buckets depend on parameter storage, and needs to be retrieved dynamically for each checkpoint. | ||
* | ||
* The ParameterLookupSource should perform the query for the current checkpoint - that is not passed | ||
* as a parameter. | ||
* | ||
* This includes parameter queries such as: | ||
* | ||
* select id as user_id from users where users.id = request.user_id() | ||
*/ | ||
queryDynamicBucketDescriptions(source: ParameterLookupSource): Promise<BucketDescription[]>; | ||
} | ||
|
||
export interface ParameterLookupSource { | ||
getParameterSets: (lookups: SqliteJsonValue[][]) => Promise<SqliteJsonRow[]>; | ||
} | ||
|
||
export interface QueryBucketDescriptorOptions extends ParameterLookupSource { | ||
parameters: RequestParameters; | ||
} | ||
|
||
export function mergeBucketParameterQueriers(queriers: BucketParameterQuerier[]): BucketParameterQuerier { | ||
return { | ||
staticBuckets: queriers.flatMap((q) => q.staticBuckets), | ||
hasDynamicBuckets: queriers.some((q) => q.hasDynamicBuckets), | ||
async queryDynamicBucketDescriptions(source: ParameterLookupSource) { | ||
let results: BucketDescription[] = []; | ||
for (let q of queriers) { | ||
if (q.hasDynamicBuckets) { | ||
results.push(...(await q.queryDynamicBucketDescriptions(source))); | ||
} | ||
} | ||
return results; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters