Skip to content

fix: Avoid quadratic relationship lookup in typescript generator - #1101

Merged
avallete merged 1 commit into
masterfrom
davidcamachocateura/api-1579-prevent-requests-to-generatorstypescript-from-overloading
Aug 11, 2026
Merged

avallete merged 1 commit into
masterfrom
davidcamachocateura/api-1579-prevent-requests-to-generatorstypescript-from-overloading

Conversation

@dcamachoc

Copy link
Copy Markdown
Contributor

What kind of change does this PR introduce?

Improves the performance of /generators/typescript endpoint

What is the current behavior?

getRelationships in the TypeScript type generator (src/server/templates/typescript.ts) is called once per table/view, and each call does a full linear scan (.filter()) over all relationships in the schema to find the ones belonging to that table. This makes the overall type-generation pass O(tables × relationships) — for schemas with many tables and foreign keys, this scales quadratically and can turn into a long-running, CPU-bound, synchronous operation for large schemas.

Because this work runs synchronously on the Node.js event loop, a single request against a large enough schema could block the process entirely for its duration — delaying or timing out every other in-flight request until it finished. In effect, one expensive request was enough to make the whole service become unresponsive.

What is the new behavior?

getRelationships now groups all relationships into a Map keyed by schema.relation in a single upfront pass, and each table/view lookup becomes an O(1) map lookup instead of a full scan. This reduces the overall complexity from O(tables × relationships) to O(tables + relationships), with no change to the generated output.

Additional context

No functional/output changes — the generated TypeScript types are identical; this is purely an algorithmic complexity fix aimed at reducing CPU usage and latency for large schemas, and preventing a single expensive request from stalling the rest of the service.

@dcamachoc
dcamachoc marked this pull request as ready for review August 11, 2026 11:46
@dcamachoc
dcamachoc requested review from a team, avallete and soedirgo as code owners August 11, 2026 11:47
@avallete
avallete merged commit 923965b into master Aug 11, 2026
10 checks passed
@avallete
avallete deleted the davidcamachocateura/api-1579-prevent-requests-to-generatorstypescript-from-overloading branch August 11, 2026 12:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants