befly 3.8.32 → 3.8.33
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/lib/redisHelper.ts +14 -25
- package/package.json +3 -3
- package/sync/syncDb/table.ts +1 -1
- package/tests/redisHelper.test.ts +3 -3
package/lib/redisHelper.ts
CHANGED
|
@@ -86,53 +86,42 @@ export class RedisHelper {
|
|
|
86
86
|
|
|
87
87
|
/**
|
|
88
88
|
* 生成基于时间的唯一 ID
|
|
89
|
-
* 格式:
|
|
90
|
-
* 容量:
|
|
91
|
-
* 范围: 到 2286年
|
|
92
|
-
* @returns 唯一 ID (
|
|
89
|
+
* 格式: 毫秒时间戳(13位) + 3位自增 = 16位纯数字
|
|
90
|
+
* 容量: 1000/毫秒 = 1,000,000/秒
|
|
91
|
+
* 范围: 到 2286年
|
|
92
|
+
* @returns 唯一 ID (16位纯数字)
|
|
93
93
|
*/
|
|
94
94
|
async genTimeID(): Promise<number> {
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const counter = await this.client.incr(key);
|
|
99
|
-
await this.client.expire(key, 1);
|
|
100
|
-
|
|
101
|
-
const counterSuffix = (counter % 10000).toString().padStart(4, '0');
|
|
102
|
-
|
|
103
|
-
return Number(`${timestamp}${counterSuffix}`);
|
|
95
|
+
const ids = await this.genTimeIDBatch(1);
|
|
96
|
+
return ids[0];
|
|
104
97
|
}
|
|
105
98
|
|
|
106
99
|
/**
|
|
107
100
|
* 批量生成基于时间的唯一 ID
|
|
108
|
-
* 格式:
|
|
101
|
+
* 格式: 毫秒时间戳(13位) + 3位自增 = 16位纯数字
|
|
109
102
|
* @param count - 需要生成的 ID 数量
|
|
110
|
-
* @returns ID 数组 (
|
|
103
|
+
* @returns ID 数组 (16位纯数字)
|
|
111
104
|
*/
|
|
112
105
|
async genTimeIDBatch(count: number): Promise<number[]> {
|
|
113
106
|
if (count <= 0) {
|
|
114
107
|
return [];
|
|
115
108
|
}
|
|
116
109
|
|
|
117
|
-
|
|
118
|
-
const MAX_BATCH_SIZE = 10000;
|
|
110
|
+
const MAX_BATCH_SIZE = 1000;
|
|
119
111
|
if (count > MAX_BATCH_SIZE) {
|
|
120
112
|
throw new Error(`批量大小 ${count} 超过最大限制 ${MAX_BATCH_SIZE}`);
|
|
121
113
|
}
|
|
122
114
|
|
|
123
|
-
const timestamp =
|
|
115
|
+
const timestamp = Date.now();
|
|
124
116
|
const key = `${this.prefix}time_id_counter:${timestamp}`;
|
|
125
|
-
|
|
126
|
-
// 使用 INCRBY 一次性获取 N 个连续计数
|
|
127
|
-
const startCounter = await this.client.incrby(key, count);
|
|
117
|
+
const endCounter = await this.client.incrby(key, count);
|
|
128
118
|
await this.client.expire(key, 1);
|
|
129
119
|
|
|
130
|
-
// 生成 ID 数组
|
|
131
120
|
const ids: number[] = [];
|
|
132
121
|
for (let i = 0; i < count; i++) {
|
|
133
|
-
const counter =
|
|
134
|
-
const
|
|
135
|
-
ids.push(Number(`${timestamp}${
|
|
122
|
+
const counter = endCounter - count + i + 1;
|
|
123
|
+
const suffix = (counter % 1000).toString().padStart(3, '0');
|
|
124
|
+
ids.push(Number(`${timestamp}${suffix}`));
|
|
136
125
|
}
|
|
137
126
|
|
|
138
127
|
return ids;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly",
|
|
3
|
-
"version": "3.8.
|
|
3
|
+
"version": "3.8.33",
|
|
4
4
|
"description": "Befly - 为 Bun 专属打造的 TypeScript API 接口框架核心引擎",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
@@ -67,13 +67,13 @@
|
|
|
67
67
|
"befly-shared": "^1.1.2",
|
|
68
68
|
"chalk": "^5.6.2",
|
|
69
69
|
"es-toolkit": "^1.42.0",
|
|
70
|
-
"fast-jwt": "^6.0
|
|
70
|
+
"fast-jwt": "^6.1.0",
|
|
71
71
|
"fast-xml-parser": "^5.3.2",
|
|
72
72
|
"pathe": "^2.0.3",
|
|
73
73
|
"pino": "^10.1.0",
|
|
74
74
|
"pino-roll": "^4.0.0"
|
|
75
75
|
},
|
|
76
|
-
"gitHead": "
|
|
76
|
+
"gitHead": "d470173b6ce7461cf8b66adbebe2ec7888e7b32d",
|
|
77
77
|
"devDependencies": {
|
|
78
78
|
"typescript": "^5.9.3"
|
|
79
79
|
}
|
package/sync/syncDb/table.ts
CHANGED
|
@@ -124,7 +124,7 @@ export async function modifyTable(sql: SQL, tableName: string, fields: Record<st
|
|
|
124
124
|
}
|
|
125
125
|
} else {
|
|
126
126
|
const lenPart = isStringOrArrayType(fieldDef.type) ? ` 长度:${parseInt(String(fieldDef.max))}` : '';
|
|
127
|
-
Logger.debug(` + 新增字段 ${dbFieldName} (${fieldDef.type}${lenPart})`);
|
|
127
|
+
// Logger.debug(` + 新增字段 ${dbFieldName} (${fieldDef.type}${lenPart})`);
|
|
128
128
|
addClauses.push(generateDDLClause(fieldKey, fieldDef, true));
|
|
129
129
|
changed = true;
|
|
130
130
|
}
|
|
@@ -461,7 +461,7 @@ describe('RedisHelper - ID 生成', () => {
|
|
|
461
461
|
expect(typeof id1).toBe('number');
|
|
462
462
|
expect(typeof id2).toBe('number');
|
|
463
463
|
expect(id1).not.toBe(id2);
|
|
464
|
-
expect(id1.toString().length).toBe(
|
|
464
|
+
expect(id1.toString().length).toBe(16);
|
|
465
465
|
});
|
|
466
466
|
|
|
467
467
|
test('genTimeIDBatch - 批量生成 ID', async () => {
|
|
@@ -469,7 +469,7 @@ describe('RedisHelper - ID 生成', () => {
|
|
|
469
469
|
|
|
470
470
|
expect(ids.length).toBe(10);
|
|
471
471
|
expect(ids.every((id) => typeof id === 'number')).toBe(true);
|
|
472
|
-
expect(ids.every((id) => id.toString().length ===
|
|
472
|
+
expect(ids.every((id) => id.toString().length === 16)).toBe(true);
|
|
473
473
|
|
|
474
474
|
// 验证 ID 唯一性
|
|
475
475
|
const uniqueIds = new Set(ids);
|
|
@@ -483,7 +483,7 @@ describe('RedisHelper - ID 生成', () => {
|
|
|
483
483
|
|
|
484
484
|
test('genTimeIDBatch - 超过最大限制', async () => {
|
|
485
485
|
try {
|
|
486
|
-
await redis.genTimeIDBatch(
|
|
486
|
+
await redis.genTimeIDBatch(1001);
|
|
487
487
|
expect(true).toBe(false); // 不应该执行到这里
|
|
488
488
|
} catch (error: any) {
|
|
489
489
|
expect(error.message).toContain('超过最大限制');
|