models

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2025 License: MIT Imports: 4 Imported by: 0

README

Models Directory

This directory contains the data models used throughout the gFly application. Models represent the structure of data in the database and provide a way to interact with that data in a type-safe manner.

Overview

Models in gFly follow a consistent structure:

  • Each model corresponds to a database table
  • Models use struct tags to define database mappings
  • Constants are defined for table names and status values
  • Models use the mb.MetaData field for table metadata

Available Models

User Model

The User model represents application users and is stored in the users table.

Key Fields:

  • ID: Unique identifier (primary key)
  • Email: User's email address
  • Password: Hashed password
  • Fullname: User's full name
  • Status: User's status (active, pending, blocked)
  • Various timestamp fields (created_at, updated_at, verified_at, etc.)

Status Constants:

  • UserStatusActive: "active"
  • UserStatusPending: "pending"
  • UserStatusBlocked: "blocked"
Role Model

The Role model represents user roles and is stored in the roles table.

Key Fields:

  • ID: Unique identifier (primary key)
  • Name: Role name
  • Slug: URL-friendly role identifier
  • CreatedAt: Timestamp when the role was created
  • UpdatedAt: Timestamp when the role was last updated

Role Types:

  • RoleAdmin: Administrator role
  • RoleModerator: Moderator role
  • RoleMember: Member role
  • RoleUser: Regular user role
  • RoleGuest: Guest role

The RoleType enum provides helper methods:

  • Name(): Get the string name of a role
  • Ordinal(): Get the numeric value of a role
  • Values(): Get all role names
  • ByName(): Get a role type by its string name
UserRole Model

The UserRole model represents the many-to-many relationship between users and roles, stored in the user_roles table.

Key Fields:

  • ID: Unique identifier (primary key)
  • RoleID: Foreign key to the roles table
  • UserID: Foreign key to the users table
  • CreatedAt: Timestamp when the relationship was created

Usage Example

// Creating a new user
user := models.User{
    Email:     "user@example.com",
    Password:  hashedPassword,
    Fullname:  "Example User",
    Phone:     "123-456-7890",
    Status:    models.UserStatusActive,
    CreatedAt: time.Now(),
    UpdatedAt: time.Now(),
}

// Using the model builder to save to database
err := mb.CreateModel(&user)

Database Mapping

Models use struct tags to map to database columns:

type Example struct {
    MetaData mb.MetaData `db:"-" model:"table:examples"`
    ID       int         `db:"id" model:"name:id; type:serial,primary"`
    Name     string      `db:"name" model:"name:name"`
}

The db tag specifies the column name, while the model tag provides additional metadata for the model builder.

Documentation

Index

Constants

View Source
const TableRole = "roles"

TableRole Table name

View Source
const TableUser = "users"

TableUser Table name

View Source
const TableUserRole = "user_roles"

TableUserRole Table name

Variables

This section is empty.

Functions

This section is empty.

Types

type Role

type Role struct {
	// Table meta data
	MetaData mb.MetaData `db:"-" model:"table:roles"`

	// Table fields
	ID        int          `db:"id" model:"name:id; type:serial,primary"`
	Name      string       `db:"name" model:"name:name"`
	Slug      types.Role   `db:"slug" model:"name:slug"`
	CreatedAt time.Time    `db:"created_at" model:"name:created_at"`
	UpdatedAt sql.NullTime `db:"updated_at" model:"name:updated_at"`
}

Role struct to describe a role object.

type User

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"`
	Email        string           `db:"email" model:"name:email"`
	Password     string           `db:"password" model:"name:password"`
	Fullname     string           `db:"fullname" model:"name:fullname"`
	Phone        string           `db:"phone" model:"name:phone"`
	Token        sql.NullString   `db:"token" model:"name:token"`
	Status       types.UserStatus `db:"status" model:"name:status"`
	CreatedAt    time.Time        `db:"created_at" model:"name:created_at"`
	Avatar       sql.NullString   `db:"avatar" model:"name:avatar"`
	UpdatedAt    time.Time        `db:"updated_at" model:"name:updated_at"`
	VerifiedAt   sql.NullTime     `db:"verified_at" model:"name:verified_at"`
	BlockedAt    sql.NullTime     `db:"blocked_at" model:"name:blocked_at"`
	DeletedAt    sql.NullTime     `db:"deleted_at" model:"name:deleted_at"`
	LastAccessAt sql.NullTime     `db:"last_access_at" model:"name:last_access_at"`
}

User struct to describe a user object.

type UserRole

type UserRole struct {
	// Table meta data
	MetaData mb.MetaData `db:"-" model:"table:user_roles"`

	// Table fields
	ID        int       `db:"id" model:"name:id; type:int,primary"`
	RoleID    int       `db:"role_id" model:"name:role_id; type:int"`
	UserID    int       `db:"user_id" model:"name:user_id; type:int"`
	CreatedAt time.Time `db:"created_at" model:"name:created_at"`
}

UserRole struct to describe a user role object.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL