@wix/wix-data-items-common 1.0.308 → 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.
@@ -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':
@@ -322,37 +217,6 @@ export class WixDataApi {
322
217
  return this.client.aggregatePipelineDataItems(request);
323
218
  });
324
219
  });
325
- this.distinct = withArgs(async (args, collectionName, fieldName, options) => {
326
- await apiValidator()
327
- .arity('distinct', args, 2, 3)
328
- .collectionName(collectionName)
329
- .fieldName(fieldName)
330
- .options(options)
331
- .validateAndReject();
332
- const limit = options?.paging?.limit ?? options?.cursorPaging?.limit;
333
- const offset = options?.paging?.offset ?? 0;
334
- const cursor = options?.cursorPaging?.cursor;
335
- return this.trace('distinct', { collectionName, field: fieldName })(async (env) => {
336
- const { distinctValues, pagingMetadata } = await this.client.queryDistinctValues({
337
- ...env,
338
- dataCollectionId: collectionName,
339
- ...toReadOptions(options),
340
- ...toPaging(limit, cursor ?? offset),
341
- ...(cursor === undefined
342
- ? {
343
- fieldName,
344
- filter: options?.filter,
345
- order: options?.order,
346
- returnTotalCount: options?.returnTotalCount,
347
- }
348
- : {}),
349
- });
350
- return {
351
- values: distinctValues,
352
- pagingMetadata: pagingMetadata,
353
- };
354
- });
355
- });
356
220
  /** @internal */
357
221
  this.count = withArgs(async (args, collectionName, options) => {
358
222
  await apiValidator()
@@ -390,45 +254,6 @@ export class WixDataApi {
390
254
  return toBulkResult(itemIds, results, ['WDE0073']);
391
255
  });
392
256
  });
393
- this.bulkInsert = withArgs(async (args, collectionName, items, options) => {
394
- await apiValidator()
395
- .arity('bulkInsert', args, 2, 3)
396
- .items(items, collectionName)
397
- .bulkInsertOptions(options)
398
- .collectionName(collectionName)
399
- .validateAndReject();
400
- return this.trace('bulkInsert', { collectionName, options })(async (appId) => this.runBulkSave(appId, collectionName, items, options, options?.overrideExisting ?? false));
401
- });
402
- this.bulkSave = withArgs(async (args, collectionName, items, options) => {
403
- await apiValidator()
404
- .arity('bulkSave', args, 2, 3)
405
- .bulkInsertOptions(options)
406
- .collectionName(collectionName)
407
- .items(items, collectionName)
408
- .validateAndReject();
409
- return this.trace('bulkSave', { collectionName })(async (env) => this.runBulkSave(env, collectionName, items, options));
410
- });
411
- this.bulkUpdate = withArgs(async (args, collectionName, items, options) => {
412
- await apiValidator()
413
- .arity('bulkUpdate', args, 2, 3)
414
- .bulkUpdateOptions(options)
415
- .collectionName(collectionName)
416
- .items(items, collectionName)
417
- .validateAndReject();
418
- return this.trace('bulkUpdate', { collectionName })(async (env) => {
419
- const { results } = await this.client.bulkUpdateDataItems({
420
- ...env,
421
- dataCollectionId: collectionName,
422
- dataItems: items.map((data) => ({
423
- id: data._id,
424
- data,
425
- })),
426
- ...toBulkUpdateOptions(options),
427
- });
428
- // Non-existing items are skipped and not reported as errors.
429
- return toBulkResult(items, results, ['WDE0073']);
430
- });
431
- });
432
257
  // --- references ---
433
258
  this.fetchReferenced = withArgs(async (args, collectionName, cursor, limit, options) => {
434
259
  await apiValidator()
@@ -496,39 +321,6 @@ export class WixDataApi {
496
321
  skipNumber: options?.skip ?? 0,
497
322
  });
498
323
  });
499
- this.queryReferencedItems = withArgs(async (args, collectionName, referringItem, referringItemFieldName, options) => {
500
- await apiValidator()
501
- .arity('queryReferencedItems', args, 3, 4)
502
- .collectionName(collectionName)
503
- .referenceParameters(asArray(referringItem))
504
- .isNonEmptyString(referringItemFieldName, 'referringItemFieldName')
505
- .options(options)
506
- .validateAndReject();
507
- const referringItemIds = asArray(referringItem).map(itemId);
508
- return this.trace('queryReferencedItems', {
509
- collectionName,
510
- referringItemIds,
511
- options,
512
- referringItemFieldName,
513
- })(async (env) => {
514
- const { results, pagingMetadata } = await this.client.queryReferencedDataItems({
515
- ...env,
516
- ...toReadOptions(options),
517
- dataCollectionId: collectionName,
518
- referringItemIds,
519
- referringItemFieldName,
520
- order: options?.order,
521
- paging: options?.paging,
522
- cursorPaging: options?.cursorPaging,
523
- fields: options?.fields,
524
- returnTotalCount: options?.returnTotalCount,
525
- });
526
- return {
527
- results: toReferencedResults(results ?? []),
528
- pagingMetadata: pagingMetadata,
529
- };
530
- });
531
- });
532
324
  this.insertReference = withArgs(async (args, collectionName, refsOrAttr, leftOrOpts, right, options) => {
533
325
  let refs;
534
326
  let opts;
@@ -643,6 +435,117 @@ export class WixDataApi {
643
435
  get filter() {
644
436
  return filterBuilder();
645
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
+ }
646
549
  patch(collectionName, itemId, fieldModifications, options) {
647
550
  if (arguments.length >= 3) {
648
551
  return this.executeDirectPatch(collectionName, itemId, fieldModifications, options);
@@ -679,7 +582,9 @@ export class WixDataApi {
679
582
  },
680
583
  ...toPatchOptions(options),
681
584
  });
682
- return result.dataItem ? toDataItem(result.dataItem) : null;
585
+ return result.dataItem
586
+ ? toDataItem(result.dataItem)
587
+ : null;
683
588
  });
684
589
  }
685
590
  bulkPatch(collectionName, itemIds, fieldModifications, options) {
@@ -1017,6 +922,37 @@ export class WixDataApi {
1017
922
  pagingMetadata: pagingMetadata,
1018
923
  };
1019
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
+ }
1020
956
  async runBulkSave(env, collectionName, items, options, overrideExisting = true) {
1021
957
  const request = {
1022
958
  ...env,
@@ -1034,6 +970,80 @@ export class WixDataApi {
1034
970
  const ignoreCodes = overrideExisting ? [] : ['WDE0074'];
1035
971
  return toBulkResult(items, results, ignoreCodes);
1036
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
+ }
1037
1047
  trace(action, opts) {
1038
1048
  return async (fn) => {
1039
1049
  const gridAppId = await get(this.gridAppId);