@feedmepos/zod-repo 1.0.13 → 1.0.14

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.
@@ -529,19 +529,19 @@ var MongodbRepositoryForType = exports.MongodbRepositoryForType = /*#__PURE__*/f
529
529
  key: "findAndPopulate",
530
530
  value: (function () {
531
531
  var _findAndPopulate = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(filter, opts, session) {
532
- var pipeline, lookUps, result;
532
+ var pipeline, lookUps, hasGroupStage, result;
533
533
  return _regenerator().w(function (_context4) {
534
534
  while (1) switch (_context4.n) {
535
535
  case 0:
536
536
  pipeline = [];
537
+ if (filter) pipeline.push({
538
+ $match: opts !== null && opts !== void 0 && opts.ignoreConvertFields ? filter : this.convertRefinedFields(filter)
539
+ });
537
540
  if (opts !== null && opts !== void 0 && opts.sort) {
538
541
  pipeline.push({
539
542
  $sort: opts.sort
540
543
  });
541
544
  }
542
- if (filter) pipeline.push({
543
- $match: opts !== null && opts !== void 0 && opts.ignoreConvertFields ? filter : this.convertRefinedFields(filter)
544
- });
545
545
  if (opts !== null && opts !== void 0 && opts.skip && (opts === null || opts === void 0 ? void 0 : opts.skip) > 0) {
546
546
  pipeline.push({
547
547
  $skip: opts === null || opts === void 0 ? void 0 : opts.skip
@@ -555,14 +555,22 @@ var MongodbRepositoryForType = exports.MongodbRepositoryForType = /*#__PURE__*/f
555
555
  if (opts !== null && opts !== void 0 && opts.fields) {
556
556
  lookUps = generateLookup(this.zodSchema, this.baseShape, (opts === null || opts === void 0 ? void 0 : opts.fields) || []);
557
557
  if (lookUps.length) {
558
+ // Determine if any of the generated lookup stages introduce a $group,
559
+ // which would invalidate the earlier sort order.
560
+ hasGroupStage = lookUps.some(function (stage) {
561
+ return stage && _typeof(stage) === 'object' && '$group' in stage;
562
+ });
558
563
  pipeline.push.apply(pipeline, _toConsumableArray(lookUps));
564
+
565
+ // Re-apply sort after lookups when a $group was introduced, so that
566
+ // the final documents are deterministically ordered according to opts.sort.
567
+ if (opts.sort && hasGroupStage) {
568
+ pipeline.push({
569
+ $sort: opts.sort
570
+ });
571
+ }
559
572
  }
560
573
  }
561
- if (opts !== null && opts !== void 0 && opts.sort) {
562
- pipeline.push({
563
- $sort: opts.sort
564
- });
565
- }
566
574
  _context4.n = 1;
567
575
  return this.aggregate(pipeline, {
568
576
  session: session
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@feedmepos/zod-repo",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/feedmepos/core.git"