Skip to main content
gFly v1.15.1
Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Model

gFly uses the Model-View-Controller (MVC) architectural pattern, where the “Model” represents the data structure and business logic.

In gFly, Models are struct types that serve as an abstraction layer for interacting with your database tables. Each model typically corresponds to a single database table, and it handles data retrieval, manipulation, and validation.

Here are the key aspects of gFly Models:

  • Models are typically stored in the app/domain/models directory
  • Model is a struct type
  • Model should have special property MetaData type mb.MetaData
  • Model use both TAG db and model helps to work with Go database and FluentModel - flexible and powerful Data-Access Layer

A basic gFly model looks like this:

package models

import (
    "database/sql"
    mb "github.com/gflydev/db" // mb is Model Builder
    "time"
)

// ====================================================================
// ============================ Data Types ============================
// ====================================================================

type Status string

// User property types
const (
    // StatusActive represents the active status of a user.
    StatusActive Status = "active"

    // StatusPending represents the pending status of a user.
    StatusPending Status= "pending"

    // StatusBlocked represents the blocked status of a user.
    StatusBlocked Status = "blocked"
)

// ====================================================================
// ============================== Table ===============================
// ====================================================================

// TableUser Table name
const TableUser = "users"

// User struct to describe a user object.
type User struct {
    // Table meta data
    MetaData mb.MetaData `db:"-" model:"table:users"`

    // Table fields
    ID           int            `db:"id" model:"name:id; type:serial,primary" doc:"User ID"`
    Email        string         `db:"email" model:"name:email" doc:"User email address"`
    Password     string         `db:"password" model:"name:password" doc:"User password (hashed)"`
    Fullname     string         `db:"fullname" model:"name:fullname" doc:"User full name"`
    Phone        string         `db:"phone" model:"name:phone" doc:"User phone number"`
    Token        sql.NullString `db:"token" model:"name:token" doc:"User authentication token"`
    Status       Status         `db:"status" model:"name:status" doc:"User status (active, pending, or blocked)"`
    Avatar       sql.NullString `db:"avatar" model:"name:avatar" doc:"User avatar URL"`
    CreatedAt    time.Time      `db:"created_at" model:"name:created_at" doc:"Timestamp when the user was created"`
    UpdatedAt    time.Time      `db:"updated_at" model:"name:updated_at" doc:"Timestamp when the user was last updated"`
    VerifiedAt   sql.NullTime   `db:"verified_at" model:"name:verified_at" doc:"Timestamp when the user was verified"`
    BlockedAt    sql.NullTime   `db:"blocked_at" model:"name:blocked_at" doc:"Timestamp when the user was blocked"`
    DeletedAt    sql.NullTime   `db:"deleted_at" model:"name:deleted_at" doc:"Timestamp when the user was deleted"`
    LastAccessAt sql.NullTime   `db:"last_access_at" model:"name:last_access_at" doc:"Timestamp when the user last accessed the system"`
}

// IsActive checks if the user status is active
func (u *User) IsActive() bool {
    return u.Status == StatusActive
}

// IsDisable checks if the user status is pending or blocked
func (u *User) IsDisable() bool {
    return u.Status == StatusPending || u.Status == StatusBlocked
}

Primary field much have type:primary in tag model

    ID        int          `db:"id" model:"name:id; type:serial,primary"`

Attribute name in tag model is column name in database.

Use sql.NullTime, sql.NullString, sql.NullFloat, sql.NullInt For some NULL able column.