feat(data-access): add Token.allBySiteId with optional tokenTypes/cycle filters#1572
Open
feat(data-access): add Token.allBySiteId with optional tokenTypes/cycle filters#1572
Conversation
|
This PR will trigger a patch release when merged. |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
TokenCollection.allBySiteId(siteId, options)to@adobe/spacecat-shared-data-access. The method returns all tokens for a site in a single PostgREST round-trip, with optionaltokenTypes(array, IN-filtered) andcycle(string, eq-filtered) narrowing. All four filter combinations are index-backed:siteId+cycletokens_site_id_cycle_idx(new — see mysticat-data-service#498)siteId+cycle+tokenTypestokens_site_id_cycle_idx+INfiltersiteId+tokenTypestokens_site_id_token_type_cycle_keyprefixsiteIdonlytokens_site_id_token_type_cycle_keyprefixThis replaces the previous app-layer fan-out pattern (one call to
findBySiteIdAndTokenTypeper type with cycle filtering in the controller) with a single query.Why
The API controller (
spacecat-api-service) needs to load multiple tokens for a site/cycle to compute remaining grant quotas. Today it parallelises N calls tofindBySiteIdAndTokenTypeand then filters by cycle — N HTTP round-trips and a query path that won't scale oncetokenTypesbecomes wildcard-able. Pushing this into the data-access layer:|tokenTypes|.tokenTypes(e.g. for an admin/all-types view) without falling into a full table scan, now that the(site_id, cycle)index exists.Changes
src/models/token/token.collection.js— newallBySiteId(siteId, { tokenTypes?, cycle?, ...queryOptions })method. Usesthis.all({ siteId, cycle? }, { where: op.in(tokenType, …) })so PostgREST emits a singleWHERE site_id = ? AND cycle = ? AND token_type IN (…)query. Filter keys are stripped from the forwarded options.test/unit/models/token/token.collection.test.js— 9 new unit tests covering all four filter combinations, option forwarding, and validation errors. 100% coverage ontoken.collection.js.test/it/token/token.test.js— 10 new integration tests against PostgREST + Aurora (usesfixtures.sites[1]and historical cycles2099-01/2099-02to avoid colliding with the existingfindBySiteIdAndTokenType/grantSuggestionssuites).Related
(site_id, cycle)index added in mysticat-data-service#498Test plan
npm test -w packages/spacecat-shared-data-access— 1913 passing, 100% lines ontoken.collection.jsnpm run lint -w packages/spacecat-shared-data-access— cleannpm run test:it -w packages/spacecat-shared-data-access— full token IT suite passes (23 tests; 10 new + 13 pre-existing)spacecat-api-serviceto swap the controller's parallelfindBySiteIdAndTokenTypeloop for oneallBySiteIdcall once the new version ships