跳转到内容

Database 系统

SQL 数据库连接池和配置。支持 PostgreSQL、MySQL 和 SQLite。

Kind描述
db.sql.postgresPostgreSQL 数据库
db.sql.mysqlMySQL 数据库
db.sql.sqliteSQLite 数据库
src/data/_index.yaml
version: "1.0"
namespace: app.data
entries:
- name: main_db
kind: db.sql.postgres
host: "localhost"
port: 5432
database: "myapp"
username: "dbuser"
password: "dbpass"
pool:
max_open: 25
max_idle: 5
max_lifetime: "1h"
options:
sslmode: "disable"
lifecycle:
auto_start: true
- name: cache_db
kind: db.sql.sqlite
file: "/var/data/cache.db" # Use :memory: for in-memory
pool:
max_open: 1
max_idle: 1
max_lifetime: "1h"
options:
cache: "shared"
lifecycle:
auto_start: true
字段类型描述
hoststring数据库主机地址
portint数据库端口号
databasestring数据库名称
usernamestring数据库用户
passwordstring数据库密码
poolobject连接池设置
optionsmap数据库特定选项
lifecycleobject生命周期配置
字段类型描述
filestring数据库文件路径或 :memory:
poolobject连接池设置
optionsmapSQLite 特定选项
lifecycleobject生命周期配置

使用 _env 后缀从环境变量或 env.variable entry 加载值:

字段描述
host_env从环境变量获取主机
port_env从环境变量获取端口
database_env从环境变量获取数据库名称
username_env从环境变量获取用户名
password_env从环境变量获取密码
- name: prod_db
kind: db.sql.postgres
host_env: "DB_HOST"
port_env: "DB_PORT"
database_env: "DB_NAME"
username_env: "DB_USER"
password_env: "app.secrets:db_password" # Reference env.variable entry
避免在配置中硬编码密码。使用环境变量或 env.variable entry 来管理凭据。参见 Environment 了解安全的密钥管理。

配置连接池行为。池设置映射到 Go 的 database/sql 连接池

字段类型默认值描述
max_openint0最大打开连接数(0 = 无限制)
max_idleint0最大空闲连接数(0 = 无限制)
max_lifetimeduration1h连接最大生命周期
pool:
max_open: 25 # Limit concurrent connections
max_idle: 5 # Keep 5 connections ready
max_lifetime: "30m" # Recycle connections every 30 minutes
max_idle 设置为小于或等于 max_open。超过 max_lifetime 的连接会被关闭并替换,有助于恢复过期连接。

每种数据库类型从配置构建 DSN:

postgres://username:password@host:port/database?sslmode=disable
username:password@tcp(host:port)/database?charset=utf8mb4
file:/path/to/database.db?cache=shared
:memory:?mode=memory

常见的数据库特定选项:

options:
sslmode: "require" # disable, require, verify-ca, verify-full
connect_timeout: "10" # Connection timeout in seconds
application_name: "myapp"
options:
charset: "utf8mb4"
parseTime: "true" # Parse time values to time.Time
loc: "Local" # Timezone
options:
cache: "shared" # shared, private
mode: "rwc" # ro, rw, rwc, memory
_journal_mode: "WAL" # DELETE, TRUNCATE, PERSIST, MEMORY, WAL, OFF
- name: secure_postgres
kind: db.sql.postgres
host: "db.example.com"
port: 5432
database: "production"
username: "app_user"
password: "${DB_PASSWORD}"
pool:
max_open: 50
max_idle: 10
max_lifetime: "1h"
options:
sslmode: "verify-full"
sslcert: "/certs/client.crt"
sslkey: "/certs/client.key"
sslrootcert: "/certs/ca.crt"
lifecycle:
auto_start: true
- name: mysql_replica
kind: db.sql.mysql
host: "replica.db.example.com"
port: 3306
database: "app"
username: "readonly"
password_env: "REPLICA_PASSWORD"
pool:
max_open: 20
max_idle: 5
max_lifetime: "30m"
options:
charset: "utf8mb4"
parseTime: "true"
readTimeout: "30s"
- name: test_db
kind: db.sql.sqlite
file: ":memory:"
pool:
max_open: 1
max_idle: 1
options:
cache: "shared"
mode: "memory"
entries:
# Primary database
- name: users_db
kind: db.sql.postgres
host_env: "USERS_DB_HOST"
port: 5432
database: "users"
username_env: "USERS_DB_USER"
password_env: "USERS_DB_PASSWORD"
lifecycle:
auto_start: true
# Analytics database
- name: analytics_db
kind: db.sql.mysql
host_env: "ANALYTICS_DB_HOST"
port: 3306
database: "analytics"
username_env: "ANALYTICS_DB_USER"
password_env: "ANALYTICS_DB_PASSWORD"
lifecycle:
auto_start: true
# Local cache
- name: cache
kind: db.sql.sqlite
file: "/var/cache/app.db"
lifecycle:
auto_start: true

数据库可以在运行时使用 registry 模块 注册,支持基于应用状态或外部配置的动态数据库配置。

参见 SQL 模块 了解数据库操作 API。

  • SQL 模块 - Lua API 参考
  • Store - 基于 database.sql 的键值存储
  • Queue - SQL 支持的队列处理器