Skip to content

feat(db): type nested include in IncludeOption (#2286)#2314

Merged
viniciusdacal merged 5 commits into
mainfrom
viniciusdacal/db-nested-include-types
Apr 5, 2026
Merged

feat(db): type nested include in IncludeOption (#2286)#2314
viniciusdacal merged 5 commits into
mainfrom
viniciusdacal/db-nested-include-types

Conversation

@viniciusdacal
Copy link
Copy Markdown
Contributor

Summary

  • Add TModels generic parameter to IncludeOption, IncludeResolve, ResolveOneInclude, FindResult, FindOptions, ModelDelegate, TypedGetOptions, TypedListOptions, DatabaseClient, and TransactionClient
  • When TModels is provided, nested include keys are validated against the target model's relations and output types reflect nested relation data
  • New type utilities: FindModelByTable (bidirectional structural match), FindModelRelations, NestedInclude
  • Depth cap at 3 typed nesting levels (matching existing IncludeResolve runtime cap)
  • Default TModels falls back to untyped Record<string, unknown> for full backward compatibility

Public API Changes

Type Change
IncludeOption New optional TModels and _Depth params (defaulted)
IncludeResolve New optional TModels param (defaulted)
FindResult New optional TModels param (defaulted)
FindOptions New optional TModels param (defaulted)
ModelDelegate New optional TModels param (defaulted)
FindModelByTable New export — find model entry by table type
FindModelRelations New export — extract relations from matched model

All changes are backward compatible — existing code using these types without TModels continues to compile unchanged.

Test Coverage

  • Input validation: valid/invalid nested keys at depth 1, 2, and 3 (fallback)
  • Output resolution: nested relation data in FindResult, nested select narrowing
  • Special cases: self-referencing relations, M2M through-relations, false/undefined exclusion
  • Registry paths: manual ModelEntry construction and createRegistry-produced models
  • Client chain: DatabaseClient.get(), DatabaseClient.list(), TransactionClient (positive + negative)
  • Standalone: FindModelByTable, FindModelRelations with backward compat verification

Phase Review

  • Adversarial review: 4 should-fix items found and resolved (test gaps + FindModelRelations never-distribution bug)

Follow-up

Closes #2286

🤖 Generated with Claude Code

viniciusdacal and others added 5 commits April 4, 2026 23:17
Add TModels generic parameter to IncludeOption, IncludeResolve,
ResolveOneInclude, FindResult, FindOptions, ModelDelegate,
TypedGetOptions, TypedListOptions, DatabaseClient, and
TransactionClient. When TModels is provided, nested include keys
are validated against the target model's relations and the output
type reflects nested relation data.

New type utilities: FindModelByTable (bidirectional structural match),
FindModelRelations, NestedInclude. Depth cap at 3 typed nesting levels
(matching existing IncludeResolve cap). Default TModels falls back to
untyped Record<string, unknown> for backward compatibility.

Type tests cover: manual models, createRegistry models,
self-referencing relations, M2M through-relations, depth cap fallback,
false/undefined exclusion, nested select narrowing, and full
DatabaseClient/TransactionClient end-to-end chain.

Follow-up: #2309 (entity-layer TypedIncludeOption)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Fix never-distribution bug in FindModelRelations (tuple-wrap check)
- Add negative tests for self-referencing and M2M through-relations
- Add negative test for list() nested include through DatabaseClient
- Add standalone tests for FindModelByTable and FindModelRelations

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
DatabaseClient<TModels> now maps delegates as ModelDelegate<TEntry, TModels>.
The Extends assertion needs the full TModels type parameter to match.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@viniciusdacal viniciusdacal merged commit 9fd72d7 into main Apr 5, 2026
8 checks passed
This was referenced Apr 5, 2026
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.

feat(db): type nested include in IncludeOption

1 participant