befly 3.10.13 → 3.10.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/befly.config.ts CHANGED
@@ -39,8 +39,7 @@ const defaultOptions: BeflyOptions = {
39
39
  username: "root",
40
40
  password: "root",
41
41
  database: "befly_demo",
42
- poolMax: 10,
43
- debug: 0
42
+ poolMax: 10
44
43
  },
45
44
 
46
45
  // ========== Redis 配置 ==========
package/docs/api/api.md CHANGED
@@ -518,14 +518,14 @@ export default {
518
518
 
519
519
  const result = await befly.db.getList({
520
520
  table: "article",
521
- columns: ["id", "title", "summary", "createdAt"],
521
+ fields: ["id", "title", "summary", "createdAt"],
522
522
  where: where,
523
523
  page: page || 1,
524
524
  limit: limit || 10,
525
- orderBy: { id: "desc" }
525
+ orderBy: ["id#DESC"]
526
526
  });
527
527
 
528
- return befly.tool.Yes("获取成功", result);
528
+ return befly.tool.Yes("获取成功", result.data);
529
529
  }
530
530
  } as ApiRoute;
531
531
  ```
@@ -542,11 +542,13 @@ export default {
542
542
  },
543
543
  required: ["id"],
544
544
  handler: async (befly, ctx) => {
545
- const article = await befly.db.getDetail({
545
+ const articleRes = await befly.db.getOne({
546
546
  table: "article",
547
547
  where: { id: ctx.body.id, state: 1 }
548
548
  });
549
549
 
550
+ const article = articleRes.data;
551
+
550
552
  if (!article?.id) {
551
553
  return befly.tool.No("文章不存在");
552
554
  }
@@ -1336,29 +1338,23 @@ interface BeflyContext {
1336
1338
 
1337
1339
  ### 常用工具方法
1338
1340
 
1339
- #### befly.db.cleanFields - 清理数据字段
1341
+ #### fieldClear - 清理数据字段
1340
1342
 
1341
- 清理对象中的 `null` 和 `undefined` 值,适用于处理可选参数:
1343
+ 清理对象中的指定值(常用:`null/undefined`),适用于处理可选参数。
1342
1344
 
1343
1345
  ```typescript
1344
- // 方法签名
1345
- befly.db.cleanFields<T>(
1346
- data: T, // 要清理的数据对象
1347
- excludeValues?: any[], // 要排除的值,默认 [null, undefined]
1348
- keepValues?: Record<string, any> // 强制保留的键值对
1349
- ): Partial<T>
1350
- ```
1346
+ import { fieldClear } from "befly/utils/fieldClear";
1351
1347
 
1352
- **基本用法:**
1353
-
1354
- ```typescript
1355
- // 默认排除 null 和 undefined
1356
- const cleanData = befly.db.cleanFields({
1357
- name: "John",
1358
- age: null,
1359
- email: undefined,
1360
- phone: ""
1361
- });
1348
+ // 常用:排除 null 和 undefined
1349
+ const cleanData = fieldClear(
1350
+ {
1351
+ name: "John",
1352
+ age: null,
1353
+ email: undefined,
1354
+ phone: ""
1355
+ },
1356
+ { excludeValues: [null, undefined] }
1357
+ );
1362
1358
  // 结果: { name: 'John', phone: '' }
1363
1359
  ```
1364
1360
 
@@ -1366,19 +1362,22 @@ const cleanData = befly.db.cleanFields({
1366
1362
 
1367
1363
  ```typescript
1368
1364
  // 同时排除 null、undefined 和空字符串
1369
- const cleanData = befly.db.cleanFields({ name: "John", phone: "", age: null }, [null, undefined, ""]);
1365
+ const cleanData = fieldClear({ name: "John", phone: "", age: null }, { excludeValues: [null, undefined, ""] });
1370
1366
  // 结果: { name: 'John' }
1371
1367
  ```
1372
1368
 
1373
1369
  **保留特定字段的特定值:**
1374
1370
 
1375
1371
  ```typescript
1376
- // 即使值在排除列表中,也保留 status 字段的 null
1377
- const cleanData = befly.db.cleanFields({ name: "John", status: null, count: 0 }, [null, undefined], { status: null });
1378
- // 结果: { name: 'John', status: null, count: 0 }
1372
+ // 保留 count 0
1373
+ const cleanData = fieldClear(
1374
+ { name: "John", status: null, count: 0 },
1375
+ { excludeValues: [null, undefined], keepMap: { count: 0 } }
1376
+ );
1377
+ // 结果: { name: 'John', count: 0 }
1379
1378
  ```
1380
1379
 
1381
- > **注意**:`insData`、`updData` 和 `where` 条件会自动调用 `cleanFields`,通常无需手动调用。
1380
+ > **注意**:DbHelper 的写入(`insData/insBatch/updData`)与查询条件(`where`)会自动过滤 `null/undefined`,通常无需手动调用。
1382
1381
 
1383
1382
  ---
1384
1383
 
@@ -1562,15 +1561,16 @@ export default {
1562
1561
  required: ["productId", "quantity"],
1563
1562
  handler: async (befly, ctx) => {
1564
1563
  // 使用事务确保库存扣减和订单创建的原子性
1565
- const result = await befly.db.transaction(async (trx) => {
1564
+ const result = await befly.db.trans(async (trx) => {
1566
1565
  // 1. 查询商品信息(带锁)
1567
- const product = await trx.getOne({
1566
+ const productRes = await trx.getOne({
1568
1567
  table: "product",
1569
- where: { id: ctx.body.productId },
1570
- forUpdate: true // 行锁
1568
+ where: { id: ctx.body.productId }
1571
1569
  });
1572
1570
 
1573
- if (!product) {
1571
+ const product = productRes.data;
1572
+
1573
+ if (!product?.id) {
1574
1574
  throw new Error("商品不存在");
1575
1575
  }
1576
1576
 
@@ -1588,7 +1588,7 @@ export default {
1588
1588
  });
1589
1589
 
1590
1590
  // 3. 创建订单
1591
- const orderId = await trx.insData({
1591
+ const orderIdRes = await trx.insData({
1592
1592
  table: "order",
1593
1593
  data: {
1594
1594
  userId: ctx.user.id,
@@ -1599,6 +1599,8 @@ export default {
1599
1599
  }
1600
1600
  });
1601
1601
 
1602
+ const orderId = orderIdRes.data;
1603
+
1602
1604
  // 4. 创建订单明细
1603
1605
  await trx.insData({
1604
1606
  table: "order_item",
@@ -1640,19 +1642,21 @@ export default {
1640
1642
  }
1641
1643
 
1642
1644
  // 批量插入
1643
- const result = await befly.db.batchInsert({
1644
- table: "user",
1645
- data: users.map((user: any) => ({
1646
- username: user.username,
1647
- email: user.email,
1648
- nickname: user.nickname || user.username,
1649
- state: 1
1650
- }))
1651
- });
1645
+ const idsRes = await befly.db.insBatch(
1646
+ "user",
1647
+ users.map((user: any) => {
1648
+ return {
1649
+ username: user.username,
1650
+ email: user.email,
1651
+ nickname: user.nickname || user.username,
1652
+ state: 1
1653
+ };
1654
+ })
1655
+ );
1652
1656
 
1653
1657
  return befly.tool.Yes("导入成功", {
1654
1658
  total: users.length,
1655
- inserted: result.affectedRows
1659
+ ids: idsRes.data
1656
1660
  });
1657
1661
  }
1658
1662
  };
@@ -1682,7 +1686,7 @@ export default {
1682
1686
  });
1683
1687
 
1684
1688
  return befly.tool.Yes("更新成功", {
1685
- updated: result.affectedRows
1689
+ updated: result.data
1686
1690
  });
1687
1691
  }
1688
1692
  };
@@ -1711,7 +1715,7 @@ export default {
1711
1715
  });
1712
1716
 
1713
1717
  return befly.tool.Yes("删除成功", {
1714
- deleted: result.affectedRows
1718
+ deleted: result.data
1715
1719
  });
1716
1720
  }
1717
1721
  };
@@ -1728,12 +1732,14 @@ export default {
1728
1732
  required: ['id'],
1729
1733
  handler: async (befly, ctx) => {
1730
1734
  // 查询订单基本信息
1731
- const order = await befly.db.getOne({
1735
+ const orderRes = await befly.db.getOne({
1732
1736
  table: 'order',
1733
1737
  where: { id: ctx.body.id }
1734
1738
  });
1735
1739
 
1736
- if (!order) {
1740
+ const order = orderRes.data;
1741
+
1742
+ if (!order?.id) {
1737
1743
  return befly.tool.No('订单不存在');
1738
1744
  }
1739
1745
 
@@ -1744,23 +1750,17 @@ export default {
1744
1750
  });
1745
1751
 
1746
1752
  // 查询用户信息
1747
- const user = await befly.db.getOne({
1753
+ const userRes = await befly.db.getOne({
1748
1754
  table: 'user',
1755
+ fields: ['id', 'username', 'nickname', 'phone'],
1749
1756
  where: { id: order.userId }
1750
1757
  });
1751
1758
 
1759
+ const user = userRes.data;
1760
+
1752
1761
  return befly.tool.Yes('查询成功', {
1753
1762
  order: order,
1754
- items: itemsResult.lists, // 订单明细列表
1755
- user: user
1756
- });
1757
- where: { id: order.userId },
1758
- columns: ['id', 'username', 'nickname', 'phone']
1759
- });
1760
-
1761
- return befly.tool.Yes('获取成功', {
1762
- ...order,
1763
- items: items,
1763
+ items: itemsResult.data.lists, // 订单明细列表
1764
1764
  user: user
1765
1765
  });
1766
1766
  }
@@ -1784,13 +1784,13 @@ export default {
1784
1784
  on: { "article.authorId": "author.id" }
1785
1785
  }
1786
1786
  ],
1787
- columns: ["article.id", "article.title", "article.createdAt", "author.nickname AS authorName"],
1787
+ fields: ["article.id", "article.title", "article.createdAt", "author.nickname AS authorName"],
1788
1788
  page: ctx.body.page || 1,
1789
1789
  limit: ctx.body.limit || 10,
1790
1790
  orderBy: ["article.createdAt#DESC"]
1791
1791
  });
1792
1792
 
1793
- return befly.tool.Yes("获取成功", result);
1793
+ return befly.tool.Yes("获取成功", result.data);
1794
1794
  }
1795
1795
  };
1796
1796
  ```
@@ -1866,14 +1866,14 @@ export default {
1866
1866
  // 查询所有用户(不分页,注意上限 10000 条)
1867
1867
  const result = await befly.db.getAll({
1868
1868
  table: "user",
1869
- columns: ["id", "username", "nickname", "email", "phone", "createdAt"],
1869
+ fields: ["id", "username", "nickname", "email", "phone", "createdAt"],
1870
1870
  where: { state: 1 },
1871
1871
  orderBy: ["createdAt#DESC"]
1872
1872
  });
1873
1873
 
1874
1874
  // 转换为 CSV 格式
1875
1875
  const headers = ["ID", "用户名", "昵称", "邮箱", "手机", "注册时间"];
1876
- const rows = result.lists.map((u: any) => [u.id, u.username, u.nickname, u.email, u.phone, new Date(u.createdAt).toLocaleString()]);
1876
+ const rows = result.data.lists.map((u: any) => [u.id, u.username, u.nickname, u.email, u.phone, new Date(u.createdAt).toLocaleString()]);
1877
1877
 
1878
1878
  const csv = [headers.join(","), ...rows.map((r: any[]) => r.join(","))].join("\n");
1879
1879