Skip to content

Separate gift-links types into database, queries and models modules#28783

Open
rob-ghost wants to merge 1 commit into
mainfrom
chore/gift-links-colocate-db-concerns
Open

Separate gift-links types into database, queries and models modules#28783
rob-ghost wants to merge 1 commit into
mainfrom
chore/gift-links-colocate-db-concerns

Conversation

@rob-ghost

@rob-ghost rob-ghost commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Problem

The gift-links module declared the DB row shape, the read projection, the codec, the domain types, and the knex table types all in a single model.ts. It was hard to follow which type represented what state — stored row vs query result vs domain — and where the mappings between them lived.

Solution

Split the types by the state they represent, each file owning a state plus the mapping out of it:

  • database.ts — the stored table schemas and the knex types
  • queries.ts — the read projection, the row-to-domain codec, and the query statements
  • models.ts — the domain types only, no codecs

Each row shape keeps a single source: the read projection and the knex types both derive from the one table schema. No behaviour change — typecheck, gift-links unit, integration, and e2e-api suites all green, lint clean.

@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: a704fecb-b4d4-46aa-ae49-e4ea17b79bc1

📥 Commits

Reviewing files that changed from the base of the PR and between 098626b and 77a8b3b.

📒 Files selected for processing (7)
  • ghost/core/core/server/api/endpoints/utils/serializers/output/gift-links.ts
  • ghost/core/core/server/services/gift-links/database.ts
  • ghost/core/core/server/services/gift-links/model.ts
  • ghost/core/core/server/services/gift-links/models.ts
  • ghost/core/core/server/services/gift-links/queries.ts
  • ghost/core/core/server/services/gift-links/service.ts
  • ghost/core/test/integration/services/gift-links.test.ts
💤 Files with no reviewable changes (1)
  • ghost/core/core/server/services/gift-links/model.ts
✅ Files skipped from review due to trivial changes (1)
  • ghost/core/core/server/services/gift-links/models.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • ghost/core/core/server/services/gift-links/service.ts

Walkthrough

The PR splits the single model.ts file into two separate modules: database.ts (DB-layer Zod schemas and Knex table type augmentation) and models.ts (domain-level types). database.ts introduces DbGiftLink with a DbDate codec and moves the knex/types/tables augmentation out of queries.ts. models.ts re-exports the domain GiftLinkToken, GiftLink, and Post types. queries.ts now derives GiftLinkRow from DbGiftLink.pick() and defines giftLinkCodec locally, replacing the imported constant. GiftLinksService drops the private run() helper and invokes query builders directly with this.knex, while referencing queries.GiftLinkRow and queries.giftLinkCodec.

Possibly related PRs

  • TryGhost/Ghost#28693: Directly related — modifies the gift-links service, models, queries, and codecs for per-post gift-link token lookups and live-link decoding, the same code paths restructured in this PR.

Suggested reviewers

  • kevinansfield
  • jonatansberg
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main refactoring change: separating gift-links types into three distinct modules (database, queries, and models) for better code organization.
Description check ✅ Passed The description is directly related to the changeset, clearly explaining the problem being solved, the solution implemented, and the verification that no behavioral changes resulted from the refactoring.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch chore/gift-links-colocate-db-concerns

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@nx-cloud

nx-cloud Bot commented Jun 22, 2026

Copy link
Copy Markdown

🤖 Nx Cloud AI Fix

Ensure the fix-ci command is configured to always run in your CI pipeline to get automatic fixes in future runs. For more information, please see https://nx.dev/ci/features/self-healing-ci


View your CI Pipeline Execution ↗ for commit 77a8b3b

Command Status Duration Result
nx run ghost:test:ci:integration:no-coverage ✅ Succeeded 2m 26s View ↗
nx run ghost:test:ci:integration ✅ Succeeded 2m 6s View ↗
nx build @tryghost/signup-form ✅ Succeeded <1s View ↗
nx run ghost:test:ci:legacy ✅ Succeeded 2m 57s View ↗
nx build @tryghost/portal ✅ Succeeded <1s View ↗
nx build @tryghost/admin-toolbar ✅ Succeeded <1s View ↗
nx build @tryghost/sodo-search ✅ Succeeded <1s View ↗
nx build @tryghost/activitypub ✅ Succeeded 1s View ↗
Additional runs (10) ✅ Succeeded ... View ↗

💡 Verify your cache is correct by running tasks in a sandbox. Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-06-22 16:02:05 UTC

Splits the single model.ts into database.ts (table schemas + knex types),
queries.ts (read projection, the row->domain codec, and query statements),
and models.ts (the domain types, no codecs). Each row shape still has one
source: the projection and knex types both derive from the DbGiftLink schema.
@rob-ghost rob-ghost force-pushed the chore/gift-links-colocate-db-concerns branch from 098626b to 77a8b3b Compare June 22, 2026 15:54
@rob-ghost rob-ghost changed the title Co-locate gift-links database row types with the model Separate gift-links types into database, queries and models modules Jun 22, 2026
@rob-ghost rob-ghost requested a review from jonatansberg June 22, 2026 16:33
@rob-ghost rob-ghost marked this pull request as ready for review June 22, 2026 16:34
@rob-ghost

Copy link
Copy Markdown
Contributor Author

FYI @jonatansberg I was finding it increasingly hard to reason about which models and types related to which layer of the data, so I split them into specific modules. Now everything should flow nicely between database -> domain -> api with codecs in the appropriate edges.

Moving serialisation into this structure to co-locate everything gift-links related will follow.

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.

1 participant