@wix/wix-data-items-common 1.0.281 → 1.0.283
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/dist/cjs/api/WixDataApi.js +159 -92
- package/dist/cjs/api/WixDataApi.js.map +1 -1
- package/dist/cjs/api/WixDataQueryBuilder.js +88 -0
- package/dist/cjs/api/WixDataQueryBuilder.js.map +1 -0
- package/dist/cjs/api/index.js +9 -1
- package/dist/cjs/api/index.js.map +1 -1
- package/dist/cjs/api/types.js.map +1 -1
- package/dist/cjs/types/data-item-types.js.map +1 -1
- package/dist/esm/api/WixDataApi.js +149 -99
- package/dist/esm/api/WixDataApi.js.map +1 -1
- package/dist/esm/api/WixDataQueryBuilder.js +74 -0
- package/dist/esm/api/WixDataQueryBuilder.js.map +1 -0
- package/dist/esm/api/index.js +1 -0
- package/dist/esm/api/index.js.map +1 -1
- package/dist/esm/types/data-item-types.js.map +1 -1
- package/dist/types/api/WixDataApi.d.ts +17 -2
- package/dist/types/api/WixDataApi.d.ts.map +1 -1
- package/dist/types/api/WixDataQueryBuilder.d.ts +79 -0
- package/dist/types/api/WixDataQueryBuilder.d.ts.map +1 -0
- package/dist/types/api/index.d.ts +2 -1
- package/dist/types/api/index.d.ts.map +1 -1
- package/dist/types/api/types.d.ts +67 -1
- package/dist/types/api/types.d.ts.map +1 -1
- package/dist/types/types/data-item-types.d.ts +5 -0
- package/dist/types/types/data-item-types.d.ts.map +1 -1
- package/package.json +4 -3
|
@@ -68,7 +68,8 @@ class WixDataApi {
|
|
|
68
68
|
dataCollectionId: collectionName,
|
|
69
69
|
dataItemId: itemId,
|
|
70
70
|
...toReadOptions(options),
|
|
71
|
-
fields: options == null ? void 0 : options.fields
|
|
71
|
+
fields: options == null ? void 0 : options.fields,
|
|
72
|
+
referencedItemOptions: options == null ? void 0 : options.referencedItemOptions
|
|
72
73
|
}).catch(recover(ItemDoesNotExistCode, {}));
|
|
73
74
|
return dataItem ? toDataItem(dataItem) : null;
|
|
74
75
|
});
|
|
@@ -239,97 +240,6 @@ class WixDataApi {
|
|
|
239
240
|
}
|
|
240
241
|
});
|
|
241
242
|
});
|
|
242
|
-
// --- query, count, distinct ---
|
|
243
|
-
(0, _defineProperty2.default)(this, "query", collectionName => {
|
|
244
|
-
const ensureValidQuery = invalidArgs => {
|
|
245
|
-
if (invalidArgs.length > 0) {
|
|
246
|
-
throw (0, _errors.wdeValidationError)(_errors.messages.queryValidations.queryInvalid(collectionName, invalidArgs));
|
|
247
|
-
}
|
|
248
|
-
};
|
|
249
|
-
return new _WixDataQueryImpl.WixDataQueryImpl({
|
|
250
|
-
collectionName,
|
|
251
|
-
onCount: async (args, params, options) => {
|
|
252
|
-
ensureValidQuery([...params.invalidArguments, ...apiValidator().collectionName(collectionName).arity('count', args, 0, 1).options(options).validateAndReturn()]);
|
|
253
|
-
return this.trace('count', {
|
|
254
|
-
collectionName
|
|
255
|
-
})(async env => {
|
|
256
|
-
const {
|
|
257
|
-
totalCount
|
|
258
|
-
} = await this.client.countDataItems({
|
|
259
|
-
...env,
|
|
260
|
-
dataCollectionId: collectionName,
|
|
261
|
-
filter: params.filterTree,
|
|
262
|
-
...toReadOptions(options)
|
|
263
|
-
});
|
|
264
|
-
return totalCount;
|
|
265
|
-
});
|
|
266
|
-
},
|
|
267
|
-
onDistinct: async (args, params, field, options) => {
|
|
268
|
-
var _toSort$find;
|
|
269
|
-
ensureValidQuery([...params.invalidArguments, ...apiValidator().collectionName(collectionName).arity('distinct', args, 1, 2).fieldName(field).options(options).validateAndReturn()]);
|
|
270
|
-
const order = (_toSort$find = toSort(params.orderBy).find(o => o.fieldName === field)) == null ? void 0 : _toSort$find.order;
|
|
271
|
-
const fetch = (cursorOrOffset, returnTotalCount) => this.trace('distinct', {
|
|
272
|
-
collectionName,
|
|
273
|
-
field
|
|
274
|
-
})(async env => {
|
|
275
|
-
const {
|
|
276
|
-
distinctValues,
|
|
277
|
-
pagingMetadata
|
|
278
|
-
} = await this.client.queryDistinctValues({
|
|
279
|
-
...env,
|
|
280
|
-
dataCollectionId: collectionName,
|
|
281
|
-
...toReadOptions(options),
|
|
282
|
-
...toPaging(params.limitNumber, cursorOrOffset),
|
|
283
|
-
...(isOffset(cursorOrOffset) ? {
|
|
284
|
-
fieldName: field,
|
|
285
|
-
filter: params.filterTree,
|
|
286
|
-
order,
|
|
287
|
-
returnTotalCount
|
|
288
|
-
} : {})
|
|
289
|
-
});
|
|
290
|
-
return [distinctValues, pagingMetadata];
|
|
291
|
-
});
|
|
292
|
-
const [items, paging] = await fetch(params.skipNumber, options == null ? void 0 : options.returnTotalCount);
|
|
293
|
-
return new _WixDataResultImpl.WixDataResultImpl(items, paging, fetch, params);
|
|
294
|
-
},
|
|
295
|
-
onFind: async (args, params, options) => {
|
|
296
|
-
ensureValidQuery([...params.invalidArguments, ...apiValidator().collectionName(collectionName).arity('find', args, 0, 1).options(options).validateAndReturn()]);
|
|
297
|
-
const fetch = (cursorOrOffset, returnTotalCount) => this.trace('query', {
|
|
298
|
-
collectionName
|
|
299
|
-
})(async env => {
|
|
300
|
-
const {
|
|
301
|
-
dataItems,
|
|
302
|
-
pagingMetadata
|
|
303
|
-
} = await this.client.queryDataItems({
|
|
304
|
-
...env,
|
|
305
|
-
dataCollectionId: collectionName,
|
|
306
|
-
query: {
|
|
307
|
-
fields: params.projectedFields,
|
|
308
|
-
...toPaging(params.limitNumber, cursorOrOffset),
|
|
309
|
-
...(isOffset(cursorOrOffset) ? {
|
|
310
|
-
filter: params.filterTree,
|
|
311
|
-
sort: toSort(params.orderBy)
|
|
312
|
-
} : {})
|
|
313
|
-
},
|
|
314
|
-
referencedItemOptions: params.included,
|
|
315
|
-
...toReadOptions(options),
|
|
316
|
-
...(isOffset(cursorOrOffset) ? {
|
|
317
|
-
returnTotalCount
|
|
318
|
-
} : {}),
|
|
319
|
-
...(params.randomSortOptions ? {
|
|
320
|
-
sortMode: apiTypes.SortMode.RANDOM,
|
|
321
|
-
randomOptions: {
|
|
322
|
-
seed: params.randomSortOptions.seed
|
|
323
|
-
}
|
|
324
|
-
} : {})
|
|
325
|
-
});
|
|
326
|
-
return [dataItems.map(toDataItem), pagingMetadata];
|
|
327
|
-
});
|
|
328
|
-
const [items, paging] = await fetch(params.skipNumber, options == null ? void 0 : options.returnTotalCount);
|
|
329
|
-
return new _WixDataResultImpl.WixDataResultImpl(items, paging, fetch, params);
|
|
330
|
-
}
|
|
331
|
-
});
|
|
332
|
-
});
|
|
333
243
|
(0, _defineProperty2.default)(this, "search", collectionName => {
|
|
334
244
|
const ensureValidQuery = invalidArgs => {
|
|
335
245
|
if (invalidArgs.length > 0) {
|
|
@@ -685,6 +595,163 @@ class WixDataApi {
|
|
|
685
595
|
}
|
|
686
596
|
get filter() {
|
|
687
597
|
return (0, _common.filterBuilder)();
|
|
598
|
+
} // --- query, count, distinct ---
|
|
599
|
+
/**
|
|
600
|
+
* Returns a query builder for the specified collection.
|
|
601
|
+
* @param collectionName - Name of the collection to query.
|
|
602
|
+
* @returns A `WixDataQuery` builder object.
|
|
603
|
+
*/
|
|
604
|
+
/**
|
|
605
|
+
* Executes a query directly without using the builder pattern.
|
|
606
|
+
* @internal
|
|
607
|
+
* @param collectionName - Name of the collection to query.
|
|
608
|
+
* @param queryRequest - Query parameters including filter, sort, paging, and fields.
|
|
609
|
+
* @param options - Additional options including referencedItemOptions.
|
|
610
|
+
* @returns Promise that resolves to the query results.
|
|
611
|
+
*/
|
|
612
|
+
query(collectionName, queryRequest, options) {
|
|
613
|
+
// Direct query execution mode
|
|
614
|
+
if (queryRequest !== undefined) {
|
|
615
|
+
return this.executeDirectQuery(collectionName, queryRequest, options);
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
// Builder mode
|
|
619
|
+
const ensureValidQuery = invalidArgs => {
|
|
620
|
+
if (invalidArgs.length > 0) {
|
|
621
|
+
throw (0, _errors.wdeValidationError)(_errors.messages.queryValidations.queryInvalid(collectionName, invalidArgs));
|
|
622
|
+
}
|
|
623
|
+
};
|
|
624
|
+
return new _WixDataQueryImpl.WixDataQueryImpl({
|
|
625
|
+
collectionName,
|
|
626
|
+
onCount: async (args, params, options) => {
|
|
627
|
+
ensureValidQuery([...params.invalidArguments, ...apiValidator().collectionName(collectionName).arity('count', args, 0, 1).options(options).validateAndReturn()]);
|
|
628
|
+
return this.trace('count', {
|
|
629
|
+
collectionName
|
|
630
|
+
})(async env => {
|
|
631
|
+
const {
|
|
632
|
+
totalCount
|
|
633
|
+
} = await this.client.countDataItems({
|
|
634
|
+
...env,
|
|
635
|
+
dataCollectionId: collectionName,
|
|
636
|
+
filter: params.filterTree,
|
|
637
|
+
...toReadOptions(options)
|
|
638
|
+
});
|
|
639
|
+
return totalCount;
|
|
640
|
+
});
|
|
641
|
+
},
|
|
642
|
+
onDistinct: async (args, params, field, options) => {
|
|
643
|
+
var _toSort$find;
|
|
644
|
+
ensureValidQuery([...params.invalidArguments, ...apiValidator().collectionName(collectionName).arity('distinct', args, 1, 2).fieldName(field).options(options).validateAndReturn()]);
|
|
645
|
+
const order = (_toSort$find = toSort(params.orderBy).find(o => o.fieldName === field)) == null ? void 0 : _toSort$find.order;
|
|
646
|
+
const fetch = (cursorOrOffset, returnTotalCount) => this.trace('distinct', {
|
|
647
|
+
collectionName,
|
|
648
|
+
field
|
|
649
|
+
})(async env => {
|
|
650
|
+
const {
|
|
651
|
+
distinctValues,
|
|
652
|
+
pagingMetadata
|
|
653
|
+
} = await this.client.queryDistinctValues({
|
|
654
|
+
...env,
|
|
655
|
+
dataCollectionId: collectionName,
|
|
656
|
+
...toReadOptions(options),
|
|
657
|
+
...toPaging(params.limitNumber, cursorOrOffset),
|
|
658
|
+
...(isOffset(cursorOrOffset) ? {
|
|
659
|
+
fieldName: field,
|
|
660
|
+
filter: params.filterTree,
|
|
661
|
+
order,
|
|
662
|
+
returnTotalCount
|
|
663
|
+
} : {})
|
|
664
|
+
});
|
|
665
|
+
return [distinctValues, pagingMetadata];
|
|
666
|
+
});
|
|
667
|
+
const [items, paging] = await fetch(params.skipNumber, options == null ? void 0 : options.returnTotalCount);
|
|
668
|
+
return new _WixDataResultImpl.WixDataResultImpl(items, paging, fetch, params);
|
|
669
|
+
},
|
|
670
|
+
onFind: async (args, params, options) => {
|
|
671
|
+
ensureValidQuery([...params.invalidArguments, ...apiValidator().collectionName(collectionName).arity('find', args, 0, 1).options(options).validateAndReturn()]);
|
|
672
|
+
const fetch = (cursorOrOffset, returnTotalCount) => this.trace('query', {
|
|
673
|
+
collectionName
|
|
674
|
+
})(async env => {
|
|
675
|
+
const {
|
|
676
|
+
dataItems,
|
|
677
|
+
pagingMetadata
|
|
678
|
+
} = await this.client.queryDataItems({
|
|
679
|
+
...env,
|
|
680
|
+
dataCollectionId: collectionName,
|
|
681
|
+
query: {
|
|
682
|
+
fields: params.projectedFields,
|
|
683
|
+
...toPaging(params.limitNumber, cursorOrOffset),
|
|
684
|
+
...(isOffset(cursorOrOffset) ? {
|
|
685
|
+
filter: params.filterTree,
|
|
686
|
+
sort: toSort(params.orderBy)
|
|
687
|
+
} : {})
|
|
688
|
+
},
|
|
689
|
+
referencedItemOptions: params.included,
|
|
690
|
+
...toReadOptions(options),
|
|
691
|
+
...(isOffset(cursorOrOffset) ? {
|
|
692
|
+
returnTotalCount
|
|
693
|
+
} : {}),
|
|
694
|
+
...(params.randomSortOptions ? {
|
|
695
|
+
sortMode: apiTypes.SortMode.RANDOM,
|
|
696
|
+
randomOptions: {
|
|
697
|
+
seed: params.randomSortOptions.seed
|
|
698
|
+
}
|
|
699
|
+
} : {})
|
|
700
|
+
});
|
|
701
|
+
return [dataItems.map(toDataItem), pagingMetadata];
|
|
702
|
+
});
|
|
703
|
+
const [items, paging] = await fetch(params.skipNumber, options == null ? void 0 : options.returnTotalCount);
|
|
704
|
+
return new _WixDataResultImpl.WixDataResultImpl(items, paging, fetch, params);
|
|
705
|
+
}
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
async executeDirectQuery(collectionName, request, options) {
|
|
709
|
+
var _request$paging, _request$cursorPaging, _request$paging2, _request$cursorPaging2, _options$referencedIt;
|
|
710
|
+
const validationErrors = apiValidator().collectionName(collectionName).options(options).validateAndReturn();
|
|
711
|
+
if (validationErrors.length > 0) {
|
|
712
|
+
throw (0, _errors.wdeValidationError)(_errors.messages.queryValidations.queryInvalid(collectionName, validationErrors));
|
|
713
|
+
}
|
|
714
|
+
const limit = ((_request$paging = request.paging) == null ? void 0 : _request$paging.limit) ?? ((_request$cursorPaging = request.cursorPaging) == null ? void 0 : _request$cursorPaging.limit);
|
|
715
|
+
const offset = ((_request$paging2 = request.paging) == null ? void 0 : _request$paging2.offset) ?? 0;
|
|
716
|
+
const cursor = (_request$cursorPaging2 = request.cursorPaging) == null ? void 0 : _request$cursorPaging2.cursor;
|
|
717
|
+
const sort = (request.sort ?? []).map(s => ({
|
|
718
|
+
[s.fieldName]: s.order === 'DESC' ? 'desc' : 'asc'
|
|
719
|
+
}));
|
|
720
|
+
const referencedItemOptions = options == null || (_options$referencedIt = options.referencedItemOptions) == null ? void 0 : _options$referencedIt.map(inc => ({
|
|
721
|
+
fieldName: inc.fieldName,
|
|
722
|
+
limit: inc.limit
|
|
723
|
+
}));
|
|
724
|
+
const fetch = (cursorOrOffset, returnTotalCount) => this.trace('query', {
|
|
725
|
+
collectionName
|
|
726
|
+
})(async env => {
|
|
727
|
+
const {
|
|
728
|
+
dataItems,
|
|
729
|
+
pagingMetadata
|
|
730
|
+
} = await this.client.queryDataItems({
|
|
731
|
+
...env,
|
|
732
|
+
dataCollectionId: collectionName,
|
|
733
|
+
query: {
|
|
734
|
+
fields: request.fields,
|
|
735
|
+
...toPaging(limit, cursorOrOffset),
|
|
736
|
+
...(isOffset(cursorOrOffset) ? {
|
|
737
|
+
filter: request.filter,
|
|
738
|
+
sort: toSort(sort)
|
|
739
|
+
} : {})
|
|
740
|
+
},
|
|
741
|
+
referencedItemOptions,
|
|
742
|
+
...toReadOptions(options),
|
|
743
|
+
...(isOffset(cursorOrOffset) ? {
|
|
744
|
+
returnTotalCount
|
|
745
|
+
} : {})
|
|
746
|
+
});
|
|
747
|
+
return [dataItems.map(toDataItem), pagingMetadata];
|
|
748
|
+
});
|
|
749
|
+
const [items, paging] = await fetch(cursor ?? offset, options == null ? void 0 : options.returnTotalCount);
|
|
750
|
+
return new _WixDataResultImpl.WixDataResultImpl(items, paging, fetch, {
|
|
751
|
+
collectionName,
|
|
752
|
+
limitNumber: limit,
|
|
753
|
+
skipNumber: offset
|
|
754
|
+
});
|
|
688
755
|
}
|
|
689
756
|
async runBulkSave(env, collectionName, items, options, overrideExisting = true) {
|
|
690
757
|
const request = {
|