midway-fatcms 0.0.8 → 0.0.9

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.
Files changed (75) hide show
  1. package/.qoder/skills/midway-fatcms/02-crud-quick.md +38 -0
  2. package/.qoder/skills/midway-fatcms/03-crud-sharding.md +37 -36
  3. package/.qoder/skills/midway-fatcms/07-examples.md +4 -0
  4. package/dist/configuration.d.ts +10 -0
  5. package/dist/configuration.js +26 -0
  6. package/dist/controller/helpers.controller.d.ts +6 -0
  7. package/dist/controller/helpers.controller.js +19 -0
  8. package/dist/libs/crud-pro/CrudPro.d.ts +29 -2
  9. package/dist/libs/crud-pro/CrudPro.js +58 -2
  10. package/dist/libs/crud-pro/exceptions.d.ts +7 -0
  11. package/dist/libs/crud-pro/exceptions.js +7 -0
  12. package/dist/libs/crud-pro/interfaces.d.ts +1 -0
  13. package/dist/libs/crud-pro/models/CrudResult.d.ts +3 -2
  14. package/dist/libs/crud-pro/models/CrudResult.js +1 -1
  15. package/dist/libs/crud-pro/models/ServiceHub.d.ts +2 -0
  16. package/dist/libs/crud-pro/services/CrudProDataTypeConvertService.d.ts +70 -2
  17. package/dist/libs/crud-pro/services/CrudProDataTypeConvertService.js +205 -13
  18. package/dist/libs/crud-pro/services/CrudProTableMetaService.d.ts +36 -0
  19. package/dist/libs/crud-pro/services/CrudProTableMetaService.js +97 -3
  20. package/dist/libs/crud-pro/services/CurdProServiceHub.d.ts +2 -0
  21. package/dist/libs/crud-pro/services/CurdProServiceHub.js +6 -0
  22. package/dist/libs/crud-pro-quick/CrudProQuick.d.ts +93 -6
  23. package/dist/libs/crud-pro-quick/CrudProQuick.js +192 -32
  24. package/dist/libs/crud-sharding/ShardingBase.d.ts +78 -0
  25. package/dist/libs/crud-sharding/ShardingBase.js +179 -0
  26. package/dist/libs/crud-sharding/ShardingByCustomCrud.d.ts +35 -0
  27. package/dist/libs/crud-sharding/ShardingByCustomCrud.js +297 -0
  28. package/dist/libs/crud-sharding/ShardingByHashCrud.d.ts +38 -0
  29. package/dist/libs/crud-sharding/ShardingByHashCrud.js +86 -0
  30. package/dist/libs/crud-sharding/ShardingByKeyCrud.d.ts +39 -0
  31. package/dist/libs/crud-sharding/ShardingByKeyCrud.js +74 -0
  32. package/dist/libs/crud-sharding/ShardingByTimeCrud.d.ts +66 -0
  33. package/dist/libs/crud-sharding/ShardingByTimeCrud.js +524 -0
  34. package/dist/libs/crud-sharding/ShardingConfig.d.ts +10 -8
  35. package/dist/libs/crud-sharding/ShardingConfig.js +3 -3
  36. package/dist/libs/crud-sharding/TIME_COLUMN_CLEAN_SPEC.md +1 -1
  37. package/dist/libs/crud-sharding/index.d.ts +10 -13
  38. package/dist/libs/crud-sharding/index.js +21 -17
  39. package/dist/models/RedisKeys.d.ts +1 -0
  40. package/dist/models/RedisKeys.js +1 -0
  41. package/dist/service/TableMetaCacheRedisSubscriber.d.ts +31 -0
  42. package/dist/service/TableMetaCacheRedisSubscriber.js +98 -0
  43. package/dist/service/curd/CurdMixService.d.ts +2 -2
  44. package/dist/service/curd/CurdProService.d.ts +109 -5
  45. package/dist/service/curd/CurdProService.js +127 -7
  46. package/package.json +1 -1
  47. package/src/configuration.ts +27 -0
  48. package/src/controller/helpers.controller.ts +15 -0
  49. package/src/libs/crud-pro/CrudPro.ts +73 -4
  50. package/src/libs/crud-pro/exceptions.ts +8 -0
  51. package/src/libs/crud-pro/interfaces.ts +1 -0
  52. package/src/libs/crud-pro/models/CrudResult.ts +5 -5
  53. package/src/libs/crud-pro/models/ServiceHub.ts +4 -0
  54. package/src/libs/crud-pro/services/CrudProDataTypeConvertService.ts +238 -15
  55. package/src/libs/crud-pro/services/CrudProTableMetaService.ts +110 -2
  56. package/src/libs/crud-pro/services/CurdProServiceHub.ts +8 -0
  57. package/src/libs/crud-pro-quick/CrudProQuick.ts +234 -46
  58. package/src/libs/crud-sharding/ShardingBase.ts +256 -0
  59. package/src/libs/crud-sharding/ShardingByCustomCrud.ts +329 -0
  60. package/src/libs/crud-sharding/ShardingByHashCrud.ts +111 -0
  61. package/src/libs/crud-sharding/ShardingByKeyCrud.ts +97 -0
  62. package/src/libs/crud-sharding/ShardingByTimeCrud.ts +628 -0
  63. package/src/libs/crud-sharding/ShardingConfig.ts +10 -8
  64. package/src/libs/crud-sharding/TIME_COLUMN_CLEAN_SPEC.md +1 -1
  65. package/src/libs/crud-sharding/index.ts +17 -14
  66. package/src/models/RedisKeys.ts +1 -0
  67. package/src/service/TableMetaCacheRedisSubscriber.ts +105 -0
  68. package/src/service/curd/CurdMixService.ts +2 -2
  69. package/src/service/curd/CurdProService.ts +131 -9
  70. package/dist/libs/crud-sharding/ShardingCrudPro.d.ts +0 -208
  71. package/dist/libs/crud-sharding/ShardingCrudPro.js +0 -879
  72. package/dist/libs/crud-sharding/ShardingRouter.d.ts +0 -70
  73. package/dist/libs/crud-sharding/ShardingRouter.js +0 -396
  74. package/src/libs/crud-sharding/ShardingCrudPro.ts +0 -1105
  75. package/src/libs/crud-sharding/ShardingRouter.ts +0 -533
@@ -0,0 +1,74 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ShardingByKeyCrud = void 0;
4
+ const ShardingBase_1 = require("./ShardingBase");
5
+ const ShardingByCustomCrud_1 = require("./ShardingByCustomCrud");
6
+ /**
7
+ * 键值分表 CRUD
8
+ *
9
+ * 字段原值即表后缀,一值一表。
10
+ * 表名格式:baseTable_{shardingColumn值}(如 t_log_east, t_log_west)
11
+ *
12
+ * 内部委托 ShardingByCustomCrud 实现所有 CRUD 操作,
13
+ * 通过 customRouter 实现键值路由逻辑。
14
+ *
15
+ * 规范:所有操作必须提供 shardingColumn,否则抛出异常。
16
+ *
17
+ * 特性:
18
+ * - 路由:shardingColumn 的原始值直接作为表后缀
19
+ * - 不预定义 keyValues,表按需动态创建
20
+ * - insertOrUpdate:统一由 condition 路由定位分表
21
+ */
22
+ class ShardingByKeyCrud extends ShardingBase_1.ShardingBase {
23
+ constructor(crudProFactory, config) {
24
+ super(crudProFactory, config);
25
+ if (!config.shardingColumn) {
26
+ throw new Error('[ShardingByKeyCrud] 键值分表必须指定 shardingColumn');
27
+ }
28
+ const col = config.shardingColumn;
29
+ const baseTable = config.baseTable;
30
+ const fmt = config.suffixFormatter;
31
+ /** 格式化表名:baseTable + _ + 后缀(可选 suffixFormatter) */
32
+ const formatTable = (suffix) => {
33
+ const formatted = fmt ? fmt(suffix) : suffix;
34
+ return `${baseTable}_${formatted}`;
35
+ };
36
+ const customRouter = (ctx) => {
37
+ var _a, _b;
38
+ // 根据操作类型确定字段来源:insert 类从 data,其余从 condition
39
+ const fromData = ShardingBase_1.ShardingBase.isInsertPhase(ctx);
40
+ const val = fromData ? (_a = ctx.data) === null || _a === void 0 ? void 0 : _a[col] : (_b = ctx.condition) === null || _b === void 0 ? void 0 : _b[col];
41
+ if (val != null && val !== '' && val !== undefined) {
42
+ return formatTable(String(val));
43
+ }
44
+ ;
45
+ throw new Error(`[ShardingByKeyCrud] ${fromData ? 'data' : 'condition'} 必须包含分表字段 '${col}'`);
46
+ };
47
+ this.delegate = new ShardingByCustomCrud_1.ShardingByCustomCrud(crudProFactory, { ...config, customRouter });
48
+ }
49
+ // ============ 状态同步 ============
50
+ setBaseCfg(cfg) {
51
+ super.setBaseCfg(cfg);
52
+ this.delegate.setBaseCfg(cfg);
53
+ return this;
54
+ }
55
+ setEnableSoftDelete(enable) {
56
+ super.setEnableSoftDelete(enable);
57
+ this.delegate.setEnableSoftDelete(enable);
58
+ return this;
59
+ }
60
+ // ============ CRUD 方法:全部委托 ============
61
+ insert(r) { return this.delegate.insert(r); }
62
+ batchInsert(r) { return this.delegate.batchInsert(r); }
63
+ update(r) { return this.delegate.update(r); }
64
+ delete(r) { return this.delegate.delete(r); }
65
+ restore(r) { return this.delegate.restore(r); }
66
+ insertOrUpdate(r) { return this.delegate.insertOrUpdate(r); }
67
+ findOne(r) { return this.delegate.findOne(r); }
68
+ findUniqueOne(r) { return this.delegate.findUniqueOne(r); }
69
+ findList(r) { return this.delegate.findList(r); }
70
+ findPage(r) { return this.delegate.findPage(r); }
71
+ findCount(r) { return this.delegate.findCount(r); }
72
+ isExist(r) { return this.delegate.isExist(r); }
73
+ }
74
+ exports.ShardingByKeyCrud = ShardingByKeyCrud;
@@ -0,0 +1,66 @@
1
+ import { IRequestModel } from '../../libs/crud-pro/interfaces';
2
+ import { IShardingConfig, CrudProFactory } from './ShardingConfig';
3
+ import { ShardingBase } from './ShardingBase';
4
+ import { CrudWriteResult, CrudQueryOneResult, CrudQueryListResult, CrudQueryPageResult, CrudExistResult, CrudCountResult, CrudUpsertResult } from '../../libs/crud-pro/models/CrudResult';
5
+ import { ShardingBatchInsertResult } from './ShardingResult';
6
+ /**
7
+ * 时间分表 CRUD
8
+ *
9
+ * 支持 YEAR / MONTH / DAY 三种时间粒度分表。
10
+ * 路由规则:根据 timeColumn 的值计算时间后缀,定位到对应分表。
11
+ *
12
+ * 特性:
13
+ * - 插入:data 中的 timeColumn 决定目标分表
14
+ * - 更新/删除:condition 中的 timeColumn 定位分表
15
+ * - 查询:时间范围条件交叉过滤候选表,无条件时回退到 recentTableCount
16
+ * - insertOrUpdate:condition 路由查找,data 路由插入(双路由)
17
+ * - 智能清理:timeColumn 在有主键时仅用于路由,不参与 WHERE 条件
18
+ * - COUNT 缓存:历史表缓存,当前表实时查
19
+ * - orderBy 约束:多表查询必须以 timeColumn 排序
20
+ */
21
+ export declare class ShardingByTimeCrud extends ShardingBase {
22
+ private readonly merger;
23
+ private readonly granularity;
24
+ constructor(crudProFactory: CrudProFactory, config: IShardingConfig);
25
+ private typeToGranularity;
26
+ /** 从 data 中提取时间字段,计算目标分表名 */
27
+ private resolveTableForInsert;
28
+ /** 从 condition 中提取时间字段,计算目标分表名(单表) */
29
+ private resolveTableForCondition;
30
+ /** 解析查询目标表列表 */
31
+ private resolveFindTables;
32
+ private intersectWithCondition;
33
+ private intersectTablesByTimeRange;
34
+ private extractTimeRange;
35
+ private tryExtractTimeFromCondition;
36
+ private sortTablesByTimeDesc;
37
+ private getDefaultRecentCount;
38
+ private formatTableName;
39
+ insert(reqJson: IRequestModel): Promise<CrudWriteResult>;
40
+ batchInsert(reqJson: IRequestModel): Promise<ShardingBatchInsertResult>;
41
+ update(reqJson: IRequestModel): Promise<CrudWriteResult>;
42
+ delete(reqJson: IRequestModel): Promise<CrudWriteResult>;
43
+ restore(reqJson: IRequestModel): Promise<CrudWriteResult>;
44
+ /**
45
+ * 插入或更新(双路由)
46
+ *
47
+ * 1. condition 路由定位查找分表
48
+ * 2. 检查记录是否存在
49
+ * 3. 存在 → UPDATE(conditionTable)
50
+ * 4. 不存在 → data 路由定位插入分表,INSERT(dataTable)
51
+ */
52
+ insertOrUpdate(reqJson: IRequestModel): Promise<CrudUpsertResult>;
53
+ findOne<T = Record<string, any>>(reqJson: IRequestModel): Promise<CrudQueryOneResult<T>>;
54
+ findUniqueOne<T = Record<string, any>>(reqJson: IRequestModel): Promise<CrudQueryOneResult<T>>;
55
+ findList<T = Record<string, any>>(reqJson: IRequestModel): Promise<CrudQueryListResult<T>>;
56
+ findPage<T = Record<string, any>>(reqJson: IRequestModel): Promise<CrudQueryPageResult<T>>;
57
+ findCount(reqJson: IRequestModel): Promise<CrudCountResult>;
58
+ isExist(reqJson: IRequestModel): Promise<CrudExistResult>;
59
+ private validateTimeColumnForData;
60
+ private validateTimeColumnForBatchData;
61
+ private validateRoutingFieldForCondition;
62
+ private validateFindOrderBy;
63
+ private sortTablesForOrderBy;
64
+ private cleanTimeColumnForSingleTableQuery;
65
+ private validateTimeOperatorPrecision;
66
+ }
@@ -0,0 +1,524 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ShardingByTimeCrud = void 0;
4
+ const moment = require("moment");
5
+ const OrderByUtils_1 = require("../../libs/crud-pro/utils/OrderByUtils");
6
+ const keys_1 = require("../../libs/crud-pro/models/keys");
7
+ const ShardingConfig_1 = require("./ShardingConfig");
8
+ const ShardingBase_1 = require("./ShardingBase");
9
+ const ShardingMerger_1 = require("./ShardingMerger");
10
+ const ShardingCountCache_1 = require("./ShardingCountCache");
11
+ const CrudResult_1 = require("../../libs/crud-pro/models/CrudResult");
12
+ const ShardingResult_1 = require("./ShardingResult");
13
+ const ShardingUtils_1 = require("./ShardingUtils");
14
+ /**
15
+ * 时间分表 CRUD
16
+ *
17
+ * 支持 YEAR / MONTH / DAY 三种时间粒度分表。
18
+ * 路由规则:根据 timeColumn 的值计算时间后缀,定位到对应分表。
19
+ *
20
+ * 特性:
21
+ * - 插入:data 中的 timeColumn 决定目标分表
22
+ * - 更新/删除:condition 中的 timeColumn 定位分表
23
+ * - 查询:时间范围条件交叉过滤候选表,无条件时回退到 recentTableCount
24
+ * - insertOrUpdate:condition 路由查找,data 路由插入(双路由)
25
+ * - 智能清理:timeColumn 在有主键时仅用于路由,不参与 WHERE 条件
26
+ * - COUNT 缓存:历史表缓存,当前表实时查
27
+ * - orderBy 约束:多表查询必须以 timeColumn 排序
28
+ */
29
+ class ShardingByTimeCrud extends ShardingBase_1.ShardingBase {
30
+ constructor(crudProFactory, config) {
31
+ super(crudProFactory, config);
32
+ // 校验
33
+ if (!config.timeColumn) {
34
+ throw new Error('[ShardingByTimeCrud] 时间分表必须显式指定 timeColumn 字段');
35
+ }
36
+ this.granularity = this.typeToGranularity(config.type);
37
+ this.merger = new ShardingMerger_1.ShardingMerger();
38
+ // 初始化 COUNT 缓存
39
+ if (config.countCache) {
40
+ const countCache = new ShardingCountCache_1.ShardingCountCache(config.type, config.baseTable, config.countCache.ttlSeconds, config.countCache.maxSize);
41
+ this.merger.setCountCache(countCache);
42
+ }
43
+ }
44
+ // ============ 路由 ============
45
+ typeToGranularity(type) {
46
+ switch (type) {
47
+ case ShardingConfig_1.ShardingType.YEAR: return 'year';
48
+ case ShardingConfig_1.ShardingType.MONTH: return 'month';
49
+ case ShardingConfig_1.ShardingType.DAY: return 'day';
50
+ default: throw new Error(`[ShardingByTimeCrud] 不支持的分表类型: ${type}`);
51
+ }
52
+ }
53
+ /** 从 data 中提取时间字段,计算目标分表名 */
54
+ resolveTableForInsert(data) {
55
+ const timeValue = data[this.config.timeColumn];
56
+ if (timeValue === undefined || timeValue === null) {
57
+ throw new Error(`[ShardingByTimeCrud] insert 操作的 data 必须包含时间字段 '${this.config.timeColumn}'`);
58
+ }
59
+ const m = moment(timeValue);
60
+ if (!m.isValid()) {
61
+ throw new Error(`[ShardingByTimeCrud] 无法解析时间值: ${timeValue}`);
62
+ }
63
+ const suffix = (0, ShardingUtils_1.getTimeSuffix)(m.toDate(), this.granularity);
64
+ return this.formatTableName(suffix);
65
+ }
66
+ /** 从 condition 中提取时间字段,计算目标分表名(单表) */
67
+ resolveTableForCondition(condition) {
68
+ const timeColumn = this.config.timeColumn;
69
+ const timeValue = condition[timeColumn];
70
+ if (timeValue === undefined || timeValue === null) {
71
+ throw new Error(`[ShardingByTimeCrud] 写操作的 condition 必须包含时间字段 '${timeColumn}',` +
72
+ `用于路由到正确的分表。`);
73
+ }
74
+ // 如果是操作符表达式($gte/$lte),尝试提取范围的 start
75
+ if ((0, ShardingUtils_1.isOperatorExpression)(timeValue)) {
76
+ const range = this.extractTimeRange(condition);
77
+ if (range) {
78
+ const suffix = (0, ShardingUtils_1.getTimeSuffix)(range.start, this.granularity);
79
+ return this.formatTableName(suffix);
80
+ }
81
+ }
82
+ const m = moment(timeValue);
83
+ if (!m.isValid()) {
84
+ throw new Error(`[ShardingByTimeCrud] 无法解析时间值: ${timeValue}`);
85
+ }
86
+ const suffix = (0, ShardingUtils_1.getTimeSuffix)(m.toDate(), this.granularity);
87
+ return this.formatTableName(suffix);
88
+ }
89
+ /** 解析查询目标表列表 */
90
+ async resolveFindTables(reqJson) {
91
+ const existingTables = await this.getExistingTablesSet();
92
+ const baseTablePrefix = `${this.config.baseTable}_`;
93
+ const shardingTables = Array.from(existingTables).filter(t => t.startsWith(baseTablePrefix));
94
+ if (shardingTables.length === 0)
95
+ return [];
96
+ shardingTables.sort();
97
+ const condition = reqJson.condition;
98
+ // 有 recentTableCount:取最近N张,再与条件取交集
99
+ if (this.config.recentTableCount !== undefined) {
100
+ const recentTables = shardingTables.slice(-this.config.recentTableCount);
101
+ return this.intersectWithCondition(recentTables, condition);
102
+ }
103
+ // 无 recentTableCount:所有表与条件取交集,最多取默认数量
104
+ const intersected = this.intersectWithCondition(shardingTables, condition);
105
+ const defaultCount = this.getDefaultRecentCount();
106
+ return intersected.slice(0, defaultCount);
107
+ }
108
+ intersectWithCondition(tables, condition) {
109
+ if (!condition)
110
+ return this.sortTablesByTimeDesc(tables);
111
+ const timeRange = this.extractTimeRange(condition);
112
+ if (timeRange) {
113
+ return this.intersectTablesByTimeRange(tables, timeRange);
114
+ }
115
+ const singleTimeValue = this.tryExtractTimeFromCondition(condition);
116
+ if (singleTimeValue) {
117
+ const suffix = (0, ShardingUtils_1.getTimeSuffix)(singleTimeValue, this.granularity);
118
+ const targetTable = this.formatTableName(suffix);
119
+ return tables.includes(targetTable) ? [targetTable] : [];
120
+ }
121
+ // condition 存在但不包含时间字段,返回所有表(降序)
122
+ return this.sortTablesByTimeDesc(tables);
123
+ }
124
+ intersectTablesByTimeRange(tables, timeRange) {
125
+ const startSuffix = (0, ShardingUtils_1.getTimeSuffix)(timeRange.start, this.granularity);
126
+ const endSuffix = (0, ShardingUtils_1.getTimeSuffix)(timeRange.end, this.granularity);
127
+ const prefix = `${this.config.baseTable}_`;
128
+ const prefixLen = prefix.length;
129
+ return this.sortTablesByTimeDesc(tables.filter(table => {
130
+ const suffix = table.substring(prefixLen);
131
+ return suffix >= startSuffix && suffix <= endSuffix;
132
+ }));
133
+ }
134
+ extractTimeRange(condition) {
135
+ var _a, _b;
136
+ const colCond = condition[this.config.timeColumn];
137
+ if (!colCond || typeof colCond !== 'object')
138
+ return null;
139
+ const start = (_a = colCond.$gte) !== null && _a !== void 0 ? _a : colCond.$gt;
140
+ const end = (_b = colCond.$lte) !== null && _b !== void 0 ? _b : colCond.$lt;
141
+ if (start && end) {
142
+ const sm = moment(start), em = moment(end);
143
+ if (!sm.isValid() || !em.isValid())
144
+ return null;
145
+ return { start: sm.toDate(), end: em.toDate() };
146
+ }
147
+ if (start) {
148
+ const sm = moment(start);
149
+ return sm.isValid() ? { start: sm.toDate(), end: new Date() } : null;
150
+ }
151
+ if (end) {
152
+ const em = moment(end);
153
+ return em.isValid() ? { start: new Date(0), end: em.toDate() } : null;
154
+ }
155
+ return null;
156
+ }
157
+ tryExtractTimeFromCondition(condition) {
158
+ const value = condition[this.config.timeColumn];
159
+ if (value === undefined || value === null)
160
+ return null;
161
+ const m = moment(value);
162
+ return m.isValid() ? m.toDate() : null;
163
+ }
164
+ sortTablesByTimeDesc(tables) {
165
+ return [...tables].sort((a, b) => b.localeCompare(a));
166
+ }
167
+ getDefaultRecentCount() {
168
+ if (this.config.recentTableCount !== undefined && this.config.recentTableCount > 0) {
169
+ return this.config.recentTableCount;
170
+ }
171
+ switch (this.granularity) {
172
+ case 'year': return 2;
173
+ case 'month': return 3;
174
+ case 'day': return 7;
175
+ }
176
+ }
177
+ formatTableName(suffix) {
178
+ const formattedSuffix = this.config.suffixFormatter ? this.config.suffixFormatter(suffix) : suffix;
179
+ return `${this.config.baseTable}_${formattedSuffix}`;
180
+ }
181
+ // ============ 写操作 ============
182
+ async insert(reqJson) {
183
+ this.validateTimeColumnForData(reqJson, 'insert');
184
+ const targetTable = this.resolveTableForInsert(reqJson.data);
185
+ await this.createShardingTableIfNeeded(targetTable);
186
+ const ctx = await this.executeOnTable(targetTable, reqJson, keys_1.KeysOfSimpleSQL.SIMPLE_INSERT);
187
+ const affected = ctx.getResModel().affected || { affectedRows: 0 };
188
+ return new CrudResult_1.CrudWriteResult({ affectedRows: affected.affectedRows, insertId: affected.insertId, rawContext: ctx });
189
+ }
190
+ async batchInsert(reqJson) {
191
+ var _a, _b;
192
+ const dataArray = reqJson.data;
193
+ if (!Array.isArray(dataArray) || dataArray.length === 0) {
194
+ throw new Error('[ShardingByTimeCrud] batchInsert requires non-empty data array');
195
+ }
196
+ this.validateTimeColumnForBatchData(dataArray, 'batchInsert');
197
+ // 按分表分组
198
+ const groupedData = new Map();
199
+ for (const item of dataArray) {
200
+ const table = this.resolveTableForInsert(item);
201
+ if (!groupedData.has(table))
202
+ groupedData.set(table, []);
203
+ groupedData.get(table).push(item);
204
+ }
205
+ // 确保所有目标分表存在
206
+ for (const tableName of groupedData.keys()) {
207
+ await this.createShardingTableIfNeeded(tableName);
208
+ }
209
+ // 串行执行各分表的批量插入
210
+ const results = [];
211
+ let lastContext = null;
212
+ for (const [table, items] of groupedData.entries()) {
213
+ try {
214
+ const ctx = await this.executeOnTable(table, { data: items }, keys_1.KeysOfSimpleSQL.SIMPLE_BATCH_INSERT);
215
+ lastContext = ctx;
216
+ results.push({ table, affected: ((_a = ctx.getResModelItem('affected')) === null || _a === void 0 ? void 0 : _a.affectedRows) || items.length, rowCount: items.length, error: null, context: ctx });
217
+ }
218
+ catch (e) {
219
+ results.push({ table, affected: 0, rowCount: items.length, error: e, context: null });
220
+ }
221
+ }
222
+ const tableResults = results.map(r => ({ table: r.table, affected: r.affected, rowCount: r.rowCount }));
223
+ const errors = results.filter(r => r.error !== null).map(r => ({ table: r.table, error: r.error }));
224
+ const totalAffected = results.reduce((sum, r) => sum + r.affected, 0);
225
+ const finalContext = lastContext || ((_b = results.find(r => r.context)) === null || _b === void 0 ? void 0 : _b.context);
226
+ if (!finalContext)
227
+ throw new Error('[ShardingByTimeCrud] batchInsert failed - no successful inserts');
228
+ return new ShardingResult_1.ShardingBatchInsertResult({ totalAffected, tableResults, tableCount: groupedData.size, success: errors.length === 0, errors, lastContext: finalContext });
229
+ }
230
+ async update(reqJson) {
231
+ this.validateRoutingFieldForCondition(reqJson, 'update');
232
+ const targetTable = this.resolveTableForCondition(reqJson.condition);
233
+ const cleanedReqJson = this.cleanTimeColumnForSingleTableQuery(reqJson);
234
+ const ctx = await this.executeOnTable(targetTable, cleanedReqJson, keys_1.KeysOfSimpleSQL.SIMPLE_UPDATE);
235
+ const affected = ctx.getResModel().affected || { affectedRows: 0 };
236
+ return new CrudResult_1.CrudWriteResult({ affectedRows: affected.affectedRows, insertId: affected.insertId, rawContext: ctx });
237
+ }
238
+ async delete(reqJson) {
239
+ this.validateRoutingFieldForCondition(reqJson, 'delete');
240
+ const targetTable = this.resolveTableForCondition(reqJson.condition);
241
+ const cleanedReqJson = this.cleanTimeColumnForSingleTableQuery(reqJson);
242
+ const ctx = await this.executeOnTable(targetTable, cleanedReqJson, keys_1.KeysOfSimpleSQL.SIMPLE_DELETE);
243
+ const affected = ctx.getResModel().affected || { affectedRows: 0 };
244
+ return new CrudResult_1.CrudWriteResult({ affectedRows: affected.affectedRows, insertId: affected.insertId, rawContext: ctx });
245
+ }
246
+ async restore(reqJson) {
247
+ this.validateRoutingFieldForCondition(reqJson, 'restore');
248
+ const targetTable = this.resolveTableForCondition(reqJson.condition);
249
+ if (!reqJson.condition || Object.keys(reqJson.condition).length === 0) {
250
+ throw new Error('[ShardingByTimeCrud] restore 操作必须指定恢复条件');
251
+ }
252
+ const cleanedReqJson = this.cleanTimeColumnForSingleTableQuery(reqJson);
253
+ const restoreReqJson = {
254
+ ...cleanedReqJson,
255
+ data: { ...cleanedReqJson.data, deleted_at: 0, deleted_by: '' },
256
+ };
257
+ const ctx = await this.executeOnTable(targetTable, restoreReqJson, keys_1.KeysOfSimpleSQL.SIMPLE_UPDATE);
258
+ const affected = ctx.getResModel().affected || { affectedRows: 0 };
259
+ return new CrudResult_1.CrudWriteResult({ affectedRows: affected.affectedRows, insertId: affected.insertId, rawContext: ctx });
260
+ }
261
+ /**
262
+ * 插入或更新(双路由)
263
+ *
264
+ * 1. condition 路由定位查找分表
265
+ * 2. 检查记录是否存在
266
+ * 3. 存在 → UPDATE(conditionTable)
267
+ * 4. 不存在 → data 路由定位插入分表,INSERT(dataTable)
268
+ */
269
+ async insertOrUpdate(reqJson) {
270
+ this.validateTimeColumnForData(reqJson, 'insertOrUpdate');
271
+ this.validateRoutingFieldForCondition(reqJson, 'insertOrUpdate');
272
+ const conditionTable = this.resolveTableForCondition(reqJson.condition);
273
+ const cleanedReqJson = this.cleanTimeColumnForSingleTableQuery(reqJson);
274
+ const existCtx = await this.executeOnTable(conditionTable, cleanedReqJson, keys_1.KeysOfSimpleSQL.SIMPLE_QUERY_EXIST);
275
+ const isExist = existCtx.getResModelItem('is_exist') === true;
276
+ if (isExist) {
277
+ const updateCtx = await this.executeOnTable(conditionTable, cleanedReqJson, keys_1.KeysOfSimpleSQL.SIMPLE_UPDATE);
278
+ const updateAffected = updateCtx.getResModel().affected;
279
+ return new CrudResult_1.CrudUpsertResult({ insertAffected: undefined, updateAffected, isExist: true, rawContext: updateCtx });
280
+ }
281
+ const dataTable = this.resolveTableForInsert(reqJson.data);
282
+ await this.createShardingTableIfNeeded(dataTable);
283
+ const insertCtx = await this.executeOnTable(dataTable, cleanedReqJson, keys_1.KeysOfSimpleSQL.SIMPLE_INSERT);
284
+ const insertAffected = insertCtx.getResModel().affected;
285
+ return new CrudResult_1.CrudUpsertResult({ insertAffected, updateAffected: undefined, isExist: false, rawContext: insertCtx });
286
+ }
287
+ // ============ 查询操作 ============
288
+ async findOne(reqJson) {
289
+ const cleanedReqJson = this.cleanTimeColumnForSingleTableQuery(reqJson);
290
+ const targetTables = await this.resolveFindTables(reqJson);
291
+ if (targetTables.length === 0) {
292
+ throw new Error('[ShardingByTimeCrud] findOne: no matching tables found');
293
+ }
294
+ let lastCtx = null;
295
+ let lastTable;
296
+ for (const table of targetTables) {
297
+ try {
298
+ const ctx = await this.executeOnTable(table, cleanedReqJson, keys_1.KeysOfSimpleSQL.SIMPLE_QUERY_ONE);
299
+ lastCtx = ctx;
300
+ lastTable = table;
301
+ const row = ctx.getOneObj();
302
+ if (row) {
303
+ return new CrudResult_1.CrudQueryOneResult({ row: row, rawContext: ctx, debugInfo: this.buildDebugInfo(table, reqJson.condition), fromTable: table });
304
+ }
305
+ }
306
+ catch (e) {
307
+ // 表不存在时跳过
308
+ }
309
+ }
310
+ return new CrudResult_1.CrudQueryOneResult({ row: null, rawContext: lastCtx, debugInfo: this.buildDebugInfo(targetTables[0], reqJson.condition), fromTable: lastTable });
311
+ }
312
+ async findUniqueOne(reqJson) {
313
+ const cleanedReqJson = this.cleanTimeColumnForSingleTableQuery(reqJson);
314
+ const targetTables = await this.resolveFindTables(reqJson);
315
+ if (targetTables.length === 0) {
316
+ return new CrudResult_1.CrudQueryOneResult({ row: null, rawContext: null, debugInfo: this.buildDebugInfo(undefined, reqJson.condition), fromTable: null });
317
+ }
318
+ const allRows = [];
319
+ const foundTables = [];
320
+ let firstCtx = null;
321
+ for (const table of targetTables) {
322
+ try {
323
+ const ctx = await this.executeOnTable(table, cleanedReqJson, keys_1.KeysOfSimpleSQL.SIMPLE_QUERY, { maxLimit: 2 });
324
+ if (!firstCtx)
325
+ firstCtx = ctx;
326
+ const rows = ctx.getResRows();
327
+ if (rows.length > 0) {
328
+ allRows.push(...rows);
329
+ foundTables.push(table);
330
+ if (allRows.length > 1) {
331
+ throw new Error(this.formatUniqueError(allRows.length, foundTables, reqJson.condition, foundTables.length > 1));
332
+ }
333
+ }
334
+ }
335
+ catch (e) {
336
+ if (e instanceof Error && e.message.startsWith('[ShardingBase] findUniqueOne'))
337
+ throw e;
338
+ }
339
+ }
340
+ const fromTable = foundTables[0] || null;
341
+ return new CrudResult_1.CrudQueryOneResult({
342
+ row: allRows[0] || null, rawContext: firstCtx,
343
+ debugInfo: this.buildDebugInfo(fromTable || targetTables[0], reqJson.condition),
344
+ fromTable: fromTable || targetTables[0],
345
+ });
346
+ }
347
+ async findList(reqJson) {
348
+ const cleanedReqJson = this.cleanTimeColumnForSingleTableQuery(reqJson);
349
+ const targetTables = await this.resolveFindTables(reqJson);
350
+ if (targetTables.length === 0) {
351
+ return new CrudResult_1.CrudQueryListResult({ rows: [], rawContext: null });
352
+ }
353
+ if (targetTables.length === 1) {
354
+ const ctx = await this.executeOnTable(targetTables[0], cleanedReqJson, keys_1.KeysOfSimpleSQL.SIMPLE_QUERY);
355
+ return new CrudResult_1.CrudQueryListResult({ rows: ctx.getResRows(), rawContext: ctx });
356
+ }
357
+ this.validateFindOrderBy(reqJson);
358
+ const tablesForMerge = this.sortTablesForOrderBy(targetTables, reqJson);
359
+ const mergeResult = await this.merger.mergeQuery(this.crudProFactory(), tablesForMerge, cleanedReqJson, this.buildCfg(keys_1.KeysOfSimpleSQL.SIMPLE_QUERY));
360
+ return new CrudResult_1.CrudQueryListResult({ rows: mergeResult.rows, rawContext: mergeResult.lastCtx });
361
+ }
362
+ async findPage(reqJson) {
363
+ const cleanedReqJson = this.cleanTimeColumnForSingleTableQuery(reqJson);
364
+ const targetTables = await this.resolveFindTables(reqJson);
365
+ if (targetTables.length === 0) {
366
+ throw new Error('[ShardingByTimeCrud] findPage: no matching tables found');
367
+ }
368
+ if (targetTables.length === 1) {
369
+ const ctx = await this.executeOnTable(targetTables[0], cleanedReqJson, keys_1.KeysOfSimpleSQL.SIMPLE_QUERY_PAGE);
370
+ const pageResult = ctx.getResModelForQueryPage();
371
+ return new CrudResult_1.CrudQueryPageResult({ rows: pageResult.rows, totalCount: pageResult.total_count, rawContext: ctx });
372
+ }
373
+ this.validateFindOrderBy(reqJson);
374
+ const tablesForMerge = this.sortTablesForOrderBy(targetTables, reqJson);
375
+ const pageResult = await this.merger.mergePageQuery(this.crudProFactory(), tablesForMerge, cleanedReqJson, this.buildCfg(keys_1.KeysOfSimpleSQL.SIMPLE_QUERY_PAGE));
376
+ return new CrudResult_1.CrudQueryPageResult({ rows: pageResult.rows, totalCount: pageResult.total_count, rawContext: pageResult.lastCtx });
377
+ }
378
+ async findCount(reqJson) {
379
+ var _a;
380
+ const cleanedReqJson = this.cleanTimeColumnForSingleTableQuery(reqJson);
381
+ const targetTables = await this.resolveFindTables(reqJson);
382
+ if (targetTables.length === 0) {
383
+ throw new Error('[ShardingByTimeCrud] findCount: no matching tables found');
384
+ }
385
+ if (targetTables.length === 1) {
386
+ const ctx = await this.executeOnTable(targetTables[0], cleanedReqJson, keys_1.KeysOfSimpleSQL.SIMPLE_QUERY_COUNT);
387
+ return new CrudResult_1.CrudCountResult({ count: ctx.getResModelItem('total_count') || 0, rawContext: ctx });
388
+ }
389
+ const countResults = [];
390
+ for (const table of targetTables) {
391
+ countResults.push(await this.findCountFromTable(table, cleanedReqJson));
392
+ }
393
+ const totalCount = countResults.reduce((sum, r) => sum + r.count, 0);
394
+ const firstCtx = ((_a = countResults.find(r => r.ctx !== null)) === null || _a === void 0 ? void 0 : _a.ctx) || null;
395
+ return new CrudResult_1.CrudCountResult({ count: totalCount, rawContext: firstCtx });
396
+ }
397
+ async isExist(reqJson) {
398
+ const cleanedReqJson = this.cleanTimeColumnForSingleTableQuery(reqJson);
399
+ const targetTables = await this.resolveFindTables(reqJson);
400
+ if (targetTables.length === 0) {
401
+ throw new Error('[ShardingByTimeCrud] isExist: no matching tables found');
402
+ }
403
+ if (targetTables.length === 1) {
404
+ const ctx = await this.executeOnTable(targetTables[0], cleanedReqJson, keys_1.KeysOfSimpleSQL.SIMPLE_QUERY_EXIST);
405
+ return new CrudResult_1.CrudExistResult({ exists: ctx.getResModelItem('is_exist') === true, rawContext: ctx });
406
+ }
407
+ let exists = false;
408
+ let firstCtx = null;
409
+ for (const table of targetTables) {
410
+ const result = await this.isExistInTable(table, cleanedReqJson);
411
+ if (!firstCtx && result.ctx)
412
+ firstCtx = result.ctx;
413
+ if (result.exists) {
414
+ exists = true;
415
+ break;
416
+ }
417
+ }
418
+ return new CrudResult_1.CrudExistResult({ exists, rawContext: firstCtx });
419
+ }
420
+ // ============ 校验 ============
421
+ validateTimeColumnForData(reqJson, operation) {
422
+ const timeColumn = this.config.timeColumn;
423
+ const data = reqJson.data;
424
+ if (!data || data[timeColumn] === undefined) {
425
+ throw new Error(`[ShardingByTimeCrud] ${operation} 操作的 data 必须包含时间字段 '${timeColumn}'`);
426
+ }
427
+ }
428
+ validateTimeColumnForBatchData(dataArray, operation) {
429
+ const timeColumn = this.config.timeColumn;
430
+ for (let i = 0; i < dataArray.length; i++) {
431
+ if (!dataArray[i] || dataArray[i][timeColumn] === undefined) {
432
+ throw new Error(`[ShardingByTimeCrud] ${operation} 操作的 data[${i}] 必须包含时间字段 '${timeColumn}'`);
433
+ }
434
+ }
435
+ }
436
+ validateRoutingFieldForCondition(reqJson, operation) {
437
+ const timeColumn = this.config.timeColumn;
438
+ const condition = reqJson.condition;
439
+ if (!condition || !condition[timeColumn]) {
440
+ throw new Error(`[ShardingByTimeCrud] ${operation} 操作的 condition 必须包含时间字段 '${timeColumn}',` +
441
+ `用于路由到正确的分表。`);
442
+ }
443
+ }
444
+ validateFindOrderBy(reqJson) {
445
+ const timeColumn = this.config.timeColumn;
446
+ const orderBy = reqJson.orderBy;
447
+ if (!orderBy) {
448
+ throw new Error(`[ShardingByTimeCrud] 多表查询必须传 orderBy 参数。期望: '${timeColumn} DESC' 或 '${timeColumn} ASC'`);
449
+ }
450
+ const firstOrderBy = OrderByUtils_1.OrderByUtils.getFirstOrderBy(orderBy);
451
+ if (!firstOrderBy) {
452
+ throw new Error(`[ShardingByTimeCrud] orderBy 参数格式错误,无法解析`);
453
+ }
454
+ if (firstOrderBy.fieldName !== timeColumn) {
455
+ throw new Error(`[ShardingByTimeCrud] orderBy 首个排序字段必须为 '${timeColumn}',当前: '${firstOrderBy.fieldName}'`);
456
+ }
457
+ const orderType = firstOrderBy.orderType.toUpperCase();
458
+ if (orderType !== 'ASC' && orderType !== 'DESC') {
459
+ throw new Error(`[ShardingByTimeCrud] orderBy 排序方向必须是 'ASC' 或 'DESC',当前: '${firstOrderBy.orderType}'`);
460
+ }
461
+ }
462
+ sortTablesForOrderBy(tables, reqJson) {
463
+ const isAsc = OrderByUtils_1.OrderByUtils.isFirstOrderByAsc(reqJson.orderBy);
464
+ return [...tables].sort((a, b) => isAsc ? a.localeCompare(b) : b.localeCompare(a));
465
+ }
466
+ // ============ 时间字段智能清理(详见 TIME_COLUMN_CLEAN_SPEC.md) ============
467
+ cleanTimeColumnForSingleTableQuery(reqJson) {
468
+ const { primaryKey, timeColumn, type } = this.config;
469
+ if (!timeColumn || ![ShardingConfig_1.ShardingType.YEAR, ShardingConfig_1.ShardingType.MONTH, ShardingConfig_1.ShardingType.DAY].includes(type))
470
+ return reqJson;
471
+ const condition = reqJson.condition;
472
+ if (!condition || typeof condition !== 'object')
473
+ return reqJson;
474
+ const timeValue = condition[timeColumn];
475
+ if (timeValue === undefined)
476
+ return reqJson;
477
+ // 操作符表达式 → 保留,但校验精度
478
+ if ((0, ShardingUtils_1.isOperatorExpression)(timeValue)) {
479
+ this.validateTimeOperatorPrecision(timeValue, timeColumn);
480
+ return reqJson;
481
+ }
482
+ const hasPrimaryKey = primaryKey && condition[primaryKey] !== undefined;
483
+ if (typeof timeValue === 'string') {
484
+ const range = (0, ShardingUtils_1.expandDateToRange)(timeValue);
485
+ if (hasPrimaryKey) {
486
+ const { [timeColumn]: _, ...cleanedCondition } = condition;
487
+ return { ...reqJson, condition: cleanedCondition };
488
+ }
489
+ if (range) {
490
+ return { ...reqJson, condition: { ...condition, [timeColumn]: range } };
491
+ }
492
+ return reqJson;
493
+ }
494
+ if (hasPrimaryKey) {
495
+ const { [timeColumn]: _, ...cleanedCondition } = condition;
496
+ return { ...reqJson, condition: cleanedCondition };
497
+ }
498
+ return reqJson;
499
+ }
500
+ validateTimeOperatorPrecision(operatorValue, timeColumn) {
501
+ const PRECISION_OPERATORS = ['$gte', '$lte', '$gt', '$lt'];
502
+ const dateOnlyPattern = /^\d{4}-\d{1,2}-\d{1,2}$/;
503
+ for (const op of PRECISION_OPERATORS) {
504
+ const value = operatorValue[op];
505
+ if (value !== undefined && typeof value === 'string' && dateOnlyPattern.test(value)) {
506
+ throw new Error(`[ShardingByTimeCrud] 时间字段 '${timeColumn}' 的 ${op} 值必须精确到秒,` +
507
+ `当前值: '${value}'。` +
508
+ `请使用 '${value} 23:59:59'($lte/$lt)或 '${value} 00:00:00'($gte/$gt),` +
509
+ `或直接传粒度字符串由系统自动转换范围。`);
510
+ }
511
+ }
512
+ const rangeValue = operatorValue.$range;
513
+ if (Array.isArray(rangeValue) && rangeValue.length >= 2) {
514
+ for (let i = 0; i < rangeValue.length; i++) {
515
+ const val = rangeValue[i];
516
+ if (typeof val === 'string' && dateOnlyPattern.test(val)) {
517
+ throw new Error(`[ShardingByTimeCrud] 时间字段 '${timeColumn}' 的 $range[${i}] 值必须精确到秒,` +
518
+ `当前值: '${val}'。请使用 '${val} ${i === 0 ? '00:00:00' : '23:59:59'}'`);
519
+ }
520
+ }
521
+ }
522
+ }
523
+ }
524
+ exports.ShardingByTimeCrud = ShardingByTimeCrud;