baja-lite 1.4.12 → 1.4.13

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/object.d.ts CHANGED
@@ -56,12 +56,41 @@ export declare const coverComplexBean: <T>(source: any, classType: any) => {
56
56
  export declare const fixEmptyPrototy: (target: any, source: {
57
57
  [key: string]: any;
58
58
  }) => Promise<void>;
59
+ /**
60
+ * 1. 统计array中某个字段key的数量:{ [k: string]: number }
61
+ *
62
+ * @param array T组成的array,数据源
63
+ * @param key 返回结果{ [k: string]: number }的key
64
+ * @param defKey 如果array中某个对象没有key字段,则归类到默认key中
65
+ * @returns
66
+ */
59
67
  export declare const mixArray: <T>(array: T[], key: keyof T, defKey?: string) => {
60
68
  [key: string]: number;
61
69
  };
70
+ /**
71
+ * 1. 将T组成的array按照某个字段(key)提取为{ [key: string]: V[] }
72
+ * @param array T组成的array,数据源
73
+ * @param key 返回结果{ [key: string]: V[] }的key
74
+ * @param value 返回结果{ [key: string]: V[] }中的V可以是T,也可以是T的某个字段(指定value参数)
75
+ * @param defKey 如果array中某个对象没有key字段,则归类到默认key中
76
+ * @returns
77
+ */
62
78
  export declare const mixList: <T, V = T>(array: T[], key: keyof T, value?: keyof T, defKey?: string) => {
63
79
  [key: string]: V[];
64
80
  };
81
+ /**
82
+ * ## 仿照Object.assign的数组方法
83
+ * ### 1. 用法参照
84
+ * ```
85
+ * // ID 是 数组中对象的关键字段,用来区分同一条记录.支持多个字段(联合主键场景)
86
+ * // 与assign的逻辑相同,后面的数组会覆盖前面数组的相同对象的同名字段
87
+ * const result = assignArray('ID', Array1, Array2, Array3...);
88
+ * ```
89
+ * @param key
90
+ * @param arrays
91
+ * @returns
92
+ */
93
+ export declare const assignArray: <T>(key: keyof T | (keyof T)[] | ((t1: T, t2: T) => boolean), ...arrays: T[][]) => T[];
65
94
  export declare const array2map: <T = string | number>(array: string[], v: T) => {
66
95
  [key: string]: T;
67
96
  };
package/object.js CHANGED
@@ -10,8 +10,7 @@ const iterate = ite.iterate;
10
10
  export const copyBean = (source, classType) => {
11
11
  const result = {};
12
12
  Object.keys(classType).forEach((key) => {
13
- result[key] =
14
- source[key] !== undefined ? source[key] : (result[key] = null);
13
+ result[key] = source[key] !== undefined ? source[key] : (result[key] = null);
15
14
  });
16
15
  return result;
17
16
  };
@@ -100,7 +99,7 @@ export const coverComplexBean = (source, classType) => {
100
99
  }
101
100
  return {
102
101
  data: convertBean(result, classType),
103
- array: arrayData
102
+ array: arrayData,
104
103
  };
105
104
  };
106
105
  /**
@@ -120,8 +119,16 @@ export const fixEmptyPrototy = async (target, source) => {
120
119
  }
121
120
  }
122
121
  };
122
+ /**
123
+ * 1. 统计array中某个字段key的数量:{ [k: string]: number }
124
+ *
125
+ * @param array T组成的array,数据源
126
+ * @param key 返回结果{ [k: string]: number }的key
127
+ * @param defKey 如果array中某个对象没有key字段,则归类到默认key中
128
+ * @returns
129
+ */
123
130
  export const mixArray = (array, key, defKey) => {
124
- const obj = array.map(item => item[key]);
131
+ const obj = array.map((item) => item[key]);
125
132
  const result = {};
126
133
  for (const i of obj) {
127
134
  let ki = '';
@@ -138,6 +145,14 @@ export const mixArray = (array, key, defKey) => {
138
145
  }
139
146
  return result;
140
147
  };
148
+ /**
149
+ * 1. 将T组成的array按照某个字段(key)提取为{ [key: string]: V[] }
150
+ * @param array T组成的array,数据源
151
+ * @param key 返回结果{ [key: string]: V[] }的key
152
+ * @param value 返回结果{ [key: string]: V[] }中的V可以是T,也可以是T的某个字段(指定value参数)
153
+ * @param defKey 如果array中某个对象没有key字段,则归类到默认key中
154
+ * @returns
155
+ */
141
156
  export const mixList = (array, key, value, defKey) => {
142
157
  const result = {};
143
158
  for (const i of array) {
@@ -160,6 +175,34 @@ export const mixList = (array, key, value, defKey) => {
160
175
  }
161
176
  return result;
162
177
  };
178
+ /**
179
+ * ## 仿照Object.assign的数组方法
180
+ * ### 1. 用法参照
181
+ * ```
182
+ * // ID 是 数组中对象的关键字段,用来区分同一条记录.支持多个字段(联合主键场景)
183
+ * // 与assign的逻辑相同,后面的数组会覆盖前面数组的相同对象的同名字段
184
+ * const result = assignArray('ID', Array1, Array2, Array3...);
185
+ * ```
186
+ * @param key
187
+ * @param arrays
188
+ * @returns
189
+ */
190
+ export const assignArray = (key, ...arrays) => {
191
+ const result = [];
192
+ const match = key instanceof Function ? key : key instanceof Array ? (t1, t2) => key.every((k) => t1[k] === t2[k]) : (t1, t2) => t1[key] === t2[key];
193
+ for (const array of arrays) {
194
+ for (const item of array) {
195
+ const find = result.find((i) => match(i, item));
196
+ if (find) {
197
+ Object.assign(find, item);
198
+ }
199
+ else {
200
+ result.push(item);
201
+ }
202
+ }
203
+ }
204
+ return result;
205
+ };
163
206
  export const array2map = (array, v) => {
164
207
  const ot = {};
165
208
  for (const item of array) {
@@ -195,9 +238,9 @@ export const arraySplit = (datas, { everyLength = 0, groupCount = 0 } = {}) => {
195
238
  }
196
239
  };
197
240
  const P2CEX = /[A-Z]/g;
198
- export const P2C = (pro, IF = true) => IF ? pro.replace(P2CEX, (a) => `_${a.toLowerCase()}`) : pro;
241
+ export const P2C = (pro, IF = true) => (IF ? pro.replace(P2CEX, (a) => `_${a.toLowerCase()}`) : pro);
199
242
  const C2PEX = /_([a-z])/g;
200
- export const C2P = (pro, IF = true) => IF ? pro.replace(C2PEX, (a, b) => `${b.toUpperCase()}`) : pro;
243
+ export const C2P = (pro, IF = true) => (IF ? pro.replace(C2PEX, (a, b) => `${b.toUpperCase()}`) : pro);
201
244
  export function C2P2(datas, hump, convert) {
202
245
  if (datas instanceof Array) {
203
246
  return iterate(datas)
@@ -233,7 +276,9 @@ export function C2P2(datas, hump, convert) {
233
276
  }
234
277
  export function P2C2(datas) {
235
278
  if (datas instanceof Array) {
236
- return iterate(datas).map((data) => Object.fromEntries(Object.entries(data).map(([K, V]) => [P2C(K), V]))).toArray();
279
+ return iterate(datas)
280
+ .map((data) => Object.fromEntries(Object.entries(data).map(([K, V]) => [P2C(K), V])))
281
+ .toArray();
237
282
  }
238
283
  else if (datas) {
239
284
  return Object.fromEntries(Object.entries(datas).map(([K, V]) => [P2C(K), V]));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "baja-lite",
3
- "version": "1.4.12",
3
+ "version": "1.4.13",
4
4
  "description": "some util for self",
5
5
  "homepage": "https://github.com/void-soul/baja-lite",
6
6
  "repository": {
package/sql.d.ts CHANGED
@@ -918,7 +918,8 @@ export declare class SqlService<T extends object> {
918
918
  2. 支持实体类删除: 用于多个ID或者按实体类某些字段删除
919
919
  3. 两种模式:`mode`=`Common` 或者 `TempTable`
920
920
  3. 如果数据多,使用 `TempTable`模式
921
- 4. 当设置实体类的字段有 `logicDelete` ,将进行逻辑删除,除非设置 forceDelete = true
921
+ 4. 当设置实体类的字段有 `logicDelete` ,将进行逻辑删除,除非设置 `forceDelete` = true
922
+ 5. 支持`whereSql`直接拼接,此时必须传递`whereParams`,不建议直接使用这种方式!为了简化逻辑,它不会和ID、WHERE共存,且优先级更高。且不支持 `TempTable` Mode
922
923
  6. `tableName`: 默认使用service注解的`tableName`,可以在某个方法中覆盖
923
924
  7. `dbName`: 默认使用service注解的`dbName`,可以在某个方法中覆盖
924
925
  8. `conn`: 仅在开启事务时需要主动传入,传入示例:
@@ -935,6 +936,8 @@ export declare class SqlService<T extends object> {
935
936
  where?: Partial<T> | Array<Partial<T>>;
936
937
  mode?: DeleteMode;
937
938
  forceDelete?: boolean;
939
+ whereSql?: string;
940
+ whereParams?: Record<string, any>;
938
941
  }): Promise<number>;
939
942
  delete(option: MethodOption & {
940
943
  sync: SyncMode.Sync;
@@ -942,6 +945,8 @@ export declare class SqlService<T extends object> {
942
945
  where?: Partial<T> | Array<Partial<T>>;
943
946
  mode?: DeleteMode;
944
947
  forceDelete?: boolean;
948
+ whereSql?: string;
949
+ whereParams?: Record<string, any>;
945
950
  }): number;
946
951
  private _template;
947
952
  /**
@@ -1844,9 +1849,11 @@ declare class StreamQuery<T extends object> {
1844
1849
  }): number;
1845
1850
  excuteDelete(option?: MethodOption & {
1846
1851
  sync?: SyncMode.Async;
1852
+ forceDelete?: boolean;
1847
1853
  }): Promise<number>;
1848
1854
  excuteDelete(option: MethodOption & {
1849
1855
  sync: SyncMode.Sync;
1856
+ forceDelete?: boolean;
1850
1857
  }): number;
1851
1858
  private _where;
1852
1859
  private _;
package/sql.js CHANGED
@@ -2523,20 +2523,32 @@ export class SqlService {
2523
2523
  option.where = ids.map(i => ({ [idName]: i }));
2524
2524
  }
2525
2525
  const wheres = option.where instanceof Array ? option.where : [option.where];
2526
- if (wheres.length === 0) {
2526
+ if (wheres.length === 0 && (!option.whereSql || !option.whereParams)) {
2527
2527
  return 0;
2528
2528
  }
2529
+ if (option.whereSql && option.whereParams) {
2530
+ option.mode = DeleteMode.Common;
2531
+ }
2529
2532
  const sqls = [];
2530
2533
  if (option.mode === DeleteMode.Common) {
2531
- const params = new Array();
2532
- const whereSql = iterate(wheres).map(where => {
2533
- return `(
2534
- ${Object.entries(where).map(([K, V]) => {
2535
- params.push(V);
2536
- return `${this[_fields][K]?.C2()} = ?`;
2537
- }).join(' AND ')}
2538
- )`;
2539
- }).join(' OR ');
2534
+ let params;
2535
+ let whereSql;
2536
+ if (option.whereSql && option.whereParams) {
2537
+ const gen = this._generSql(option.dbType, option.whereSql, option.whereParams);
2538
+ whereSql = gen.sql;
2539
+ params = gen.params;
2540
+ }
2541
+ else {
2542
+ params = new Array();
2543
+ whereSql = iterate(wheres).map(where => {
2544
+ return `(
2545
+ ${Object.entries(where).map(([K, V]) => {
2546
+ params.push(V);
2547
+ return `${this[_fields][K]?.C2()} = ?`;
2548
+ }).join(' AND ')}
2549
+ )`;
2550
+ }).join(' OR ');
2551
+ }
2540
2552
  if (this[_stateFileName] !== undefined && option.forceDelete !== true) {
2541
2553
  params.unshift(this[_deleteState]);
2542
2554
  sqls.push({
@@ -3865,15 +3877,15 @@ class StreamQuery {
3865
3877
  option ?? (option = {});
3866
3878
  option.sync ?? (option.sync = SyncMode.Async);
3867
3879
  const { where, params } = this._where();
3868
- const sql = `DELETE FROM ${option.tableName ?? this._service[_tableName]}
3869
- ${where ? ' WHERE ' : ''}
3870
- ${where}
3871
- `;
3880
+ // const sql = `DELETE FROM ${option.tableName ?? this._service[_tableName]}
3881
+ // ${where ? ' WHERE ' : ''}
3882
+ // ${where}
3883
+ // `;
3872
3884
  if (option.sync === SyncMode.Async) {
3873
- return this._service.excute({ ...option, sync: SyncMode.Async, sql, params });
3885
+ return this._service.delete({ ...option, sync: SyncMode.Async, whereSql: where, whereParams: params });
3874
3886
  }
3875
3887
  else {
3876
- return this._service.excute({ ...option, sync: SyncMode.Sync, sql, params });
3888
+ return this._service.delete({ ...option, sync: SyncMode.Sync, whereSql: where, whereParams: params });
3877
3889
  }
3878
3890
  }
3879
3891
  _where() {