Skip to content

Conversation

@yamcodes
Copy link
Contributor

@yamcodes yamcodes commented Jun 14, 2025

Description

Infer rows from schema instead of manually defining them


PR Checklist

  • Read the Developer's Guide in CONTRIBUTING.md
  • Use a concise title to represent the changes introduced in this PR
  • Provide a detailed description of the changes introduced in this PR, and, if necessary, some screenshots
  • Reference an issue or discussion where the feature or changes have been previously discussed
  • Add a failing test that passes with the changes introduced in this PR, or explain why it's not feasible
  • Add documentation for the feature or changes introduced in this PR to the docs; you can run them with bun docs

Summary by CodeRabbit

  • Refactor

    • Updated several data type definitions to use new utility types for improved consistency with database schemas.
    • Replaced explicit interfaces with inferred types for articles, comments, tags, and users.
    • Removed unused or redundant type definitions and exports.
  • Chores

    • Updated dependencies by removing an unused package and reordering another in the dependency list.
  • Style

    • Simplified and unified type definitions for new and update operations across multiple modules.
  • Bug Fixes

    • Removed default values for certain user profile fields, ensuring explicit values are required where necessary.

- Removed the outdated version of @sinclair/typebox from dependencies.
- Added an override for @sinclair/typebox to enforce the use of version 0.34.0.
- Updated bun.lockb to reflect changes in package versions.
@yamcodes yamcodes self-assigned this Jun 14, 2025
@yamcodes yamcodes added the infra label Jun 14, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 14, 2025

Walkthrough

This update refactors several type definitions across the codebase to use schema-inferred types, introduces new type utilities for insert models, and removes the drizzle-typebox dependency. It also adjusts database schema defaults for user fields and updates dependency declarations in the project configuration.

Changes

File(s) Change Summary
package.json Removed drizzle-typebox dependency; reordered @sinclair/typebox in dependencies.
src/articles/interfaces/article-feed-row.interface.ts Refactored ArticleFeedRow to use InferSelectModel with omitted fields; explicit fields remain as needed.
src/articles/interfaces/article-row.interface.ts Changed ArticleRow from interface to type alias using intersection; removed ArticleFavoritedBy alias.
src/articles/interfaces/new-article-row.interface.ts Switched from InferInsertModel to InferNewRow for NewArticleRow.
src/comments/interfaces/comment-row.interface.ts,
src/comments/interfaces/new-comment-row.interface.ts
Replaced explicit interfaces with schema-inferred type aliases using InferSelectModel and InferNewRow.
src/common/interfaces/common.interface.ts Added SystemFields, InferNewRow, and Prettify type utilities.
src/tags/interfaces/tag-row.interface.ts Changed new row types to use InferNewRow instead of InferInsertModel.
src/tags/tags.schema.ts Removed drizzle-typebox import and deleted tagsSelectSchema export.
src/users/interfaces/user-follow-row.interface.ts,
src/users/interfaces/user-row.interface.ts
Changed new row types to use InferNewRow; simplified UpdateUserRow type.
src/users/users.schema.ts Removed default values from bio and image columns in users table schema.

Sequence Diagram(s)

sequenceDiagram
    participant Developer
    participant TypeSystem
    participant Schema

    Developer->>TypeSystem: Define NewRow type for Table
    TypeSystem->>Schema: Use InferNewRow utility with Table schema
    Schema-->>TypeSystem: Return inferred type (excluding system fields)
    TypeSystem-->>Developer: Provides accurate, schema-aligned type
Loading

Poem

A bunny with a schema in mind,
Hopped through the code to see what it could find.
Types inferred, defaults gone,
Old dependencies now withdrawn.
With type utilities crisp and new,
The codebase shines—so fresh, so true!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@yamcodes yamcodes changed the base branch from main to layered-phase6-misc June 14, 2025 07:26
@yamcodes yamcodes changed the title Drizzle typebox Infer rows Jun 14, 2025
yamcodes added 5 commits June 14, 2025 12:51
- Updated ArticleFeedRow, ArticleRow, NewArticleRow, CommentRow, and NewCommentRow interfaces to utilize the new InferNewRow type for better type inference and consistency.
- Removed redundant properties from ArticleFeedRow and ArticleRow to streamline the data structure.
- Enhanced type definitions across various interfaces to improve clarity and maintainability.
- Updated the ArticleRow type definition to omit the 'body' property, enhancing type clarity and reducing redundancy in the interface.
- Maintained the inclusion of 'author', 'favoritedBy', and 'tags' properties for comprehensive article representation.
- Deleted the import statement for createSelectSchema and the associated tagsSelectSchema export, as they were no longer needed in the tags schema file, streamlining the codebase.
- Removed default values for 'bio' and 'image' fields in the users schema to allow for more flexible data input.
- Updated the NewUserRow type to omit system fields, enhancing type clarity and ensuring better compatibility with the updated schema.
…ility type

- Updated the InferNewRow type to accept a configuration object for database column names and an override option, enhancing type inference capabilities.
- Introduced a new Prettify utility type to improve type representation across the codebase.
@yamcodes yamcodes marked this pull request as ready for review June 14, 2025 08:15
@yamcodes
Copy link
Contributor Author

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jun 14, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (5)
package.json (1)

39-39: Consider moving @sinclair/typebox to devDependencies
If @sinclair/typebox is only used for static schema validation or type-level utilities and not needed at runtime, relocating it to devDependencies can help keep your production bundle lean.

src/common/interfaces/common.interface.ts (2)

43-60: Expose system field list as a constant for re-use at runtime

SystemFields is only a type alias.
In several places (serialization, audit-logging, object sanitisation, etc.) we often need the same list of keys at runtime. Exporting the literal array alongside the type avoids the usual duplication / as unknown as gymnastics while keeping the existing type alias intact.

+export const SYSTEM_FIELDS = ['id', 'createdAt', 'updatedAt'] as const;
+export type SystemFields = typeof SYSTEM_FIELDS[number];
-export type SystemFields = 'id' | 'createdAt' | 'updatedAt';

50-59: TConfig default contains a value for an optional key

override is declared optional (override?: boolean) but is still given a default of false.
That makes the question “is the caller omitting the key” indistinguishable from “the caller set false”, so the optionality provides no extra information. Either:

  1. Drop override from the default, or
  2. Make override required in the constraint.
-    override?: boolean;
+    override: boolean;
 ...
-    override: false;

Pick whichever reflects the intended API.

src/comments/interfaces/comment-row.interface.ts (1)

1-4: Prefer path-alias import for consistency

Elsewhere the project imports schemas via the @/ alias (import { comments } from '@/comments/comments.schema').
Using the same style here avoids future churn if folders are moved.

-import type { comments } from '../comments.schema';
+import type { comments } from '@/comments/comments.schema';
src/users/interfaces/user-row.interface.ts (1)

6-8: UpdateUserRow may omit fields you sometimes need to patch

Using Partial<NewUserRow> blocks updates to createdAt/updatedAt and any future system columns by design. If you ever issue administrative updates that legitimately touch those fields (e.g. backfilling data), consider:

-export type UpdateUserRow = Partial<NewUserRow>;
+export type UpdateUserRow = Partial<
+  Omit<InferSelectModel<typeof users>, 'id'>
+>;

This keeps id immutable but allows optional modification of all other columns when necessary.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7aee1eb and 5253df9.

⛔ Files ignored due to path filters (1)
  • bun.lockb is excluded by !**/bun.lockb
📒 Files selected for processing (12)
  • package.json (1 hunks)
  • src/articles/interfaces/article-feed-row.interface.ts (1 hunks)
  • src/articles/interfaces/article-row.interface.ts (1 hunks)
  • src/articles/interfaces/new-article-row.interface.ts (1 hunks)
  • src/comments/interfaces/comment-row.interface.ts (1 hunks)
  • src/comments/interfaces/new-comment-row.interface.ts (1 hunks)
  • src/common/interfaces/common.interface.ts (2 hunks)
  • src/tags/interfaces/tag-row.interface.ts (1 hunks)
  • src/tags/tags.schema.ts (0 hunks)
  • src/users/interfaces/user-follow-row.interface.ts (1 hunks)
  • src/users/interfaces/user-row.interface.ts (1 hunks)
  • src/users/users.schema.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • src/tags/tags.schema.ts
🧰 Additional context used
🧬 Code Graph Analysis (7)
src/comments/interfaces/comment-row.interface.ts (1)
src/comments/comments.schema.ts (1)
  • comments (6-17)
src/comments/interfaces/new-comment-row.interface.ts (2)
src/common/interfaces/common.interface.ts (1)
  • InferNewRow (50-59)
src/comments/comments.schema.ts (1)
  • comments (6-17)
src/articles/interfaces/new-article-row.interface.ts (2)
src/common/interfaces/common.interface.ts (1)
  • InferNewRow (50-59)
src/articles/articles.schema.ts (1)
  • articles (14-25)
src/users/interfaces/user-follow-row.interface.ts (2)
src/users/users.schema.ts (1)
  • userFollows (30-43)
src/common/interfaces/common.interface.ts (1)
  • InferNewRow (50-59)
src/tags/interfaces/tag-row.interface.ts (2)
src/tags/tags.schema.ts (2)
  • tags (11-15)
  • articleTags (23-36)
src/common/interfaces/common.interface.ts (1)
  • InferNewRow (50-59)
src/articles/interfaces/article-row.interface.ts (3)
src/articles/articles.schema.ts (2)
  • articles (14-25)
  • favoriteArticles (27-40)
src/profiles/interfaces/profile-row.interface.ts (1)
  • ProfileRow (4-6)
src/tags/interfaces/tag-row.interface.ts (1)
  • ArticleTagRow (8-8)
src/articles/interfaces/article-feed-row.interface.ts (1)
src/articles/articles.schema.ts (1)
  • articles (14-25)
🔇 Additional comments (8)
package.json (1)

38-38: Validate runtime necessity of radashi
Ensure that radashi is actually required in your production code. If it’s only used during development (e.g., for tooling or scripts), consider moving it to devDependencies.

src/comments/interfaces/new-comment-row.interface.ts (1)

1-4: LGTM – switch to InferNewRow simplifies maintenance

The new alias cleanly removes system columns from the insert model and avoids duplication.
No further issues spotted.

src/articles/interfaces/new-article-row.interface.ts (1)

2-4: LGTM – consistent with new typing strategy

InferNewRow is applied correctly; the article insert model now omits system fields.
Nothing else to flag.

src/users/interfaces/user-follow-row.interface.ts (1)

1-10: Composite-PK table benefits from the new helper

userFollows has no id column, so InferNewRow conveniently drops only the timestamps.
Change looks sound and mirrors other modules.

src/users/users.schema.ts (1)

15-16: Nullable bio / image — confirm downstream handling

Removing the defaults and the .notNull() constraint means both columns will now resolve to string | null at the type level and can store NULL in the database. Double-check:

  1. All serializers / DTOs tolerate null (e.g. JSON response for profiles).
  2. Validation layers no longer assume empty-string defaults.
  3. Existing data migrations set bio / image to NULL instead of '' to avoid silent casting issues.

If any of these surfaces still expect a plain string, consider keeping a default or adding explicit notNull() constraints.

src/tags/interfaces/tag-row.interface.ts (1)

1-10: New InferNewRow adoption looks good

Transitioning to InferNewRow cleanly drops createdAt/updatedAt from insert types while retaining the required name PK. No further action needed.

src/articles/interfaces/article-row.interface.ts (1)

6-10: Consistent infer-based ArticleRow definition

Refactoring to Omit<InferSelectModel<typeof articles>, 'authorId'> avoids manual drift and keeps the extra domain fields (author, favoritedBy, tags) explicit. Nice cleanup.

src/articles/interfaces/article-feed-row.interface.ts (1)

4-7: Feed row inference is concise and accurate

Leveraging InferSelectModel and omitting only the unneeded columns (id, authorId, body) keeps the type aligned with the schema while preserving previously hand-written fields. Good move.

@yamcodes yamcodes merged commit c05fb59 into layered-phase6-misc Jun 14, 2025
1 check passed
@yamcodes yamcodes deleted the drizzle-typebox branch June 14, 2025 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants