Cargo Features
afastorm 通过 Cargo features 选择数据库驱动、派生宏、迁移功能和可选 SQLx 类型。以下列表与
crate 的 Cargo.toml 保持一致。
默认功能
默认启用:
derive
sqlite
仅使用 PostgreSQL 时,关闭默认功能并启用 postgres。设置
default-features = false 后,需要显式启用应用所需的所有功能。
功能列表
| Feature | 默认 | 功能与说明 |
|---|---|---|
derive | 是 | 启用 #[derive(Model)],依赖 afastorm-derive;手动实现 Model 时可关闭。 |
sqlite | 是 | 启用 SQLx SQLite 驱动,识别 sqlite: URL。 |
postgres | 否 | 启用 SQLx PostgreSQL 驱动,识别 postgres:// 和 postgresql:// URL。 |
migrate | 否 | 启用迁移 API 和 afastorm CLI。 |
uuid | 否 | 支持 uuid::Uuid。 |
chrono | 否 | 支持文档列出的 chrono 日期和时间类型。 |
time | 否 | 支持 time crate 的日期和时间类型。 |
rust_decimal | 否 | 支持 rust_decimal::Decimal;SQLite 以文本存储,PostgreSQL 使用 NUMERIC。 |
bigdecimal | 否 | 支持 bigdecimal::BigDecimal;SQLite 以文本存储,PostgreSQL 使用 NUMERIC。 |
ipnetwork | 否 | 支持 IpAddr 和 ipnetwork::IpNetwork,同时启用 postgres。 |
ipnet | 否 | 支持 IpAddr 和 ipnet::IpNet,同时启用 postgres。 |
mac_address | 否 | 支持 mac_address::MacAddress,同时启用 postgres。 |
bit-vec | 否 | 支持 bit_vec::BitVec,同时启用 postgres。 |
serde_json::Value 始终可用,不需要额外的 JSON feature。SQLite 将 JSON 存为文本,PostgreSQL
默认映射为 JSONB。具体行为见SQLite 类型和PostgreSQL 类型。
ipnetwork、ipnet、mac_address 和 bit-vec 会在编译时启用 PostgreSQL feature,因为 SQLx
提供的相关类型集成面向 PostgreSQL。开启这些 feature 后仍可连接 SQLite,其 SQLite 映射使用文本。
可选 feature 只启用 afastorm 明确支持的模型字段类型,并不意味着 derive 支持 SQLx 的所有类型。 详情见模型。
示例
默认 SQLite 并启用迁移:
afastorm = { version = "0.0.1", features = ["migrate"] }
PostgreSQL、迁移、UUID 和 chrono:
afastorm = { version = "0.0.1", default-features = false, features = [
"derive", "postgres", "migrate", "uuid", "chrono"
] }