-
Notifications
You must be signed in to change notification settings - Fork 730
feat: add schema-aligned db types and test factory primitives #4326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
b19cb84
test: add db schema types and test-kit factory primitives
skwowet 663690c
Merge branch 'main' into test-kit-primitives
skwowet a57cc89
fix: make prettier and linter happy
skwowet b2aca6e
fix: resolve pr review comments
skwowet f1eb31b
Merge branch 'main' into test-kit-primitives
skwowet 79a8ad3
refactor: nitpicks after self pr review
skwowet dc21b99
refactor: simplify dal inserts and drop createMemberIdentity
skwowet cd6e86f
Merge branch 'main' into test-kit-primitives
skwowet 974ad43
refactor: collapse member segment affiliation inserts into one helper
skwowet d2b3ab4
fix: ensure default value for verified field in upsertOrgIdentities
skwowet 3913399
fix: add default value for verified field in member identity and affi…
skwowet c89f167
Merge branch 'main' into test-kit-primitives
skwowet 541e3a8
fix: resolve pr review comments
skwowet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # ADR-0006: Database schema types as the source of truth | ||
|
|
||
| **Date**: 2026-07-09 | ||
| **Status**: accepted | ||
| **Deciders**: Yeganathan S | ||
|
|
||
| ## Context | ||
|
|
||
| Working with entity types in CDP is messy. The same concepts appear as overlapping shapes across `@crowd/types`, DAL inputs, merge helpers, and API/domain models — often incomplete relative to the database, with inconsistent nullability and no single source of truth. That makes it hard to change data access safely, reuse types across packages, and keep tests aligned with production. | ||
|
|
||
| We need a durable way to model persisted data that the rest of the codebase can build on, without inventing yet another parallel type family per feature. | ||
|
|
||
| ## Decision | ||
|
|
||
| Treat the database schema as the source of truth for persisted entity shapes. Introduce a dedicated `db/` area under `@crowd/types` for schema-aligned row types, and derive narrower types (create/update payloads, factory options, domain views) from those via composition (`Pick`, `Partial`, `Omit`, intersections) rather than hand-maintaining duplicates. | ||
|
|
||
| These types live in `@crowd/types`, not in the data-access layer: they are shared vocabulary for backend, workers, libraries, and test utilities. DAL remains responsible for queries and persistence; it consumes and returns these types instead of owning the canonical shapes. | ||
|
|
||
| This applies to entities generally (members, identities, organizations, and others as we touch them), not only the first tables we migrate. | ||
|
|
||
| ## Conventions | ||
|
|
||
| These rules exist so people (and tools) do not confuse the row type with write payloads, or nullability with “field may be omitted.” | ||
|
|
||
| 1. **One row type per table** — describes the entity as stored. Field nullability matches the schema. Database defaults do not make read fields optional. | ||
| 2. **Write payloads are derived from the row type** — use `Pick`, `Partial`, `Omit`, and intersections instead of duplicating the full interface. | ||
| 3. **Nullability ≠ optionality** — `| null` means the column can store null; `?` / `Partial` means the caller may omit the key. Both can appear; they mean different things. | ||
| 4. **System-owned fields stay off write payloads** — things like tenant id, audit timestamps, and soft-delete markers are set by persistence code, not by every caller. | ||
| 5. **Layers that supply defaults may widen further** — e.g. a test factory can take a partial write payload and fill required fields before calling DAL. That does not change the row type or the DAL contract. | ||
|
|
||
| ## Alternatives Considered | ||
|
|
||
| ### Alternative 1: Keep defining types ad hoc next to each feature | ||
| - **Pros**: Fast locally; no cross-package coordination. | ||
| - **Cons**: Duplicates and drift continue; nullability and field sets disagree across call sites. | ||
| - **Why not**: That is the current pain. It does not scale as more packages share the same entities. | ||
|
|
||
| ### Alternative 2: Own canonical entity types inside `@crowd/data-access-layer` | ||
| - **Pros**: Types sit next to SQL; easy for DAL authors to update. | ||
| - **Cons**: Anything that needs a row shape (test-kit, common services, workers, backend) must depend on DAL or re-copy types. | ||
| - **Why not**: Row shapes are shared contracts, not DAL implementation details. Putting them in `@crowd/types` keeps the dependency graph thin and matches how other shared models already live. | ||
|
|
||
| ### Alternative 3: Generate types from the schema (e.g. introspection tooling) | ||
| - **Pros**: Always in sync with migrations; less manual work. | ||
| - **Cons**: Tooling and review process are not in place; generated output can be noisy and hard to evolve with domain naming. | ||
| - **Why not**: Manual schema-aligned types are enough to establish the pattern now. Generation can be revisited once the convention is stable. | ||
|
|
||
| ## Consequences | ||
|
|
||
| ### Positive | ||
| - One place to look for “what does this table look like in TypeScript.” | ||
| - Downstream APIs and tests extend from the same base instead of inventing parallel models. | ||
| - Clearer package boundaries: `@crowd/types` for shapes, DAL for access, factories/services for composition. | ||
|
|
||
| ### Negative | ||
| - Existing aliases and legacy shapes (`MemberRow`, feature-local inputs, etc.) will coexist during migration. | ||
| - Authors must update `db/` types when migrations change columns or nullability. | ||
|
|
||
| ### Risks | ||
| - **Partial adoption** — mitigated by migrating types when a table is touched (factories, DAL hardening, new features), not a big-bang rewrite. | ||
| - **Drift from schema** — mitigated by checking live schema (or migrations) when adding or changing `db/` types; optional codegen later. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| # ADR-0007: Test factory primitives and defaults | ||
|
|
||
| **Date**: 2026-07-10 | ||
| **Status**: accepted | ||
| **Deciders**: Yeganathan S | ||
|
|
||
| ## Context | ||
|
|
||
| Shared test factories are easy to turn into hidden scenario builders: they invent data the test never declared, blur what is under assertion, and drift from the real insert shapes used in production code (see ADR-0006). | ||
|
|
||
| We need a simple, durable rule for how test data is created — what the factory does, what defaults may fill, and what the test must own — so the pattern does not divert as more factories are added. | ||
|
Copilot marked this conversation as resolved.
Outdated
|
||
|
|
||
| ## Decision | ||
|
|
||
| Treat test setup as **composition of sharp primitives**, not smart world-builders. Keep three layers separate. | ||
|
|
||
|
|
||
| ### 1. Primitives | ||
|
|
||
| - One job: **persist what they are given**, preferably in bulk, via the DAL. | ||
| - Input stays close to schema insert types (`*DbInsert` and small, explicit extensions when a factory accepts related children in one call). | ||
| - No faker inside primitives. No silent graph scaffolding. No behavioral guesses. | ||
| - When a primitive accepts structured input (for example a hierarchy), it may **derive structural fields** from that shape (links, type/level, denormalized parent labels). That derivation is part of the primitive’s contract — not a default. | ||
|
|
||
| ### 2. Defaults (opt-in) | ||
|
|
||
| - Separate helpers (`withXDefaults`) fill an **allowlist** of missing label / harmless system fields **on rows the caller already wrote**. | ||
| - Call sites opt in: `createX(qx, withXDefaults([...]))`. Defaults are never baked into primitives. | ||
| - Semantics: | ||
| - **Allowlist only** — unlisted fields are untouched. | ||
| - **Explicit always wins** — a provided value is never overwritten. | ||
| - **Only** `undefined` **is missing** — omitted / `undefined` may be filled; `null` **is intentional and sticks**. | ||
| - **Labels / harmless flags only** — e.g. generated `id`, display names, join timestamps, inactive-path status flags. Ask per field: *if we invent this, could we change which production branch the test hits?* If yes, it does not belong in defaults. | ||
| - **Caller owns scenario identity** when the value defines the case (names, slugs, platforms, dates, relationship targets, and any field that makes two cases different). | ||
|
|
||
| Defaults do **not** build a realistic entity or graph. They only patch allowlisted gaps (e.g. `displayName`, `joinedAt`). Identities, org stints, activities, and other scenario data stay with the test. | ||
|
|
||
| ### 3. Test composition | ||
|
|
||
| - The suite owns behavioral fields and graph shape. | ||
| - Prefer inline fixtures and spreads in the test file. | ||
| - File-local seed helpers only after real repetition; promote into the shared test kit only when a second suite needs the same helper. | ||
| - Do not grow suite-specific “build the whole world” APIs in the shared kit. | ||
|
|
||
| This stays aligned with ADR-0006: factories may widen partial write payloads and fill defaults; they do not redefine row or DAL contracts. | ||
|
|
||
| ## Alternatives Considered | ||
|
|
||
| ### Alternative 1: Always-on smart factories that scaffold graphs | ||
|
|
||
| - **Pros**: Shortest test setup; one call builds a “ready” world. | ||
| - **Cons**: Hides scenario data; easy to pass for the wrong reason; hard to see what a test asserts. | ||
| - **Why not**: Tests must declare behavior. Defaults are ergonomics, not fixtures. | ||
|
|
||
| ### Alternative 2: No shared defaults — every test passes full insert rows | ||
|
|
||
| - **Pros**: Maximum explicitness; zero magic. | ||
| - **Cons**: Noisy boilerplate for ids/labels/flags that rarely matter to the assertion. | ||
| - **Why not**: Opt-in allowlisted defaults keep noise down without hiding scenario data. | ||
|
|
||
| ### Alternative 3: Defaults baked into every primitive call | ||
|
|
||
| - **Pros**: Slightly shorter call sites. | ||
| - **Cons**: Forces defaults even when a test wants raw inserts; harder to see the boundary between persistence and convenience. | ||
| - **Why not**: Opt-in composition keeps primitives sharp and defaults obvious at the call site. | ||
|
|
||
| ## Consequences | ||
|
|
||
| ### Positive | ||
|
|
||
| - Factories stay readable, composable, and reviewable; scenario intent stays in the test. | ||
| - Same overwrite semantics everywhere (`undefined` fill, `null` preserved, allowlist only). | ||
| - New factories have a clear checklist instead of reinventing “helpful” behavior. | ||
| - Stays aligned with ADR-0006’s insert/row types. | ||
|
|
||
| ### Negative | ||
|
|
||
| - Callers must compose `withXDefaults` explicitly. | ||
| - Suites carry more scenario data in the test file than a mega-helper would. | ||
|
|
||
| ### Risks | ||
|
|
||
| - **Defaults creep** — mitigate by reviewing allowlists against “could this change the branch we hit?” and rejecting behavioral defaults in review. | ||
| - **Scenario helpers leaking into the shared kit** — mitigate by keeping suite-specific seeds file-local until a second suite needs them. | ||
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
|
skwowet marked this conversation as resolved.
|
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.