@questwork/q-utilities 0.1.33 → 0.1.35

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.
@@ -103,6 +103,51 @@ export declare function changeCreatorOwner(that: any, { source, target }: {
103
103
  target: any;
104
104
  }): any;
105
105
 
106
+ export declare class CheckResult {
107
+ dataSets: DataSet[];
108
+ constructor(opts?: CheckResultOptions);
109
+ static init(opts?: CheckResultOptions): CheckResult | null;
110
+ static initFromArray(arr?: any[]): Array<CheckResult>;
111
+ static initOnlyValidFromArray(arr?: any[]): Array<CheckResult>;
112
+ addDataSet(dataSet: DataSet): void;
113
+ getDataSetByName(name: string): DataSet | undefined;
114
+ getAllSummary(): {
115
+ summary: DataSetSummaryItem[];
116
+ };
117
+ getSummary(): DataSetSummaryItem[];
118
+ getDuplicatedDetails(): DetailsResultItem[];
119
+ getIgnoredDetails(): DetailsResultItem[];
120
+ getInvalidDetails(): DetailsResultItem[];
121
+ getValidDetails(): Record<string, DataWrapper[]>;
122
+ getAllDetails(): {
123
+ details: {
124
+ ignoredDetails: DetailsResultItem[];
125
+ invalidDetails: DetailsResultItem[];
126
+ duplicatedDetails: DetailsResultItem[];
127
+ };
128
+ };
129
+ getDetails(): {
130
+ ignoredDetails: DetailsResultItem[];
131
+ invalidDetails: DetailsResultItem[];
132
+ duplicatedDetails: DetailsResultItem[];
133
+ };
134
+ hasInvalidData(): boolean;
135
+ }
136
+
137
+ declare interface CheckResultConfig {
138
+ hidden?: boolean;
139
+ }
140
+
141
+ declare interface CheckResultOptions {
142
+ dataSets: DataSet[] | undefined;
143
+ getDataSetByName(name: string): DataSet | undefined;
144
+ getSummary(): DataSetSummaryItem[];
145
+ getIgnoredDetails(): DetailsResultItem[];
146
+ getInvalidDetails(): DetailsResultItem[];
147
+ getDuplicatedDetails(): DetailsResultItem[];
148
+ getValidDetails(): Record<string, DataWrapper[]>;
149
+ }
150
+
106
151
  export declare function concatStringByArray(arrTemplate: any, data: any): any;
107
152
 
108
153
  export declare class ConsoleLog extends QLog {
@@ -122,6 +167,136 @@ declare function customHandler({ responseHelper, handler, ignoreError }: {
122
167
  ignoreError?: boolean;
123
168
  }): (reqOrCtx: any, resOrNext: any, nextInExpress: any) => Promise<any>;
124
169
 
170
+ declare class DataSet {
171
+ name: string;
172
+ data?: Array<DataWrapper>;
173
+ orphanData: Array<DataWrapper>;
174
+ checkResultConfig: CheckResultConfig;
175
+ saveResultConfig: SaveResultConfig;
176
+ orphanBehavior?: 'DELETE' | 'KEEP' | null;
177
+ notExistBehavior?: 'INSERT' | 'SKIP';
178
+ existBehavior?: 'UPDATE' | 'SKIP';
179
+ constructor(opts?: any);
180
+ static init(opts?: DataSet): DataSet | null;
181
+ static initFromArray(arr?: any[]): Array<DataSet>;
182
+ static initOnlyValidFromArray(arr?: any[]): Array<DataSet>;
183
+ add(data: DataWrapper): DataSetOptions;
184
+ getExistedData(): Array<DataWrapper>;
185
+ getInvalidData(): Array<DataWrapper>;
186
+ getNotExistedData(): Array<DataWrapper>;
187
+ getSkipData(): Array<DataWrapper>;
188
+ addOrphan(data: Record<string, any>): DataSet;
189
+ getOrphanData(): Array<DataWrapper>;
190
+ }
191
+
192
+ declare interface DataSetOptions {
193
+ name: string;
194
+ data?: Array<DataWrapper>;
195
+ checkResultConfig?: CheckResultConfig;
196
+ saveResultConfig?: SaveResultConfig;
197
+ orphanBehavior?: 'DELETE' | 'KEEP' | null;
198
+ notExistBehavior?: 'INSERT' | 'SKIP';
199
+ existBehavior?: 'UPDATE' | 'SKIP';
200
+ }
201
+
202
+ declare interface DataSetSummary {
203
+ total: number;
204
+ ignored: number;
205
+ valid: number;
206
+ invalid: number;
207
+ duplicated: number;
208
+ }
209
+
210
+ declare interface DataSetSummaryItem {
211
+ name: string;
212
+ hidden: boolean;
213
+ summary: DataSetSummary;
214
+ }
215
+
216
+ declare interface DataSourceConfig {
217
+ name?: string;
218
+ checkExisted?: (params: {
219
+ dataSet: DataSet;
220
+ ctx: Record<string, any>;
221
+ }) => Promise<DataSet>;
222
+ checkOrphans?: (params: {
223
+ dataSet: DataSet;
224
+ ctx: Record<string, any>;
225
+ }) => Promise<void>;
226
+ converter?: (value: any) => any;
227
+ isIgnore?: (value: any) => any;
228
+ primaryKeys?: string[];
229
+ notExistBehavior: 'INSERT' | 'SKIP';
230
+ existBehavior: 'UPDATE' | 'SKIP';
231
+ orphanBehavior?: 'DELETE' | 'KEEP';
232
+ handleData?: (params: {
233
+ dataSet: DataSet;
234
+ dataSource: DataSourceConfig;
235
+ ctx: Record<string, any>;
236
+ }) => Promise<DataSet>;
237
+ headerConfigs: HeaderConfig[];
238
+ checkResultConfig?: CheckResultConfig;
239
+ saveResultConfig?: SaveResultConfig;
240
+ }
241
+
242
+ declare class DataWrapper {
243
+ data: Record<string, any>;
244
+ logs: Array<Log>;
245
+ rawData: Record<string, any>;
246
+ rowIndex: number;
247
+ status?: Status_2;
248
+ transformedData: Record<string, any>;
249
+ constructor(opts: DataWrapperOptions);
250
+ static init(opts?: DataWrapperOptions): DataWrapper | null;
251
+ static initFromArray(arr?: any[]): Array<DataWrapper>;
252
+ static initOnlyValidFromArray(arr?: any[]): Array<DataWrapper>;
253
+ get errorLogs(): Array<Log>;
254
+ get ignoreLog(): Log | undefined;
255
+ addLog(detail: LogInput): DataWrapper;
256
+ addError(detail: LogInput): DataWrapper;
257
+ addOrphanDeleteError(msg?: string): DataWrapper;
258
+ addInsertError(msg?: string): DataWrapper;
259
+ addUpdateError(msg?: string): DataWrapper;
260
+ addDuplicatedLog(msg?: string): DataWrapper;
261
+ addSkippedLog(msg?: string): DataWrapper;
262
+ addIgnoreLog(msg?: string): DataWrapper;
263
+ addInsertedLog(msg?: string): DataWrapper;
264
+ addUpdatedLog(msg?: string): DataWrapper;
265
+ addOrphanDeletedLog(msg?: string): DataWrapper;
266
+ isCheckValid(): boolean;
267
+ isIgnored(): boolean;
268
+ isDuplicated(): boolean;
269
+ hasErrors(): boolean;
270
+ setStatus(status: keyof typeof DATAWRAPPER_STATUS): DataWrapper;
271
+ setExisted(): DataWrapper;
272
+ setFailed(): DataWrapper;
273
+ setInserted(): DataWrapper;
274
+ setSkipped(): DataWrapper;
275
+ setUpdated(): DataWrapper;
276
+ setOrphanDeleted(): DataWrapper;
277
+ setData(data: Record<string, any>): DataWrapper;
278
+ setRawData(rawData: Record<string, any>): DataWrapper;
279
+ setTransformedData(transformedData: Record<string, any>): DataWrapper;
280
+ }
281
+
282
+ declare const DATAWRAPPER_STATUS: {
283
+ ORPHAN_DELETED: string;
284
+ EXISTED: string;
285
+ FAILED: string;
286
+ INSERTED: string;
287
+ SKIPPED: string;
288
+ UPDATED: string;
289
+ };
290
+
291
+ declare interface DataWrapperOptions {
292
+ data?: Record<string, any>;
293
+ logs?: Array<Log>;
294
+ rawData?: Record<string, any>;
295
+ rowIndex?: number;
296
+ status?: Status_2;
297
+ transformedData?: Record<string, any>;
298
+ }
299
+
125
300
  declare const _default: {
126
301
  ApiResponse: typeof models.ApiResponse;
127
302
  makeApiResponse: typeof models.makeApiResponse;
@@ -178,7 +353,9 @@ declare const _default: {
178
353
  QMeta: typeof models.QMeta;
179
354
  Repo: typeof models.Repo;
180
355
  makeService: typeof models.makeService;
356
+ makeServiceBulkWrite: typeof models.makeServiceBulkWrite;
181
357
  Service: typeof models.Service;
358
+ ServiceBulkWrite: typeof models.ServiceBulkWrite;
182
359
  ActionRecord: typeof models.ActionRecord;
183
360
  Status: typeof models.Status;
184
361
  StatusDocument: typeof models.StatusDocument;
@@ -219,6 +396,22 @@ declare const _default: {
219
396
  displayName(user: any, options: any): any;
220
397
  downloadFileByUrl: typeof helpers.downloadFileByUrl;
221
398
  escapeRegex(string: any): string;
399
+ ExcelImportHelper: typeof helpers.ExcelImportHelper;
400
+ CheckResult: typeof helpers.CheckResult;
401
+ LOG_TYPE: {
402
+ readonly IGNORE: "IGNORE";
403
+ readonly DUPLICATED: "DUPLICATED";
404
+ readonly REQUIRED_ERROR: "REQUIRED_ERROR";
405
+ readonly TRANSFORM_ERROR: "TRANSFORM_ERROR";
406
+ readonly CONVERT_ERROR: "CONVERT_ERROR";
407
+ readonly ORPHAN_DELETE_ERROR: "ORPHAN_DELETE_ERROR";
408
+ readonly INSERT_ERROR: "INSERT_ERROR";
409
+ readonly UPDATE_ERROR: "UPDATE_ERROR";
410
+ readonly SKIPPED: "SKIPPED";
411
+ readonly INSERTED: "INSERTED";
412
+ readonly UPDATED: "UPDATED";
413
+ readonly ORPHAN_DELETED: "ORPHAN_DELETED";
414
+ };
222
415
  expressHelper: {
223
416
  customHandler: customHandler;
224
417
  findAllResult: findAllResult;
@@ -292,6 +485,12 @@ declare const _default: {
292
485
  };
293
486
  export default _default;
294
487
 
488
+ declare interface DetailsResultItem {
489
+ name: string;
490
+ hidden: boolean;
491
+ logs: Log[];
492
+ }
493
+
295
494
  /**
296
495
  * Detects and reports hidden/control characters in a string without modifying it.
297
496
  * @param {string} input - The string to analyze.
@@ -339,6 +538,37 @@ export declare interface ErrorDetailOptions {
339
538
 
340
539
  export declare function escapeRegex(string: any): string;
341
540
 
541
+ declare interface ExcelDataItem {
542
+ worksheetName: string;
543
+ headers: string[];
544
+ data: ExcelDataRow[];
545
+ }
546
+
547
+ declare interface ExcelDataRow {
548
+ [key: string]: any;
549
+ }
550
+
551
+ export declare class ExcelImportHelper {
552
+ static check(params: {
553
+ config: ImportConfig;
554
+ excelData: ExcelDataItem[];
555
+ context?: Record<string, any>;
556
+ }): Promise<CheckResultOptions>;
557
+ private static createExcelRow;
558
+ private static getRawDataFromHeaders;
559
+ private static isIgnore;
560
+ private static checkRequired;
561
+ private static checkDuplicated;
562
+ private static transform;
563
+ private static convert;
564
+ static save(params: {
565
+ result?: CheckResult;
566
+ config?: ImportConfig;
567
+ data?: ExcelDataItem[];
568
+ context?: Record<string, any>;
569
+ }): Promise<SaveResult>;
570
+ }
571
+
342
572
  export declare namespace expressHelper {
343
573
  export { customHandler };
344
574
  export { findAllResult };
@@ -386,6 +616,12 @@ export declare function getValueByKeys(keys: any, data: any): any;
386
616
 
387
617
  export declare function groupArrayByKey(arr: any, key: any): any;
388
618
 
619
+ declare interface HeaderConfig {
620
+ name: string;
621
+ required?: boolean;
622
+ transform?: (value: any) => any;
623
+ }
624
+
389
625
  declare namespace helpers {
390
626
  export {
391
627
  authorize,
@@ -399,6 +635,9 @@ declare namespace helpers {
399
635
  displayName,
400
636
  downloadFileByUrl,
401
637
  escapeRegex,
638
+ ExcelImportHelper,
639
+ CheckResult,
640
+ LOG_TYPE,
402
641
  expressHelper,
403
642
  extractEmails,
404
643
  formatDate,
@@ -434,6 +673,13 @@ declare function hideSensitiveFields(aggregation: any, { query }?: {
434
673
  query?: {};
435
674
  }): void;
436
675
 
676
+ declare interface ImportConfig {
677
+ beforeCheck?: (value: any) => any;
678
+ beforeSave?: (value: any) => any;
679
+ name: string;
680
+ worksheets: WorksheetConfig[];
681
+ }
682
+
437
683
  export declare function init(_class: any, options: any): any;
438
684
 
439
685
  export declare function initFromArray(_class: any, arr: any): any[];
@@ -482,6 +728,34 @@ export declare class KeyValueObject {
482
728
 
483
729
  declare type KnownErrorCode = keyof typeof ERROR_CODES;
484
730
 
731
+ declare interface Log {
732
+ rowIndex: number;
733
+ column: string;
734
+ message: string;
735
+ type: LogType;
736
+ }
737
+
738
+ export declare const LOG_TYPE: {
739
+ readonly IGNORE: "IGNORE";
740
+ readonly DUPLICATED: "DUPLICATED";
741
+ readonly REQUIRED_ERROR: "REQUIRED_ERROR";
742
+ readonly TRANSFORM_ERROR: "TRANSFORM_ERROR";
743
+ readonly CONVERT_ERROR: "CONVERT_ERROR";
744
+ readonly ORPHAN_DELETE_ERROR: "ORPHAN_DELETE_ERROR";
745
+ readonly INSERT_ERROR: "INSERT_ERROR";
746
+ readonly UPDATE_ERROR: "UPDATE_ERROR";
747
+ readonly SKIPPED: "SKIPPED";
748
+ readonly INSERTED: "INSERTED";
749
+ readonly UPDATED: "UPDATED";
750
+ readonly ORPHAN_DELETED: "ORPHAN_DELETED";
751
+ };
752
+
753
+ declare interface LogInput {
754
+ column: string;
755
+ message: string;
756
+ type: LogType;
757
+ }
758
+
485
759
  export declare class LogRecord {
486
760
  constructor(options?: {});
487
761
  data: any;
@@ -495,6 +769,8 @@ export declare class LogRecord {
495
769
  serialize(): any;
496
770
  }
497
771
 
772
+ declare type LogType = typeof LOG_TYPE[keyof typeof LOG_TYPE];
773
+
498
774
  export declare function makeApiResponse({ repo, result }: {
499
775
  repo: any;
500
776
  result: any;
@@ -504,6 +780,10 @@ export declare function makeService({ repo }: {
504
780
  repo: any;
505
781
  }): Service;
506
782
 
783
+ export declare function makeServiceBulkWrite({ repo }: {
784
+ repo: any;
785
+ }): ServiceBulkWrite;
786
+
507
787
  export declare function mergeArraysByKey(arr1: any, arr2: any): {
508
788
  key: any;
509
789
  value: any;
@@ -529,7 +809,9 @@ declare namespace models {
529
809
  QMeta,
530
810
  Repo,
531
811
  makeService,
812
+ makeServiceBulkWrite,
532
813
  Service,
814
+ ServiceBulkWrite,
533
815
  ActionRecord,
534
816
  Status,
535
817
  StatusDocument,
@@ -555,6 +837,17 @@ export declare namespace objectHelper {
555
837
  export { set };
556
838
  }
557
839
 
840
+ declare interface OrphanDetail {
841
+ deleted: DataWrapper[];
842
+ failed: DataWrapper[];
843
+ }
844
+
845
+ declare interface OrphanSummary {
846
+ total: number;
847
+ deleted: number;
848
+ failed: number;
849
+ }
850
+
558
851
  export declare function padZeros(num: any, minLength?: number): any;
559
852
 
560
853
  export declare function pMap(iterable: any, mapper: any, { concurrency, stopOnError, signal, }?: {
@@ -715,6 +1008,11 @@ export declare class Repo {
715
1008
  get queryOptions(): any;
716
1009
  get saveOptions(): any;
717
1010
  init(options: any): any;
1011
+ bulkWriteDocs({ operations, config, systemLog }: {
1012
+ operations: any;
1013
+ config: any;
1014
+ systemLog: any;
1015
+ }): Promise<any>;
718
1016
  deleteOne({ id }: {
719
1017
  id: any;
720
1018
  }): Promise<any>;
@@ -757,6 +1055,156 @@ export declare function sanitizeText(input: string, options?: {
757
1055
  debug?: boolean;
758
1056
  }): string;
759
1057
 
1058
+ declare class SaveResult {
1059
+ dataSets: DataSet[];
1060
+ constructor(opt?: any);
1061
+ addDataSet(dataSet: DataSet): this;
1062
+ getAllDetails(): {
1063
+ details: {
1064
+ failed: {
1065
+ name: string;
1066
+ hidden: boolean;
1067
+ logs: any[];
1068
+ }[];
1069
+ skipped: {
1070
+ name: string;
1071
+ hidden: boolean;
1072
+ logs: any[];
1073
+ }[];
1074
+ inserted: {
1075
+ name: string;
1076
+ hidden: boolean;
1077
+ logs: any[];
1078
+ }[];
1079
+ updated: {
1080
+ name: string;
1081
+ hidden: boolean;
1082
+ logs: any[];
1083
+ }[];
1084
+ };
1085
+ orphanDetails: {
1086
+ deleted: any[];
1087
+ failed: any[];
1088
+ };
1089
+ };
1090
+ getDetails(): {
1091
+ failed: {
1092
+ name: string;
1093
+ hidden: boolean;
1094
+ logs: any[];
1095
+ }[];
1096
+ skipped: {
1097
+ name: string;
1098
+ hidden: boolean;
1099
+ logs: any[];
1100
+ }[];
1101
+ inserted: {
1102
+ name: string;
1103
+ hidden: boolean;
1104
+ logs: any[];
1105
+ }[];
1106
+ updated: {
1107
+ name: string;
1108
+ hidden: boolean;
1109
+ logs: any[];
1110
+ }[];
1111
+ };
1112
+ getFailedDetails(): {
1113
+ name: string;
1114
+ hidden: boolean;
1115
+ logs: any[];
1116
+ }[];
1117
+ getSkippedDetails(): {
1118
+ name: string;
1119
+ hidden: boolean;
1120
+ logs: any[];
1121
+ }[];
1122
+ getInsertedDetails(): {
1123
+ name: string;
1124
+ hidden: boolean;
1125
+ logs: any[];
1126
+ }[];
1127
+ getUpdatedDetails(): {
1128
+ name: string;
1129
+ hidden: boolean;
1130
+ logs: any[];
1131
+ }[];
1132
+ getOrphanDetails(): {
1133
+ deleted: any[];
1134
+ failed: any[];
1135
+ };
1136
+ getOrphanFailedDetails(): any[];
1137
+ getOrphanDeletedDetails(): any[];
1138
+ getSaveDetails(): SaveResultDetailItem[];
1139
+ getSaveSummary(): SaveResultSummaryItem[];
1140
+ getAllSummary(): {
1141
+ summary: {
1142
+ name: string;
1143
+ hidden: boolean;
1144
+ summary: {
1145
+ total: number;
1146
+ failed: number;
1147
+ inserted: number;
1148
+ skipped: number;
1149
+ updated: number;
1150
+ };
1151
+ }[];
1152
+ orphanSummary: any[];
1153
+ };
1154
+ getSummary(): {
1155
+ name: string;
1156
+ hidden: boolean;
1157
+ summary: {
1158
+ total: number;
1159
+ failed: number;
1160
+ inserted: number;
1161
+ skipped: number;
1162
+ updated: number;
1163
+ };
1164
+ }[];
1165
+ getOrphanSummary(): any[];
1166
+ }
1167
+
1168
+ declare interface SaveResultConfig {
1169
+ hidden?: boolean;
1170
+ returnDetail?: {
1171
+ failed: boolean;
1172
+ skipped: boolean;
1173
+ inserted: boolean;
1174
+ updated: boolean;
1175
+ orphanDeleted?: boolean;
1176
+ };
1177
+ }
1178
+
1179
+ declare interface SaveResultDetail {
1180
+ failed: DataWrapper[] | null;
1181
+ inserted: DataWrapper[] | null;
1182
+ skipped: DataWrapper[] | null;
1183
+ updated: DataWrapper[] | null;
1184
+ }
1185
+
1186
+ declare interface SaveResultDetailItem {
1187
+ name: string;
1188
+ hidden: boolean;
1189
+ orphanDetails: OrphanDetail | null;
1190
+ details: SaveResultDetail;
1191
+ }
1192
+
1193
+ declare interface SaveResultSummary {
1194
+ failed: number;
1195
+ inserted: number;
1196
+ skipped: number;
1197
+ total: number;
1198
+ updated: number;
1199
+ }
1200
+
1201
+ declare interface SaveResultSummaryItem {
1202
+ name: string;
1203
+ hidden: boolean;
1204
+ orphanSummary: OrphanSummary | null;
1205
+ summary: SaveResultSummary;
1206
+ }
1207
+
760
1208
  export declare class Service {
761
1209
  static get _classname(): string;
762
1210
  static get _superclass(): string;
@@ -792,6 +1240,13 @@ export declare class Service {
792
1240
  }>;
793
1241
  }
794
1242
 
1243
+ export declare class ServiceBulkWrite extends Service {
1244
+ bulkWriteDocs({ operations, config }: {
1245
+ operations: any;
1246
+ config: any;
1247
+ }): Promise<any>;
1248
+ }
1249
+
795
1250
  export declare function setId(id: any): any;
796
1251
 
797
1252
  export declare function shuffleArray(array: any): any[];
@@ -814,6 +1269,15 @@ export declare class Status {
814
1269
  update(update: any): this;
815
1270
  }
816
1271
 
1272
+ declare interface Status_2 {
1273
+ orphanDeleted?: number;
1274
+ existed?: number;
1275
+ failed?: number;
1276
+ inserted?: number;
1277
+ skipped?: number;
1278
+ updated?: number;
1279
+ }
1280
+
817
1281
  export declare class StatusDocument extends Status {
818
1282
  archived: any;
819
1283
  completed: any;
@@ -1032,4 +1496,13 @@ declare interface WebAppResponseOptions {
1032
1496
  success?: boolean;
1033
1497
  }
1034
1498
 
1499
+ declare interface WorksheetConfig {
1500
+ name: string;
1501
+ headerRow?: number;
1502
+ dataStartRow?: number;
1503
+ dataEndRow?: number;
1504
+ dataSources: DataSourceConfig[];
1505
+ rowIndexKey?: string;
1506
+ }
1507
+
1035
1508
  export { }