@payloadcms/db-mongodb 3.56.0-canary.7 → 3.56.0-canary.8

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.
@@ -1 +1 @@
1
- {"version":3,"file":"buildSortParam.d.ts","sourceRoot":"","sources":["../../src/queries/buildSortParam.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAE7C,OAAO,EAEL,KAAK,cAAc,EAEnB,KAAK,eAAe,EACpB,KAAK,IAAI,EACV,MAAM,SAAS,CAAA;AAEhB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAKlD,KAAK,IAAI,GAAG;IACV,OAAO,EAAE,eAAe,CAAA;IACxB,MAAM,EAAE,eAAe,CAAA;IACvB,MAAM,EAAE,cAAc,EAAE,CAAA;IACxB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,iBAAiB,CAAC,EAAE,OAAO,CAAA;IAC3B,IAAI,CAAC,EAAE,IAAI,CAAA;IACX,eAAe,CAAC,EAAE,aAAa,EAAE,CAAA;IACjC,UAAU,EAAE,OAAO,CAAA;IACnB,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB,CAAA;AAED,MAAM,MAAM,QAAQ,GAAG;IACrB,SAAS,EAAE,aAAa,CAAA;IACxB,QAAQ,EAAE,MAAM,CAAA;CACjB,EAAE,CAAA;AAEH,MAAM,MAAM,aAAa,GAAG,KAAK,GAAG,MAAM,CAAA;AA8H1C,eAAO,MAAM,cAAc,yGAUxB,IAAI,KAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAgF9B,CAAA"}
1
+ {"version":3,"file":"buildSortParam.d.ts","sourceRoot":"","sources":["../../src/queries/buildSortParam.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAE7C,OAAO,EAEL,KAAK,cAAc,EAEnB,KAAK,eAAe,EACpB,KAAK,IAAI,EACV,MAAM,SAAS,CAAA;AAEhB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAKlD,KAAK,IAAI,GAAG;IACV,OAAO,EAAE,eAAe,CAAA;IACxB,MAAM,EAAE,eAAe,CAAA;IACvB,MAAM,EAAE,cAAc,EAAE,CAAA;IACxB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,iBAAiB,CAAC,EAAE,OAAO,CAAA;IAC3B,IAAI,CAAC,EAAE,IAAI,CAAA;IACX,eAAe,CAAC,EAAE,aAAa,EAAE,CAAA;IACjC,UAAU,EAAE,OAAO,CAAA;IACnB,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB,CAAA;AAED,MAAM,MAAM,QAAQ,GAAG;IACrB,SAAS,EAAE,aAAa,CAAA;IACxB,QAAQ,EAAE,MAAM,CAAA;CACjB,EAAE,CAAA;AAEH,MAAM,MAAM,aAAa,GAAG,KAAK,GAAG,MAAM,CAAA;AAgH1C,eAAO,MAAM,cAAc,yGAUxB,IAAI,KAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAgF9B,CAAA"}
@@ -1,93 +1,75 @@
1
- import { APIError, getFieldByPath } from 'payload';
1
+ import { getFieldByPath } from 'payload';
2
2
  import { getCollection } from '../utilities/getEntity.js';
3
3
  import { getLocalizedSortProperty } from './getLocalizedSortProperty.js';
4
- const relationshipSort = ({ adapter, fields, locale, path, sort, sortAggregation, sortDirection, versions })=>{
4
+ const relationshipSort = ({ adapter, fields, locale, path, previousField = '', sortAggregation })=>{
5
5
  let currentFields = fields;
6
6
  const segments = path.split('.');
7
7
  if (segments.length < 2) {
8
- return false;
8
+ return null;
9
9
  }
10
10
  for(let i = 0; i < segments.length; i++){
11
11
  const segment = segments[i];
12
12
  const field = currentFields.find((each)=>each.name === segment);
13
13
  if (!field) {
14
- return false;
14
+ return null;
15
15
  }
16
16
  if ('fields' in field) {
17
17
  currentFields = field.flattenedFields;
18
- if (field.name === 'version' && versions && i === 0) {
19
- segments.shift();
20
- i--;
21
- }
22
18
  } else if ((field.type === 'relationship' || field.type === 'upload') && i !== segments.length - 1) {
23
19
  const relationshipPath = segments.slice(0, i + 1).join('.');
24
- let sortFieldPath = segments.slice(i + 1, segments.length).join('.');
25
- if (sortFieldPath.endsWith('.id')) {
26
- sortFieldPath = sortFieldPath.split('.').slice(0, -1).join('.');
27
- }
28
- if (Array.isArray(field.relationTo)) {
29
- throw new APIError('Not supported');
20
+ const nextPath = segments.slice(i + 1, segments.length);
21
+ const relationshipFieldResult = getFieldByPath({
22
+ fields,
23
+ path: relationshipPath
24
+ });
25
+ if (!relationshipFieldResult || !('relationTo' in relationshipFieldResult.field) || typeof relationshipFieldResult.field.relationTo !== 'string') {
26
+ return null;
30
27
  }
31
- const foreignCollection = getCollection({
28
+ const { collectionConfig, Model } = getCollection({
32
29
  adapter,
33
- collectionSlug: field.relationTo
30
+ collectionSlug: relationshipFieldResult.field.relationTo
34
31
  });
35
- const foreignFieldPath = getFieldByPath({
36
- fields: foreignCollection.collectionConfig.flattenedFields,
37
- path: sortFieldPath
38
- });
39
- if (!foreignFieldPath) {
40
- return false;
32
+ let localizedRelationshipPath = relationshipFieldResult.localizedPath;
33
+ if (locale && relationshipFieldResult.pathHasLocalized) {
34
+ localizedRelationshipPath = relationshipFieldResult.localizedPath.replace('<locale>', locale);
41
35
  }
42
- if (foreignFieldPath.pathHasLocalized && locale) {
43
- sortFieldPath = foreignFieldPath.localizedPath.replace('<locale>', locale);
36
+ if (nextPath.join('.') === 'id') {
37
+ return `${previousField}${localizedRelationshipPath}`;
44
38
  }
45
- const as = `__${relationshipPath.replace(/\./g, '__')}`;
46
- // If we have not already sorted on this relationship yet, we need to add a lookup stage
47
- if (!sortAggregation.some((each)=>'$lookup' in each && each.$lookup.as === as)) {
48
- let localField = versions ? `version.${relationshipPath}` : relationshipPath;
49
- if (adapter.usePipelineInSortLookup) {
50
- const flattenedField = `__${localField.replace(/\./g, '__')}_lookup`;
51
- sortAggregation.push({
52
- $addFields: {
53
- [flattenedField]: `$${localField}`
54
- }
55
- });
56
- localField = flattenedField;
39
+ const as = `__${previousField}${localizedRelationshipPath}`;
40
+ sortAggregation.push({
41
+ $lookup: {
42
+ as: `__${previousField}${localizedRelationshipPath}`,
43
+ foreignField: '_id',
44
+ from: Model.collection.name,
45
+ localField: `${previousField}${localizedRelationshipPath}`
57
46
  }
58
- sortAggregation.push({
59
- $lookup: {
60
- as,
61
- foreignField: '_id',
62
- from: foreignCollection.Model.collection.name,
63
- localField,
64
- ...!adapter.usePipelineInSortLookup && {
65
- pipeline: [
66
- {
67
- $project: {
68
- [sortFieldPath]: true
69
- }
70
- }
71
- ]
72
- }
73
- }
47
+ });
48
+ if (nextPath.length > 1) {
49
+ const nextRes = relationshipSort({
50
+ adapter,
51
+ fields: collectionConfig.flattenedFields,
52
+ locale,
53
+ path: nextPath.join('.'),
54
+ previousField: `${as}.`,
55
+ sortAggregation
74
56
  });
75
- if (adapter.usePipelineInSortLookup) {
76
- sortAggregation.push({
77
- $unset: localField
78
- });
57
+ if (nextRes) {
58
+ return nextRes;
79
59
  }
60
+ return `${as}.${nextPath.join('.')}`;
80
61
  }
81
- if (!adapter.usePipelineInSortLookup) {
82
- const lookup = sortAggregation.find((each)=>'$lookup' in each && each.$lookup.as === as);
83
- const pipeline = lookup.$lookup.pipeline[0];
84
- pipeline.$project[sortFieldPath] = true;
62
+ const nextField = getFieldByPath({
63
+ fields: collectionConfig.flattenedFields,
64
+ path: nextPath[0]
65
+ });
66
+ if (nextField && nextField.pathHasLocalized && locale) {
67
+ return `${as}.${nextField.localizedPath.replace('<locale>', locale)}`;
85
68
  }
86
- sort[`${as}.${sortFieldPath}`] = sortDirection;
87
- return true;
69
+ return `${as}.${nextPath[0]}`;
88
70
  }
89
71
  }
90
- return false;
72
+ return null;
91
73
  };
92
74
  export const buildSortParam = ({ adapter, config, fields, locale, parentIsLocalized = false, sort, sortAggregation, timestamps, versions })=>{
93
75
  if (!sort) {
@@ -134,17 +116,19 @@ export const buildSortParam = ({ adapter, config, fields, locale, parentIsLocali
134
116
  acc['_id'] = sortDirection;
135
117
  return acc;
136
118
  }
137
- if (sortAggregation && relationshipSort({
138
- adapter,
139
- fields,
140
- locale,
141
- path: sortProperty,
142
- sort: acc,
143
- sortAggregation,
144
- sortDirection,
145
- versions
146
- })) {
147
- return acc;
119
+ if (sortAggregation) {
120
+ const sortRelProperty = relationshipSort({
121
+ adapter,
122
+ fields,
123
+ locale,
124
+ path: sortProperty,
125
+ sortAggregation,
126
+ versions
127
+ });
128
+ if (sortRelProperty) {
129
+ acc[sortRelProperty] = sortDirection;
130
+ return acc;
131
+ }
148
132
  }
149
133
  const localizedProperty = getLocalizedSortProperty({
150
134
  config,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/queries/buildSortParam.ts"],"sourcesContent":["import type { PipelineStage } from 'mongoose'\n\nimport {\n APIError,\n type FlattenedField,\n getFieldByPath,\n type SanitizedConfig,\n type Sort,\n} from 'payload'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { getCollection } from '../utilities/getEntity.js'\nimport { getLocalizedSortProperty } from './getLocalizedSortProperty.js'\n\ntype Args = {\n adapter: MongooseAdapter\n config: SanitizedConfig\n fields: FlattenedField[]\n locale?: string\n parentIsLocalized?: boolean\n sort?: Sort\n sortAggregation?: PipelineStage[]\n timestamps: boolean\n versions?: boolean\n}\n\nexport type SortArgs = {\n direction: SortDirection\n property: string\n}[]\n\nexport type SortDirection = 'asc' | 'desc'\n\nconst relationshipSort = ({\n adapter,\n fields,\n locale,\n path,\n sort,\n sortAggregation,\n sortDirection,\n versions,\n}: {\n adapter: MongooseAdapter\n fields: FlattenedField[]\n locale?: string\n path: string\n sort: Record<string, string>\n sortAggregation: PipelineStage[]\n sortDirection: SortDirection\n versions?: boolean\n}) => {\n let currentFields = fields\n const segments = path.split('.')\n if (segments.length < 2) {\n return false\n }\n\n for (let i = 0; i < segments.length; i++) {\n const segment = segments[i]\n const field = currentFields.find((each) => each.name === segment)\n\n if (!field) {\n return false\n }\n\n if ('fields' in field) {\n currentFields = field.flattenedFields\n if (field.name === 'version' && versions && i === 0) {\n segments.shift()\n i--\n }\n } else if (\n (field.type === 'relationship' || field.type === 'upload') &&\n i !== segments.length - 1\n ) {\n const relationshipPath = segments.slice(0, i + 1).join('.')\n let sortFieldPath = segments.slice(i + 1, segments.length).join('.')\n if (sortFieldPath.endsWith('.id')) {\n sortFieldPath = sortFieldPath.split('.').slice(0, -1).join('.')\n }\n if (Array.isArray(field.relationTo)) {\n throw new APIError('Not supported')\n }\n\n const foreignCollection = getCollection({ adapter, collectionSlug: field.relationTo })\n\n const foreignFieldPath = getFieldByPath({\n fields: foreignCollection.collectionConfig.flattenedFields,\n path: sortFieldPath,\n })\n\n if (!foreignFieldPath) {\n return false\n }\n\n if (foreignFieldPath.pathHasLocalized && locale) {\n sortFieldPath = foreignFieldPath.localizedPath.replace('<locale>', locale)\n }\n\n const as = `__${relationshipPath.replace(/\\./g, '__')}`\n\n // If we have not already sorted on this relationship yet, we need to add a lookup stage\n if (!sortAggregation.some((each) => '$lookup' in each && each.$lookup.as === as)) {\n let localField = versions ? `version.${relationshipPath}` : relationshipPath\n\n if (adapter.usePipelineInSortLookup) {\n const flattenedField = `__${localField.replace(/\\./g, '__')}_lookup`\n sortAggregation.push({\n $addFields: {\n [flattenedField]: `$${localField}`,\n },\n })\n localField = flattenedField\n }\n\n sortAggregation.push({\n $lookup: {\n as,\n foreignField: '_id',\n from: foreignCollection.Model.collection.name,\n localField,\n ...(!adapter.usePipelineInSortLookup && {\n pipeline: [\n {\n $project: {\n [sortFieldPath]: true,\n },\n },\n ],\n }),\n },\n })\n\n if (adapter.usePipelineInSortLookup) {\n sortAggregation.push({\n $unset: localField,\n })\n }\n }\n\n if (!adapter.usePipelineInSortLookup) {\n const lookup = sortAggregation.find(\n (each) => '$lookup' in each && each.$lookup.as === as,\n ) as PipelineStage.Lookup\n const pipeline = lookup.$lookup.pipeline![0] as PipelineStage.Project\n pipeline.$project[sortFieldPath] = true\n }\n\n sort[`${as}.${sortFieldPath}`] = sortDirection\n return true\n }\n }\n\n return false\n}\n\nexport const buildSortParam = ({\n adapter,\n config,\n fields,\n locale,\n parentIsLocalized = false,\n sort,\n sortAggregation,\n timestamps,\n versions,\n}: Args): Record<string, string> => {\n if (!sort) {\n if (timestamps) {\n sort = '-createdAt'\n } else {\n sort = '-id'\n }\n }\n\n if (typeof sort === 'string') {\n sort = [sort]\n }\n\n // We use this flag to determine if the sort is unique or not to decide whether to add a fallback sort.\n const isUniqueSort = sort.some((item) => {\n const field = getFieldByPath({ fields, path: item })\n return field?.field?.unique\n })\n\n // In the case of Mongo, when sorting by a field that is not unique, the results are not guaranteed to be in the same order each time.\n // So we add a fallback sort to ensure that the results are always in the same order.\n let fallbackSort = '-id'\n\n if (timestamps) {\n fallbackSort = '-createdAt'\n }\n\n const includeFallbackSort =\n !adapter.disableFallbackSort &&\n !isUniqueSort &&\n !(sort.includes(fallbackSort) || sort.includes(fallbackSort.replace('-', '')))\n\n if (includeFallbackSort) {\n sort.push(fallbackSort)\n }\n\n const sorting = sort.reduce<Record<string, string>>((acc, item) => {\n let sortProperty: string\n let sortDirection: SortDirection\n if (item.indexOf('-') === 0) {\n sortProperty = item.substring(1)\n sortDirection = 'desc'\n } else {\n sortProperty = item\n sortDirection = 'asc'\n }\n if (sortProperty === 'id') {\n acc['_id'] = sortDirection\n return acc\n }\n\n if (\n sortAggregation &&\n relationshipSort({\n adapter,\n fields,\n locale,\n path: sortProperty,\n sort: acc,\n sortAggregation,\n sortDirection,\n versions,\n })\n ) {\n return acc\n }\n\n const localizedProperty = getLocalizedSortProperty({\n config,\n fields,\n locale,\n parentIsLocalized,\n segments: sortProperty.split('.'),\n })\n acc[localizedProperty] = sortDirection\n\n return acc\n }, {})\n\n return sorting\n}\n"],"names":["APIError","getFieldByPath","getCollection","getLocalizedSortProperty","relationshipSort","adapter","fields","locale","path","sort","sortAggregation","sortDirection","versions","currentFields","segments","split","length","i","segment","field","find","each","name","flattenedFields","shift","type","relationshipPath","slice","join","sortFieldPath","endsWith","Array","isArray","relationTo","foreignCollection","collectionSlug","foreignFieldPath","collectionConfig","pathHasLocalized","localizedPath","replace","as","some","$lookup","localField","usePipelineInSortLookup","flattenedField","push","$addFields","foreignField","from","Model","collection","pipeline","$project","$unset","lookup","buildSortParam","config","parentIsLocalized","timestamps","isUniqueSort","item","unique","fallbackSort","includeFallbackSort","disableFallbackSort","includes","sorting","reduce","acc","sortProperty","indexOf","substring","localizedProperty"],"mappings":"AAEA,SACEA,QAAQ,EAERC,cAAc,QAGT,UAAS;AAIhB,SAASC,aAAa,QAAQ,4BAA2B;AACzD,SAASC,wBAAwB,QAAQ,gCAA+B;AAqBxE,MAAMC,mBAAmB,CAAC,EACxBC,OAAO,EACPC,MAAM,EACNC,MAAM,EACNC,IAAI,EACJC,IAAI,EACJC,eAAe,EACfC,aAAa,EACbC,QAAQ,EAUT;IACC,IAAIC,gBAAgBP;IACpB,MAAMQ,WAAWN,KAAKO,KAAK,CAAC;IAC5B,IAAID,SAASE,MAAM,GAAG,GAAG;QACvB,OAAO;IACT;IAEA,IAAK,IAAIC,IAAI,GAAGA,IAAIH,SAASE,MAAM,EAAEC,IAAK;QACxC,MAAMC,UAAUJ,QAAQ,CAACG,EAAE;QAC3B,MAAME,QAAQN,cAAcO,IAAI,CAAC,CAACC,OAASA,KAAKC,IAAI,KAAKJ;QAEzD,IAAI,CAACC,OAAO;YACV,OAAO;QACT;QAEA,IAAI,YAAYA,OAAO;YACrBN,gBAAgBM,MAAMI,eAAe;YACrC,IAAIJ,MAAMG,IAAI,KAAK,aAAaV,YAAYK,MAAM,GAAG;gBACnDH,SAASU,KAAK;gBACdP;YACF;QACF,OAAO,IACL,AAACE,CAAAA,MAAMM,IAAI,KAAK,kBAAkBN,MAAMM,IAAI,KAAK,QAAO,KACxDR,MAAMH,SAASE,MAAM,GAAG,GACxB;YACA,MAAMU,mBAAmBZ,SAASa,KAA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FAAqF;IACrF,IAAIC,eAAe;IAEnB,IAAIJ,YAAY;QACdI,eAAe;IACjB;IAEA,MAAMC,sBACJ,CAAC5D,QAAQ6D,mBAAmB,IAC5B,CAACL,gBACD,CAAEpD,CAAAA,KAAK0D,QAAQ,CAACH,iBAAiBvD,KAAK0D,QAAQ,CAACH,aAAaxB,OAAO,CAAC,KAAK,IAAG;IAE9E,IAAIyB,qBAAqB;QACvBxD,KAAKsC,IAAI,CAACiB;IACZ;IAEA,MAAMI,UAAU3D,KAAK4D,MAAM,CAAyB,CAACC,KAAKR;QACxD,IAAIS;QACJ,IAAI5D;QACJ,IAAImD,KAAKU,OAAO,CAAC,SAAS,GAAG;YAC3BD,eAAeT,KAAKW,SAAS,CAAC;YAC9B9D,gBAAgB;QAClB,OAAO;YACL4D,eAAeT;YACfnD,gBAAgB;QAClB;QACA,IAAI4D,iBAAiB,MAAM;YACzBD,GAAG,CAAC,MAAM,GAAG3D;YACb,OAAO2D;QACT;QAEA,IACE5D,mBACAN,iBAAiB;YACfC;YACAC;YACAC;YACAC,MAAM+D;YACN9D,MAAM6D;YACN5D;YACAC;YACAC;QACF,IACA;YACA,OAAO0D;QACT;QAEA,MAAMI,oBAAoBvE,yBAAyB;YACjDuD;YACApD;YACAC;YACAoD;YACA7C,UAAUyD,aAAaxD,KAAK,CAAC;QAC/B;QACAuD,GAAG,CAACI,kBAAkB,GAAG/D;QAEzB,OAAO2D;IACT,GAAG,CAAC;IAEJ,OAAOF;AACT,EAAC"}
1
+ {"version":3,"sources":["../../src/queries/buildSortParam.ts"],"sourcesContent":["import type { PipelineStage } from 'mongoose'\n\nimport {\n APIError,\n type FlattenedField,\n getFieldByPath,\n type SanitizedConfig,\n type Sort,\n} from 'payload'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { getCollection } from '../utilities/getEntity.js'\nimport { getLocalizedSortProperty } from './getLocalizedSortProperty.js'\n\ntype Args = {\n adapter: MongooseAdapter\n config: SanitizedConfig\n fields: FlattenedField[]\n locale?: string\n parentIsLocalized?: boolean\n sort?: Sort\n sortAggregation?: PipelineStage[]\n timestamps: boolean\n versions?: boolean\n}\n\nexport type SortArgs = {\n direction: SortDirection\n property: string\n}[]\n\nexport type SortDirection = 'asc' | 'desc'\n\nconst relationshipSort = ({\n adapter,\n fields,\n locale,\n path,\n previousField = '',\n sortAggregation,\n}: {\n adapter: MongooseAdapter\n fields: FlattenedField[]\n locale?: string\n path: string\n previousField?: string\n sortAggregation: PipelineStage[]\n versions?: boolean\n}): null | string => {\n let currentFields = fields\n const segments = path.split('.')\n if (segments.length < 2) {\n return null\n }\n\n for (let i = 0; i < segments.length; i++) {\n const segment = segments[i]\n const field = currentFields.find((each) => each.name === segment)\n\n if (!field) {\n return null\n }\n\n if ('fields' in field) {\n currentFields = field.flattenedFields\n } else if (\n (field.type === 'relationship' || field.type === 'upload') &&\n i !== segments.length - 1\n ) {\n const relationshipPath = segments.slice(0, i + 1).join('.')\n const nextPath = segments.slice(i + 1, segments.length)\n const relationshipFieldResult = getFieldByPath({ fields, path: relationshipPath })\n\n if (\n !relationshipFieldResult ||\n !('relationTo' in relationshipFieldResult.field) ||\n typeof relationshipFieldResult.field.relationTo !== 'string'\n ) {\n return null\n }\n\n const { collectionConfig, Model } = getCollection({\n adapter,\n collectionSlug: relationshipFieldResult.field.relationTo,\n })\n\n let localizedRelationshipPath: string = relationshipFieldResult.localizedPath\n\n if (locale && relationshipFieldResult.pathHasLocalized) {\n localizedRelationshipPath = relationshipFieldResult.localizedPath.replace(\n '<locale>',\n locale,\n )\n }\n\n if (nextPath.join('.') === 'id') {\n return `${previousField}${localizedRelationshipPath}`\n }\n\n const as = `__${previousField}${localizedRelationshipPath}`\n\n sortAggregation.push({\n $lookup: {\n as: `__${previousField}${localizedRelationshipPath}`,\n foreignField: '_id',\n from: Model.collection.name,\n localField: `${previousField}${localizedRelationshipPath}`,\n },\n })\n\n if (nextPath.length > 1) {\n const nextRes = relationshipSort({\n adapter,\n fields: collectionConfig.flattenedFields,\n locale,\n path: nextPath.join('.'),\n previousField: `${as}.`,\n sortAggregation,\n })\n\n if (nextRes) {\n return nextRes\n }\n\n return `${as}.${nextPath.join('.')}`\n }\n\n const nextField = getFieldByPath({\n fields: collectionConfig.flattenedFields,\n path: nextPath[0]!,\n })\n\n if (nextField && nextField.pathHasLocalized && locale) {\n return `${as}.${nextField.localizedPath.replace('<locale>', locale)}`\n }\n\n return `${as}.${nextPath[0]}`\n }\n }\n\n return null\n}\n\nexport const buildSortParam = ({\n adapter,\n config,\n fields,\n locale,\n parentIsLocalized = false,\n sort,\n sortAggregation,\n timestamps,\n versions,\n}: Args): Record<string, string> => {\n if (!sort) {\n if (timestamps) {\n sort = '-createdAt'\n } else {\n sort = '-id'\n }\n }\n\n if (typeof sort === 'string') {\n sort = [sort]\n }\n\n // We use this flag to determine if the sort is unique or not to decide whether to add a fallback sort.\n const isUniqueSort = sort.some((item) => {\n const field = getFieldByPath({ fields, path: item })\n return field?.field?.unique\n })\n\n // In the case of Mongo, when sorting by a field that is not unique, the results are not guaranteed to be in the same order each time.\n // So we add a fallback sort to ensure that the results are always in the same order.\n let fallbackSort = '-id'\n\n if (timestamps) {\n fallbackSort = '-createdAt'\n }\n\n const includeFallbackSort =\n !adapter.disableFallbackSort &&\n !isUniqueSort &&\n !(sort.includes(fallbackSort) || sort.includes(fallbackSort.replace('-', '')))\n\n if (includeFallbackSort) {\n sort.push(fallbackSort)\n }\n\n const sorting = sort.reduce<Record<string, string>>((acc, item) => {\n let sortProperty: string\n let sortDirection: SortDirection\n if (item.indexOf('-') === 0) {\n sortProperty = item.substring(1)\n sortDirection = 'desc'\n } else {\n sortProperty = item\n sortDirection = 'asc'\n }\n if (sortProperty === 'id') {\n acc['_id'] = sortDirection\n return acc\n }\n\n if (sortAggregation) {\n const sortRelProperty = relationshipSort({\n adapter,\n fields,\n locale,\n path: sortProperty,\n sortAggregation,\n versions,\n })\n\n if (sortRelProperty) {\n acc[sortRelProperty] = sortDirection\n return acc\n }\n }\n\n const localizedProperty = getLocalizedSortProperty({\n config,\n fields,\n locale,\n parentIsLocalized,\n segments: sortProperty.split('.'),\n })\n acc[localizedProperty] = sortDirection\n\n return acc\n }, {})\n\n return sorting\n}\n"],"names":["getFieldByPath","getCollection","getLocalizedSortProperty","relationshipSort","adapter","fields","locale","path","previousField","sortAggregation","currentFields","segments","split","length","i","segment","field","find","each","name","flattenedFields","type","relationshipPath","slice","join","nextPath","relationshipFieldResult","relationTo","collectionConfig","Model","collectionSlug","localizedRelationshipPath","localizedPath","pathHasLocalized","replace","as","push","$lookup","foreignField","from","collection","localField","nextRes","nextField","buildSortParam","config","parentIsLocalized","sort","timestamps","versions","isUniqueSort","some","item","unique","fallbackSort","includeFallbackSort","disableFallbackSort","includes","sorting","reduce","acc","sortProperty","sortDirection","indexOf","substring","sortRelProperty","localizedProperty"],"mappings":"AAEA,SAGEA,cAAc,QAGT,UAAS;AAIhB,SAASC,aAAa,QAAQ,4BAA2B;AACzD,SAASC,wBAAwB,QAAQ,gCAA+B;AAqBxE,MAAMC,mBAAmB,CAAC,EACxBC,OAAO,EACPC,MAAM,EACNC,MAAM,EACNC,IAAI,EACJC,gBAAgB,EAAE,EAClBC,eAAe,EAShB;IACC,IAAIC,gBAAgBL;IACpB,MAAMM,WAAWJ,KAAKK,KAAK,CAAC;IAC5B,IAAID,SAASE,MAAM,GAAG,GAAG;QACvB,OAAO;IACT;IAEA,IAAK,IAAIC,IAAI,GAAGA,IAAIH,SAASE,MAAM,EAAEC,IAAK;QACxC,MAAMC,UAAUJ,QAAQ,CAACG,EAAE;QAC3B,MAAME,QAAQN,cAAcO,IAAI,CAAC,CAACC,OAASA,KAAKC,IAAI,KAAKJ;QAEzD,IAAI,CAACC,OAAO;YACV,OAAO;QACT;QAEA,IAAI,YAAYA,OAAO;YACrBN,gBAAgBM,MAAMI,eAAe;QACvC,OAAO,IACL,AAACJ,CAAAA,MAAMK,IAAI,KAAK,kBAAkBL,MAAMK,IAAI,KAAK,QAAO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MAAMI,UAAUX,KAAKY,MAAM,CAAyB,CAACC,KAAKR;QACxD,IAAIS;QACJ,IAAIC;QACJ,IAAIV,KAAKW,OAAO,CAAC,SAAS,GAAG;YAC3BF,eAAeT,KAAKY,SAAS,CAAC;YAC9BF,gBAAgB;QAClB,OAAO;YACLD,eAAeT;YACfU,gBAAgB;QAClB;QACA,IAAID,iBAAiB,MAAM;YACzBD,GAAG,CAAC,MAAM,GAAGE;YACb,OAAOF;QACT;QAEA,IAAInD,iBAAiB;YACnB,MAAMwD,kBAAkB9D,iBAAiB;gBACvCC;gBACAC;gBACAC;gBACAC,MAAMsD;gBACNpD;gBACAwC;YACF;YAEA,IAAIgB,iBAAiB;gBACnBL,GAAG,CAACK,gBAAgB,GAAGH;gBACvB,OAAOF;YACT;QACF;QAEA,MAAMM,oBAAoBhE,yBAAyB;YACjD2C;YACAxC;YACAC;YACAwC;YACAnC,UAAUkD,aAAajD,KAAK,CAAC;QAC/B;QACAgD,GAAG,CAACM,kBAAkB,GAAGJ;QAEzB,OAAOF;IACT,GAAG,CAAC;IAEJ,OAAOF;AACT,EAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@payloadcms/db-mongodb",
3
- "version": "3.56.0-canary.7",
3
+ "version": "3.56.0-canary.8",
4
4
  "description": "The officially supported MongoDB database adapter for Payload",
5
5
  "homepage": "https://payloadcms.com",
6
6
  "repository": {
@@ -50,10 +50,10 @@
50
50
  "mongodb": "6.16.0",
51
51
  "mongodb-memory-server": "10.1.4",
52
52
  "@payloadcms/eslint-config": "3.28.0",
53
- "payload": "3.56.0-canary.7"
53
+ "payload": "3.56.0-canary.8"
54
54
  },
55
55
  "peerDependencies": {
56
- "payload": "3.56.0-canary.7"
56
+ "payload": "3.56.0-canary.8"
57
57
  },
58
58
  "scripts": {
59
59
  "build": "pnpm build:types && pnpm build:swc",