Course Overview
Locked video preview

Paid video

Please purchase the course to watch.

Building the home view

The article model

Here is a summary of the video transcript:

  • Model Initialization
    A shared queries variable is created using the SQLC-generated query struct. This instance is reused across all models to interact with the database efficiently.

  • Article Domain Model
    The Article struct is updated to mirror the database schema, including fields such as UID, timestamps, title, excerpt, and content. Although currently aligned one-to-one with the database, the model is designed for future extensions (e.g., tags) to better fit the application domain.

  • Database-to-Domain Mapping
    Custom mapping is implemented to convert SQLC-generated database types (e.g., pgx types) into standard library types. This avoids tight coupling to external libraries and reduces the risk of breaking changes from dependency updates.

  • Fetching a Single Article
    The findArticle function retrieves an article by UID using a database transaction interface (DBTX). It executes a generated query, handles errors, and maps the result into the domain Article struct.

  • Fetching Multiple Articles
    The findArticles function retrieves multiple records and preallocates a slice for memory efficiency. Each database row is converted into the domain model before being returned.

  • Refactoring with Row Mapping Function
    A rowToArticle helper function is introduced to eliminate duplicated mapping logic. This centralizes transformation logic and improves maintainability.

  • Creating an Article
    An ArticleData struct is introduced to encapsulate input data for article creation. The createArticle function inserts a new record, generates a UID, handles nullable content fields, and returns the mapped domain model.

  • Separation of Concerns and Validation
    The design separates domain models from database entities and prepares for future data validation. Timestamp fields are handled by the database, and validation logic will be added later.