Skip to content

Conversation

antony-lambdacurry
Copy link
Collaborator

@antony-lambdacurry antony-lambdacurry commented Mar 1, 2025

Summary by CodeRabbit

  • New Features
    • Introduced an enhanced page building solution offering dynamic content creation and organization.
    • Added streamlined user linking for content authors and tags.
    • Updated the system with comprehensive database enhancements supporting improved post management, image handling, navigation elements, and custom site settings.
    • Launched a new service layer to empower a robust and flexible page building experience.
    • Added new models for posts, authors, tags, sections, templates, images, and site settings to enhance content management capabilities.

Copy link

coderabbitai bot commented Mar 1, 2025

Walkthrough

This pull request introduces new link definitions for connecting post authors and post tags with user entities in the page builder plugin. It adds a new module configuration (PAGE_BUILDER_MODULE) that registers a dedicated service (PageBuilderService) and provides comprehensive database schema definitions through a snapshot and migration. Additionally, several new models are defined to represent page builder entities—including posts, authors, tags, navigation items, images, site settings, and templates—with consolidated exports via an index file.

Changes

File(s) Change Summary
plugins/page-builder/src/links/post-author-user.ts
plugins/page-builder/src/links/post-tag-user.ts
Added new link definitions using defineLink for associating post authors and post tags with users (read-only).
plugins/page-builder/src/modules/page-builder/index.ts Introduced PAGE_BUILDER_MODULE and registered the PageBuilderService via the module configuration.
plugins/page-builder/src/modules/page-builder/migrations/.snapshot-medusa-page-builder.json
plugins/page-builder/src/modules/page-builder/migrations/Migration20250305164601.ts
Added a JSON schema snapshot and a migration class (Migration20250305164601) defining and modifying the DB schema with new tables, indexes, and constraints.
plugins/page-builder/src/modules/page-builder/models/post-author.ts
plugins/page-builder/src/modules/page-builder/models/post-tag.ts
plugins/page-builder/src/modules/page-builder/models/navigation-item.ts
plugins/page-builder/src/modules/page-builder/models/post-section.ts
plugins/page-builder/src/modules/page-builder/models/post-template.ts
plugins/page-builder/src/modules/page-builder/models/image.ts
plugins/page-builder/src/modules/page-builder/models/site-settings.ts
plugins/page-builder/src/modules/page-builder/models/post.ts
Defined multiple new models (e.g., Post, PostAuthor, PostTag, NavigationItem, PostSection, PostTemplate, Image, SiteSettings) and created an index file to consolidate exports.
plugins/page-builder/src/modules/page-builder/service.ts Added PageBuilderService class extending MedusaService that integrates the new models.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant PBS as PageBuilderService
    participant P as Post Model
    participant PA as PostAuthor Model
    participant PT as PostTag Model
    participant PN as PostTemplate Model

    U->>PBS: Submit post creation request
    PBS->>P: Create post record
    PBS->>PA: Link associated authors
    PBS->>PT: Attach tags to post
    PBS->>PN: Associate post template
    PBS-->>U: Return confirmation response
Loading

📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between dd687f2 and 0d5a8b4.

📒 Files selected for processing (15)
  • plugins/page-builder/src/links/post-author-user.ts (1 hunks)
  • plugins/page-builder/src/links/post-tag-user.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/index.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/migrations/.snapshot-medusa-page-builder.json (1 hunks)
  • plugins/page-builder/src/modules/page-builder/migrations/Migration20250305164601.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/models/image.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/models/index.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/models/navigation-item.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/models/post-author.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/models/post-section.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/models/post-tag.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/models/post-template.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/models/post.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/models/site-settings.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/service.ts (1 hunks)

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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

…5-migrate-core-page-builder-models-to-medusa-v2
@antony-lambdacurry antony-lambdacurry force-pushed the antony/mkt-135-migrate-core-page-builder-models-to-medusa-v2 branch from f0169f5 to 4617a56 Compare March 4, 2025 20:40
@antony-lambdacurry antony-lambdacurry changed the title feat(page-builder): add core models/links feat(page-builder): add core models/links/services/module Mar 6, 2025
…5-migrate-core-page-builder-models-to-medusa-v2
@antony-lambdacurry antony-lambdacurry marked this pull request as ready for review March 6, 2025 23:25
@antony-lambdacurry
Copy link
Collaborator Author

@coderabbitai review

Copy link

coderabbitai bot commented Mar 6, 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.

@antony-lambdacurry
Copy link
Collaborator Author

@coderabbitai summary

Copy link

coderabbitai bot commented Mar 6, 2025

✅ Actions performed

Summary regeneration triggered.

Copy link

@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: 2

🧹 Nitpick comments (11)
plugins/page-builder/src/modules/page-builder/models/post-author.ts (1)

9-9: Consider adding documentation for the medusa_user_id relationship

While the code is clear, adding a brief comment explaining that this field links to user IDs in the Medusa core would improve maintainability.

-  medusa_user_id: model.text().unique(),
+  // References a User entity ID from the Medusa core
+  medusa_user_id: model.text().unique(),
plugins/page-builder/src/modules/page-builder/models/site-settings.ts (2)

10-14: Consider adding type definitions for theme and font JSON structures

The JSON fields for theme colors and fonts would benefit from TypeScript type definitions to ensure consistent data structure and improve developer experience when working with these fields.

+// Add these type definitions above the model definition
+type ThemeColors = {
+  main: string;
+  light?: string;
+  dark?: string;
+};
+
+type FontSettings = {
+  family: string;
+  url?: string;
+  weights?: number[];
+};
+
 export const SiteSettings = model.define('site_settings', {
   id: model.id({ prefix: 'site_sett' }).primaryKey(),
   description: model.text().nullable(),
   header_code: model.text().nullable(),
   footer_code: model.text().nullable(),
   storefront_url: model.text().nullable(),
-  primary_theme_colors: model.json().nullable(),
-  accent_theme_colors: model.json().nullable(),
-  highlight_theme_colors: model.json().nullable(),
-  display_font: model.json().nullable(),
-  body_font: model.json().nullable(),
+  primary_theme_colors: model.json<ThemeColors>().nullable(),
+  accent_theme_colors: model.json<ThemeColors>().nullable(),
+  highlight_theme_colors: model.json<ThemeColors>().nullable(),
+  display_font: model.json<FontSettings>().nullable(),
+  body_font: model.json<FontSettings>().nullable(),

26-26: Clarify the purpose of shipping_sort field

The shipping_sort field's purpose isn't immediately clear from its name. Consider renaming to be more descriptive or adding documentation to explain its intended use.

-  shipping_sort: model.text().nullable(),
+  // Defines the sorting method for shipping options (e.g., 'price_asc', 'price_desc', 'alphabetical')
+  shipping_sort: model.text().nullable(),
plugins/page-builder/src/modules/page-builder/models/post-template.ts (1)

4-14: Model definition looks good but consider adding validation for the title field.

The PostTemplate model is well-structured with appropriate fields for a template entity. The relationships with PostSection are defined correctly.

Consider adding validation for the required title field to ensure it's not empty:

- title: model.text(),
+ title: model.text().notNull(),
plugins/page-builder/src/modules/page-builder/models/post.ts (1)

21-35: Relationships are properly defined, but consider adding a created_at field.

The relationship definitions look good. The model establishes appropriate connections with related entities.

Consider adding created_at and updated_at timestamps to track when posts are created and modified:

is_home_page: model.boolean().default(false),
+ created_at: model.timestamp().default(() => new Date()),
+ updated_at: model.timestamp().default(() => new Date()),

// relations fields
plugins/page-builder/src/modules/page-builder/models/image.ts (2)

5-22: Image model relationships look good, but consider adding alt text field.

The model definition for Image is well-structured with appropriate relationships to Post and SiteSettings.

For accessibility and SEO purposes, consider adding an alt_text field:

id: model.id({ prefix: 'img' }).primaryKey(),
url: model.text(),
+ alt_text: model.text().nullable(),
metadata: model.json().nullable(),

23-30: Index naming could be more specific.

The index definition is good for optimizing URL lookups, but the name could be more specific to this table.

.indexes([
  {
-   name: 'IDX_product_image_url',
+   name: 'IDX_image_url',
    on: ['url'],
    unique: false,
    where: 'deleted_at IS NULL',
  },
])
plugins/page-builder/src/modules/page-builder/models/post-section.ts (1)

5-26: PostSection model has comprehensive section types, but consider future extensibility.

The model definition for PostSection is robust with a good set of predefined section types and appropriate fields for content and styling.

The current enum-based approach for section types works well initially but may limit extensibility. Consider one of these approaches for future development:

  1. Use a more generic approach where section type is a string rather than an enum
  2. Include a mechanism to register new section types (such as through a plugin system)
  3. Add a custom type with additional metadata to support user-defined section types

This would make it easier to add new section types without changing the schema.

plugins/page-builder/src/modules/page-builder/service.ts (1)

1-24: Well-structured service class implementation

The PageBuilderService class is properly defined with all the necessary model imports and extends MedusaService as expected. The implementation follows the standard Medusa service pattern.

Consider adding JSDoc comments to document what this service does and how it should be used, which would improve the maintainability of the code in the long term.

plugins/page-builder/src/modules/page-builder/index.ts (1)

1-2: Consider adding documentation for imports.
It might be helpful to add short doc comments or inline explanations describing what PageBuilderService and the Module wrapper do, especially for future maintainers.

plugins/page-builder/src/modules/page-builder/migrations/.snapshot-medusa-page-builder.json (1)

227-234: Prefer timestamp for published_at instead of text.
published_at is declared as text; using a timestamp-like type or timestamptz might provide better date/time comparison, filtering, and indexing benefits.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between dd687f2 and 0d5a8b4.

📒 Files selected for processing (15)
  • plugins/page-builder/src/links/post-author-user.ts (1 hunks)
  • plugins/page-builder/src/links/post-tag-user.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/index.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/migrations/.snapshot-medusa-page-builder.json (1 hunks)
  • plugins/page-builder/src/modules/page-builder/migrations/Migration20250305164601.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/models/image.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/models/index.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/models/navigation-item.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/models/post-author.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/models/post-section.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/models/post-tag.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/models/post-template.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/models/post.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/models/site-settings.ts (1 hunks)
  • plugins/page-builder/src/modules/page-builder/service.ts (1 hunks)
🔇 Additional comments (15)
plugins/page-builder/src/modules/page-builder/models/post-author.ts (1)

4-13: Solid model definition for PostAuthor

The PostAuthor model looks well-structured with:

  • A properly defined primary key with prefix
  • A name field for display purposes
  • A unique reference to a Medusa user through medusa_user_id
  • A well-defined many-to-many relationship with the Post model

This approach of linking authors to Medusa users provides good integration with the core platform.

plugins/page-builder/src/modules/page-builder/models/post-tag.ts (2)

4-15: Good tag model definition with appropriate fields

The PostTag model provides:

  • A properly defined ID with appropriate prefix
  • Label and description for UI display
  • A unique handle for URL-friendly references
  • Many-to-many relationship with Post model
  • Tracking of who created the tag

This gives you a solid foundation for categorizing posts.


11-11:

❓ Verification inconclusive

Verify the type of created_by_id field and consider adding validation

The created_by_id field is defined as text, but is likely referring to a user ID. Consider:

  1. Adding documentation about what entity this references
  2. Adding validation or a relationship definition if it references Medusa users

This will improve code maintainability and data integrity.


🏁 Script executed:

#!/bin/bash
# Check for other instances of created_by_id to see if there's a consistent pattern
rg "created_by_id" --type ts -A 1 -B 1

Length of output: 1303


Action Required: Confirm and Enhance the 'created_by_id' Field

  • The field is consistently defined as model.text() across models and migrations.
  • Verify that created_by_id is indeed intended to store a Medusa user ID.
  • If so, add clear documentation about its reference and consider implementing validation (or a relationship/foreign key definition) to ensure data integrity.
plugins/page-builder/src/modules/page-builder/models/navigation-item.ts (2)

3-10: Clean navigation item model with good default values

The NavigationItem model is well-defined with:

  • Clear primary key with appropriate prefix
  • Appropriate fields for a navigation item (label, URL, targeting)
  • Good default values for boolean and numeric fields
  • Enumeration for location that restricts values to valid options

The sort_order field will be particularly valuable for maintaining display order in the UI.


6-6: Consider future-proofing the location enum

The current enum only includes 'header' and 'footer' locations. Consider if you might need additional locations in the future (like 'sidebar', 'mobile_menu', etc.) and whether the design should accommodate that flexibility.

Are there any other potential locations where navigation items might appear in your application? If so, consider expanding the enum or making it more extensible.

plugins/page-builder/src/modules/page-builder/models/site-settings.ts (1)

4-32: Comprehensive site settings model with good organization

The SiteSettings model provides an extensive set of configurable options:

  • SEO-related fields (description, storefront_url)
  • Custom code injection (header_code, footer_code)
  • Theme customization (colors, fonts)
  • Social media links
  • Analytics configuration

The nullable fields appropriately allow for optional settings, and the relationship with the Image model for the favicon is well-structured.

plugins/page-builder/src/modules/page-builder/models/post.ts (1)

7-19: The Post model looks solid, but verify that content_mode is required.

The Post model definition is comprehensive with appropriate fields for a blog/page entry. The fields and their types are well-chosen.

I notice that content_mode is defined as an enum without a default value. Is this intentional? If it should always have a value, consider adding a default:

- content_mode: model.enum(['basic', 'advanced']),
+ content_mode: model.enum(['basic', 'advanced']).default('basic'),
plugins/page-builder/src/modules/page-builder/models/post-section.ts (1)

28-39:

❓ Verification inconclusive

Nullable relationships provide flexibility but might need validation.

The relationships to Post and PostTemplate are well-defined, allowing a section to belong to either entity.

Since both relationships are nullable, ensure that validation exists elsewhere in your code to guarantee that a PostSection always belongs to either a Post or a PostTemplate, but not both or neither:


🏁 Script executed:

#!/bin/bash
# Look for validation code that ensures a section belongs to exactly one parent
rg -A 10 -B 10 "validate.*PostSection" --type ts
# Or alternative validation approaches
rg -A 10 "post_section.*validation" --type ts

Length of output: 98


Action Required: Validate Parent Relationship for PostSection

The model in plugins/page-builder/src/modules/page-builder/models/post-section.ts correctly defines nullable relationships for both Post and PostTemplate. However, our initial search for any associated validation logic (to ensure that a PostSection is linked to exactly one parent) returned no results. Please verify manually whether this validation is implemented elsewhere (for example, in service or controller layers handling PostSection creation/updating). If it isn’t, you may need to add explicit logic to guarantee that a PostSection belongs to either a Post or a PostTemplate—but not both or neither.

  • Confirm if validation exists outside the model definition, possibly where PostSection instances are created or updated.
  • If absent, consider implementing validation to enforce the correct association.
plugins/page-builder/src/links/post-tag-user.ts (1)

1-14: Link definition looks good

The link between post tags and users is properly defined using the defineLink function, with appropriate field mapping and read-only setting.

plugins/page-builder/src/links/post-author-user.ts (1)

1-14:

✅ Verification successful

Link definition is correct

The link between post authors and users is properly defined. Note that this link uses medusa_user_id as the field name while the post-tag-user link uses created_by_id.

This difference appears intentional, but it's worth confirming that these different field names are correct for their respective contexts:


🏁 Script executed:

#!/bin/bash
# Check the model definitions to verify the field names are consistent with the link definitions
echo "Checking PostAuthor model for medusa_user_id field:"
fd -e ts "post-author.ts" -x cat {}

echo -e "\nChecking PostTag model for created_by_id field:"
fd -e ts "post-tag.ts" -x cat {}

Length of output: 1043


Action: Confirm model field names are consistent with link definitions.

The verification confirms that the PostAuthor model uses medusa_user_id and the PostTag model uses created_by_id, which aligns with their respective link definitions. The difference is intentional and correct.

plugins/page-builder/src/modules/page-builder/models/index.ts (1)

1-8: Clean barrel export pattern

This index file effectively uses the barrel pattern to export all models from their respective files, making imports cleaner throughout the codebase.

plugins/page-builder/src/modules/page-builder/index.ts (2)

4-4: Constant looks good.
Naming the module identifier as PAGE_BUILDER_MODULE follows clear conventions and is immediately understandable.


6-8: Validate module configuration.
The module setup looks straightforward, but consider verifying that PageBuilderService implements all necessary interfaces (if any) or error-handling logic for a robust module.

plugins/page-builder/src/modules/page-builder/migrations/Migration20250305164601.ts (2)

5-11: Ensure constraint removal aligns with intended schema changes.
Dropping these unique constraints in the up method is valid if you intend to allow multiple associations. Confirm that you really want to remove uniqueness on fields like image_post_id_unique to permit multiple references.


63-101: Validate down() method reversibility.
The down() method properly drops the foreign keys and tables. Confirm that you are comfortable with a destructive rollback (tables dropped). In case partial rollbacks are desired, consider implementing smaller incremental rollbacks.

@antony-lambdacurry antony-lambdacurry merged commit 0ea43de into page-builder Mar 31, 2025
1 check passed
@antony-lambdacurry antony-lambdacurry deleted the antony/mkt-135-migrate-core-page-builder-models-to-medusa-v2 branch March 31, 2025 12:45
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