befly 3.15.2 → 3.15.3

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.
@@ -24,6 +24,7 @@ const defaultOptions = {
24
24
  },
25
25
  // ========== 数据库配置 ==========
26
26
  db: {
27
+ idMode: "timeId",
27
28
  host: "127.0.0.1",
28
29
  port: 3306,
29
30
  username: "root",
@@ -93,5 +94,9 @@ export async function loadBeflyConfig(nodeEnv) {
93
94
  throw new Error(`配置错误:redis.prefix 不允许包含 ':'(RedisHelper 会自动拼接分隔符 ':'),请改为不带冒号的前缀,例如 'befly_demo',当前值=${redisPrefix}`);
94
95
  }
95
96
  }
97
+ const idMode = config.db?.idMode;
98
+ if (idMode !== undefined && idMode !== "timeId" && idMode !== "autoId") {
99
+ throw new Error(`配置错误:db.idMode 只能是 'timeId' 或 'autoId',当前值=${String(idMode)}`);
100
+ }
96
101
  return config;
97
102
  }
package/dist/befly.js CHANGED
@@ -7547,6 +7547,7 @@ var defaultOptions = {
7547
7547
  maxArrayItems: 100
7548
7548
  },
7549
7549
  db: {
7550
+ idMode: "timeId",
7550
7551
  host: "127.0.0.1",
7551
7552
  port: 3306,
7552
7553
  username: "root",
@@ -7602,6 +7603,10 @@ async function loadBeflyConfig(nodeEnv) {
7602
7603
  throw new Error(`\u914D\u7F6E\u9519\u8BEF\uFF1Aredis.prefix \u4E0D\u5141\u8BB8\u5305\u542B ':'\uFF08RedisHelper \u4F1A\u81EA\u52A8\u62FC\u63A5\u5206\u9694\u7B26 ':'\uFF09\uFF0C\u8BF7\u6539\u4E3A\u4E0D\u5E26\u5192\u53F7\u7684\u524D\u7F00\uFF0C\u4F8B\u5982 'befly_demo'\uFF0C\u5F53\u524D\u503C=${redisPrefix}`);
7603
7604
  }
7604
7605
  }
7606
+ const idMode = config2.db?.idMode;
7607
+ if (idMode !== undefined && idMode !== "timeId" && idMode !== "autoId") {
7608
+ throw new Error(`\u914D\u7F6E\u9519\u8BEF\uFF1Adb.idMode \u53EA\u80FD\u662F 'timeId' \u6216 'autoId'\uFF0C\u5F53\u524D\u503C=${String(idMode)}`);
7609
+ }
7605
7610
  return config2;
7606
7611
  }
7607
7612
 
@@ -13324,7 +13329,12 @@ class DbUtils {
13324
13329
  for (const [key, value] of Object.entries(userData)) {
13325
13330
  result[key] = value;
13326
13331
  }
13327
- result["id"] = options.id;
13332
+ if (options.idMode === "timeId") {
13333
+ if (!Number.isFinite(options.id) || options.id <= 0) {
13334
+ throw new Error(`buildInsertRow(timeId) \u5931\u8D25\uFF1Aid \u5FC5\u987B\u4E3A > 0 \u7684\u6709\u9650 number (id: ${String(options.id)})`);
13335
+ }
13336
+ result["id"] = options.id;
13337
+ }
13328
13338
  result["created_at"] = options.now;
13329
13339
  result["updated_at"] = options.now;
13330
13340
  result["state"] = 1;
@@ -14095,6 +14105,7 @@ class DbHelper {
14095
14105
  dbName;
14096
14106
  sql = null;
14097
14107
  isTransaction = false;
14108
+ idMode;
14098
14109
  constructor(options) {
14099
14110
  this.redis = options.redis;
14100
14111
  if (typeof options.dbName !== "string" || options.dbName.trim() === "") {
@@ -14103,6 +14114,7 @@ class DbHelper {
14103
14114
  this.dbName = options.dbName;
14104
14115
  this.sql = options.sql || null;
14105
14116
  this.isTransaction = Boolean(options.sql);
14117
+ this.idMode = options.idMode === "autoId" ? "autoId" : "timeId";
14106
14118
  }
14107
14119
  createSqlBuilder() {
14108
14120
  return new SqlBuilder({ quoteIdent: quoteIdentMySql });
@@ -14397,13 +14409,18 @@ class DbHelper {
14397
14409
  const { table, data } = options;
14398
14410
  const snakeTable = snakeCase(table);
14399
14411
  const now = Date.now();
14400
- let id;
14401
- try {
14402
- id = await this.redis.genTimeID();
14403
- } catch (error) {
14404
- throw new Error(`\u751F\u6210 ID \u5931\u8D25\uFF0CRedis \u53EF\u80FD\u4E0D\u53EF\u7528 (table: ${table})`, { cause: error });
14412
+ let processed;
14413
+ if (this.idMode === "autoId") {
14414
+ processed = DbUtils.buildInsertRow({ idMode: "autoId", data, now });
14415
+ } else {
14416
+ let id;
14417
+ try {
14418
+ id = await this.redis.genTimeID();
14419
+ } catch (error) {
14420
+ throw new Error(`\u751F\u6210 ID \u5931\u8D25\uFF0CRedis \u53EF\u80FD\u4E0D\u53EF\u7528 (table: ${table})`, { cause: error });
14421
+ }
14422
+ processed = DbUtils.buildInsertRow({ idMode: "timeId", data, id, now });
14405
14423
  }
14406
- const processed = DbUtils.buildInsertRow({ data, id, now });
14407
14424
  SqlCheck.assertNoUndefinedInRecord(processed, `insData \u63D2\u5165\u6570\u636E (table: ${snakeTable})`);
14408
14425
  const builder = this.createSqlBuilder();
14409
14426
  const { sql, params } = builder.toInsertSql(snakeTable, processed);
@@ -14411,7 +14428,10 @@ class DbHelper {
14411
14428
  const processedId = processed["id"];
14412
14429
  const processedIdNum = typeof processedId === "number" ? processedId : 0;
14413
14430
  const lastInsertRowidNum = toNumberFromSql(execRes.data?.lastInsertRowid);
14414
- const insertedId = processedIdNum || lastInsertRowidNum || 0;
14431
+ const insertedId = this.idMode === "autoId" ? lastInsertRowidNum || 0 : processedIdNum || lastInsertRowidNum || 0;
14432
+ if (this.idMode === "autoId" && insertedId <= 0) {
14433
+ throw new Error(`\u63D2\u5165\u5931\u8D25\uFF1AautoId \u6A21\u5F0F\u4E0B\u65E0\u6CD5\u83B7\u53D6 lastInsertRowid (table: ${table})`);
14434
+ }
14415
14435
  return {
14416
14436
  data: insertedId,
14417
14437
  sql: execRes.sql
@@ -14430,23 +14450,46 @@ class DbHelper {
14430
14450
  throw new Error(`\u6279\u91CF\u63D2\u5165\u6570\u91CF ${dataList.length} \u8D85\u8FC7\u6700\u5927\u9650\u5236 ${MAX_BATCH_SIZE}`);
14431
14451
  }
14432
14452
  const snakeTable = snakeCase(table);
14433
- const ids = [];
14434
- for (let i = 0;i < dataList.length; i++) {
14435
- ids.push(await this.redis.genTimeID());
14436
- }
14437
14453
  const now = Date.now();
14438
- const processedList = dataList.map((data, index) => {
14439
- const id = ids[index];
14440
- if (typeof id !== "number") {
14441
- throw new Error(`\u6279\u91CF\u63D2\u5165\u751F\u6210 ID \u5931\u8D25\uFF1Aids[${index}] \u4E0D\u662F number (table: ${snakeTable})`);
14454
+ let ids = [];
14455
+ let processedList;
14456
+ if (this.idMode === "autoId") {
14457
+ processedList = dataList.map((data) => {
14458
+ return DbUtils.buildInsertRow({ idMode: "autoId", data, now });
14459
+ });
14460
+ } else {
14461
+ const nextIds = [];
14462
+ for (let i = 0;i < dataList.length; i++) {
14463
+ nextIds.push(await this.redis.genTimeID());
14442
14464
  }
14443
- return DbUtils.buildInsertRow({ data, id, now });
14444
- });
14465
+ ids = nextIds;
14466
+ processedList = dataList.map((data, index) => {
14467
+ const id = nextIds[index];
14468
+ if (typeof id !== "number") {
14469
+ throw new Error(`\u6279\u91CF\u63D2\u5165\u751F\u6210 ID \u5931\u8D25\uFF1Aids[${index}] \u4E0D\u662F number (table: ${snakeTable})`);
14470
+ }
14471
+ return DbUtils.buildInsertRow({ idMode: "timeId", data, id, now });
14472
+ });
14473
+ }
14445
14474
  const insertFields = SqlCheck.assertBatchInsertRowsConsistent(processedList, { table: snakeTable });
14446
14475
  const builder = this.createSqlBuilder();
14447
14476
  const { sql, params } = builder.toInsertSql(snakeTable, processedList);
14448
14477
  try {
14449
14478
  const execRes = await this.executeRun(sql, params);
14479
+ if (this.idMode === "autoId") {
14480
+ const firstId = toNumberFromSql(execRes.data?.lastInsertRowid);
14481
+ if (firstId <= 0) {
14482
+ throw new Error(`\u6279\u91CF\u63D2\u5165\u5931\u8D25\uFF1AautoId \u6A21\u5F0F\u4E0B\u65E0\u6CD5\u83B7\u53D6 lastInsertRowid (table: ${table})`);
14483
+ }
14484
+ const outIds = [];
14485
+ for (let i = 0;i < dataList.length; i++) {
14486
+ outIds.push(firstId + i);
14487
+ }
14488
+ return {
14489
+ data: outIds,
14490
+ sql: execRes.sql
14491
+ };
14492
+ }
14450
14493
  return {
14451
14494
  data: ids,
14452
14495
  sql: execRes.sql
@@ -14600,7 +14643,7 @@ class DbHelper {
14600
14643
  throw new Error("\u5F53\u524D SQL \u5BA2\u6237\u7AEF\u4E0D\u652F\u6301\u4E8B\u52A1 begin() \u65B9\u6CD5");
14601
14644
  }
14602
14645
  return await sql.begin(async (tx) => {
14603
- const trans = new DbHelper({ redis: this.redis, dbName: this.dbName, sql: tx });
14646
+ const trans = new DbHelper({ redis: this.redis, dbName: this.dbName, sql: tx, idMode: this.idMode });
14604
14647
  return await callback(trans);
14605
14648
  });
14606
14649
  }
@@ -14745,7 +14788,9 @@ var dbPlugin = {
14745
14788
  }
14746
14789
  try {
14747
14790
  const sql = Connect.getSql();
14748
- const dbManager = new DbHelper({ redis: befly.redis, dbName, sql });
14791
+ const idMode = befly.config?.db?.idMode;
14792
+ const normalizedIdMode = idMode === "autoId" ? "autoId" : "timeId";
14793
+ const dbManager = new DbHelper({ redis: befly.redis, dbName, sql, idMode: normalizedIdMode });
14749
14794
  return dbManager;
14750
14795
  } catch (error) {
14751
14796
  Logger.error({ env, err: error, msg: "\u6570\u636E\u5E93\u521D\u59CB\u5316\u5931\u8D25" });