dbnexus 0.1.3

An enterprise-grade database abstraction layer for Rust with built-in permission control and connection pooling
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
# DBNexus 用户指南

在应用程序中使用 DBNexus 的完整指南。

## 目录

- [入门指南]#入门指南
- [安装]#安装
- [配置]#配置
- [定义实体]#定义实体
- [使用连接]#使用连接
- [CRUD 操作]#crud-操作
- [权限控制]#权限控制
- [事务]#事务
- [高级特性]#高级特性
- [最佳实践]#最佳实践
- [故障排除]#故障排除

---

## 入门指南

本指南将带您从零开始到生产就绪的数据库使用。

### 您将学到

- 如何在项目中设置 DBNexus
- 如何定义数据库实体
- 如何执行 CRUD 操作
- 如何实现基于角色的访问控制
- 如何配置和优化连接
- 如何使用缓存和指标等高级特性

### 先决条件

- Rust 1.85 或更高版本
- Rust 和 SQL 基础知识
- 数据库(PostgreSQL、MySQL 或 SQLite)

---

## 安装

### 1. 添加依赖

添加到您的 `Cargo.toml`:

```toml
[dependencies]
dbnexus = "0.1.2"
tokio = { version = "1.42", features = ["rt-multi-thread", "macros"] }
```

### 2. 选择特性

选择您需要的特性:

```toml
# 嵌入式设备最小配置
dbnexus = { version = "0.1.2", default-features = false, features = ["minimal"] }

# 带企业特性的 PostgreSQL
dbnexus = { version = "0.1.2", features = [
    "postgres",
    "permission",
    "metrics",
    "tracing",
    "audit"
] }

# 带基础特性的 SQLite
dbnexus = { version = "0.1.2", features = ["sqlite", "permission", "sql-parser"] }
```

完整特性列表请参见 [README.md](README.md#feature-flags)。

### 3. 启用数据库驱动

选择一个数据库驱动:

```toml
# SQLite(默认)
dbnexus = { version = "0.1.2", features = ["sqlite"] }

# PostgreSQL
dbnexus = { version = "0.1.2", features = ["postgres"] }

# MySQL
dbnexus = { version = "0.1.2", features = ["mysql"] }
```

**重要:**一次只能启用一个数据库驱动。

### 4. 验证安装

```rust
use dbnexus::DbPool;

fn main() {
    println!("DBNexus 已准备就绪!");
}
```

---

## 配置

### 使用环境变量快速开始

配置 DBNexus 最简单的方法是使用环境变量。

#### 步骤 1:设置环境变量

```bash
export DATABASE_URL="postgresql://user:password@localhost/mydb"
export DB_MAX_CONNECTIONS=20
export DB_MIN_CONNECTIONS=5
export DB_ADMIN_ROLE=admin
```

#### 步骤 2:加载配置

```rust
use dbnexus::DbPool;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let pool = DbPool::new().await?;
    println!("已连接!");
    Ok(())
}
```

### 使用配置文件

#### YAML 配置

创建 `dbnexus.yaml`:

```yaml
url: "postgresql://localhost/mydb"
max_connections: 20
min_connections: 5
idle_timeout: 300
acquire_timeout: 5000
auto_migrate: true
admin_role: admin
```

#### 加载 YAML 配置

```rust
use dbnexus::config::DbConfig;

let config = DbConfig::from_yaml_file("dbnexus.yaml")?;
let pool = DbPool::with_config(config).await?;
```

#### TOML 配置

创建 `dbnexus.toml`:

```toml
url = "postgresql://localhost/mydb"
max_connections = 20
min_connections = 5
idle_timeout = 300
acquire_timeout = 5000
auto_migrate = true
admin_role = "admin"
```

#### 加载 TOML 配置

```rust
#[cfg(feature = "config-toml")]
use dbnexus::config::DbConfig;

let config = DbConfig::from_toml_file("dbnexus.toml")?;
let pool = DbPool::with_config(config).await?;
```

### 使用构建器模式

对于程序化配置:

```rust
use dbnexus::{DbPool, config::DbConfigBuilder};

let config = DbConfigBuilder::new()
    .url("postgresql://localhost/mydb")
    .max_connections(20)
    .min_connections(5)
    .idle_timeout(300)
    .acquire_timeout(5000)
    .auto_migrate(true)
    .admin_role("admin")
    .build()?;

let pool = DbPool::with_config(config).await?;
```

### 配置参数

| 参数 | 类型 | 默认值 | 描述 |
|-----------|-------|----------|-------------|
| `url` | `String` | 必需 | 数据库连接 URL |
| `max_connections` | `u32` | 20 | 最大池大小 |
| `min_connections` | `u32` | 5 | 最小池大小 |
| `idle_timeout` | `u64` | 300 | 空闲连接超时(秒) |
| `acquire_timeout` | `u64` | 5000 | 连接获取超时(毫秒) |
| `permissions_path` | `Option<String>` | None | 权限配置路径 |
| `migrations_dir` | `Option<PathBuf>` | None | 迁移目录 |
| `auto_migrate` | `bool` | false | 自动运行迁移 |
| `migration_timeout` | `u64` | 60 | 迁移超时(秒) |
| `admin_role` | `String` | "admin" | 管理员角色名称 |

---

## 定义实体

### 基本实体定义

定义映射到数据库表的结构体:

```rust
use dbnexus::{DbEntity, db_crud};
use sea_orm::entity::prelude::*;

#[derive(DbEntity, DeriveEntityModel, DeriveModel, DeriveActiveModel)]
#[sea_orm(table_name = "users")]
#[db_crud]
pub struct User {
    #[sea_orm(primary_key)]
    pub id: i64,
    pub name: String,
    pub email: String,
    pub created_at: chrono::DateTime<chrono::Utc>,
}
```

**必需属性:**

- `#[derive(DbEntity, DeriveEntityModel, DeriveModel, DeriveActiveModel)]` - 启用 DBNexus 和 Sea-ORM 实体特性
- `#[sea_orm(table_name = "...")]` - 指定表名
- `#[sea_orm(primary_key)]` - 标记主键字段

**可选属性:**

- `#[db_crud]` - 生成 CRUD 方法
- `#[db_permission(...)]` - 添加权限控制
- `#[db_cache]` - 启用缓存(需要 `cache` 特性)
- `#[db_audit]` - 启用审计日志(需要 `audit` 特性)

### 带权限控制的实体

添加基于角色的访问控制:

```rust
use dbnexus::{DbEntity, db_crud, db_permission};
use sea_orm::entity::prelude::*;

#[derive(DbEntity, DeriveEntityModel, DeriveModel, DeriveActiveModel)]
#[sea_orm(table_name = "users")]
#[db_crud]
#[db_permission(roles = ["admin", "manager"])]
pub struct User {
    #[sea_orm(primary_key)]
    pub id: i64,
    pub name: String,
}
```

### 带操作级控制的实体

指定允许的操作:

```rust
use dbnexus::{DbEntity, db_crud, db_permission};
use sea_orm::entity::prelude::*;

#[derive(DbEntity, DeriveEntityModel, DeriveModel, DeriveActiveModel)]
#[sea_orm(table_name = "users")]
#[db_crud]
#[db_permission(
    roles = ["admin", "manager"],
    operations = ["SELECT", "INSERT", "UPDATE"]
)]
pub struct User {
    #[sea_orm(primary_key)]
    pub id: i64,
    pub name: String,
}
```

### 带缓存的实体

为读操作启用缓存:

```rust
use dbnexus::{DbEntity, db_crud};
use sea_orm::entity::prelude::*;

#[derive(DbEntity, DeriveEntityModel, DeriveModel, DeriveActiveModel)]
#[sea_orm(table_name = "users")]
#[db_crud]
#[db_cache]
pub struct User {
    #[sea_orm(primary_key)]
    pub id: i64,
    pub name: String,
}
```

### 带审计日志的实体

为所有操作启用审计日志:

```rust
use dbnexus::{DbEntity, db_crud};
use sea_orm::entity::prelude::*;

#[derive(DbEntity, DeriveEntityModel, DeriveModel, DeriveActiveModel)]
#[sea_orm(table_name = "users")]
#[db_crud]
#[db_audit]
pub struct User {
    #[sea_orm(primary_key)]
    pub id: i64,
    pub name: String,
}
```

### 复杂实体示例

```rust
use dbnexus::{DbEntity, db_crud, db_permission, db_cache, db_audit};
use sea_orm::entity::prelude::*;

#[derive(DbEntity, DeriveEntityModel, DeriveModel, DeriveActiveModel)]
#[sea_orm(table_name = "orders")]
#[db_crud]
#[db_permission(
    roles = ["admin", "sales_manager"],
    operations = ["SELECT", "INSERT", "UPDATE", "DELETE"]
)]
#[db_cache]
#[db_audit]
pub struct Order {
    #[sea_orm(primary_key)]
    pub id: i64,
    pub user_id: i64,
    pub amount: f64,
    pub status: String,
    pub created_at: chrono::DateTime<chrono::Utc>,
    pub updated_at: chrono::DateTime<chrono::Utc>,
}
```

---

## 使用连接

### 获取会话

使用 `get_session()` 获取数据库连接:

```rust
use dbnexus::DbPool;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let pool = DbPool::new("postgresql://localhost/mydb").await?;

    // 获取带角色的会话
    let session = pool.get_session("admin").await?;

    // 使用会话...
    // 连接在丢弃时自动释放

    Ok(())
}
```

### 会话生命周期

会话是 RAII 管理的:

```rust
{
    let session = pool.get_session("admin").await?;
    // 连接在此处活跃

    User::find_all(&session).await?;
    User::insert(&session, new_user).await?;

} // 连接在此处自动释放
```

### 检查池状态

监控连接池健康:

```rust
let status = pool.status();

println!("总连接数: {}", status.total);
println!("活跃连接数: {}", status.active);
println!("空闲连接数: {}", status.idle);
println!("等待计数: {}", status.wait_count);
println!("观察到的最大活跃数: {}", status.max_active);
```

### 手动健康检查

触发连接池健康检查:

```rust
let invalid_count = pool.clean_invalid_connections().await?;
println!("移除了 {} 个无效连接", invalid_count);
```

---

## CRUD 操作

### 创建(插入)

插入新记录:

```rust
let user = User {
    id: 1,
    name: "Alice".to_string(),
    email: "alice@example.com".to_string(),
    created_at: chrono::Utc::now(),
};

let inserted = User::insert(&session, user).await?;
println!("插入用户: {}", inserted.name);
```

### 读取(选择)

#### 按主键查找

```rust
let user = User::find_by_id(&session, 1).await?;
if let Some(user) = user {
    println!("找到用户: {}", user.name);
}
```

#### 查找所有记录

```rust
let users = User::find_all(&session).await?;
println!("找到 {} 个用户", users.len());
```

#### 按条件查找

```rust
use dbnexus::entity::*;

let condition = Condition::all()
    .add(Column::Name.like("%Alice%"))
    .add(Column::CreatedAt.gte(chrono::Utc::now() - chrono::Duration::days(7)));

let users = User::find_by_condition(&session, condition).await?;
```

#### 记录计数

```rust
let count = User::count(&session).await?;
println!("总用户数: {}", count);
```

### 更新

更新现有记录:

```rust
let mut user = User::find_by_id(&session, 1).await?.unwrap();
user.email = "alice_new@example.com".to_string();
user.updated_at = chrono::Utc::now();

let updated = User::update(&session, user).await?;
println!("更新用户: {}", updated.email);
```

### 删除

#### 按主键删除

```rust
User::delete(&session, 1).await?;
println!("删除了 ID 为 1 的用户");
```

#### 按条件删除

```rust
use dbnexus::entity::*;

let condition = Column::CreatedAt.lt(chrono::Utc::now() - chrono::Duration::days(365));
let deleted_count = User::delete_many(&session, condition).await?;
println!("删除了 {} 个旧用户", deleted_count);
```

---

## 权限控制

### 设置权限

创建 `permissions.yaml` 文件:

```yaml
roles:
  admin:
    tables:
      - name: "*"
        operations:
          - select
          - insert
          - update
          - delete

  manager:
    tables:
      - name: "users"
        operations:
          - select
          - insert
          - update
      - name: "orders"
        operations:
          - select

  user:
    tables:
      - name: "users"
        operations:
          - select
```

### 在实体上定义权限

```rust
use dbnexus::{DbEntity, db_crud, db_permission};
use sea_orm::entity::prelude::*;

#[derive(DbEntity, DeriveEntityModel, DeriveModel, DeriveActiveModel)]
#[sea_orm(table_name = "users")]
#[db_crud]
#[db_permission(roles = ["admin", "manager"])]
pub struct User {
    #[sea_orm(primary_key)]
    pub id: i64,
    pub name: String,
}
```

### 使用权限

```rust
// 管理员可以做任何事
let admin_session = pool.get_session("admin").await?;
User::insert(&admin_session, user).await?;
User::delete(&admin_session, 1).await?;

// 经理只能对用户进行选择/插入/更新
let manager_session = pool.get_session("manager").await?;
User::find_all(&manager_session).await?; // OK
User::insert(&manager_session, user).await?; // OK
User::delete(&manager_session, 1).await?; // 错误:权限被拒绝

// 用户只能选择
let user_session = pool.get_session("user").await?;
User::find_all(&user_session).await?; // OK
User::insert(&user_session, user).await?; // 错误:权限被拒绝
```

### 通配符表

使用 `"*"` 授予对所有表的访问权限:

```yaml
roles:
  admin:
    tables:
      - name: "*"  # 所有表
        operations:
          - select
          - insert
          - update
          - delete
```

### 操作级控制

限制特定操作:

```yaml
roles:
  readonly:
    tables:
      - name: "reports"
        operations:
          - select  # 只允许 SELECT
```

---

## 事务

### 基本事务

开始、提交和回滚:

```rust
use dbnexus::DbPool;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let pool = DbPool::new("postgresql://localhost/mydb").await?;
    let mut session = pool.get_session("admin").await?;

    // 开始事务
    session.begin_transaction().await?;

    // 执行操作
    User::insert(&session, user1).await?;
    User::insert(&session, user2).await?;

    // 提交事务
    session.commit().await?;

    Ok(())
}
```

### 带错误处理的事务

```rust
session.begin_transaction().await?;

match perform_operations(&session).await {
    Ok(_) => {
        session.commit().await?;
    }
    Err(e) => {
        eprintln!("错误: {}", e);
        session.rollback().await?;
    }
}
```

### RAII 事务守护

使用 RAII 模式进行自动回滚:

```rust
use dbnexus::DbPool;

struct TransactionGuard<'a> {
    session: &'a mut Session,
}

impl<'a> TransactionGuard<'a> {
    fn new(session: &'a mut Session) -> Self {
        session.begin_transaction().await.ok();
        Self { session }
    }

    pub async fn commit(self) {
        self.session.commit().await.ok();
    }
}

impl<'a> Drop for TransactionGuard<'a> {
    fn drop(&mut self) {
        if self.session.is_in_transaction() {
            let _ = self.session.rollback().now_or_never();
        }
    }
}

// 使用
{
    let tx = TransactionGuard::new(&mut session);
    User::insert(&session, user).await?;
    tx.commit().await; // 显式提交
} // 或在丢弃时隐式回滚
```

---

## 高级特性

### 缓存

为读密集型操作启用缓存:

```rust
use dbnexus::{DbEntity, db_crud};
use sea_orm::entity::prelude::*;

#[derive(DbEntity, DeriveEntityModel, DeriveModel, DeriveActiveModel)]
#[sea_orm(table_name = "products")]
#[db_crud]
#[db_cache]
pub struct Product {
    #[sea_orm(primary_key)]
    pub id: i64,
    pub name: String,
    pub price: f64,
}

// 使用缓存读取
let product = Product::find_cached(&session, 1).await?;

// 写入时使缓存失效
Product::invalidate_cache(&session, 1).await?;
```

### 指标

启用 Prometheus 指标:

```toml
[dependencies.dbnexus]
version = "0.1.2"
features = ["metrics"]
```

收集和导出指标:

```rust
use dbnexus::metrics::MetricsCollector;

let collector = MetricsCollector::new(&pool);

// 获取池指标
let pool_metrics = collector.get_pool_metrics();
println!("活跃连接: {}", pool_metrics.active);

// 获取查询指标
let query_metrics = collector.get_query_metrics();
println!("P99 延迟: {}ms", query_metrics.latency_p99);

// 导出 Prometheus 格式
let prometheus_metrics = collector.export_prometheus();
println!("{}", prometheus_metrics);
```

### 审计日志

启用审计日志:

```toml
[dependencies.dbnexus]
version = "0.1.2"
features = ["audit"]
```

使用 `#[db_audit]` 自动进行审计日志:

```rust
use dbnexus::{DbEntity, db_crud};
use sea_orm::entity::prelude::*;

#[derive(DbEntity, DeriveEntityModel, DeriveModel, DeriveActiveModel)]
#[sea_orm(table_name = "sensitive_data")]
#[db_crud]
#[db_audit]
pub struct SensitiveData {
    #[sea_orm(primary_key)]
    pub id: i64,
    pub data: String,
}

// 所有操作自动记录
SensitiveData::insert(&session, data).await?;
SensitiveData::find_by_id(&session, 1).await?;
```

### 分布式跟踪

启用 OpenTelemetry 跟踪:

```toml
[dependencies.dbnexus]
version = "0.1.2"
features = ["tracing"]
```

初始化跟踪:

```rust
use dbnexus::tracing::TracingGuard;

// 初始化跟踪
let _guard = TracingGuard::init_with_otlp("http://localhost:4317")?;

// 所有 DB 操作自动跟踪
let session = pool.get_session("admin").await?;
User::find_all(&session).await?;
```

---

## 最佳实践

### 1. 对敏感数据使用环境变量

永远不要硬编码凭据:

```rust
// ❌ 不好
let url = "postgresql://user:password@localhost/db";

// ✅ 好
let url = std::env::var("DATABASE_URL")?;
```

### 2. 对多步操作始终使用事务

```rust
// ❌ 不好
User::insert(&session, user1).await?;
User::insert(&session, user2).await?;
// 如果 user2 失败,user1 仍然插入!

// ✅ 好
session.begin_transaction().await?;
User::insert(&session, user1).await?;
User::insert(&session, user2).await?;
session.commit().await?;
// 要么都成功,要么都失败
```

### 3. 使用 RAII 进行资源管理

```rust
// ❌ 不好
let session = pool.get_session("admin").await?;
User::find_all(&session).await?;
pool.release_connection(session); // 容易忘记

// ✅ 好
{
    let session = pool.get_session("admin").await?;
    User::find_all(&session).await?;
} // 连接自动释放
```

### 4. 配置适当的池大小

对于低流量应用程序:
```rust
.min_connections(1)
.max_connections(5)
```

对于高流量应用程序:
```rust
.min_connections(10)
.max_connections(100)
```

### 5. 使用权限控制

即使是内部工具:

```rust
// ❌ 不好
let session = pool.get_session("root").await?;
// 完全访问,无检查

// ✅ 好
let session = pool.get_session("read_only").await?;
// 有限访问,明确权限
```

### 6. 监控池状态

定期检查池健康:

```rust
let status = pool.status();
if status.wait_count > 1000 {
    eprintln!("警告:高连接等待计数");
}
```

### 7. 优雅处理错误

```rust
// ❌ 不好
let user = User::find_by_id(&session, 1).await?.unwrap();
// 错误时恐慌

// ✅ 好
match User::find_by_id(&session, 1).await {
    Ok(Some(user)) => { /* 使用用户 */ }
    Ok(None) => { /* 处理未找到 */ }
    Err(e) => { /* 处理错误 */ }
}
```

### 8. 使用类型安全操作

```rust
// ❌ 不好
session.execute("DELETE FROM users WHERE id = 1").await?;
// 无类型安全,容易出错

// ✅ 好
User::delete(&session, 1).await?;
// 类型安全,自动权限检查
```

---

## 故障排除

### 连接池耗尽

**症状:** `DbError::ConnectionPool("Connection pool exhausted")`

**解决方案:**

1. 增加池大小:
   ```rust
   .max_connections(50)
   ```

2. 检查连接泄漏:
   ```rust
   let status = pool.status();
   println!("活跃: {}, 总计: {}", status.active, status.total);
   ```

3. 确保会话被丢弃:
   ```rust
   // 使用 RAII 模式
   {
       let session = pool.get_session("admin").await?;
       // 使用会话...
   } // 连接释放
   ```

### 权限被拒绝错误

**症状:** `DbError::Permission("Permission denied...")`

**解决方案:**

1. 检查角色是否在权限配置中:
   ```yaml
   roles:
     my_role:  # 必须完全匹配
   ```

2. 验证操作是否被允许:
   ```yaml
   roles:
     my_role:
       tables:
         - name: "users"
           operations:
             - select  # 确保操作被列出
   ```

3. 检查角色大小写:
   ```rust
   // 必须与配置完全匹配
   let session = pool.get_session("My_Role").await?; // ❌ 错误
   let session = pool.get_session("my_role").await?; // ✅ 正确
   ```

### 数据库连接错误

**症状:** `DbError::Connection("Failed to connect...")`

**解决方案:**

1. 验证 URL 格式:
   ```bash
   # SQLite
   sqlite::memory:
   sqlite:///path/to/db

   # PostgreSQL
   postgresql://user:password@host:port/database

   # MySQL
   mysql://user:password@host:port/database
   ```

2. 检查网络连接:
   ```bash
   ping postgres-server
   telnet postgres-server 5432
   ```

3. 验证凭据和权限:
   ```bash
   psql -U username -h postgres-server -d database
   ```

### 慢查询性能

**症状:** 查询耗时过长

**解决方案:**

1. 启用并检查指标:
   ```rust
   let collector = MetricsCollector::new(&pool);
   println!("P99 延迟: {}ms", collector.get_query_metrics().latency_p99);
   ```

2. 使用索引:
   ```sql
   CREATE INDEX idx_users_email ON users(email);
   ```

3. 优化查询:
   ```rust
   // ❌ 不好:获取所有
   let users = User::find_all(&session).await?;
   let filtered: Vec<_> = users.into_iter()
       .filter(|u| u.name.contains("Alice"))
       .collect();

   // ✅ 好:在数据库中过滤
   let users = User::find_by_condition(&session, Column::Name.like("%Alice%")).await?;
   ```

### 高内存使用

**症状:** 应用程序使用过多内存

**解决方案:**

1. 减少池大小:
   ```rust
   .max_connections(10)
   ```

2. 启用连接空闲超时:
   ```rust
   .idle_timeout(60)  // 更快关闭空闲连接
   ```

3. 检查缓存大小:
   ```rust
   #[db_cache(capacity = 100)]  // 限制缓存条目
   ```

---

## 示例:完整应用程序

```rust
use dbnexus::{DbPool, DbEntity, db_crud, db_permission};
use sea_orm::entity::prelude::*;
use chrono::Utc;

#[derive(DbEntity, DeriveEntityModel, DeriveModel, DeriveActiveModel)]
#[sea_orm(table_name = "users")]
#[db_crud]
#[db_permission(roles = ["admin", "manager"])]
pub struct User {
    #[sea_orm(primary_key)]
    pub id: i64,
    pub name: String,
    pub email: String,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // 初始化连接池
    let pool = DbPool::new("postgresql://localhost/mydb").await?;
    println!("✓ 已连接到数据库");

    // 管理员操作
    {
        let admin_session = pool.get_session("admin").await?;
        println!("✓ 已获取管理员会话");

        // 插入用户
        let user1 = User {
            id: 1,
            name: "Alice".to_string(),
            email: "alice@example.com".to_string(),
        };
        User::insert(&admin_session, user1).await?;
        println!("✓ 插入用户: Alice");

        let user2 = User {
            id: 2,
            name: "Bob".to_string(),
            email: "bob@example.com".to_string(),
        };
        User::insert(&admin_session, user2).await?;
        println!("✓ 插入用户: Bob");
    }

    // 经理操作
    {
        let manager_session = pool.get_session("manager").await?;
        println!("✓ 已获取经理会话");

        // 查询用户
        let users = User::find_all(&manager_session).await?;
        println!("✓ 找到 {} 个用户", users.len());

        // 更新用户
        if let Some(mut user) = User::find_by_id(&manager_session, 1).await? {
            user.email = "alice_new@example.com".to_string();
            User::update(&manager_session, user).await?;
            println!("✓ 更新用户邮箱");
        }
    }

    // 池状态
    let status = pool.status();
    println!("\n📊 池状态:");
    println!("  总计: {}", status.total);
    println!("  活跃: {}", status.active);
    println!("  空闲: {}", status.idle);

    println!("\n✨ 应用程序成功完成!");

    Ok(())
}
```

---

更多信息:
- [API 参考]API_REFERENCE.md
- [架构]ARCHITECTURE.md
- [Rust 文档]https://docs.rs/dbnexus