MySQL

Provides an API for accessing MySQL databases

Technology

The mysql module provides MySQL database integration for Project Forge applications. This module extends the base database module with MySQL-specific functionality, including driver integration, connection management, and MySQL-optimized features.

Overview

This module adds MySQL/MariaDB database support to Project Forge applications and requires the database module. It provides:

Key Features

MySQL Compatibility

Performance

Security

Configuration

Environment Variables

The module reads configuration from environment variables (with optional prefix):

Usage

Basic Setup

1
2
3
4
5
6
7
8
9
// Load configuration from environment
params := MySQLParamsFromEnv("")

// Open database connection
db, err := database.OpenMySQLDatabase(params)
if err != nil {
return err
}
defer db.Close()

Custom Configuration

 1
2
3
4
5
6
7
8
9
10
11
12
// Manual configuration
params := &MySQLParams{
Host: "mysql.example.com",
Port: 3306,
User: "app_user",
Password: "secure_password",
Database: "app_database",
MaxConnections: 25,
Debug: false,
}

db, err := database.OpenMySQLDatabase(params)

With Environment Prefix

1
2
3
// Use custom environment variable prefix
// Reads myapp_db_host, myapp_db_port, etc.
params := MySQLParamsFromEnv("myapp_")

MySQL-Specific Features

JSON Support

MySQL’s native JSON data type is fully supported:

1
2
3
4
5
6
// Insert JSON data
_, err := db.Exec(`insert into users (data) values (?)`,
`{"name": "John", "preferences": {"theme": "dark"}}`)

// Query with JSON functions
rows, err := db.Query(`select json_extract(data, '$.name') from users`)

Character Set Handling

The module automatically configures UTF8MB4 for full Unicode support:

1
2
// Automatic UTF8MB4 configuration
// Supports emojis and 4-byte Unicode characters

Dependencies

This module requires:

Production Considerations

Connection Pooling

Performance Tuning

Source Code

See Also