MySQL
MySQL backend for environments using MySQL or MariaDB databases.
Installation
Add the MySQL backend to your target:
.product(name: "MySQLBackend", package: "SwiftDataServer.SDK")
Configuration
import SwiftDataServer
import MySQLBackend
let backend = MySQLBackend(
hostname: "localhost",
port: 3306,
username: "root",
password: "password",
database: "myapp"
)
try await backend.connect()
Environment Variables
let backend = MySQLBackend(
hostname: Environment.get("DB_HOST") ?? "localhost",
port: Environment.get("DB_PORT").flatMap(Int.init) ?? 3306,
username: Environment.get("DB_USER") ?? "root",
password: Environment.get("DB_PASS") ?? "password",
database: Environment.get("DB_NAME") ?? "myapp"
)
Type Mappings
| Swift Type | MySQL Type |
|---|---|
String | TEXT |
Int | BIGINT |
Int32 | INT |
Int16 | SMALLINT |
Double | DOUBLE |
Float | FLOAT |
Bool | TINYINT(1) |
Date | DATETIME |
UUID | CHAR(36) |
Data | BLOB |
[Codable] | JSON |
Connection Pooling
Set the pool size with DatabaseConfiguration and use the configuration: initializer:
let config = DatabaseConfiguration(
hostname: "localhost",
port: 3306,
username: "root",
password: "password",
database: "myapp",
maxConnections: 20, // pool size (default 10)
connectionTimeout: 30 // seconds (default 30)
)
let backend = MySQLBackend(configuration: config)
Requirements
- MySQL 8.0 or later
- MariaDB 10.5 or later (compatible)
Next Steps
- Migrations - Manage schema changes
- PostgreSQL - Alternative database