@wix/wix-data-items-common 1.0.307 → 1.0.309

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.
@@ -6,7 +6,7 @@ import { WixDataQueryImpl } from './impl/WixDataQueryImpl';
6
6
  import { WixDataResultImpl } from './impl/WixDataResultImpl';
7
7
  import { WixDataAggregateImpl } from './impl/WixDataAggregateImpl';
8
8
  import { ApiClient } from './ApiClient';
9
- import { WixDataBulkPatchImpl, WixDataPatchByFilterImpl, WixDataPatchImpl, } from './impl/WixDataPatchImpl';
9
+ import { WixDataBulkPatchImpl, WixDataPatchImpl } from './impl/WixDataPatchImpl';
10
10
  import { WixDataSearchImpl } from './impl/WixDataSearchImpl';
11
11
  function createFilter(filter) {
12
12
  if (filter == null) {
@@ -68,111 +68,6 @@ export class WixDataApi {
68
68
  });
69
69
  });
70
70
  });
71
- // --- single-item methods ---
72
- this.get = withArgs(async (args, collectionName, itemId, options) => {
73
- await apiValidator()
74
- .arity('get', args, 2, 3)
75
- .collectionName(collectionName)
76
- .itemId(itemId)
77
- .options(options)
78
- .validateAndReject();
79
- return this.trace('get', { collectionName, itemId })(async (env) => {
80
- const { dataItem } = await this.client
81
- .getDataItem({
82
- ...env,
83
- dataCollectionId: collectionName,
84
- dataItemId: itemId,
85
- ...toReadOptions(options),
86
- fields: options?.fields,
87
- includeReferences: options?.includeReferences,
88
- ...(options?.includeFieldGroups?.length
89
- ? { includeFieldGroups: options.includeFieldGroups }
90
- : {}),
91
- })
92
- .catch(recover(ItemDoesNotExistCode, {}));
93
- return dataItem ? toDataItem(dataItem) : null;
94
- });
95
- });
96
- this.insert = withArgs(async (args, collectionName, item, options) => {
97
- await apiValidator()
98
- .arity('insert', args, 2, 3)
99
- .collectionName(collectionName)
100
- .item(item, collectionName, false)
101
- .options(options)
102
- .validateAndReject();
103
- warnAboutBrokenFields(item);
104
- return this.trace('insert', { collectionName })(async (env) => {
105
- const { dataItem } = await this.client.insertDataItem({
106
- ...env,
107
- dataCollectionId: collectionName,
108
- dataItem: {
109
- id: item._id,
110
- data: item,
111
- },
112
- ...toInsertOptions(options),
113
- });
114
- return toDataItem(dataItem);
115
- });
116
- });
117
- this.save = withArgs(async (args, collectionName, item, options) => {
118
- await apiValidator()
119
- .arity('save', args, 2, 3)
120
- .collectionName(collectionName)
121
- .item(item, collectionName, false)
122
- .options(options)
123
- .validateAndReject();
124
- return this.trace('save', { collectionName })(async (env) => {
125
- const { dataItem } = await this.client.saveDataItem({
126
- ...env,
127
- dataCollectionId: collectionName,
128
- dataItem: {
129
- id: item._id,
130
- data: item,
131
- },
132
- ...toSaveOptions(options),
133
- });
134
- return toDataItem(dataItem);
135
- });
136
- });
137
- this.update = withArgs(async (args, collectionName, item, options) => {
138
- await apiValidator()
139
- .arity('update', args, 2, 3)
140
- .collectionName(collectionName)
141
- .item(item, collectionName, false)
142
- .options(options)
143
- .validateAndReject();
144
- return this.trace('update', { collectionName })(async (env) => {
145
- const { dataItem } = await this.client.updateDataItem({
146
- ...env,
147
- dataCollectionId: collectionName,
148
- dataItem: {
149
- id: item._id,
150
- data: item,
151
- },
152
- ...toUpdateOptions(options),
153
- });
154
- return toDataItem(dataItem);
155
- });
156
- });
157
- this.remove = withArgs(async (args, collectionName, itemId, options) => {
158
- await apiValidator()
159
- .arity('remove', args, 2, 3)
160
- .collectionName(collectionName)
161
- .itemId(itemId)
162
- .options(options)
163
- .validateAndReject();
164
- return this.trace('remove', { collectionName, itemId })(async (env) => {
165
- const { dataItem } = await this.client
166
- .removeDataItem({
167
- ...env,
168
- dataCollectionId: collectionName,
169
- dataItemId: itemId,
170
- ...toRemoveOptions(options),
171
- })
172
- .catch(recover(ItemDoesNotExistCode, {}));
173
- return dataItem ? toDataItem(dataItem) : null;
174
- });
175
- });
176
71
  this.toFieldModificationApi = (fieldModification) => {
177
72
  switch (fieldModification.action) {
178
73
  case 'SET_FIELD':
@@ -214,26 +109,25 @@ export class WixDataApi {
214
109
  };
215
110
  }
216
111
  };
217
- this.asyncPatchByFilter = (collectionName, filter) => {
112
+ this.asyncPatchByFilter = withArgs(async (args, collectionName, filter, fieldModifications, options) => {
113
+ await apiValidator()
114
+ .arity('asyncPatchByFilter', args, 3, 4)
115
+ .collectionName(collectionName)
116
+ .options(options)
117
+ .validateAndReject();
218
118
  const apiFilter = createExplicitFilter(filter);
219
- return new WixDataPatchByFilterImpl({
220
- collectionName,
221
- filter: apiFilter,
222
- onRun: async (_args, patchParams, options) => {
223
- const fieldModifications = patchParams.fieldModifications.map(this.toFieldModificationApi);
224
- return this.trace('asyncPatchByFilter', { collectionName })(async (env) => {
225
- const { jobId } = await this.client.asyncPatchDataItemsByFilter({
226
- ...env,
227
- dataCollectionId: patchParams.collectionName,
228
- filter: patchParams.filter,
229
- fieldModifications,
230
- ...toAsyncByFilterOptions(options),
231
- });
232
- return { jobId: jobId };
233
- });
234
- },
119
+ const apiFieldModifications = fieldModifications.map(this.toFieldModificationApi);
120
+ return this.trace('asyncPatchByFilter', { collectionName })(async (env) => {
121
+ const { jobId } = await this.client.asyncPatchDataItemsByFilter({
122
+ ...env,
123
+ dataCollectionId: collectionName,
124
+ filter: apiFilter,
125
+ fieldModifications: apiFieldModifications,
126
+ ...toAsyncPatchByFilterOptions(options),
127
+ });
128
+ return { jobId: jobId };
235
129
  });
236
- };
130
+ });
237
131
  this.asyncRemoveByFilter = withArgs(async (args, collectionName, filter, options) => {
238
132
  await apiValidator()
239
133
  .arity('asyncRemoveByFilter', args, 2, 3)
@@ -323,37 +217,6 @@ export class WixDataApi {
323
217
  return this.client.aggregatePipelineDataItems(request);
324
218
  });
325
219
  });
326
- this.distinct = withArgs(async (args, collectionName, fieldName, options) => {
327
- await apiValidator()
328
- .arity('distinct', args, 2, 3)
329
- .collectionName(collectionName)
330
- .fieldName(fieldName)
331
- .options(options)
332
- .validateAndReject();
333
- const limit = options?.paging?.limit ?? options?.cursorPaging?.limit;
334
- const offset = options?.paging?.offset ?? 0;
335
- const cursor = options?.cursorPaging?.cursor;
336
- return this.trace('distinct', { collectionName, field: fieldName })(async (env) => {
337
- const { distinctValues, pagingMetadata } = await this.client.queryDistinctValues({
338
- ...env,
339
- dataCollectionId: collectionName,
340
- ...toReadOptions(options),
341
- ...toPaging(limit, cursor ?? offset),
342
- ...(cursor === undefined
343
- ? {
344
- fieldName,
345
- filter: options?.filter,
346
- order: options?.order,
347
- returnTotalCount: options?.returnTotalCount,
348
- }
349
- : {}),
350
- });
351
- return {
352
- values: distinctValues,
353
- pagingMetadata: pagingMetadata,
354
- };
355
- });
356
- });
357
220
  /** @internal */
358
221
  this.count = withArgs(async (args, collectionName, options) => {
359
222
  await apiValidator()
@@ -391,45 +254,6 @@ export class WixDataApi {
391
254
  return toBulkResult(itemIds, results, ['WDE0073']);
392
255
  });
393
256
  });
394
- this.bulkInsert = withArgs(async (args, collectionName, items, options) => {
395
- await apiValidator()
396
- .arity('bulkInsert', args, 2, 3)
397
- .items(items, collectionName)
398
- .bulkInsertOptions(options)
399
- .collectionName(collectionName)
400
- .validateAndReject();
401
- return this.trace('bulkInsert', { collectionName, options })(async (appId) => this.runBulkSave(appId, collectionName, items, options, options?.overrideExisting ?? false));
402
- });
403
- this.bulkSave = withArgs(async (args, collectionName, items, options) => {
404
- await apiValidator()
405
- .arity('bulkSave', args, 2, 3)
406
- .bulkInsertOptions(options)
407
- .collectionName(collectionName)
408
- .items(items, collectionName)
409
- .validateAndReject();
410
- return this.trace('bulkSave', { collectionName })(async (env) => this.runBulkSave(env, collectionName, items, options));
411
- });
412
- this.bulkUpdate = withArgs(async (args, collectionName, items, options) => {
413
- await apiValidator()
414
- .arity('bulkUpdate', args, 2, 3)
415
- .bulkUpdateOptions(options)
416
- .collectionName(collectionName)
417
- .items(items, collectionName)
418
- .validateAndReject();
419
- return this.trace('bulkUpdate', { collectionName })(async (env) => {
420
- const { results } = await this.client.bulkUpdateDataItems({
421
- ...env,
422
- dataCollectionId: collectionName,
423
- dataItems: items.map((data) => ({
424
- id: data._id,
425
- data,
426
- })),
427
- ...toBulkUpdateOptions(options),
428
- });
429
- // Non-existing items are skipped and not reported as errors.
430
- return toBulkResult(items, results, ['WDE0073']);
431
- });
432
- });
433
257
  // --- references ---
434
258
  this.fetchReferenced = withArgs(async (args, collectionName, cursor, limit, options) => {
435
259
  await apiValidator()
@@ -497,39 +321,6 @@ export class WixDataApi {
497
321
  skipNumber: options?.skip ?? 0,
498
322
  });
499
323
  });
500
- this.queryReferencedItems = withArgs(async (args, collectionName, referringItem, referringItemFieldName, options) => {
501
- await apiValidator()
502
- .arity('queryReferencedItems', args, 3, 4)
503
- .collectionName(collectionName)
504
- .referenceParameters(asArray(referringItem))
505
- .isNonEmptyString(referringItemFieldName, 'referringItemFieldName')
506
- .options(options)
507
- .validateAndReject();
508
- const referringItemIds = asArray(referringItem).map(itemId);
509
- return this.trace('queryReferencedItems', {
510
- collectionName,
511
- referringItemIds,
512
- options,
513
- referringItemFieldName,
514
- })(async (env) => {
515
- const { results, pagingMetadata } = await this.client.queryReferencedDataItems({
516
- ...env,
517
- ...toReadOptions(options),
518
- dataCollectionId: collectionName,
519
- referringItemIds,
520
- referringItemFieldName,
521
- order: options?.order,
522
- paging: options?.paging,
523
- cursorPaging: options?.cursorPaging,
524
- fields: options?.fields,
525
- returnTotalCount: options?.returnTotalCount,
526
- });
527
- return {
528
- results: toReferencedResults(results ?? []),
529
- pagingMetadata: pagingMetadata,
530
- };
531
- });
532
- });
533
324
  this.insertReference = withArgs(async (args, collectionName, refsOrAttr, leftOrOpts, right, options) => {
534
325
  let refs;
535
326
  let opts;
@@ -644,6 +435,117 @@ export class WixDataApi {
644
435
  get filter() {
645
436
  return filterBuilder();
646
437
  }
438
+ // --- single-item methods ---
439
+ async get(collectionName, itemId, options) {
440
+ await apiValidator()
441
+ .arity('get', arguments, 2, 3)
442
+ .collectionName(collectionName)
443
+ .itemId(itemId)
444
+ .options(options)
445
+ .validateAndReject();
446
+ return this.trace('get', { collectionName, itemId })(async (env) => {
447
+ const { dataItem } = await this.client
448
+ .getDataItem({
449
+ ...env,
450
+ dataCollectionId: collectionName,
451
+ dataItemId: itemId,
452
+ ...toReadOptions(options),
453
+ fields: options?.fields,
454
+ includeReferences: options?.includeReferences,
455
+ ...(options?.includeFieldGroups?.length
456
+ ? { includeFieldGroups: options.includeFieldGroups }
457
+ : {}),
458
+ })
459
+ .catch(recover(ItemDoesNotExistCode, {}));
460
+ return dataItem
461
+ ? toDataItem(dataItem)
462
+ : null;
463
+ });
464
+ }
465
+ async insert(collectionName, item, options) {
466
+ const itemAsWixData = item;
467
+ await apiValidator()
468
+ .arity('insert', arguments, 2, 3)
469
+ .collectionName(collectionName)
470
+ .item(itemAsWixData, collectionName, false)
471
+ .options(options)
472
+ .validateAndReject();
473
+ warnAboutBrokenFields(itemAsWixData);
474
+ return this.trace('insert', { collectionName })(async (env) => {
475
+ const { dataItem } = await this.client.insertDataItem({
476
+ ...env,
477
+ dataCollectionId: collectionName,
478
+ dataItem: {
479
+ id: itemAsWixData._id,
480
+ data: itemAsWixData,
481
+ },
482
+ ...toInsertOptions(options),
483
+ });
484
+ return toDataItem(dataItem);
485
+ });
486
+ }
487
+ async save(collectionName, item, options) {
488
+ const itemAsWixData = item;
489
+ await apiValidator()
490
+ .arity('save', arguments, 2, 3)
491
+ .collectionName(collectionName)
492
+ .item(itemAsWixData, collectionName, false)
493
+ .options(options)
494
+ .validateAndReject();
495
+ return this.trace('save', { collectionName })(async (env) => {
496
+ const { dataItem } = await this.client.saveDataItem({
497
+ ...env,
498
+ dataCollectionId: collectionName,
499
+ dataItem: {
500
+ id: itemAsWixData._id,
501
+ data: itemAsWixData,
502
+ },
503
+ ...toSaveOptions(options),
504
+ });
505
+ return toDataItem(dataItem);
506
+ });
507
+ }
508
+ async update(collectionName, item, options) {
509
+ await apiValidator()
510
+ .arity('update', arguments, 2, 3)
511
+ .collectionName(collectionName)
512
+ .item(item, collectionName, false)
513
+ .options(options)
514
+ .validateAndReject();
515
+ return this.trace('update', { collectionName })(async (env) => {
516
+ const { dataItem } = await this.client.updateDataItem({
517
+ ...env,
518
+ dataCollectionId: collectionName,
519
+ dataItem: {
520
+ id: item._id,
521
+ data: item,
522
+ },
523
+ ...toUpdateOptions(options),
524
+ });
525
+ return toDataItem(dataItem);
526
+ });
527
+ }
528
+ async remove(collectionName, itemId, options) {
529
+ await apiValidator()
530
+ .arity('remove', arguments, 2, 3)
531
+ .collectionName(collectionName)
532
+ .itemId(itemId)
533
+ .options(options)
534
+ .validateAndReject();
535
+ return this.trace('remove', { collectionName, itemId })(async (env) => {
536
+ const { dataItem } = await this.client
537
+ .removeDataItem({
538
+ ...env,
539
+ dataCollectionId: collectionName,
540
+ dataItemId: itemId,
541
+ ...toRemoveOptions(options),
542
+ })
543
+ .catch(recover(ItemDoesNotExistCode, {}));
544
+ return dataItem
545
+ ? toDataItem(dataItem)
546
+ : null;
547
+ });
548
+ }
647
549
  patch(collectionName, itemId, fieldModifications, options) {
648
550
  if (arguments.length >= 3) {
649
551
  return this.executeDirectPatch(collectionName, itemId, fieldModifications, options);
@@ -680,7 +582,9 @@ export class WixDataApi {
680
582
  },
681
583
  ...toPatchOptions(options),
682
584
  });
683
- return result.dataItem ? toDataItem(result.dataItem) : null;
585
+ return result.dataItem
586
+ ? toDataItem(result.dataItem)
587
+ : null;
684
588
  });
685
589
  }
686
590
  bulkPatch(collectionName, itemIds, fieldModifications, options) {
@@ -1018,6 +922,37 @@ export class WixDataApi {
1018
922
  pagingMetadata: pagingMetadata,
1019
923
  };
1020
924
  }
925
+ async distinct(collectionName, fieldName, options) {
926
+ await apiValidator()
927
+ .arity('distinct', arguments, 2, 3)
928
+ .collectionName(collectionName)
929
+ .fieldName(fieldName)
930
+ .options(options)
931
+ .validateAndReject();
932
+ const limit = options?.paging?.limit ?? options?.cursorPaging?.limit;
933
+ const offset = options?.paging?.offset ?? 0;
934
+ const cursor = options?.cursorPaging?.cursor;
935
+ return this.trace('distinct', { collectionName, field: fieldName })(async (env) => {
936
+ const { distinctValues, pagingMetadata } = await this.client.queryDistinctValues({
937
+ ...env,
938
+ dataCollectionId: collectionName,
939
+ ...toReadOptions(options),
940
+ ...toPaging(limit, cursor ?? offset),
941
+ ...(cursor === undefined
942
+ ? {
943
+ fieldName,
944
+ filter: options?.filter,
945
+ order: options?.order,
946
+ returnTotalCount: options?.returnTotalCount,
947
+ }
948
+ : {}),
949
+ });
950
+ return {
951
+ values: distinctValues,
952
+ pagingMetadata: pagingMetadata,
953
+ };
954
+ });
955
+ }
1021
956
  async runBulkSave(env, collectionName, items, options, overrideExisting = true) {
1022
957
  const request = {
1023
958
  ...env,
@@ -1035,6 +970,80 @@ export class WixDataApi {
1035
970
  const ignoreCodes = overrideExisting ? [] : ['WDE0074'];
1036
971
  return toBulkResult(items, results, ignoreCodes);
1037
972
  }
973
+ async bulkInsert(collectionName, items, options) {
974
+ const itemsAsWixData = items;
975
+ await apiValidator()
976
+ .arity('bulkInsert', arguments, 2, 3)
977
+ .items(itemsAsWixData, collectionName)
978
+ .bulkInsertOptions(options)
979
+ .collectionName(collectionName)
980
+ .validateAndReject();
981
+ return this.trace('bulkInsert', { collectionName, options })(async (appId) => this.runBulkSave(appId, collectionName, itemsAsWixData, options, options?.overrideExisting ?? false));
982
+ }
983
+ async bulkSave(collectionName, items, options) {
984
+ const itemsAsWixData = items;
985
+ await apiValidator()
986
+ .arity('bulkSave', arguments, 2, 3)
987
+ .bulkInsertOptions(options)
988
+ .collectionName(collectionName)
989
+ .items(itemsAsWixData, collectionName)
990
+ .validateAndReject();
991
+ return this.trace('bulkSave', { collectionName })(async (env) => this.runBulkSave(env, collectionName, itemsAsWixData, options));
992
+ }
993
+ async bulkUpdate(collectionName, items, options) {
994
+ await apiValidator()
995
+ .arity('bulkUpdate', arguments, 2, 3)
996
+ .bulkUpdateOptions(options)
997
+ .collectionName(collectionName)
998
+ .items(items, collectionName)
999
+ .validateAndReject();
1000
+ return this.trace('bulkUpdate', { collectionName })(async (env) => {
1001
+ const { results } = await this.client.bulkUpdateDataItems({
1002
+ ...env,
1003
+ dataCollectionId: collectionName,
1004
+ dataItems: items.map((data) => ({
1005
+ id: data._id,
1006
+ data,
1007
+ })),
1008
+ ...toBulkUpdateOptions(options),
1009
+ });
1010
+ // Non-existing items are skipped and not reported as errors.
1011
+ return toBulkResult(items, results, ['WDE0073']);
1012
+ });
1013
+ }
1014
+ async queryReferencedItems(collectionName, referringItem, referringItemFieldName, options) {
1015
+ await apiValidator()
1016
+ .arity('queryReferencedItems', arguments, 3, 4)
1017
+ .collectionName(collectionName)
1018
+ .referenceParameters(asArray(referringItem))
1019
+ .isNonEmptyString(referringItemFieldName, 'referringItemFieldName')
1020
+ .options(options)
1021
+ .validateAndReject();
1022
+ const referringItemIds = asArray(referringItem).map(itemId);
1023
+ return this.trace('queryReferencedItems', {
1024
+ collectionName,
1025
+ referringItemIds,
1026
+ options,
1027
+ referringItemFieldName,
1028
+ })(async (env) => {
1029
+ const { results, pagingMetadata } = await this.client.queryReferencedDataItems({
1030
+ ...env,
1031
+ ...toReadOptions(options),
1032
+ dataCollectionId: collectionName,
1033
+ referringItemIds,
1034
+ referringItemFieldName,
1035
+ order: options?.order,
1036
+ paging: options?.paging,
1037
+ cursorPaging: options?.cursorPaging,
1038
+ fields: options?.fields,
1039
+ returnTotalCount: options?.returnTotalCount,
1040
+ });
1041
+ return {
1042
+ results: toReferencedResults(results ?? []),
1043
+ pagingMetadata: pagingMetadata,
1044
+ };
1045
+ });
1046
+ }
1038
1047
  trace(action, opts) {
1039
1048
  return async (fn) => {
1040
1049
  const gridAppId = await get(this.gridAppId);
@@ -1068,10 +1077,10 @@ function toBulkPatchOptions(options) {
1068
1077
  ...conditionOptions(options?.condition),
1069
1078
  };
1070
1079
  }
1071
- function toAsyncByFilterOptions(options) {
1080
+ function toAsyncRemoveByFilterOptions(options) {
1072
1081
  return toAsyncOptions(options);
1073
1082
  }
1074
- function toAsyncRemoveByFilterOptions(options) {
1083
+ function toAsyncPatchByFilterOptions(options) {
1075
1084
  return toAsyncOptions(options);
1076
1085
  }
1077
1086
  function toAsyncOptions(options) {