jinbi-utils 1.0.19 → 1.0.21
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/.babelrc +19 -0
- package/.cz-config.js +55 -0
- package/.dockerignore +3 -0
- package/.editorconfig +12 -0
- package/.eslintignore +8 -0
- package/.eslintrc.js +54 -0
- package/.versionrc.json +9 -0
- package/CHUNK_OPTIMIZER_USAGE.md +132 -0
- package/Dockerfile +3 -0
- package/QUICK_RELEASE.md +85 -0
- package/RELEASE_GUIDE.md +243 -0
- package/api-extractor.json +15 -0
- package/commitlint.config.js +3 -0
- package/jest.config.js +15 -0
- package/package.json +8 -35
- package/rollup.config.chunk-optimizer.js +32 -0
- package/rollup.config.js +73 -0
- package/src/array/index.ts +85 -0
- package/src/build/chunk-optimizer/ARCHITECTURE.md +347 -0
- package/src/build/chunk-optimizer/QUICK_START.md +370 -0
- package/src/build/chunk-optimizer/README.md +240 -0
- package/src/build/chunk-optimizer/core/chunk-generator.ts +166 -0
- package/src/build/chunk-optimizer/core/classifier.ts +148 -0
- package/src/build/chunk-optimizer/core/dependency-reader.ts +138 -0
- package/src/build/chunk-optimizer/examples/basic-usage.ts +234 -0
- package/src/build/chunk-optimizer/index.ts +166 -0
- package/src/build/chunk-optimizer/rules/common-rules.ts +131 -0
- package/src/build/chunk-optimizer/rules/framework-rules.ts +93 -0
- package/src/build/chunk-optimizer/rules/index.ts +27 -0
- package/src/build/chunk-optimizer/test.ts +94 -0
- package/src/build/chunk-optimizer/types.ts +128 -0
- package/src/color/index.ts +58 -0
- package/src/common/index.ts +353 -0
- package/src/constant/common.constant.ts +13 -0
- package/src/date/index.ts +143 -0
- package/src/dom/index.ts +198 -0
- package/src/file/index.ts +319 -0
- package/src/http/apiBuilder/README.md +648 -0
- package/src/http/apiBuilder/api-builder.ts +502 -0
- package/src/http/apiBuilder/example.ts +243 -0
- package/src/http/apiBuilder/index.ts +1 -0
- package/src/http/apiBuilder//345/277/253/351/200/237/345/217/202/350/200/203.md +199 -0
- package/src/http/http.ts +79 -0
- package/src/http/httpEnums.ts +61 -0
- package/src/iam/index.ts +46 -0
- package/src/index.ts +20 -0
- package/src/middleware/requestLogger.middware.ts +371 -0
- package/src/middleware/requestLoggerUnified.ts +371 -0
- package/src/number/index.ts +362 -0
- package/src/object/index.ts +54 -0
- package/src/print/index.ts +102 -0
- package/src/string/index.ts +189 -0
- package/src/utils/curl.ts +108 -0
- package/src/validate/index.ts +100 -0
- package/src/websocket/emitter.ts +39 -0
- package/src/websocket/index.ts +6 -0
- package/src/websocket/manager.ts +151 -0
- package/src/websocket/pinia-store.ts +91 -0
- package/src/websocket/service.ts +34 -0
- package/src/websocket/types.ts +45 -0
- package/test/common/index.test.ts +19 -0
- package/test/date/index.test.ts +107 -0
- package/test/file/index.test.ts +104 -0
- package/test/number/index.test.ts +108 -0
- package/test/object/index.test.ts +20 -0
- package/test/string/index.test.ts +82 -0
- package/tsconfig.json +39 -0
- package/typedoc.json +12 -0
- package/types/file/index.d.ts +7 -0
- package/types/index.d.ts +1 -0
- package/types/websocket/emitter.d.ts +16 -0
- package/types/websocket/index.d.ts +6 -0
- package/types/websocket/manager.d.ts +36 -0
- package/types/websocket/pinia-store.d.ts +25 -0
- package/types/websocket/service.d.ts +13 -0
- package/types/websocket/types.d.ts +34 -0
- package/dist/chunk-optimizer.cjs +0 -703
- package/dist/index.esm.js +0 -2791
- package/dist/index.esm.min.js +0 -15
- package/dist/index.umd.js +0 -2899
- package/dist/index.umd.min.js +0 -16
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* API 请求参数构建工具
|
|
3
|
+
* @description 用于快速组装后端 JSON 格式请求参数
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export interface FieldCatalogItem {
|
|
7
|
+
type: 'number' | 'string' | 'date' | 'boolean' | 'json' | 'object' | 'bigint' | 'int' | 'tinyint' | 'varchar' | 'datetime';
|
|
8
|
+
label: string;
|
|
9
|
+
allowed_field: string;
|
|
10
|
+
nullable: boolean;
|
|
11
|
+
dict_field: boolean;
|
|
12
|
+
length?: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface FieldCatalog {
|
|
16
|
+
[key: string]: FieldCatalogItem;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface Filter {
|
|
20
|
+
field: string;
|
|
21
|
+
op: string;
|
|
22
|
+
value: any;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface NestedFilters {
|
|
26
|
+
logic: 'and' | 'or';
|
|
27
|
+
filters: (Filter | NestedFilters)[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface OrderBy {
|
|
31
|
+
field: string;
|
|
32
|
+
direction: 'asc' | 'desc';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface QueryParams {
|
|
36
|
+
filters: NestedFilters;
|
|
37
|
+
order_by: OrderBy[];
|
|
38
|
+
limit: number;
|
|
39
|
+
offset: number;
|
|
40
|
+
field_catalog?: FieldCatalog;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface InsertParams {
|
|
44
|
+
insert: Record<string, any>[];
|
|
45
|
+
field_catalog?: FieldCatalog;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface UpdateItem {
|
|
49
|
+
set: Record<string, any>;
|
|
50
|
+
where: Record<string, any>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface UpdateParams {
|
|
54
|
+
update: UpdateItem[];
|
|
55
|
+
field_catalog?: FieldCatalog;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* API 构建器基类
|
|
60
|
+
*/
|
|
61
|
+
abstract class BaseBuilder<T = any> {
|
|
62
|
+
protected fieldCatalog?: FieldCatalog;
|
|
63
|
+
protected params: T;
|
|
64
|
+
|
|
65
|
+
constructor(fieldCatalog?: FieldCatalog) {
|
|
66
|
+
this.fieldCatalog = fieldCatalog;
|
|
67
|
+
// 只有传入 fieldCatalog 时才添加到 params
|
|
68
|
+
this.params = (fieldCatalog ? { field_catalog: fieldCatalog } : {}) as T;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 获取构建的参数
|
|
73
|
+
*/
|
|
74
|
+
build(): T {
|
|
75
|
+
return this.params;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* API 构建器主类
|
|
81
|
+
*/
|
|
82
|
+
class ApiBuilder {
|
|
83
|
+
private fieldCatalog?: FieldCatalog;
|
|
84
|
+
|
|
85
|
+
constructor(fieldCatalog?: FieldCatalog) {
|
|
86
|
+
this.fieldCatalog = fieldCatalog;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* 创建查询请求构建器
|
|
91
|
+
*/
|
|
92
|
+
query(): QueryBuilder {
|
|
93
|
+
return new QueryBuilder(this.fieldCatalog);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 创建新增请求构建器
|
|
98
|
+
*/
|
|
99
|
+
insert(): InsertBuilder {
|
|
100
|
+
return new InsertBuilder(this.fieldCatalog);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* 创建更新请求构建器
|
|
105
|
+
*/
|
|
106
|
+
update(): UpdateBuilder {
|
|
107
|
+
return new UpdateBuilder(this.fieldCatalog);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// 静态方法保持向后兼容
|
|
111
|
+
static query(fieldCatalog?: FieldCatalog): QueryBuilder {
|
|
112
|
+
return new QueryBuilder(fieldCatalog);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
static insert(fieldCatalog?: FieldCatalog): InsertBuilder {
|
|
116
|
+
return new InsertBuilder(fieldCatalog);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
static update(fieldCatalog?: FieldCatalog): UpdateBuilder {
|
|
120
|
+
return new UpdateBuilder(fieldCatalog);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 嵌套查询构建器类
|
|
126
|
+
* 用于构建嵌套的过滤条件
|
|
127
|
+
*/
|
|
128
|
+
class NestedQueryBuilder {
|
|
129
|
+
private parent: QueryBuilder | NestedQueryBuilder;
|
|
130
|
+
private nestedFilters: NestedFilters;
|
|
131
|
+
|
|
132
|
+
constructor(logic: 'and' | 'or', parent: QueryBuilder | NestedQueryBuilder) {
|
|
133
|
+
this.parent = parent;
|
|
134
|
+
this.nestedFilters = {
|
|
135
|
+
logic,
|
|
136
|
+
filters: []
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* 添加 WHERE 条件到嵌套查询中
|
|
142
|
+
*/
|
|
143
|
+
where(field: string, op: string, value?: any): this;
|
|
144
|
+
where(field: string, value: any): this;
|
|
145
|
+
where(conditions: Record<string, any>): this;
|
|
146
|
+
where(fieldOrConditions: string | Record<string, any>, opOrValue?: any, value?: any): this {
|
|
147
|
+
// 对象形式: where({ user_id: 10086, status: 1 })
|
|
148
|
+
if (typeof fieldOrConditions === 'object') {
|
|
149
|
+
Object.entries(fieldOrConditions).forEach(([field, val]) => {
|
|
150
|
+
this.nestedFilters.filters.push({ field, op: '=', value: val });
|
|
151
|
+
});
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// 两参数形式: where('user_id', 10086) - 默认 =
|
|
156
|
+
if (value === undefined) {
|
|
157
|
+
this.nestedFilters.filters.push({ field: fieldOrConditions, op: '=', value: opOrValue });
|
|
158
|
+
return this;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// 三参数形式: where('user_id', '=', 10086)
|
|
162
|
+
this.nestedFilters.filters.push({ field: fieldOrConditions, op: opOrValue, value });
|
|
163
|
+
return this;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* 在嵌套查询中添加另一个嵌套组
|
|
168
|
+
*/
|
|
169
|
+
group(logic: 'and' | 'or' = 'and'): NestedQueryBuilder {
|
|
170
|
+
return new NestedQueryBuilder(logic, this);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* 结束当前嵌套组并返回父查询构建器或父嵌套构建器
|
|
175
|
+
*/
|
|
176
|
+
endGroup(): QueryBuilder | NestedQueryBuilder {
|
|
177
|
+
if (this.parent instanceof NestedQueryBuilder) {
|
|
178
|
+
this.parent.addNestedFilters(this.nestedFilters);
|
|
179
|
+
return this.parent;
|
|
180
|
+
} else {
|
|
181
|
+
this.parent.whereNested(this.nestedFilters);
|
|
182
|
+
return this.parent;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* 添加嵌套过滤条件到当前嵌套组
|
|
188
|
+
*/
|
|
189
|
+
private addNestedFilters(nestedFilters: NestedFilters) {
|
|
190
|
+
this.nestedFilters.filters.push(nestedFilters);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* 获取构建的嵌套过滤条件
|
|
195
|
+
*/
|
|
196
|
+
build(): NestedFilters {
|
|
197
|
+
return this.nestedFilters;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* 查询构建器
|
|
203
|
+
*/
|
|
204
|
+
export class QueryBuilder extends BaseBuilder<QueryParams> {
|
|
205
|
+
constructor(fieldCatalog?: FieldCatalog) {
|
|
206
|
+
super(fieldCatalog);
|
|
207
|
+
this.params = {
|
|
208
|
+
...this.params,
|
|
209
|
+
filters: { logic: 'and', filters: [] },
|
|
210
|
+
order_by: [],
|
|
211
|
+
limit: 100,
|
|
212
|
+
offset: 0,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* 添加 WHERE 条件(支持多种形式)
|
|
218
|
+
* @example
|
|
219
|
+
* .where('user_id', '=', 10086)
|
|
220
|
+
* .where('user_id', 10086) // 默认 =
|
|
221
|
+
* .where({ user_id: 10086, status: 1 }) // 对象形式
|
|
222
|
+
*/
|
|
223
|
+
where(field: string, op: string, value?: any): this;
|
|
224
|
+
where(field: string, value: any): this;
|
|
225
|
+
where(conditions: Record<string, any>): this;
|
|
226
|
+
where(fieldOrConditions: string | Record<string, any>, opOrValue?: any, value?: any): this {
|
|
227
|
+
// 对象形式: where({ user_id: 10086, status: 1 })
|
|
228
|
+
if (typeof fieldOrConditions === 'object') {
|
|
229
|
+
Object.entries(fieldOrConditions).forEach(([field, val]) => {
|
|
230
|
+
this.params.filters.filters.push({ field, op: '=', value: val });
|
|
231
|
+
});
|
|
232
|
+
return this;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// 两参数形式: where('user_id', 10086) - 默认 =
|
|
236
|
+
if (value === undefined) {
|
|
237
|
+
this.params.filters.filters.push({ field: fieldOrConditions, op: '=', value: opOrValue });
|
|
238
|
+
return this;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// 三参数形式: where('user_id', '=', 10086)
|
|
242
|
+
this.params.filters.filters.push({ field: fieldOrConditions, op: opOrValue, value });
|
|
243
|
+
return this;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* 添加嵌套的 WHERE 条件
|
|
248
|
+
* @param nestedFilters 嵌套的过滤条件
|
|
249
|
+
*/
|
|
250
|
+
whereNested(nestedFilters: NestedFilters): this {
|
|
251
|
+
this.params.filters.filters.push(nestedFilters);
|
|
252
|
+
return this;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* 创建并返回一个嵌套查询构建器,用于构建复杂的嵌套条件
|
|
257
|
+
*/
|
|
258
|
+
group(logic: 'and' | 'or' = 'and'): NestedQueryBuilder {
|
|
259
|
+
return new NestedQueryBuilder(logic, this);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* OR WHERE 条件
|
|
264
|
+
*/
|
|
265
|
+
orWhere(field: string, op: string, value?: any): this;
|
|
266
|
+
orWhere(field: string, value: any): this;
|
|
267
|
+
orWhere(conditions: Record<string, any>): this;
|
|
268
|
+
orWhere(fieldOrConditions: string | Record<string, any>, opOrValue?: any, value?: any): this {
|
|
269
|
+
// 创建一个新的 OR 组
|
|
270
|
+
const orGroup: NestedFilters = {
|
|
271
|
+
logic: 'or',
|
|
272
|
+
filters: []
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
// 对象形式: orWhere({ user_id: 10086, status: 1 })
|
|
276
|
+
if (typeof fieldOrConditions === 'object') {
|
|
277
|
+
Object.entries(fieldOrConditions).forEach(([field, val]) => {
|
|
278
|
+
orGroup.filters.push({ field, op: '=', value: val });
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
// 两参数形式: orWhere('user_id', 10086) - 默认 =
|
|
282
|
+
else if (value === undefined) {
|
|
283
|
+
orGroup.filters.push({ field: fieldOrConditions, op: '=', value: opOrValue });
|
|
284
|
+
}
|
|
285
|
+
// 三参数形式: orWhere('user_id', '=', 10086)
|
|
286
|
+
else {
|
|
287
|
+
orGroup.filters.push({ field: fieldOrConditions, op: opOrValue, value });
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// 如果当前 filters 为空或者已经是 OR 逻辑,则直接添加
|
|
291
|
+
if (this.params.filters.filters.length === 0 || this.params.filters.logic === 'or') {
|
|
292
|
+
this.params.filters.filters.push(...orGroup.filters);
|
|
293
|
+
this.params.filters.logic = 'or';
|
|
294
|
+
} else {
|
|
295
|
+
// 否则将当前所有条件包装成一个 AND 组,然后与新的 OR 组一起放在顶层
|
|
296
|
+
const andGroup: NestedFilters = {
|
|
297
|
+
logic: 'and',
|
|
298
|
+
filters: [...this.params.filters.filters]
|
|
299
|
+
};
|
|
300
|
+
this.params.filters.filters = [andGroup, orGroup];
|
|
301
|
+
this.params.filters.logic = 'and';
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
return this;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* WHERE IN
|
|
309
|
+
*/
|
|
310
|
+
whereIn(field: string, values: any[]): this {
|
|
311
|
+
this.params.filters.filters.push({ field, op: 'in', value: values });
|
|
312
|
+
return this;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* WHERE NOT IN
|
|
317
|
+
*/
|
|
318
|
+
whereNotIn(field: string, values: any[]): this {
|
|
319
|
+
this.params.filters.filters.push({ field, op: 'not in', value: values });
|
|
320
|
+
return this;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* WHERE LIKE
|
|
325
|
+
*/
|
|
326
|
+
whereLike(field: string, value: string): this {
|
|
327
|
+
this.params.filters.filters.push({ field, op: 'like', value });
|
|
328
|
+
return this;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* WHERE BETWEEN
|
|
333
|
+
*/
|
|
334
|
+
whereBetween(field: string, min: any, max: any): this {
|
|
335
|
+
this.params.filters.filters.push({ field, op: 'between', value: [min, max] });
|
|
336
|
+
return this;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* WHERE NULL
|
|
341
|
+
*/
|
|
342
|
+
whereNull(field: string): this {
|
|
343
|
+
this.params.filters.filters.push({ field, op: 'is null', value: null });
|
|
344
|
+
return this;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* WHERE NOT NULL
|
|
349
|
+
*/
|
|
350
|
+
whereNotNull(field: string): this {
|
|
351
|
+
this.params.filters.filters.push({ field, op: 'is not null', value: null });
|
|
352
|
+
return this;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* 设置逻辑运算符(不推荐直接使用,建议用 orWhere)
|
|
357
|
+
*/
|
|
358
|
+
logic(logic: 'and' | 'or'): this {
|
|
359
|
+
this.params.filters.logic = logic;
|
|
360
|
+
return this;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* 添加排序
|
|
365
|
+
*/
|
|
366
|
+
orderBy(field: string, direction: 'asc' | 'desc' = 'asc'): this {
|
|
367
|
+
this.params.order_by.push({ field, direction });
|
|
368
|
+
return this;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* 设置 LIMIT
|
|
373
|
+
*/
|
|
374
|
+
limit(limit: number): this {
|
|
375
|
+
this.params.limit = limit;
|
|
376
|
+
return this;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* 设置 OFFSET
|
|
381
|
+
*/
|
|
382
|
+
offset(offset: number): this {
|
|
383
|
+
this.params.offset = offset;
|
|
384
|
+
return this;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* 设置分页(便捷方法)
|
|
389
|
+
*/
|
|
390
|
+
paginate(limit: number, offset: number = 0): this {
|
|
391
|
+
this.params.limit = limit;
|
|
392
|
+
this.params.offset = offset;
|
|
393
|
+
return this;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
/**
|
|
398
|
+
* 新增构建器
|
|
399
|
+
*/
|
|
400
|
+
export class InsertBuilder extends BaseBuilder<InsertParams> {
|
|
401
|
+
constructor(fieldCatalog?: FieldCatalog) {
|
|
402
|
+
super(fieldCatalog);
|
|
403
|
+
this.params = {
|
|
404
|
+
...this.params,
|
|
405
|
+
insert: [],
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* 添加一条记录
|
|
411
|
+
*/
|
|
412
|
+
add(data: Record<string, any>): this {
|
|
413
|
+
this.params.insert.push(data);
|
|
414
|
+
return this;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
/**
|
|
418
|
+
* 批量添加记录
|
|
419
|
+
*/
|
|
420
|
+
addBatch(dataList: Record<string, any>[]): this {
|
|
421
|
+
this.params.insert.push(...dataList);
|
|
422
|
+
return this;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* 更新构建器
|
|
428
|
+
*/
|
|
429
|
+
export class UpdateBuilder extends BaseBuilder<UpdateParams> {
|
|
430
|
+
private currentUpdate: UpdateItem | null = null;
|
|
431
|
+
|
|
432
|
+
constructor(fieldCatalog?: FieldCatalog) {
|
|
433
|
+
super(fieldCatalog);
|
|
434
|
+
this.params = {
|
|
435
|
+
...this.params,
|
|
436
|
+
update: [],
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* 设置要更新的字段和值
|
|
442
|
+
*/
|
|
443
|
+
set(fields: Record<string, any>): this {
|
|
444
|
+
this.currentUpdate = { set: fields, where: {} };
|
|
445
|
+
return this;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
/**
|
|
449
|
+
* 设置更新条件(对象形式)
|
|
450
|
+
*/
|
|
451
|
+
where(conditions: Record<string, any>): this;
|
|
452
|
+
/**
|
|
453
|
+
* 设置更新条件(键值对形式)
|
|
454
|
+
*/
|
|
455
|
+
where(field: string, value: any): this;
|
|
456
|
+
where(fieldOrConditions: string | Record<string, any>, value?: any): this {
|
|
457
|
+
if (!this.currentUpdate) {
|
|
458
|
+
throw new Error('必须先调用 set() 方法');
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
if (typeof fieldOrConditions === 'object') {
|
|
462
|
+
this.currentUpdate.where = { ...this.currentUpdate.where, ...fieldOrConditions };
|
|
463
|
+
}
|
|
464
|
+
else {
|
|
465
|
+
this.currentUpdate.where[fieldOrConditions] = value;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
return this;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* 完成当前更新操作,开始下一个
|
|
473
|
+
*/
|
|
474
|
+
and(): this {
|
|
475
|
+
if (this.currentUpdate) {
|
|
476
|
+
this.params.update.push(this.currentUpdate);
|
|
477
|
+
this.currentUpdate = null;
|
|
478
|
+
}
|
|
479
|
+
return this;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* 批量添加更新操作
|
|
484
|
+
*/
|
|
485
|
+
setBatch(updates: UpdateItem[]): this {
|
|
486
|
+
this.params.update.push(...updates);
|
|
487
|
+
return this;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
/**
|
|
491
|
+
* 构建最终参数
|
|
492
|
+
*/
|
|
493
|
+
build(): UpdateParams {
|
|
494
|
+
if (this.currentUpdate) {
|
|
495
|
+
this.params.update.push(this.currentUpdate);
|
|
496
|
+
this.currentUpdate = null;
|
|
497
|
+
}
|
|
498
|
+
return this.params;
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
export { ApiBuilder };
|