Home Projects Portfolio Dashboard Export PDF Log in

Evolving Data Models: Structuring Content with Hexagonal Architecture

In the LautaroZ01/Blog-Server project, we recently focused on refining how we manage and store structured data. Specifically, we updated our domain models to incorporate 'sections' within our posts, providing more granular control over how content is organized and displayed.

The Challenge: Moving Beyond Flat Models

Initially, our post model was a flat structure. As we aimed to support more complex blog layouts, this became a bottleneck. Adding new types of content—like code blocks, image galleries, or headers—required breaking changes to our database schema and API responses.

We needed a way to treat a post not just as a single string of text, but as a collection of modular segments.

Implementation Strategy

By leveraging our existing Hexagonal Architecture, we were able to isolate the domain logic from the persistence layer. In TypeScript, we updated our entity definition to include a sections array, allowing for better composition:

interface PostSection {
  type: 'text' | 'code' | 'image';
  content: string;
  metadata?: Record<string, any>;
}

class Post {
  constructor(
    public id: string,
    public title: string,
    public sections: PostSection[]
  ) {}
}

By decoupling the domain model from the MongoDB implementation, we ensured that the REST API remains clean. The storage layer now simply maps these sections to document fields without bleeding infrastructure concerns into our business rules.

The Outcome

This refactor provides several advantages:

  1. Flexibility: We can now iterate on individual section types without migrating the entire post collection.
  2. Type Safety: Using TypeScript interfaces ensures that our API consumers know exactly what kind of payload to expect.
  3. Maintainability: Because we followed the Hexagonal Architecture, adding a new section type is as simple as updating a factory function, rather than rewriting our persistence logic.

The Takeaway

When your data model starts to grow in complexity, stop and ask if you are building a 'god object' or a composition of smaller, manageable parts. Start by identifying the smallest reusable piece of your data, and refactor your persistence layer to support that composition rather than just growing a single flat record. Your future self will thank you when the requirements inevitably change.


Generated with Gitvlg.com

Evolving Data Models: Structuring Content with Hexagonal Architecture
LAUTARO NICOLAS ZULETA

LAUTARO NICOLAS ZULETA

Author

Share: