@payloadcms/db-mongodb 3.25.0-canary.f62445f → 3.25.0-canary.fd53f68

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.
Files changed (63) hide show
  1. package/dist/create.js +15 -15
  2. package/dist/create.js.map +1 -1
  3. package/dist/createGlobal.d.ts.map +1 -1
  4. package/dist/createGlobal.js +15 -14
  5. package/dist/createGlobal.js.map +1 -1
  6. package/dist/createGlobalVersion.d.ts.map +1 -1
  7. package/dist/createGlobalVersion.js +26 -23
  8. package/dist/createGlobalVersion.js.map +1 -1
  9. package/dist/createVersion.d.ts.map +1 -1
  10. package/dist/createVersion.js +26 -31
  11. package/dist/createVersion.js.map +1 -1
  12. package/dist/deleteOne.d.ts.map +1 -1
  13. package/dist/deleteOne.js +8 -6
  14. package/dist/deleteOne.js.map +1 -1
  15. package/dist/find.d.ts.map +1 -1
  16. package/dist/find.js +8 -9
  17. package/dist/find.js.map +1 -1
  18. package/dist/findGlobal.d.ts.map +1 -1
  19. package/dist/findGlobal.js +10 -9
  20. package/dist/findGlobal.js.map +1 -1
  21. package/dist/findGlobalVersions.d.ts.map +1 -1
  22. package/dist/findGlobalVersions.js +10 -10
  23. package/dist/findGlobalVersions.js.map +1 -1
  24. package/dist/findOne.d.ts.map +1 -1
  25. package/dist/findOne.js +8 -6
  26. package/dist/findOne.js.map +1 -1
  27. package/dist/findVersions.js +8 -9
  28. package/dist/findVersions.js.map +1 -1
  29. package/dist/models/buildSchema.js +2 -0
  30. package/dist/models/buildSchema.js.map +1 -1
  31. package/dist/predefinedMigrations/migrateRelationshipsV2_V3.d.ts.map +1 -1
  32. package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js +12 -7
  33. package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js.map +1 -1
  34. package/dist/queryDrafts.js +13 -13
  35. package/dist/queryDrafts.js.map +1 -1
  36. package/dist/updateGlobal.d.ts.map +1 -1
  37. package/dist/updateGlobal.js +15 -15
  38. package/dist/updateGlobal.js.map +1 -1
  39. package/dist/updateGlobalVersion.d.ts.map +1 -1
  40. package/dist/updateGlobalVersion.js +13 -13
  41. package/dist/updateGlobalVersion.js.map +1 -1
  42. package/dist/updateOne.d.ts.map +1 -1
  43. package/dist/updateOne.js +12 -9
  44. package/dist/updateOne.js.map +1 -1
  45. package/dist/updateVersion.d.ts.map +1 -1
  46. package/dist/updateVersion.js +13 -13
  47. package/dist/updateVersion.js.map +1 -1
  48. package/dist/utilities/transform.d.ts +27 -0
  49. package/dist/utilities/transform.d.ts.map +1 -0
  50. package/dist/utilities/transform.js +224 -0
  51. package/dist/utilities/transform.js.map +1 -0
  52. package/dist/utilities/{sanitizeRelationshipIDs.spec.js → transform.spec.js} +11 -5
  53. package/dist/utilities/transform.spec.js.map +1 -0
  54. package/package.json +3 -3
  55. package/dist/utilities/sanitizeInternalFields.d.ts +0 -2
  56. package/dist/utilities/sanitizeInternalFields.d.ts.map +0 -1
  57. package/dist/utilities/sanitizeInternalFields.js +0 -20
  58. package/dist/utilities/sanitizeInternalFields.js.map +0 -1
  59. package/dist/utilities/sanitizeRelationshipIDs.d.ts +0 -10
  60. package/dist/utilities/sanitizeRelationshipIDs.d.ts.map +0 -1
  61. package/dist/utilities/sanitizeRelationshipIDs.js +0 -130
  62. package/dist/utilities/sanitizeRelationshipIDs.js.map +0 -1
  63. package/dist/utilities/sanitizeRelationshipIDs.spec.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/findGlobalVersions.ts"],"sourcesContent":["import type { PaginateOptions, QueryOptions } from 'mongoose'\nimport type { FindGlobalVersions } from 'payload'\n\nimport { buildVersionGlobalFields, flattenWhereToOperators } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildSortParam } from './queries/buildSortParam.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getSession } from './utilities/getSession.js'\nimport { sanitizeInternalFields } from './utilities/sanitizeInternalFields.js'\n\nexport const findGlobalVersions: FindGlobalVersions = async function findGlobalVersions(\n this: MongooseAdapter,\n { global, limit, locale, page, pagination, req, select, skip, sort: sortArg, where },\n) {\n const Model = this.versions[global]\n const versionFields = buildVersionGlobalFields(\n this.payload.config,\n this.payload.globals.config.find(({ slug }) => slug === global),\n true,\n )\n\n const session = await getSession(this, req)\n const options: QueryOptions = {\n limit,\n session,\n skip,\n }\n\n let hasNearConstraint = false\n\n if (where) {\n const constraints = flattenWhereToOperators(where)\n hasNearConstraint = constraints.some((prop) => Object.keys(prop).some((key) => key === 'near'))\n }\n\n let sort\n if (!hasNearConstraint) {\n sort = buildSortParam({\n config: this.payload.config,\n fields: versionFields,\n locale,\n sort: sortArg || '-updatedAt',\n timestamps: true,\n })\n }\n\n const query = await buildQuery({\n adapter: this,\n fields: versionFields,\n locale,\n where,\n })\n\n // useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters.\n const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0\n const paginationOptions: PaginateOptions = {\n lean: true,\n leanWithId: true,\n limit,\n options,\n page,\n pagination,\n projection: buildProjectionFromSelect({ adapter: this, fields: versionFields, select }),\n sort,\n useEstimatedCount,\n }\n\n if (this.collation) {\n const defaultLocale = 'en'\n paginationOptions.collation = {\n locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,\n ...this.collation,\n }\n }\n\n if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {\n // Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding\n // a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,\n // which makes queries very slow. This only happens when no query (filter) is provided. If one is provided, it uses\n // the correct indexed field\n paginationOptions.useCustomCountFn = () => {\n return Promise.resolve(\n Model.countDocuments(query, {\n hint: { _id: 1 },\n session,\n }),\n )\n }\n }\n\n if (limit >= 0) {\n paginationOptions.limit = limit\n // limit must also be set here, it's ignored when pagination is false\n paginationOptions.options.limit = limit\n\n // Disable pagination if limit is 0\n if (limit === 0) {\n paginationOptions.pagination = false\n }\n }\n\n const result = await Model.paginate(query, paginationOptions)\n const docs = JSON.parse(JSON.stringify(result.docs))\n\n return {\n ...result,\n docs: docs.map((doc) => {\n doc.id = doc._id\n return sanitizeInternalFields(doc)\n }),\n }\n}\n"],"names":["buildVersionGlobalFields","flattenWhereToOperators","buildQuery","buildSortParam","buildProjectionFromSelect","getSession","sanitizeInternalFields","findGlobalVersions","global","limit","locale","page","pagination","req","select","skip","sort","sortArg","where","Model","versions","versionFields","payload","config","globals","find","slug","session","options","hasNearConstraint","constraints","some","prop","Object","keys","key","fields","timestamps","query","adapter","useEstimatedCount","length","paginationOptions","lean","leanWithId","projection","collation","defaultLocale","disableIndexHints","useCustomCountFn","Promise","resolve","countDocuments","hint","_id","result","paginate","docs","JSON","parse","stringify","map","doc","id"],"mappings":"AAGA,SAASA,wBAAwB,EAAEC,uBAAuB,QAAQ,UAAS;AAI3E,SAASC,UAAU,QAAQ,0BAAyB;AACpD,SAASC,cAAc,QAAQ,8BAA6B;AAC5D,SAASC,yBAAyB,QAAQ,2CAA0C;AACpF,SAASC,UAAU,QAAQ,4BAA2B;AACtD,SAASC,sBAAsB,QAAQ,wCAAuC;AAE9E,OAAO,MAAMC,qBAAyC,eAAeA,mBAEnE,EAAEC,MAAM,EAAEC,KAAK,EAAEC,MAAM,EAAEC,IAAI,EAAEC,UAAU,EAAEC,GAAG,EAAEC,MAAM,EAAEC,IAAI,EAAEC,MAAMC,OAAO,EAAEC,KAAK,EAAE;IAEpF,MAAMC,QAAQ,IAAI,CAACC,QAAQ,CAACZ,OAAO;IACnC,MAAMa,gBAAgBrB,yBACpB,IAAI,CAACsB,OAAO,CAACC,MAAM,EACnB,IAAI,CAACD,OAAO,CAACE,OAAO,CAACD,MAAM,CAACE,IAAI,CAAC,CAAC,EAAEC,IAAI,EAAE,GAAKA,SAASlB,SACxD;IAGF,MAAMmB,UAAU,M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AC1Be,MAAM;oBAAEC,KAAK;gBAAE;gBACf3B;YACF;QAEJ;IACF;IAEA,IAAIlB,SAAS,GAAG;QACdiC,kBAAkBjC,KAAK,GAAGA;QAC1B,qEAAqE;QACrEiC,kBAAkBd,OAAO,CAACnB,KAAK,GAAGA;QAElC,mCAAmC;QACnC,IAAIA,UAAU,GAAG;YACfiC,kBAAkB9B,UAAU,GAAG;QACjC;IACF;IAEA,MAAM2C,SAAS,MAAMpC,MAAMqC,QAAQ,CAAClB,OAAOI;IAC3C,MAAMe,OAAOC,KAAKC,KAAK,CAACD,KAAKE,SAAS,CAACL,OAAOE,IAAI;IAElD,OAAO;QACL,GAAGF,MAAM;QACTE,MAAMA,KAAKI,GAAG,CAAC,CAACC;YACdA,IAAIC,EAAE,GAAGD,IAAIR,GAAG;YAChB,OAAOhD,uBAAuBwD;QAChC;IACF;AACF,EAAC"}
1
+ {"version":3,"sources":["../src/findGlobalVersions.ts"],"sourcesContent":["import type { PaginateOptions, QueryOptions } from 'mongoose'\nimport type { FindGlobalVersions } from 'payload'\n\nimport { buildVersionGlobalFields, flattenWhereToOperators } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildSortParam } from './queries/buildSortParam.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getSession } from './utilities/getSession.js'\nimport { transform } from './utilities/transform.js'\n\nexport const findGlobalVersions: FindGlobalVersions = async function findGlobalVersions(\n this: MongooseAdapter,\n { global, limit, locale, page, pagination, req, select, skip, sort: sortArg, where },\n) {\n const globalConfig = this.payload.globals.config.find(({ slug }) => slug === global)\n const Model = this.versions[global]\n const versionFields = buildVersionGlobalFields(this.payload.config, globalConfig, true)\n\n const session = await getSession(this, req)\n const options: QueryOptions = {\n limit,\n session,\n skip,\n }\n\n let hasNearConstraint = false\n\n if (where) {\n const constraints = flattenWhereToOperators(where)\n hasNearConstraint = constraints.some((prop) => Object.keys(prop).some((key) => key === 'near'))\n }\n\n let sort\n if (!hasNearConstraint) {\n sort = buildSortParam({\n config: this.payload.config,\n fields: versionFields,\n locale,\n sort: sortArg || '-updatedAt',\n timestamps: true,\n })\n }\n\n const query = await buildQuery({\n adapter: this,\n fields: versionFields,\n locale,\n where,\n })\n\n // useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters.\n const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0\n const paginationOptions: PaginateOptions = {\n lean: true,\n leanWithId: true,\n limit,\n options,\n page,\n pagination,\n projection: buildProjectionFromSelect({ adapter: this, fields: versionFields, select }),\n sort,\n useEstimatedCount,\n }\n\n if (this.collation) {\n const defaultLocale = 'en'\n paginationOptions.collation = {\n locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,\n ...this.collation,\n }\n }\n\n if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {\n // Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding\n // a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,\n // which makes queries very slow. This only happens when no query (filter) is provided. If one is provided, it uses\n // the correct indexed field\n paginationOptions.useCustomCountFn = () => {\n return Promise.resolve(\n Model.countDocuments(query, {\n hint: { _id: 1 },\n session,\n }),\n )\n }\n }\n\n if (limit >= 0) {\n paginationOptions.limit = limit\n // limit must also be set here, it's ignored when pagination is false\n paginationOptions.options.limit = limit\n\n // Disable pagination if limit is 0\n if (limit === 0) {\n paginationOptions.pagination = false\n }\n }\n\n const result = await Model.paginate(query, paginationOptions)\n\n transform({\n adapter: this,\n data: result.docs,\n fields: buildVersionGlobalFields(this.payload.config, globalConfig),\n operation: 'read',\n })\n\n return result\n}\n"],"names":["buildVersionGlobalFields","flattenWhereToOperators","buildQuery","buildSortParam","buildProjectionFromSelect","getSession","transform","findGlobalVersions","global","limit","locale","page","pagination","req","select","skip","sort","sortArg","where","globalConfig","payload","globals","config","find","slug","Model","versions","versionFields","session","options","hasNearConstraint","constraints","some","prop","Object","keys","key","fields","timestamps","query","adapter","useEstimatedCount","length","paginationOptions","lean","leanWithId","projection","collation","defaultLocale","disableIndexHints","useCustomCountFn","Promise","resolve","countDocuments","hint","_id","result","paginate","data","docs","operation"],"mappings":"AAGA,SAASA,wBAAwB,EAAEC,uBAAuB,QAAQ,UAAS;AAI3E,SAASC,UAAU,QAAQ,0BAAyB;AACpD,SAASC,cAAc,QAAQ,8BAA6B;AAC5D,SAASC,yBAAyB,QAAQ,2CAA0C;AACpF,SAASC,UAAU,QAAQ,4BAA2B;AACtD,SAASC,SAAS,QAAQ,2BAA0B;AAEpD,OAAO,MAAMC,qBAAyC,eAAeA,mBAEnE,EAAEC,MAAM,EAAEC,KAAK,EAAEC,MAAM,EAAEC,IAAI,EAAEC,UAAU,EAAEC,GAAG,EAAEC,MAAM,EAAEC,IAAI,EAAEC,MAAMC,OAAO,EAAEC,KAAK,EAAE;IAEpF,MAAMC,eAAe,IAAI,CAACC,OAAO,CAACC,OAAO,CAACC,MAAM,CAACC,IAAI,CAAC,CAAC,EAAEC,IAAI,EAAE,GAAKA,SAAShB;IAC7E,MAAMiB,QAAQ,IAAI,CAACC,QAAQ,CAAClB,OAAO;IACnC,MAAMmB,gBAAgB3B,yBAAyB,IAAI,CAACoB,OAAO,CAACE,MAAM,EAAEH,cAAc;IAElF,MAAMS,UAAU,MAAMvB,WAAW,IAAI,EAAEQ;IACvC,MAAMgB,UAAwB;QAC5BpB;QACAmB;QACAb;IACF;IAEA,IAAIe,oBAAoB;IAExB,IAAIZ,OAAO;QACT,MAAMa,cAAc9B,wBAAwBiB;QAC5CY,oBAAoBC,YAAYC,IAAI,CAAC,CAACC,OAASC,OAAOC,IAAI,CAACF,MAAMD,IAAI,CAAC,CAACI,MAAQA,QAAQ;IACzF;IAEA,IAAIpB;IACJ,IAAI,CAACc,mBAAmB;QACtBd,OAAOb,eAAe;YACpBmB,QAAQ,IAAI,CAACF,OAAO,CAACE,MAAM;YAC3Be,QA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QACjBtB,QAAQrC,yBAAyB,IAAI,CAACoB,OAAO,CAACE,MAAM,EAAEH;QACtDyC,WAAW;IACb;IAEA,OAAOJ;AACT,EAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"findOne.d.ts","sourceRoot":"","sources":["../src/findOne.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAY,OAAO,EAAE,MAAM,SAAS,CAAA;AAUhD,eAAO,MAAM,OAAO,EAAE,OAwDrB,CAAA"}
1
+ {"version":3,"file":"findOne.d.ts","sourceRoot":"","sources":["../src/findOne.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAUtC,eAAO,MAAM,OAAO,EAAE,OAoDrB,CAAA"}
package/dist/findOne.js CHANGED
@@ -2,7 +2,7 @@ import { buildQuery } from './queries/buildQuery.js';
2
2
  import { buildJoinAggregation } from './utilities/buildJoinAggregation.js';
3
3
  import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js';
4
4
  import { getSession } from './utilities/getSession.js';
5
- import { sanitizeInternalFields } from './utilities/sanitizeInternalFields.js';
5
+ import { transform } from './utilities/transform.js';
6
6
  export const findOne = async function findOne({ collection, joins, locale, req, select, where }) {
7
7
  const Model = this.collections[collection];
8
8
  const collectionConfig = this.payload.collections[collection].config;
@@ -47,11 +47,13 @@ export const findOne = async function findOne({ collection, joins, locale, req,
47
47
  if (!doc) {
48
48
  return null;
49
49
  }
50
- let result = JSON.parse(JSON.stringify(doc));
51
- // custom id type reset
52
- result.id = result._id;
53
- result = sanitizeInternalFields(result);
54
- return result;
50
+ transform({
51
+ adapter: this,
52
+ data: doc,
53
+ fields: collectionConfig.fields,
54
+ operation: 'read'
55
+ });
56
+ return doc;
55
57
  };
56
58
 
57
59
  //# sourceMappingURL=findOne.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/findOne.ts"],"sourcesContent":["import type { AggregateOptions, QueryOptions } from 'mongoose'\nimport type { Document, FindOne } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildJoinAggregation } from './utilities/buildJoinAggregation.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getSession } from './utilities/getSession.js'\nimport { sanitizeInternalFields } from './utilities/sanitizeInternalFields.js'\n\nexport const findOne: FindOne = async function findOne(\n this: MongooseAdapter,\n { collection, joins, locale, req, select, where },\n) {\n const Model = this.collections[collection]\n const collectionConfig = this.payload.collections[collection].config\n const session = await getSession(this, req)\n const options: AggregateOptions & QueryOptions = {\n lean: true,\n session,\n }\n\n const query = await buildQuery({\n adapter: this,\n collectionSlug: collection,\n fields: collectionConfig.flattenedFields,\n locale,\n where,\n })\n\n const projection = buildProjectionFromSelect({\n adapter: this,\n fields: collectionConfig.flattenedFields,\n select,\n })\n\n const aggregate = await buildJoinAggregation({\n adapter: this,\n collection,\n collectionConfig,\n joins,\n limit: 1,\n locale,\n projection,\n query,\n })\n\n let doc\n if (aggregate) {\n ;[doc] = await Model.aggregate(aggregate, { session })\n } else {\n ;(options as Record<string, unknown>).projection = projection\n doc = await Model.findOne(query, {}, options)\n }\n\n if (!doc) {\n return null\n }\n\n let result: Document = JSON.parse(JSON.stringify(doc))\n\n // custom id type reset\n result.id = result._id\n result = sanitizeInternalFields(result)\n\n return result\n}\n"],"names":["buildQuery","buildJoinAggregation","buildProjectionFromSelect","getSession","sanitizeInternalFields","findOne","collection","joins","locale","req","select","where","Model","collections","collectionConfig","payload","config","session","options","lean","query","adapter","collectionSlug","fields","flattenedFields","projection","aggregate","limit","doc","result","JSON","parse","stringify","id","_id"],"mappings":"AAKA,SAASA,UAAU,QAAQ,0BAAyB;AACpD,SAASC,oBAAoB,QAAQ,sCAAqC;AAC1E,SAASC,yBAAyB,QAAQ,2CAA0C;AACpF,SAASC,UAAU,QAAQ,4BAA2B;AACtD,SAASC,sBAAsB,QAAQ,wCAAuC;AAE9E,OAAO,MAAMC,UAAmB,eAAeA,QAE7C,EAAEC,UAAU,EAAEC,KAAK,EAAEC,MAAM,EAAEC,GAAG,EAAEC,MAAM,EAAEC,KAAK,EAAE;IAEjD,MAAMC,QAAQ,IAAI,CAACC,WAAW,CAACP,WAAW;IAC1C,MAAMQ,mBAAmB,IAAI,CAACC,OAAO,CAACF,WAAW,CAACP,WAAW,CAACU,MAAM;IACpE,MAAMC,UAAU,MAAMd,WAAW,IAAI,EAAEM;IACvC,MAAMS,UAA2C;QAC/CC,MAAM;QACNF;IACF;IAEA,MAAMG,QAAQ,MAAMpB,WAAW;QAC7BqB,SAAS,IAAI;QACbC,gBAAgBhB;QAChBiB,QAAQT,iBAAiBU,eAAe;QACxChB;QACAG;IACF;IAEA,MAAMc,aAAavB,0BAA0B;QAC3CmB,SAAS,IAAI;QACbE,QAAQT,iBAAiBU,eAAe;QACxCd;IACF;IAEA,MAAMgB,YAAY,MAAMzB,qBAAqB;QAC3CoB,SAAS,IAAI;QACbf;QACAQ;QACAP;QACAoB,OAAO;QACPnB;QACAiB;QACAL;IACF;IAEA,IAAIQ;IACJ,IAAIF,WAAW;;QACZ,CAACE,IAAI,GAAG,MAAMhB,MAAMc,SAAS,CAACA,WAAW;YAAET;QAAQ;IACtD,OAAO;;QACHC,QAAoCO,UAAU,GAAGA;QACnDG,MAAM,MAAMhB,MAAMP,OAAO,CAACe,OAAO,CAAC,GAAGF;IACvC;IAEA,IAAI,CAACU,KAAK;QACR,OAAO;IACT;IAEA,IAAIC,SAAmBC,KAAKC,KAAK,CAACD,KAAKE,SAAS,CAACJ;IAEjD,uBAAuB;IACvBC,OAAOI,EAAE,GAAGJ,OAAOK,GAAG;IACtBL,SAASzB,uBAAuByB;IAEhC,OAAOA;AACT,EAAC"}
1
+ {"version":3,"sources":["../src/findOne.ts"],"sourcesContent":["import type { AggregateOptions, QueryOptions } from 'mongoose'\nimport type { FindOne } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildJoinAggregation } from './utilities/buildJoinAggregation.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getSession } from './utilities/getSession.js'\nimport { transform } from './utilities/transform.js'\n\nexport const findOne: FindOne = async function findOne(\n this: MongooseAdapter,\n { collection, joins, locale, req, select, where },\n) {\n const Model = this.collections[collection]\n const collectionConfig = this.payload.collections[collection].config\n const session = await getSession(this, req)\n const options: AggregateOptions & QueryOptions = {\n lean: true,\n session,\n }\n\n const query = await buildQuery({\n adapter: this,\n collectionSlug: collection,\n fields: collectionConfig.flattenedFields,\n locale,\n where,\n })\n\n const projection = buildProjectionFromSelect({\n adapter: this,\n fields: collectionConfig.flattenedFields,\n select,\n })\n\n const aggregate = await buildJoinAggregation({\n adapter: this,\n collection,\n collectionConfig,\n joins,\n limit: 1,\n locale,\n projection,\n query,\n })\n\n let doc\n if (aggregate) {\n ;[doc] = await Model.aggregate(aggregate, { session })\n } else {\n ;(options as Record<string, unknown>).projection = projection\n doc = await Model.findOne(query, {}, options)\n }\n\n if (!doc) {\n return null\n }\n\n transform({ adapter: this, data: doc, fields: collectionConfig.fields, operation: 'read' })\n\n return doc\n}\n"],"names":["buildQuery","buildJoinAggregation","buildProjectionFromSelect","getSession","transform","findOne","collection","joins","locale","req","select","where","Model","collections","collectionConfig","payload","config","session","options","lean","query","adapter","collectionSlug","fields","flattenedFields","projection","aggregate","limit","doc","data","operation"],"mappings":"AAKA,SAASA,UAAU,QAAQ,0BAAyB;AACpD,SAASC,oBAAoB,QAAQ,sCAAqC;AAC1E,SAASC,yBAAyB,QAAQ,2CAA0C;AACpF,SAASC,UAAU,QAAQ,4BAA2B;AACtD,SAASC,SAAS,QAAQ,2BAA0B;AAEpD,OAAO,MAAMC,UAAmB,eAAeA,QAE7C,EAAEC,UAAU,EAAEC,KAAK,EAAEC,MAAM,EAAEC,GAAG,EAAEC,MAAM,EAAEC,KAAK,EAAE;IAEjD,MAAMC,QAAQ,IAAI,CAACC,WAAW,CAACP,WAAW;IAC1C,MAAMQ,mBAAmB,IAAI,CAACC,OAAO,CAACF,WAAW,CAACP,WAAW,CAACU,MAAM;IACpE,MAAMC,UAAU,MAAMd,WAAW,IAAI,EAAEM;IACvC,MAAMS,UAA2C;QAC/CC,MAAM;QACNF;IACF;IAEA,MAAMG,QAAQ,MAAMpB,WAAW;QAC7BqB,SAAS,IAAI;QACbC,gBAAgBhB;QAChBiB,QAAQT,iBAAiBU,eAAe;QACxChB;QACAG;IACF;IAEA,MAAMc,aAAavB,0BAA0B;QAC3CmB,SAAS,IAAI;QACbE,QAAQT,iBAAiBU,eAAe;QACxCd;IACF;IAEA,MAAMgB,YAAY,MAAMzB,qBAAqB;QAC3CoB,SAAS,IAAI;QACbf;QACAQ;QACAP;QACAoB,OAAO;QACPnB;QACAiB;QACAL;IACF;IAEA,IAAIQ;IACJ,IAAIF,WAAW;;QACZ,CAACE,IAAI,GAAG,MAAMhB,MAAMc,SAAS,CAACA,WAAW;YAAET;QAAQ;IACtD,OAAO;;QACHC,QAAoCO,UAAU,GAAGA;QACnDG,MAAM,MAAMhB,MAAMP,OAAO,CAACe,OAAO,CAAC,GAAGF;IACvC;IAEA,IAAI,CAACU,KAAK;QACR,OAAO;IACT;IAEAxB,UAAU;QAAEiB,SAAS,IAAI;QAAEQ,MAAMD;QAAKL,QAAQT,iBAAiBS,MAAM;QAAEO,WAAW;IAAO;IAEzF,OAAOF;AACT,EAAC"}
@@ -3,7 +3,7 @@ import { buildQuery } from './queries/buildQuery.js';
3
3
  import { buildSortParam } from './queries/buildSortParam.js';
4
4
  import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js';
5
5
  import { getSession } from './utilities/getSession.js';
6
- import { sanitizeInternalFields } from './utilities/sanitizeInternalFields.js';
6
+ import { transform } from './utilities/transform.js';
7
7
  export const findVersions = async function findVersions({ collection, limit, locale, page, pagination, req = {}, select, skip, sort: sortArg, where }) {
8
8
  const Model = this.versions[collection];
9
9
  const collectionConfig = this.payload.collections[collection].config;
@@ -83,14 +83,13 @@ export const findVersions = async function findVersions({ collection, limit, loc
83
83
  }
84
84
  }
85
85
  const result = await Model.paginate(query, paginationOptions);
86
- const docs = JSON.parse(JSON.stringify(result.docs));
87
- return {
88
- ...result,
89
- docs: docs.map((doc)=>{
90
- doc.id = doc._id;
91
- return sanitizeInternalFields(doc);
92
- })
93
- };
86
+ transform({
87
+ adapter: this,
88
+ data: result.docs,
89
+ fields: buildVersionCollectionFields(this.payload.config, collectionConfig),
90
+ operation: 'read'
91
+ });
92
+ return result;
94
93
  };
95
94
 
96
95
  //# sourceMappingURL=findVersions.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/findVersions.ts"],"sourcesContent":["import type { PaginateOptions, QueryOptions } from 'mongoose'\nimport type { FindVersions } from 'payload'\n\nimport { buildVersionCollectionFields, flattenWhereToOperators } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildSortParam } from './queries/buildSortParam.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getSession } from './utilities/getSession.js'\nimport { sanitizeInternalFields } from './utilities/sanitizeInternalFields.js'\n\nexport const findVersions: FindVersions = async function findVersions(\n this: MongooseAdapter,\n { collection, limit, locale, page, pagination, req = {}, select, skip, sort: sortArg, where },\n) {\n const Model = this.versions[collection]\n const collectionConfig = this.payload.collections[collection].config\n const session = await getSession(this, req)\n const options: QueryOptions = {\n limit,\n session,\n skip,\n }\n\n let hasNearConstraint = false\n\n if (where) {\n const constraints = flattenWhereToOperators(where)\n hasNearConstraint = constraints.some((prop) => Object.keys(prop).some((key) => key === 'near'))\n }\n\n let sort\n if (!hasNearConstraint) {\n sort = buildSortParam({\n config: this.payload.config,\n fields: collectionConfig.flattenedFields,\n locale,\n sort: sortArg || '-updatedAt',\n timestamps: true,\n })\n }\n\n const fields = buildVersionCollectionFields(this.payload.config, collectionConfig, true)\n\n const query = await buildQuery({\n adapter: this,\n fields,\n locale,\n where,\n })\n\n // useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters.\n const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0\n const paginationOptions: PaginateOptions = {\n lean: true,\n leanWithId: true,\n limit,\n options,\n page,\n pagination,\n projection: buildProjectionFromSelect({\n adapter: this,\n fields,\n select,\n }),\n sort,\n useEstimatedCount,\n }\n\n if (this.collation) {\n const defaultLocale = 'en'\n paginationOptions.collation = {\n locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,\n ...this.collation,\n }\n }\n\n if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {\n // Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding\n // a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,\n // which makes queries very slow. This only happens when no query (filter) is provided. If one is provided, it uses\n // the correct indexed field\n paginationOptions.useCustomCountFn = () => {\n return Promise.resolve(\n Model.countDocuments(query, {\n hint: { _id: 1 },\n session,\n }),\n )\n }\n }\n\n if (limit >= 0) {\n paginationOptions.limit = limit\n // limit must also be set here, it's ignored when pagination is false\n paginationOptions.options.limit = limit\n\n // Disable pagination if limit is 0\n if (limit === 0) {\n paginationOptions.pagination = false\n }\n }\n\n const result = await Model.paginate(query, paginationOptions)\n const docs = JSON.parse(JSON.stringify(result.docs))\n\n return {\n ...result,\n docs: docs.map((doc) => {\n doc.id = doc._id\n return sanitizeInternalFields(doc)\n }),\n }\n}\n"],"names":["buildVersionCollectionFields","flattenWhereToOperators","buildQuery","buildSortParam","buildProjectionFromSelect","getSession","sanitizeInternalFields","findVersions","collection","limit","locale","page","pagination","req","select","skip","sort","sortArg","where","Model","versions","collectionConfig","payload","collections","config","session","options","hasNearConstraint","constraints","some","prop","Object","keys","key","fields","flattenedFields","timestamps","query","adapter","useEstimatedCount","length","paginationOptions","lean","leanWithId","projection","collation","defaultLocale","disableIndexHints","useCustomCountFn","Promise","resolve","countDocuments","hint","_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F;AACF,EAAC"}
1
+ {"version":3,"sources":["../src/findVersions.ts"],"sourcesContent":["import type { PaginateOptions, QueryOptions } from 'mongoose'\nimport type { FindVersions } from 'payload'\n\nimport { buildVersionCollectionFields, flattenWhereToOperators } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildSortParam } from './queries/buildSortParam.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getSession } from './utilities/getSession.js'\nimport { transform } from './utilities/transform.js'\n\nexport const findVersions: FindVersions = async function findVersions(\n this: MongooseAdapter,\n { collection, limit, locale, page, pagination, req = {}, select, skip, sort: sortArg, where },\n) {\n const Model = this.versions[collection]\n const collectionConfig = this.payload.collections[collection].config\n const session = await getSession(this, req)\n const options: QueryOptions = {\n limit,\n session,\n skip,\n }\n\n let hasNearConstraint = false\n\n if (where) {\n const constraints = flattenWhereToOperators(where)\n hasNearConstraint = constraints.some((prop) => Object.keys(prop).some((key) => key === 'near'))\n }\n\n let sort\n if (!hasNearConstraint) {\n sort = buildSortParam({\n config: this.payload.config,\n fields: collectionConfig.flattenedFields,\n locale,\n sort: sortArg || '-updatedAt',\n timestamps: true,\n })\n }\n\n const fields = buildVersionCollectionFields(this.payload.config, collectionConfig, true)\n\n const query = await buildQuery({\n adapter: this,\n fields,\n locale,\n where,\n })\n\n // useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters.\n const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0\n const paginationOptions: PaginateOptions = {\n lean: true,\n leanWithId: true,\n limit,\n options,\n page,\n pagination,\n projection: buildProjectionFromSelect({\n adapter: this,\n fields,\n select,\n }),\n sort,\n useEstimatedCount,\n }\n\n if (this.collation) {\n const defaultLocale = 'en'\n paginationOptions.collation = {\n locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,\n ...this.collation,\n }\n }\n\n if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {\n // Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding\n // a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,\n // which makes queries very slow. This only happens when no query (filter) is provided. If one is provided, it uses\n // the correct indexed field\n paginationOptions.useCustomCountFn = () => {\n return Promise.resolve(\n Model.countDocuments(query, {\n hint: { _id: 1 },\n session,\n }),\n )\n }\n }\n\n if (limit >= 0) {\n paginationOptions.limit = limit\n // limit must also be set here, it's ignored when pagination is false\n paginationOptions.options.limit = limit\n\n // Disable pagination if limit is 0\n if (limit === 0) {\n paginationOptions.pagination = false\n }\n }\n\n const result = await Model.paginate(query, paginationOptions)\n\n transform({\n adapter: this,\n data: result.docs,\n fields: buildVersionCollectionFields(this.payload.config, collectionConfig),\n operation: 'read',\n })\n\n return result\n}\n"],"names":["buildVersionCollectionFields","flattenWhereToOperators","buildQuery","buildSortParam","buildProjectionFromSelect","getSession","transform","findVersions","collection","limit","locale","page","pagination","req","select","skip","sort","sortArg","where","Model","versions","collectionConfig","payload","collections","config","session","options","hasNearConstraint","constraints","some","prop","Object","keys","key","fields","flattenedFields","timestamps","query","adapter","useEstimatedCount","length","paginationOptions","lean","leanWithId","projection","collation","defaultLocale","disableIndexHints","useCustomCountFn","Promise","resolve","countDocuments","hint","_id","result","paginate","data","docs","operation"],"mappings":"AAGA,SAASA,4BAA4B,EAAEC,uBAAuB,QAAQ,UAAS;AAI/E,SAASC,UAAU,QAAQ,0BAAyB;AACpD,SAASC,cAAc,QAAQ,8BAA6B;AAC5D,SAASC,yBAAyB,QAAQ,2CAA0C;AACpF,SAASC,UAAU,QAAQ,4BAA2B;AACtD,SAASC,SAAS,QAAQ,2BAA0B;AAEpD,OAAO,MAAMC,eAA6B,eAAeA,aAEvD,EAAEC,UAAU,EAAEC,KAAK,EAAEC,MAAM,EAAEC,IAAI,EAAEC,UAAU,EAAEC,MAAM,CAAC,CAAC,EAAEC,MAAM,EAAEC,IAAI,EAAEC,MAAMC,OA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IAAI,CAACN,qBAAqBR,OAAOC,IAAI,CAACK,OAAOG,MAAM,KAAK,KAAK,IAAI,CAACO,iBAAiB,KAAK,MAAM;QAC5F,mHAAmH;QACnH,qHAAqH;QACrH,mHAAmH;QACnH,4BAA4B;QAC5BN,kBAAkBO,gBAAgB,GAAG;YACnC,OAAOC,QAAQC,OAAO,CACpB/B,MAAMgC,cAAc,CAACd,OAAO;gBAC1Be,MAAM;oBAAEC,KAAK;gBAAE;gBACf5B;YACF;QAEJ;IACF;IAEA,IAAIhB,SAAS,GAAG;QACdgC,kBAAkBhC,KAAK,GAAGA;QAC1B,qEAAqE;QACrEgC,kBAAkBf,OAAO,CAACjB,KAAK,GAAGA;QAElC,mCAAmC;QACnC,IAAIA,UAAU,GAAG;YACfgC,kBAAkB7B,UAAU,GAAG;QACjC;IACF;IAEA,MAAM0C,SAAS,MAAMnC,MAAMoC,QAAQ,CAAClB,OAAOI;IAE3CnC,UAAU;QACRgC,SAAS,IAAI;QACbkB,MAAMF,OAAOG,IAAI;QACjBvB,QAAQlC,6BAA6B,IAAI,CAACsB,OAAO,CAACE,MAAM,EAAEH;QAC1DqC,WAAW;IACb;IAEA,OAAOJ;AACT,EAAC"}
@@ -335,6 +335,7 @@ const fieldToSchemaMap = {
335
335
  parentIsLocalized
336
336
  }) && payload.config.localization) {
337
337
  schemaToReturn = {
338
+ _id: false,
338
339
  type: payload.config.localization.localeCodes.reduce((locales, locale)=>{
339
340
  let localeSchema = {};
340
341
  if (hasManyRelations) {
@@ -550,6 +551,7 @@ const fieldToSchemaMap = {
550
551
  parentIsLocalized
551
552
  }) && payload.config.localization) {
552
553
  schemaToReturn = {
554
+ _id: false,
553
555
  type: payload.config.localization.localeCodes.reduce((locales, locale)=>{
554
556
  let localeSchema = {};
555
557
  if (hasManyRelations) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/models/buildSchema.ts"],"sourcesContent":["import type { IndexOptions, Schema, SchemaOptions, SchemaTypeOptions } from 'mongoose'\n\nimport mongoose from 'mongoose'\nimport {\n type ArrayField,\n type BlocksField,\n type CheckboxField,\n type CodeField,\n type CollapsibleField,\n type DateField,\n type EmailField,\n type Field,\n type FieldAffectingData,\n type GroupField,\n type JSONField,\n type NonPresentationalField,\n type NumberField,\n type Payload,\n type PointField,\n type RadioField,\n type RelationshipField,\n type RichTextField,\n type RowField,\n type SanitizedLocalizationConfig,\n type SelectField,\n type Tab,\n type TabsField,\n type TextareaField,\n type TextField,\n type UploadField,\n} from 'payload'\nimport {\n fieldAffectsData,\n fieldIsPresentationalOnly,\n fieldIsVirtual,\n fieldShouldBeLocalized,\n tabHasName,\n} from 'payload/shared'\n\nexport type BuildSchemaOptions = {\n allowIDField?: boolean\n disableUnique?: boolean\n draftsEnabled?: boolean\n indexSortableFields?: boolean\n options?: SchemaOptions\n}\n\ntype FieldSchemaGenerator = (\n field: Field,\n schema: Schema,\n config: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n parentIsLocalized: boolean,\n) => void\n\n/**\n * get a field's defaultValue only if defined and not dynamic so that it can be set on the field schema\n * @param field\n */\nconst formatDefaultValue = (field: FieldAffectingData) =>\n typeof field.defaultValue !== 'undefined' && typeof field.defaultValue !== 'function'\n ? field.defaultValue\n : undefined\n\nconst formatBaseSchema = ({\n buildSchemaOptions,\n field,\n parentIsLocalized,\n}: {\n buildSchemaOptions: BuildSchemaOptions\n field: FieldAffectingData\n parentIsLocalized: boolean\n}) => {\n const { disableUnique, draftsEnabled, indexSortableFields } = buildSchemaOptions\n const schema: SchemaTypeOptions<unknown> = {\n default: formatDefaultValue(field),\n index: field.index || (!disableUnique && field.unique) || indexSortableFields || false,\n required: false,\n unique: (!disableUnique && field.unique) || false,\n }\n\n if (\n schema.unique &&\n (fieldShouldBeLocalized({ field, parentIsLocalized }) ||\n draftsEnabled ||\n (fieldAffectsData(field) &&\n field.type !== 'group' &&\n field.type !== 'tab' &&\n field.required !== true))\n ) {\n schema.sparse = true\n }\n\n if (field.hidden) {\n schema.hidden = true\n }\n\n return schema\n}\n\nconst localizeSchema = (\n entity: NonPresentationalField | Tab,\n schema,\n localization: false | SanitizedLocalizationConfig,\n parentIsLocalized: boolean,\n) => {\n if (\n fieldShouldBeLocalized({ field: entity, parentIsLocalized }) &&\n localization &&\n Array.isArray(localization.locales)\n ) {\n return {\n type: localization.localeCodes.reduce(\n (localeSchema, locale) => ({\n ...localeSchema,\n [locale]: schema,\n }),\n {\n _id: false,\n },\n ),\n localized: true,\n }\n }\n return schema\n}\n\nexport const buildSchema = (args: {\n buildSchemaOptions: BuildSchemaOptions\n configFields: Field[]\n parentIsLocalized?: boolean\n payload: Payload\n}): Schema => {\n const { buildSchemaOptions = {}, configFields, parentIsLocalized, payload } = args\n const { allowIDField, options } = buildSchemaOptions\n let fields = {}\n\n let schemaFields = configFields\n\n if (!allowIDField) {\n const idField = schemaFields.find((field) => fieldAffectsData(field) && field.name === 'id')\n if (idField) {\n fields = {\n _id: idField.type === 'number' ? Number : String,\n }\n schemaFields = schemaFields.filter(\n (field) => !(fieldAffectsData(field) && field.name === 'id'),\n )\n }\n }\n\n const schema = new mongoose.Schema(fields, options)\n\n schemaFields.forEach((field) => {\n if (fieldIsVirtual(field)) {\n return\n }\n\n if (!fieldIsPresentationalOnly(field)) {\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[field.type]\n\n if (addFieldSchema) {\n addFieldSchema(field, schema, payload, buildSchemaOptions, parentIsLocalized)\n }\n }\n })\n\n return schema\n}\n\nconst fieldToSchemaMap: Record<string, FieldSchemaGenerator> = {\n array: (field: ArrayField, schema, payload, buildSchemaOptions, parentIsLocalized) => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: [\n buildSchema({\n buildSchemaOptions: {\n allowIDField: true,\n disableUnique: buildSchemaOptions.disableUnique,\n draftsEnabled: buildSchemaOptions.draftsEnabled,\n options: {\n _id: false,\n id: false,\n minimize: false,\n },\n },\n configFields: field.fields,\n parentIsLocalized: parentIsLocalized || field.localized,\n payload,\n }),\n ],\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n blocks: (field: BlocksField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const fieldSchema = {\n type: [new mongoose.Schema({}, { _id: false, discriminatorKey: 'blockType' })],\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n fieldSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n ;(field.blockReferences ?? field.blocks).forEach((blockItem) => {\n const blockSchema = new mongoose.Schema({}, { _id: false, id: false })\n\n const block = typeof blockItem === 'string' ? payload.blocks[blockItem] : blockItem\n\n block.fields.forEach((blockField) => {\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[blockField.type]\n if (addFieldSchema) {\n addFieldSchema(\n blockField,\n blockSchema,\n payload,\n buildSchemaOptions,\n parentIsLocalized || field.localized,\n )\n }\n })\n\n if (fieldShouldBeLocalized({ field, parentIsLocalized }) && payload.config.localization) {\n payload.config.localization.localeCodes.forEach((localeCode) => {\n // @ts-expect-error Possible incorrect typing in mongoose types, this works\n schema.path(`${field.name}.${localeCode}`).discriminator(block.slug, blockSchema)\n })\n } else {\n // @ts-expect-error Possible incorrect typing in mongoose types, this works\n schema.path(field.name).discriminator(block.slug, blockSchema)\n }\n })\n },\n checkbox: (\n field: CheckboxField,\n schema,\n payload,\n buildSchemaOptions,\n parentIsLocalized,\n ): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: Boolean,\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n code: (field: CodeField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: String,\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n collapsible: (\n field: CollapsibleField,\n schema,\n payload,\n buildSchemaOptions,\n parentIsLocalized,\n ): void => {\n field.fields.forEach((subField: Field) => {\n if (fieldIsVirtual(subField)) {\n return\n }\n\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[subField.type]\n\n if (addFieldSchema) {\n addFieldSchema(subField, schema, payload, buildSchemaOptions, parentIsLocalized)\n }\n })\n },\n date: (field: DateField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: Date,\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n email: (field: EmailField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: String,\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n group: (field: GroupField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const formattedBaseSchema = formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized })\n\n // carry indexSortableFields through to versions if drafts enabled\n const indexSortableFields =\n buildSchemaOptions.indexSortableFields &&\n field.name === 'version' &&\n buildSchemaOptions.draftsEnabled\n\n const baseSchema = {\n ...formattedBaseSchema,\n type: buildSchema({\n buildSchemaOptions: {\n disableUnique: buildSchemaOptions.disableUnique,\n draftsEnabled: buildSchemaOptions.draftsEnabled,\n indexSortableFields,\n options: {\n _id: false,\n id: false,\n minimize: false,\n },\n },\n configFields: field.fields,\n parentIsLocalized: parentIsLocalized || field.localized,\n payload,\n }),\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n json: (field: JSONField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: mongoose.Schema.Types.Mixed,\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n number: (field: NumberField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: field.hasMany ? [Number] : Number,\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n point: (field: PointField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const baseSchema: SchemaTypeOptions<unknown> = {\n type: {\n type: String,\n enum: ['Point'],\n ...(typeof field.defaultValue !== 'undefined' && {\n default: 'Point',\n }),\n },\n coordinates: {\n type: [Number],\n default: formatDefaultValue(field),\n required: false,\n },\n }\n if (\n buildSchemaOptions.disableUnique &&\n field.unique &&\n fieldShouldBeLocalized({ field, parentIsLocalized })\n ) {\n baseSchema.coordinates.sparse = true\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n\n if (field.index === true || field.index === undefined) {\n const indexOptions: IndexOptions = {}\n if (!buildSchemaOptions.disableUnique && field.unique) {\n indexOptions.sparse = true\n indexOptions.unique = true\n }\n if (fieldShouldBeLocalized({ field, parentIsLocalized }) && payload.config.localization) {\n payload.config.localization.locales.forEach((locale) => {\n schema.index({ [`${field.name}.${locale.code}`]: '2dsphere' }, indexOptions)\n })\n } else {\n schema.index({ [field.name]: '2dsphere' }, indexOptions)\n }\n }\n },\n radio: (field: RadioField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: String,\n enum: field.options.map((option) => {\n if (typeof option === 'object') {\n return option.value\n }\n return option\n }),\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n relationship: (\n field: RelationshipField,\n schema,\n payload,\n buildSchemaOptions,\n parentIsLocalized,\n ) => {\n const hasManyRelations = Array.isArray(field.relationTo)\n let schemaToReturn: { [key: string]: any } = {}\n\n const valueType = getRelationshipValueType(field, payload)\n\n if (fieldShouldBeLocalized({ field, parentIsLocalized }) && payload.config.localization) {\n schemaToReturn = {\n type: payload.config.localization.localeCodes.reduce((locales, locale) => {\n let localeSchema: { [key: string]: any } = {}\n\n if (hasManyRelations) {\n localeSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n _id: false,\n type: mongoose.Schema.Types.Mixed,\n relationTo: { type: String, enum: field.relationTo },\n value: {\n type: valueType,\n refPath: `${field.name}.${locale}.relationTo`,\n },\n }\n } else {\n localeSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: valueType,\n ref: field.relationTo,\n }\n }\n\n return {\n ...locales,\n [locale]: field.hasMany\n ? { type: [localeSchema], default: formatDefaultValue(field) }\n : localeSchema,\n }\n }, {}),\n localized: true,\n }\n } else if (hasManyRelations) {\n schemaToReturn = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n _id: false,\n type: mongoose.Schema.Types.Mixed,\n relationTo: { type: String, enum: field.relationTo },\n value: {\n type: valueType,\n refPath: `${field.name}.relationTo`,\n },\n }\n\n if (field.hasMany) {\n schemaToReturn = {\n type: [schemaToReturn],\n default: formatDefaultValue(field),\n }\n }\n } else {\n schemaToReturn = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: valueType,\n ref: field.relationTo,\n }\n\n if (field.hasMany) {\n schemaToReturn = {\n type: [schemaToReturn],\n default: formatDefaultValue(field),\n }\n }\n }\n\n schema.add({\n [field.name]: schemaToReturn,\n })\n },\n richText: (\n field: RichTextField,\n schema,\n payload,\n buildSchemaOptions,\n parentIsLocalized,\n ): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: mongoose.Schema.Types.Mixed,\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n row: (field: RowField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n field.fields.forEach((subField: Field) => {\n if (fieldIsVirtual(subField)) {\n return\n }\n\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[subField.type]\n\n if (addFieldSchema) {\n addFieldSchema(subField, schema, payload, buildSchemaOptions, parentIsLocalized)\n }\n })\n },\n select: (field: SelectField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: String,\n enum: field.options.map((option) => {\n if (typeof option === 'object') {\n return option.value\n }\n return option\n }),\n }\n\n if (buildSchemaOptions.draftsEnabled || !field.required) {\n baseSchema.enum.push(null)\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n field.hasMany ? [baseSchema] : baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n tabs: (field: TabsField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n field.tabs.forEach((tab) => {\n if (tabHasName(tab)) {\n if (fieldIsVirtual(tab)) {\n return\n }\n const baseSchema = {\n type: buildSchema({\n buildSchemaOptions: {\n disableUnique: buildSchemaOptions.disableUnique,\n draftsEnabled: buildSchemaOptions.draftsEnabled,\n options: {\n _id: false,\n id: false,\n minimize: false,\n },\n },\n configFields: tab.fields,\n parentIsLocalized: parentIsLocalized || tab.localized,\n payload,\n }),\n }\n\n schema.add({\n [tab.name]: localizeSchema(\n tab,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n } else {\n tab.fields.forEach((subField: Field) => {\n if (fieldIsVirtual(subField)) {\n return\n }\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[subField.type]\n\n if (addFieldSchema) {\n addFieldSchema(\n subField,\n schema,\n payload,\n buildSchemaOptions,\n parentIsLocalized || tab.localized,\n )\n }\n })\n }\n })\n },\n text: (field: TextField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: field.hasMany ? [String] : String,\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n textarea: (\n field: TextareaField,\n schema,\n payload,\n buildSchemaOptions,\n parentIsLocalized,\n ): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: String,\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n upload: (field: UploadField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const hasManyRelations = Array.isArray(field.relationTo)\n let schemaToReturn: { [key: string]: any } = {}\n\n const valueType = getRelationshipValueType(field, payload)\n\n if (fieldShouldBeLocalized({ field, parentIsLocalized }) && payload.config.localization) {\n schemaToReturn = {\n type: payload.config.localization.localeCodes.reduce((locales, locale) => {\n let localeSchema: { [key: string]: any } = {}\n\n if (hasManyRelations) {\n localeSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n _id: false,\n type: mongoose.Schema.Types.Mixed,\n relationTo: { type: String, enum: field.relationTo },\n value: {\n type: valueType,\n refPath: `${field.name}.${locale}.relationTo`,\n },\n }\n } else {\n localeSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: valueType,\n ref: field.relationTo,\n }\n }\n\n return {\n ...locales,\n [locale]: field.hasMany\n ? { type: [localeSchema], default: formatDefaultValue(field) }\n : localeSchema,\n }\n }, {}),\n localized: true,\n }\n } else if (hasManyRelations) {\n schemaToReturn = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n _id: false,\n type: mongoose.Schema.Types.Mixed,\n relationTo: { type: String, enum: field.relationTo },\n value: {\n type: valueType,\n refPath: `${field.name}.relationTo`,\n },\n }\n\n if (field.hasMany) {\n schemaToReturn = {\n type: [schemaToReturn],\n default: formatDefaultValue(field),\n }\n }\n } else {\n schemaToReturn = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: valueType,\n ref: field.relationTo,\n }\n\n if (field.hasMany) {\n schemaToReturn = {\n type: [schemaToReturn],\n default: formatDefaultValue(field),\n }\n }\n }\n\n schema.add({\n [field.name]: schemaToReturn,\n })\n },\n}\n\nconst getRelationshipValueType = (field: RelationshipField | UploadField, payload: Payload) => {\n if (typeof field.relationTo === 'string') {\n const { customIDType } = payload.collections[field.relationTo]\n\n if (!customIDType) {\n return mongoose.Schema.Types.ObjectId\n }\n\n if (customIDType === 'number') {\n return mongoose.Schema.Types.Number\n }\n\n return mongoose.Schema.Types.String\n }\n\n // has custom id relationTo\n if (\n field.relationTo.some((relationTo) => {\n return !!payload.collections[relationTo].customIDType\n })\n ) {\n return mongoose.Schema.Types.Mixed\n }\n\n return mongoose.Schema.Types.ObjectId\n}\n"],"names":["mongoose","fieldAffectsData","fieldIsPresentationalOnly","fieldIsVirtual","fieldShouldBeLocalized","tabHasName","formatDefaultValue","field","defaultValue","undefined","formatBaseSchema","buildSchemaOptions","parentIsLocalized","disableUnique","draftsEnabled","indexSortableFields","schema","default","index","unique","required","type","sparse","hidden","localizeSchema","entity","localization","Array","isArray","locales","localeCodes","reduce","localeSchema","locale","_id","localized","buildSchema","args","configFields","payload","allowIDField","options","fields","schemaFields","idField","find","name","Number","String","filter","Schema","forEach","addFieldSchema","fieldToSchemaMap","array","baseSchema","id","minimize","add","config","blocks","fieldSchema","discriminatorKey","blockReferences","blockItem","blockSchema","block","blockField","localeCode","path","discriminator","slug","checkbox","Boolean","code","collapsible","subField","date","Date","email","group","formattedBaseSchema","json","Types","Mixed","number","hasMany","point","enum","coordinates","indexOptions","radio","map","option","value","relationship","hasManyRelations","relationTo","schemaToReturn","valueType","getRelationshipValueType","refPath","ref","richText","row","select","push","tabs","tab","text","textarea","upload","customIDType","collections","ObjectId","some"],"mappings":"AAEA,OAAOA,cAAc,WAAU;AA6B/B,SACEC,gBAAgB,EAChBC,yBAAyB,EACzBC,cAAc,EACdC,sBAAsB,EACtBC,UAAU,QACL,iBAAgB;AAkBvB;;;CAGC,GACD,MAAMC,qBAAqB,CAACC,QAC1B,OAAOA,MAAMC,YAAY,KAAK,eAAe,OAAOD,MAAMC,YAAY,KAAK,aACvED,MAAMC,YAAY,GAClBC;AAEN,MAAMC,mBAAmB,CAAC,EACxBC,kBAAkB,EAClBJ,KAAK,EACLK,iBAAiB,EAKlB;IACC,MAAM,EAAEC,aAAa,EAAEC,aAAa,EAAEC,mBAAmB,EAAE,GAAGJ;IAC9D,MAAMK,SAAqC;QACzCC,SAASX,mBAAmBC;QAC5BW,OAAOX,MAAMW,KAAK,IAAK,CAACL,iBAAiBN,MAAMY,MAAM,IAAKJ,uBAAuB;QACjFK,UAAU;QACVD,QAAQ,AAAC,CAACN,iBAAiBN,MAAMY,MAAM,IAAK;IAC9C;IAEA,IACEH,OAAOG,MAAM,IACZf,CAAAA,uBAAuB;QAAEG;QAAOK;IAAkB,MACjDE,iBACCb,iBAAiBM,UAChBA,MAAMc,IAAI,KAAK,WACfd,MAAMc,IAAI,KAAK,SACfd,MAAMa,QAAQ,KAAK,IAAI,GAC3B;QACAJ,OAAOM,MAAM,GAAG;IAClB;IAEA,IAAIf,MAAMgB,MAAM,EAAE;QAChBP,OAAOO,MAAM,GAAG;IAClB;IAEA,OAAOP;AACT;AAEA,MAAMQ,iBAAiB,CACrBC,QACAT,QACAU,cACAd;IAEA,IACER,uBAAuB;QAAEG,OAAOkB;QAAQb;IAAkB,MAC1Dc,gBACAC,MAAMC,OAAO,CAACF,aAAaG,OAAO,GAClC;QACA,OAAO;YACLR,MAAMK,aAAaI,W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IAAIvC,MAAM+E,OAAO,EAAE;gBACjBY,iBAAiB;oBACf7E,MAAM;wBAAC6E;qBAAe;oBACtBjF,SAASX,mBAAmBC;gBAC9B;YACF;QACF,OAAO;YACL2F,iBAAiB;gBACf,GAAGxF,iBAAiB;oBAAEC;oBAAoBJ;oBAAOK;gBAAkB,EAAE;gBACrES,MAAM8E;gBACNG,KAAK/F,MAAM0F,UAAU;YACvB;YAEA,IAAI1F,MAAM+E,OAAO,EAAE;gBACjBY,iBAAiB;oBACf7E,MAAM;wBAAC6E;qBAAe;oBACtBjF,SAASX,mBAAmBC;gBAC9B;YACF;QACF;QAEAS,OAAO0C,GAAG,CAAC;YACT,CAACnD,MAAMuC,IAAI,CAAC,EAAEoD;QAChB;IACF;AACF;AAEA,MAAME,2BAA2B,CAAC7F,OAAwCgC;IACxE,IAAI,OAAOhC,MAAM0F,UAAU,KAAK,UAAU;QACxC,MAAM,EAAEe,YAAY,EAAE,GAAGzE,QAAQ0E,WAAW,CAAC1G,MAAM0F,UAAU,CAAC;QAE9D,IAAI,CAACe,cAAc;YACjB,OAAOhH,SAASkD,MAAM,CAACiC,KAAK,CAAC+B,QAAQ;QACvC;QAEA,IAAIF,iBAAiB,UAAU;YAC7B,OAAOhH,SAASkD,MAAM,CAACiC,KAAK,CAACpC,MAAM;QACrC;QAEA,OAAO/C,SAASkD,MAAM,CAACiC,KAAK,CAACnC,MAAM;IACrC;IAEA,2BAA2B;IAC3B,IACEzC,MAAM0F,UAAU,CAACkB,IAAI,CAAC,CAAClB;QACrB,OAAO,CAAC,CAAC1D,QAAQ0E,WAAW,CAAChB,WAAW,CAACe,YAAY;IACvD,IACA;QACA,OAAOhH,SAASkD,MAAM,CAACiC,KAAK,CAACC,KAAK;IACpC;IAEA,OAAOpF,SAASkD,MAAM,CAACiC,KAAK,CAAC+B,QAAQ;AACvC"}
1
+ {"version":3,"sources":["../../src/models/buildSchema.ts"],"sourcesContent":["import type { IndexOptions, Schema, SchemaOptions, SchemaTypeOptions } from 'mongoose'\n\nimport mongoose from 'mongoose'\nimport {\n type ArrayField,\n type BlocksField,\n type CheckboxField,\n type CodeField,\n type CollapsibleField,\n type DateField,\n type EmailField,\n type Field,\n type FieldAffectingData,\n type GroupField,\n type JSONField,\n type NonPresentationalField,\n type NumberField,\n type Payload,\n type PointField,\n type RadioField,\n type RelationshipField,\n type RichTextField,\n type RowField,\n type SanitizedLocalizationConfig,\n type SelectField,\n type Tab,\n type TabsField,\n type TextareaField,\n type TextField,\n type UploadField,\n} from 'payload'\nimport {\n fieldAffectsData,\n fieldIsPresentationalOnly,\n fieldIsVirtual,\n fieldShouldBeLocalized,\n tabHasName,\n} from 'payload/shared'\n\nexport type BuildSchemaOptions = {\n allowIDField?: boolean\n disableUnique?: boolean\n draftsEnabled?: boolean\n indexSortableFields?: boolean\n options?: SchemaOptions\n}\n\ntype FieldSchemaGenerator = (\n field: Field,\n schema: Schema,\n config: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n parentIsLocalized: boolean,\n) => void\n\n/**\n * get a field's defaultValue only if defined and not dynamic so that it can be set on the field schema\n * @param field\n */\nconst formatDefaultValue = (field: FieldAffectingData) =>\n typeof field.defaultValue !== 'undefined' && typeof field.defaultValue !== 'function'\n ? field.defaultValue\n : undefined\n\nconst formatBaseSchema = ({\n buildSchemaOptions,\n field,\n parentIsLocalized,\n}: {\n buildSchemaOptions: BuildSchemaOptions\n field: FieldAffectingData\n parentIsLocalized: boolean\n}) => {\n const { disableUnique, draftsEnabled, indexSortableFields } = buildSchemaOptions\n const schema: SchemaTypeOptions<unknown> = {\n default: formatDefaultValue(field),\n index: field.index || (!disableUnique && field.unique) || indexSortableFields || false,\n required: false,\n unique: (!disableUnique && field.unique) || false,\n }\n\n if (\n schema.unique &&\n (fieldShouldBeLocalized({ field, parentIsLocalized }) ||\n draftsEnabled ||\n (fieldAffectsData(field) &&\n field.type !== 'group' &&\n field.type !== 'tab' &&\n field.required !== true))\n ) {\n schema.sparse = true\n }\n\n if (field.hidden) {\n schema.hidden = true\n }\n\n return schema\n}\n\nconst localizeSchema = (\n entity: NonPresentationalField | Tab,\n schema,\n localization: false | SanitizedLocalizationConfig,\n parentIsLocalized: boolean,\n) => {\n if (\n fieldShouldBeLocalized({ field: entity, parentIsLocalized }) &&\n localization &&\n Array.isArray(localization.locales)\n ) {\n return {\n type: localization.localeCodes.reduce(\n (localeSchema, locale) => ({\n ...localeSchema,\n [locale]: schema,\n }),\n {\n _id: false,\n },\n ),\n localized: true,\n }\n }\n return schema\n}\n\nexport const buildSchema = (args: {\n buildSchemaOptions: BuildSchemaOptions\n configFields: Field[]\n parentIsLocalized?: boolean\n payload: Payload\n}): Schema => {\n const { buildSchemaOptions = {}, configFields, parentIsLocalized, payload } = args\n const { allowIDField, options } = buildSchemaOptions\n let fields = {}\n\n let schemaFields = configFields\n\n if (!allowIDField) {\n const idField = schemaFields.find((field) => fieldAffectsData(field) && field.name === 'id')\n if (idField) {\n fields = {\n _id: idField.type === 'number' ? Number : String,\n }\n schemaFields = schemaFields.filter(\n (field) => !(fieldAffectsData(field) && field.name === 'id'),\n )\n }\n }\n\n const schema = new mongoose.Schema(fields, options)\n\n schemaFields.forEach((field) => {\n if (fieldIsVirtual(field)) {\n return\n }\n\n if (!fieldIsPresentationalOnly(field)) {\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[field.type]\n\n if (addFieldSchema) {\n addFieldSchema(field, schema, payload, buildSchemaOptions, parentIsLocalized)\n }\n }\n })\n\n return schema\n}\n\nconst fieldToSchemaMap: Record<string, FieldSchemaGenerator> = {\n array: (field: ArrayField, schema, payload, buildSchemaOptions, parentIsLocalized) => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: [\n buildSchema({\n buildSchemaOptions: {\n allowIDField: true,\n disableUnique: buildSchemaOptions.disableUnique,\n draftsEnabled: buildSchemaOptions.draftsEnabled,\n options: {\n _id: false,\n id: false,\n minimize: false,\n },\n },\n configFields: field.fields,\n parentIsLocalized: parentIsLocalized || field.localized,\n payload,\n }),\n ],\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n blocks: (field: BlocksField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const fieldSchema = {\n type: [new mongoose.Schema({}, { _id: false, discriminatorKey: 'blockType' })],\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n fieldSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n ;(field.blockReferences ?? field.blocks).forEach((blockItem) => {\n const blockSchema = new mongoose.Schema({}, { _id: false, id: false })\n\n const block = typeof blockItem === 'string' ? payload.blocks[blockItem] : blockItem\n\n block.fields.forEach((blockField) => {\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[blockField.type]\n if (addFieldSchema) {\n addFieldSchema(\n blockField,\n blockSchema,\n payload,\n buildSchemaOptions,\n parentIsLocalized || field.localized,\n )\n }\n })\n\n if (fieldShouldBeLocalized({ field, parentIsLocalized }) && payload.config.localization) {\n payload.config.localization.localeCodes.forEach((localeCode) => {\n // @ts-expect-error Possible incorrect typing in mongoose types, this works\n schema.path(`${field.name}.${localeCode}`).discriminator(block.slug, blockSchema)\n })\n } else {\n // @ts-expect-error Possible incorrect typing in mongoose types, this works\n schema.path(field.name).discriminator(block.slug, blockSchema)\n }\n })\n },\n checkbox: (\n field: CheckboxField,\n schema,\n payload,\n buildSchemaOptions,\n parentIsLocalized,\n ): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: Boolean,\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n code: (field: CodeField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: String,\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n collapsible: (\n field: CollapsibleField,\n schema,\n payload,\n buildSchemaOptions,\n parentIsLocalized,\n ): void => {\n field.fields.forEach((subField: Field) => {\n if (fieldIsVirtual(subField)) {\n return\n }\n\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[subField.type]\n\n if (addFieldSchema) {\n addFieldSchema(subField, schema, payload, buildSchemaOptions, parentIsLocalized)\n }\n })\n },\n date: (field: DateField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: Date,\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n email: (field: EmailField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: String,\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n group: (field: GroupField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const formattedBaseSchema = formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized })\n\n // carry indexSortableFields through to versions if drafts enabled\n const indexSortableFields =\n buildSchemaOptions.indexSortableFields &&\n field.name === 'version' &&\n buildSchemaOptions.draftsEnabled\n\n const baseSchema = {\n ...formattedBaseSchema,\n type: buildSchema({\n buildSchemaOptions: {\n disableUnique: buildSchemaOptions.disableUnique,\n draftsEnabled: buildSchemaOptions.draftsEnabled,\n indexSortableFields,\n options: {\n _id: false,\n id: false,\n minimize: false,\n },\n },\n configFields: field.fields,\n parentIsLocalized: parentIsLocalized || field.localized,\n payload,\n }),\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n json: (field: JSONField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: mongoose.Schema.Types.Mixed,\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n number: (field: NumberField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: field.hasMany ? [Number] : Number,\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n point: (field: PointField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const baseSchema: SchemaTypeOptions<unknown> = {\n type: {\n type: String,\n enum: ['Point'],\n ...(typeof field.defaultValue !== 'undefined' && {\n default: 'Point',\n }),\n },\n coordinates: {\n type: [Number],\n default: formatDefaultValue(field),\n required: false,\n },\n }\n if (\n buildSchemaOptions.disableUnique &&\n field.unique &&\n fieldShouldBeLocalized({ field, parentIsLocalized })\n ) {\n baseSchema.coordinates.sparse = true\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n\n if (field.index === true || field.index === undefined) {\n const indexOptions: IndexOptions = {}\n if (!buildSchemaOptions.disableUnique && field.unique) {\n indexOptions.sparse = true\n indexOptions.unique = true\n }\n if (fieldShouldBeLocalized({ field, parentIsLocalized }) && payload.config.localization) {\n payload.config.localization.locales.forEach((locale) => {\n schema.index({ [`${field.name}.${locale.code}`]: '2dsphere' }, indexOptions)\n })\n } else {\n schema.index({ [field.name]: '2dsphere' }, indexOptions)\n }\n }\n },\n radio: (field: RadioField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: String,\n enum: field.options.map((option) => {\n if (typeof option === 'object') {\n return option.value\n }\n return option\n }),\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n relationship: (\n field: RelationshipField,\n schema,\n payload,\n buildSchemaOptions,\n parentIsLocalized,\n ) => {\n const hasManyRelations = Array.isArray(field.relationTo)\n let schemaToReturn: { [key: string]: any } = {}\n\n const valueType = getRelationshipValueType(field, payload)\n\n if (fieldShouldBeLocalized({ field, parentIsLocalized }) && payload.config.localization) {\n schemaToReturn = {\n _id: false,\n type: payload.config.localization.localeCodes.reduce((locales, locale) => {\n let localeSchema: { [key: string]: any } = {}\n\n if (hasManyRelations) {\n localeSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n _id: false,\n type: mongoose.Schema.Types.Mixed,\n relationTo: { type: String, enum: field.relationTo },\n value: {\n type: valueType,\n refPath: `${field.name}.${locale}.relationTo`,\n },\n }\n } else {\n localeSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: valueType,\n ref: field.relationTo,\n }\n }\n\n return {\n ...locales,\n [locale]: field.hasMany\n ? { type: [localeSchema], default: formatDefaultValue(field) }\n : localeSchema,\n }\n }, {}),\n localized: true,\n }\n } else if (hasManyRelations) {\n schemaToReturn = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n _id: false,\n type: mongoose.Schema.Types.Mixed,\n relationTo: { type: String, enum: field.relationTo },\n value: {\n type: valueType,\n refPath: `${field.name}.relationTo`,\n },\n }\n\n if (field.hasMany) {\n schemaToReturn = {\n type: [schemaToReturn],\n default: formatDefaultValue(field),\n }\n }\n } else {\n schemaToReturn = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: valueType,\n ref: field.relationTo,\n }\n\n if (field.hasMany) {\n schemaToReturn = {\n type: [schemaToReturn],\n default: formatDefaultValue(field),\n }\n }\n }\n\n schema.add({\n [field.name]: schemaToReturn,\n })\n },\n richText: (\n field: RichTextField,\n schema,\n payload,\n buildSchemaOptions,\n parentIsLocalized,\n ): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: mongoose.Schema.Types.Mixed,\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n row: (field: RowField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n field.fields.forEach((subField: Field) => {\n if (fieldIsVirtual(subField)) {\n return\n }\n\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[subField.type]\n\n if (addFieldSchema) {\n addFieldSchema(subField, schema, payload, buildSchemaOptions, parentIsLocalized)\n }\n })\n },\n select: (field: SelectField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: String,\n enum: field.options.map((option) => {\n if (typeof option === 'object') {\n return option.value\n }\n return option\n }),\n }\n\n if (buildSchemaOptions.draftsEnabled || !field.required) {\n baseSchema.enum.push(null)\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n field.hasMany ? [baseSchema] : baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n tabs: (field: TabsField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n field.tabs.forEach((tab) => {\n if (tabHasName(tab)) {\n if (fieldIsVirtual(tab)) {\n return\n }\n const baseSchema = {\n type: buildSchema({\n buildSchemaOptions: {\n disableUnique: buildSchemaOptions.disableUnique,\n draftsEnabled: buildSchemaOptions.draftsEnabled,\n options: {\n _id: false,\n id: false,\n minimize: false,\n },\n },\n configFields: tab.fields,\n parentIsLocalized: parentIsLocalized || tab.localized,\n payload,\n }),\n }\n\n schema.add({\n [tab.name]: localizeSchema(\n tab,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n } else {\n tab.fields.forEach((subField: Field) => {\n if (fieldIsVirtual(subField)) {\n return\n }\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[subField.type]\n\n if (addFieldSchema) {\n addFieldSchema(\n subField,\n schema,\n payload,\n buildSchemaOptions,\n parentIsLocalized || tab.localized,\n )\n }\n })\n }\n })\n },\n text: (field: TextField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: field.hasMany ? [String] : String,\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n textarea: (\n field: TextareaField,\n schema,\n payload,\n buildSchemaOptions,\n parentIsLocalized,\n ): void => {\n const baseSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: String,\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n baseSchema,\n payload.config.localization,\n parentIsLocalized,\n ),\n })\n },\n upload: (field: UploadField, schema, payload, buildSchemaOptions, parentIsLocalized): void => {\n const hasManyRelations = Array.isArray(field.relationTo)\n let schemaToReturn: { [key: string]: any } = {}\n\n const valueType = getRelationshipValueType(field, payload)\n\n if (fieldShouldBeLocalized({ field, parentIsLocalized }) && payload.config.localization) {\n schemaToReturn = {\n _id: false,\n type: payload.config.localization.localeCodes.reduce((locales, locale) => {\n let localeSchema: { [key: string]: any } = {}\n\n if (hasManyRelations) {\n localeSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n _id: false,\n type: mongoose.Schema.Types.Mixed,\n relationTo: { type: String, enum: field.relationTo },\n value: {\n type: valueType,\n refPath: `${field.name}.${locale}.relationTo`,\n },\n }\n } else {\n localeSchema = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: valueType,\n ref: field.relationTo,\n }\n }\n\n return {\n ...locales,\n [locale]: field.hasMany\n ? { type: [localeSchema], default: formatDefaultValue(field) }\n : localeSchema,\n }\n }, {}),\n localized: true,\n }\n } else if (hasManyRelations) {\n schemaToReturn = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n _id: false,\n type: mongoose.Schema.Types.Mixed,\n relationTo: { type: String, enum: field.relationTo },\n value: {\n type: valueType,\n refPath: `${field.name}.relationTo`,\n },\n }\n\n if (field.hasMany) {\n schemaToReturn = {\n type: [schemaToReturn],\n default: formatDefaultValue(field),\n }\n }\n } else {\n schemaToReturn = {\n ...formatBaseSchema({ buildSchemaOptions, field, parentIsLocalized }),\n type: valueType,\n ref: field.relationTo,\n }\n\n if (field.hasMany) {\n schemaToReturn = {\n type: [schemaToReturn],\n default: formatDefaultValue(field),\n }\n }\n }\n\n schema.add({\n [field.name]: schemaToReturn,\n })\n },\n}\n\nconst getRelationshipValueType = (field: RelationshipField | UploadField, payload: Payload) => {\n if (typeof field.relationTo === 'string') {\n const { customIDType } = payload.collections[field.relationTo]\n\n if (!customIDType) {\n return mongoose.Schema.Types.ObjectId\n }\n\n if (customIDType === 'number') {\n return mongoose.Schema.Types.Number\n }\n\n return mongoose.Schema.Types.String\n }\n\n // has custom id relationTo\n if (\n field.relationTo.some((relationTo) => {\n return !!payload.collections[relationTo].customIDType\n })\n ) {\n return mongoose.Schema.Types.Mixed\n }\n\n return mongoose.Schema.Types.ObjectId\n}\n"],"names":["mongoose","fieldAffectsData","fieldIsPresentationalOnly","fieldIsVirtual","fieldShouldBeLocalized","tabHasName","formatDefaultValue","field","defaultValue","undefined","formatBaseSchema","buildSchemaOptions","parentIsLocalized","disableUnique","draftsEnabled","indexSortableFields","schema","default","index","unique","required","type","sparse","hidden","localizeSchema","entity","localization","Array","isArray","locales","localeCodes","reduce","localeSchema","locale","_id","localized","buildSchema","args","configFields","payload","allowIDField","options","fields","schemaFields","idField","find","name","Number","String","filter","Schema","forEach","addFieldSchema","fieldToSchemaMap","array","baseSchema","id","minimize","add","config","blocks","fieldSchema","discriminatorKey","blockReferences","blockItem","blockSchema","block","blockField","localeCode","path","discriminator","slug","checkbox","Boolean","code","collapsible","subField","date","Date","email","group","formattedBaseSchema","json","Types","Mixed","number","hasMany","point","enum","coordinates","indexOptions","radio","map","option","value","relationship","hasManyRelations","relationTo","schemaToReturn","valueType","getRelationshipValueType","refPath","ref","richText","row","select","push","tabs","tab","text","textarea","upload","customIDType","collections","ObjectId","some"],"mappings":"AAEA,OAAOA,cAAc,WAAU;AA6B/B,SACEC,gBAAgB,EAChBC,yBAAyB,EACzBC,cAAc,EACdC,sBAAsB,EACtBC,UAAU,QACL,iBAAgB;AAkBvB;;;CAGC,GACD,MAAMC,qBAAqB,CAACC,QAC1B,OAAOA,MAAMC,YAAY,KAAK,eAAe,OAAOD,MAAMC,YAAY,KAAK,aACvED,MAAMC,YAAY,GAClBC;AAEN,MAAMC,mBAAmB,CAAC,EACxBC,kBAAkB,EAClBJ,KAAK,EACLK,iBAAiB,EAKlB;IACC,MAAM,EAAEC,aAAa,EAAEC,aAAa,EAAEC,mBAAmB,EAAE,GAAGJ;IAC9D,MAAMK,SAAqC;QACzCC,SAASX,mBAAmBC;QAC5BW,OAAOX,MAAMW,KAAK,IAAK,CAACL,iBAAiBN,MAAMY,MAAM,IAAKJ,uBAAuB;QACjFK,UAAU;QACVD,QAAQ,AAAC,CAACN,iBAAiBN,MAAMY,MAAM,IAAK;IAC9C;IAEA,IACEH,OAAOG,MAAM,IACZf,CAAAA,uBAAuB;QAAEG;QAAOK;IAAkB,MACjDE,iBACCb,iBAAiBM,UAChBA,MAAMc,IAAI,KAAK,WACfd,MAAMc,IAAI,KAAK,SACfd,MAAMa,QAAQ,KAAK,IAAI,GAC3B;QACAJ,OAAOM,MAAM,GAAG;IAClB;IAEA,IAAIf,MAAMgB,MAAM,EAAE;QAChBP,OAAOO,MAAM,GAAG;IAClB;IAEA,OAAOP;AACT;AAEA,MAAMQ,iBAAiB,CACrBC,QACAT,QACAU,cACAd;IAEA,IACER,uBAAuB;QAAEG,OAAOkB;QAAQb;IAAkB,MAC1Dc,gBACAC,MAAMC,OAAO,CAACF,aAAaG,OAAO,GAClC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"file":"migrateRelationshipsV2_V3.d.ts","sourceRoot":"","sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAS,cAAc,EAAmB,MAAM,SAAS,CAAA;AAyGrE,wBAAsB,yBAAyB,CAAC,EAC9C,SAAS,EACT,GAAG,GACJ,EAAE;IACD,SAAS,EAAE,MAAM,CAAA;IACjB,GAAG,EAAE,cAAc,CAAA;CACpB,GAAG,OAAO,CAAC,IAAI,CAAC,CA0FhB"}
1
+ {"version":3,"file":"migrateRelationshipsV2_V3.d.ts","sourceRoot":"","sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAS,cAAc,EAAmB,MAAM,SAAS,CAAA;AA2GrE,wBAAsB,yBAAyB,CAAC,EAC9C,SAAS,EACT,GAAG,GACJ,EAAE;IACD,SAAS,EAAE,MAAM,CAAA;IACjB,GAAG,EAAE,cAAc,CAAA;CACpB,GAAG,OAAO,CAAC,IAAI,CAAC,CA8FhB"}
@@ -1,7 +1,7 @@
1
1
  import { buildVersionCollectionFields, buildVersionGlobalFields } from 'payload';
2
2
  import { getSession } from '../utilities/getSession.js';
3
- import { sanitizeRelationshipIDs } from '../utilities/sanitizeRelationshipIDs.js';
4
- const migrateModelWithBatching = async ({ batchSize, config, fields, Model, parentIsLocalized, session })=>{
3
+ import { transform } from '../utilities/transform.js';
4
+ const migrateModelWithBatching = async ({ batchSize, config, db, fields, Model, parentIsLocalized, session })=>{
5
5
  let hasNext = true;
6
6
  let skip = 0;
7
7
  while(hasNext){
@@ -19,10 +19,11 @@ const migrateModelWithBatching = async ({ batchSize, config, fields, Model, pare
19
19
  docs.pop();
20
20
  }
21
21
  for (const doc of docs){
22
- sanitizeRelationshipIDs({
23
- config,
22
+ transform({
23
+ adapter: db,
24
24
  data: doc,
25
25
  fields,
26
+ operation: 'write',
26
27
  parentIsLocalized
27
28
  });
28
29
  }
@@ -88,6 +89,7 @@ export async function migrateRelationshipsV2_V3({ batchSize, req }) {
88
89
  await migrateModelWithBatching({
89
90
  batchSize,
90
91
  config,
92
+ db,
91
93
  fields: collection.fields,
92
94
  Model: db.collections[collection.slug],
93
95
  parentIsLocalized: false,
@@ -100,6 +102,7 @@ export async function migrateRelationshipsV2_V3({ batchSize, req }) {
100
102
  await migrateModelWithBatching({
101
103
  batchSize,
102
104
  config,
105
+ db,
103
106
  fields: buildVersionCollectionFields(config, collection),
104
107
  Model: db.versions[collection.slug],
105
108
  parentIsLocalized: false,
@@ -122,10 +125,11 @@ export async function migrateRelationshipsV2_V3({ batchSize, req }) {
122
125
  });
123
126
  // in case if the global doesn't exist in the database yet (not saved)
124
127
  if (doc) {
125
- sanitizeRelationshipIDs({
126
- config,
128
+ transform({
129
+ adapter: db,
127
130
  data: doc,
128
- fields: global.fields
131
+ fields: global.fields,
132
+ operation: 'write'
129
133
  });
130
134
  await GlobalsModel.collection.updateOne({
131
135
  globalType: global.slug
@@ -142,6 +146,7 @@ export async function migrateRelationshipsV2_V3({ batchSize, req }) {
142
146
  await migrateModelWithBatching({
143
147
  batchSize,
144
148
  config,
149
+ db,
145
150
  fields: buildVersionGlobalFields(config, global),
146
151
  Model: db.versions[global.slug],
147
152
  parentIsLocalized: false,
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"sourcesContent":["import type { ClientSession, Model } from 'mongoose'\nimport type { Field, PayloadRequest, SanitizedConfig } from 'payload'\n\nimport { buildVersionCollectionFields, buildVersionGlobalFields } from 'payload'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { getSession } from '../utilities/getSession.js'\nimport { sanitizeRelationshipIDs } from '../utilities/sanitizeRelationshipIDs.js'\n\nconst migrateModelWithBatching = async ({\n batchSize,\n config,\n fields,\n Model,\n parentIsLocalized,\n session,\n}: {\n batchSize: number\n config: SanitizedConfig\n fields: Field[]\n Model: Model<any>\n parentIsLocalized: boolean\n session: ClientSession\n}): Promise<void> => {\n let hasNext = true\n let skip = 0\n\n while (hasNext) {\n const docs = await Model.find(\n {},\n {},\n {\n lean: true,\n limit: batchSize + 1,\n session,\n skip,\n },\n )\n\n if (docs.length === 0) {\n break\n }\n\n hasNext = docs.length > batchSize\n\n if (hasNext) {\n docs.pop()\n }\n\n for (const doc of docs) {\n sanitizeRelationshipIDs({ config, data: doc, fields, parentIsLocalized })\n }\n\n await Model.collection.bulkWrite(\n docs.map((doc) => ({\n updateOne: {\n filter: { _id: doc._id },\n update: {\n $set: doc,\n },\n },\n })),\n { session },\n )\n\n skip += batchSize\n }\n}\n\nconst hasRelationshipOrUploadField = ({ fields }: { fields: Field[] }): boolean => {\n for (const field of fields) {\n if (field.type === 'relationship' || field.type === 'upload') {\n return true\n }\n\n if ('fields' in field) {\n if (hasRelationshipOrUploadField({ fields: field.fields })) {\n return true\n }\n }\n\n if ('blocks' in field) {\n for (const block of field.blocks) {\n if (typeof block === 'string') {\n // Skip - string blocks have been added in v3 and thus don't need to be migrated\n continue\n }\n if (hasRelationshipOrUploadField({ fields: block.fields })) {\n return true\n }\n }\n }\n\n if ('tabs' in field) {\n for (const tab of field.tabs) {\n if (hasRelationshipOrUploadField({ fields: tab.fields })) {\n return true\n }\n }\n }\n }\n\n return false\n}\n\nexport async function migrateRelationshipsV2_V3({\n batchSize,\n req,\n}: {\n batchSize: number\n req: PayloadRequest\n}): Promise<void> {\n const { payload } = req\n const db = payload.db as MongooseAdapter\n const config = payload.config\n\n const session = await getSession(db, req)\n\n for (const collection of payload.config.collections) {\n if (hasRelationshipOrUploadField(collection)) {\n payload.logger.info(`Migrating collection \"${collection.slug}\"`)\n\n await migrateModelWithBatching({\n batchSize,\n config,\n fields: collection.fields,\n Model: db.collections[collection.slug],\n parentIsLocalized: false,\n session,\n })\n\n payload.logger.info(`Migrated collection \"${collection.slug}\"`)\n }\n\n if (collection.versions) {\n payload.logger.info(`Migrating collection versions \"${collection.slug}\"`)\n\n await migrateModelWithBatching({\n batchSize,\n config,\n fields: buildVersionCollectionFields(config, collection),\n Model: db.versions[collection.slug],\n parentIsLocalized: false,\n session,\n })\n\n payload.logger.info(`Migrated collection versions \"${collection.slug}\"`)\n }\n }\n\n const { globals: GlobalsModel } = db\n\n for (const global of payload.config.globals) {\n if (hasRelationshipOrUploadField(global)) {\n payload.logger.info(`Migrating global \"${global.slug}\"`)\n\n const doc = await GlobalsModel.findOne<Record<string, unknown>>(\n {\n globalType: {\n $eq: global.slug,\n },\n },\n {},\n { lean: true, session },\n )\n\n // in case if the global doesn't exist in the database yet (not saved)\n if (doc) {\n sanitizeRelationshipIDs({\n config,\n data: doc,\n fields: global.fields,\n })\n\n await GlobalsModel.collection.updateOne(\n {\n globalType: global.slug,\n },\n { $set: doc },\n { session },\n )\n }\n\n payload.logger.info(`Migrated global \"${global.slug}\"`)\n }\n\n if (global.versions) {\n payload.logger.info(`Migrating global versions \"${global.slug}\"`)\n\n await migrateModelWithBatching({\n batchSize,\n config,\n fields: buildVersionGlobalFields(config, global),\n Model: db.versions[global.slug],\n parentIsLocalized: false,\n session,\n })\n\n payload.logger.info(`Migrated global versions \"${global.slug}\"`)\n }\n }\n}\n"],"names":["buildVersionCollectionFields","buildVersionGlobalFields","getSession","sanitizeRelationshipIDs","migrateModelWithBatching","batchSize","config","fields","Model","parentIsLocalized","session","hasNext","skip","docs","find","lean","limit","length","pop","doc","data","collection","bulkWrite","map","updateOne","filter","_id","update","$set","hasRelationshipOrUploadField","field","type","block","blocks","tab","tabs","migrateRelationshipsV2_V3","req","payload","db","collections","logger","info","slug","versions","globals","GlobalsModel","global","findOne","globalType","$eq"],"mappings":"AAGA,SAASA,4BAA4B,EAAEC,wBAAwB,QAAQ,UAAS;AAIhF,SAASC,UAAU,QAAQ,6BAA4B;AACvD,SAASC,uBAAuB,QAAQ,0CAAyC;AAEjF,MAAMC,2BAA2B,OAAO,EACtCC,SAAS,EACTC,MAAM,EACNC,MAAM,EACNC,KAAK,EACLC,iBAAiB,E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AK,MAAMQ,UAAUT,QAAQhC,MAAM,CAACuC,OAAO,CAAE;QAC3C,IAAIhB,6BAA6BkB,SAAS;YACxCT,QAAQG,MAAM,CAACC,IAAI,CAAC,CAAC,kBAAkB,EAAEK,OAAOJ,IAAI,CAAC,CAAC,CAAC;YAEvD,MAAMxB,MAAM,MAAM2B,aAAaE,OAAO,CACpC;gBACEC,YAAY;oBACVC,KAAKH,OAAOJ,IAAI;gBAClB;YACF,GACA,CAAC,GACD;gBAAE5B,MAAM;gBAAML;YAAQ;YAGxB,uEAAuE;YACvE,IAAIS,KAAK;gBACPhB,wBAAwB;oBACtBG;oBACAc,MAAMD;oBACNZ,QAAQwC,OAAOxC,MAAM;gBACvB;gBAEA,MAAMuC,aAAazB,UAAU,CAACG,SAAS,CACrC;oBACEyB,YAAYF,OAAOJ,IAAI;gBACzB,GACA;oBAAEf,MAAMT;gBAAI,GACZ;oBAAET;gBAAQ;YAEd;YAEA4B,QAAQG,MAAM,CAACC,IAAI,CAAC,CAAC,iBAAiB,EAAEK,OAAOJ,IAAI,CAAC,CAAC,CAAC;QACxD;QAEA,IAAII,OAAOH,QAAQ,EAAE;YACnBN,QAAQG,MAAM,CAACC,IAAI,CAAC,CAAC,2BAA2B,EAAEK,OAAOJ,IAAI,CAAC,CAAC,CAAC;YAEhE,MAAMvC,yBAAyB;gBAC7BC;gBACAC;gBACAC,QAAQN,yBAAyBK,QAAQyC;gBACzCvC,OAAO+B,GAAGK,QAAQ,CAACG,OAAOJ,IAAI,CAAC;gBAC/BlC,mBAAmB;gBACnBC;YACF;YAEA4B,QAAQG,MAAM,CAACC,IAAI,CAAC,CAAC,0BAA0B,EAAEK,OAAOJ,IAAI,CAAC,CAAC,CAAC;QACjE;IACF;AACF"}
1
+ {"version":3,"sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"sourcesContent":["import type { ClientSession, Model } from 'mongoose'\nimport type { Field, PayloadRequest, SanitizedConfig } from 'payload'\n\nimport { buildVersionCollectionFields, buildVersionGlobalFields } from 'payload'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { getSession } from '../utilities/getSession.js'\nimport { transform } from '../utilities/transform.js'\n\nconst migrateModelWithBatching = async ({\n batchSize,\n config,\n db,\n fields,\n Model,\n parentIsLocalized,\n session,\n}: {\n batchSize: number\n config: SanitizedConfig\n db: MongooseAdapter\n fields: Field[]\n Model: Model<any>\n parentIsLocalized: boolean\n session: ClientSession\n}): Promise<void> => {\n let hasNext = true\n let skip = 0\n\n while (hasNext) {\n const docs = await Model.find(\n {},\n {},\n {\n lean: true,\n limit: batchSize + 1,\n session,\n skip,\n },\n )\n\n if (docs.length === 0) {\n break\n }\n\n hasNext = docs.length > batchSize\n\n if (hasNext) {\n docs.pop()\n }\n\n for (const doc of docs) {\n transform({ adapter: db, data: doc, fields, operation: 'write', parentIsLocalized })\n }\n\n await Model.collection.bulkWrite(\n docs.map((doc) => ({\n updateOne: {\n filter: { _id: doc._id },\n update: {\n $set: doc,\n },\n },\n })),\n { session },\n )\n\n skip += batchSize\n }\n}\n\nconst hasRelationshipOrUploadField = ({ fields }: { fields: Field[] }): boolean => {\n for (const field of fields) {\n if (field.type === 'relationship' || field.type === 'upload') {\n return true\n }\n\n if ('fields' in field) {\n if (hasRelationshipOrUploadField({ fields: field.fields })) {\n return true\n }\n }\n\n if ('blocks' in field) {\n for (const block of field.blocks) {\n if (typeof block === 'string') {\n // Skip - string blocks have been added in v3 and thus don't need to be migrated\n continue\n }\n if (hasRelationshipOrUploadField({ fields: block.fields })) {\n return true\n }\n }\n }\n\n if ('tabs' in field) {\n for (const tab of field.tabs) {\n if (hasRelationshipOrUploadField({ fields: tab.fields })) {\n return true\n }\n }\n }\n }\n\n return false\n}\n\nexport async function migrateRelationshipsV2_V3({\n batchSize,\n req,\n}: {\n batchSize: number\n req: PayloadRequest\n}): Promise<void> {\n const { payload } = req\n const db = payload.db as MongooseAdapter\n const config = payload.config\n\n const session = await getSession(db, req)\n\n for (const collection of payload.config.collections) {\n if (hasRelationshipOrUploadField(collection)) {\n payload.logger.info(`Migrating collection \"${collection.slug}\"`)\n\n await migrateModelWithBatching({\n batchSize,\n config,\n db,\n fields: collection.fields,\n Model: db.collections[collection.slug],\n parentIsLocalized: false,\n session,\n })\n\n payload.logger.info(`Migrated collection \"${collection.slug}\"`)\n }\n\n if (collection.versions) {\n payload.logger.info(`Migrating collection versions \"${collection.slug}\"`)\n\n await migrateModelWithBatching({\n batchSize,\n config,\n db,\n fields: buildVersionCollectionFields(config, collection),\n Model: db.versions[collection.slug],\n parentIsLocalized: false,\n session,\n })\n\n payload.logger.info(`Migrated collection versions \"${collection.slug}\"`)\n }\n }\n\n const { globals: GlobalsModel } = db\n\n for (const global of payload.config.globals) {\n if (hasRelationshipOrUploadField(global)) {\n payload.logger.info(`Migrating global \"${global.slug}\"`)\n\n const doc = await GlobalsModel.findOne<Record<string, unknown>>(\n {\n globalType: {\n $eq: global.slug,\n },\n },\n {},\n { lean: true, session },\n )\n\n // in case if the global doesn't exist in the database yet (not saved)\n if (doc) {\n transform({\n adapter: db,\n data: doc,\n fields: global.fields,\n operation: 'write',\n })\n\n await GlobalsModel.collection.updateOne(\n {\n globalType: global.slug,\n },\n { $set: doc },\n { session },\n )\n }\n\n payload.logger.info(`Migrated global \"${global.slug}\"`)\n }\n\n if (global.versions) {\n payload.logger.info(`Migrating global versions \"${global.slug}\"`)\n\n await migrateModelWithBatching({\n batchSize,\n config,\n db,\n fields: buildVersionGlobalFields(config, global),\n Model: db.versions[global.slug],\n parentIsLocalized: false,\n session,\n })\n\n payload.logger.info(`Migrated global versions \"${global.slug}\"`)\n }\n }\n}\n"],"names":["buildVersionCollectionFields","buildVersionGlobalFields","getSession","transform","migrateModelWithBatching","batchSize","config","db","fields","Model","parentIsLocalized","session","hasNext","skip","docs","find","lean","limit","length","pop","doc","adapter","data","operation","collection","bulkWrite","map","updateOne","filter","_id","update","$set","hasRelationshipOrUploadField","field","type","block","blocks","tab","tabs","migrateRelationshipsV2_V3","req","payload","collections","logger","info","slug","versions","globals","GlobalsModel","global","findOne","globalType","$eq"],"mappings":"AAGA,SAASA,4BAA4B,EAAEC,wBAAwB,QAAQ,UAAS;AAIhF,SAASC,UAAU,QAAQ,6BAA4B;AACvD,SAASC,SAAS,QAAQ,4BAA2B;AAErD,MAAMC,2BAA2B,OAAO,EACtCC,SAAS,EACTC,MAAM,EACNC,EAAE,EACFC,MAAM,EACNC,KAAK,EACLC,iBAAiB,EACjBC,OAAO,EASR;IACC,IAAIC,UAAU;IACd,IAAIC,OAAO;IAEX,MAAOD,QAAS;QACd,MAAME,OAAO,MAAML,MAAMM,IAAI,CAC3B,CAAC,GACD,CAAC,GACD;YACEC,MAAM;YACNC,OAAOZ,YAAY;YACnBM;YACAE;QACF;QAGF,IAAIC,KAAKI,MAAM,KAAK,GAAG;YACrB;QACF;QAEAN,UAAUE,KAAKI,MAAM,GAAGb;QAExB,IAAIO,SAAS;YACXE,KAAKK,GAAG;QACV;QAEA,KAAK,MAAMC,OAAON,KAAM;YACtBX,UAAU;gBAAEkB,SAASd;gBAAIe,MAAMF;gBAAKZ;gBAAQe,WAAW;gBAASb;YAAkB;QACpF;QAEA,MAAMD,MAAMe,UAAU,CAACC,SAAS,CAC9BX,KAAKY,GAAG,CAAC,CAACN,MAAS,CAAA;gBACjBO,WAAW;oBACTC,QAAQ;wBAAEC,KAAKT,IAAIS,GAAG;oBAAC;oBACvBC,QAAQ;wBACNC,MAAMX;oBACR;gBACF;YACF,CAAA,IACA;YAAET;QAAQ;QAGZE,QAAQR;IACV;AACF;AAEA,MAAM2B,+BAA+B,CAAC,EAAExB,MAAM,EAAuB;IACnE,KAAK,MAAMyB,SAASzB,OAAQ;QAC1B,IAAIyB,MAAMC,IAAI,KAAK,kBAAkBD,MAAMC,IAAI,KAAK,UAAU;YAC5D,OAAO;QACT;QAEA,IAAI,YAAYD,OAAO;YACrB,IAAID,6BAA6B;gBAAExB,QAAQyB,MAAMzB,MAAM;YAAC,IAAI;gB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2C;gBACzCxC,OAAOF,GAAGuC,QAAQ,CAACG,OAAOJ,IAAI,CAAC;gBAC/BnC,mBAAmB;gBACnBC;YACF;YAEA8B,QAAQE,MAAM,CAACC,IAAI,CAAC,CAAC,0BAA0B,EAAEK,OAAOJ,IAAI,CAAC,CAAC,CAAC;QACjE;IACF;AACF"}
@@ -4,7 +4,7 @@ import { buildSortParam } from './queries/buildSortParam.js';
4
4
  import { buildJoinAggregation } from './utilities/buildJoinAggregation.js';
5
5
  import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js';
6
6
  import { getSession } from './utilities/getSession.js';
7
- import { sanitizeInternalFields } from './utilities/sanitizeInternalFields.js';
7
+ import { transform } from './utilities/transform.js';
8
8
  export const queryDrafts = async function queryDrafts({ collection, joins, limit, locale, page, pagination, req, select, sort: sortArg, where }) {
9
9
  const VersionModel = this.versions[collection];
10
10
  const collectionConfig = this.payload.collections[collection].config;
@@ -97,18 +97,18 @@ export const queryDrafts = async function queryDrafts({ collection, joins, limit
97
97
  } else {
98
98
  result = await VersionModel.paginate(versionQuery, paginationOptions);
99
99
  }
100
- const docs = JSON.parse(JSON.stringify(result.docs));
101
- return {
102
- ...result,
103
- docs: docs.map((doc)=>{
104
- doc = {
105
- _id: doc.parent,
106
- id: doc.parent,
107
- ...doc.version
108
- };
109
- return sanitizeInternalFields(doc);
110
- })
111
- };
100
+ transform({
101
+ adapter: this,
102
+ data: result.docs,
103
+ fields: buildVersionCollectionFields(this.payload.config, collectionConfig),
104
+ operation: 'read'
105
+ });
106
+ for(let i = 0; i < result.docs.length; i++){
107
+ const id = result.docs[i].parent;
108
+ result.docs[i] = result.docs[i].version;
109
+ result.docs[i].id = id;
110
+ }
111
+ return result;
112
112
  };
113
113
 
114
114
  //# sourceMappingURL=queryDrafts.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/queryDrafts.ts"],"sourcesContent":["import type { PaginateOptions, QueryOptions } from 'mongoose'\nimport type { QueryDrafts } from 'payload'\n\nimport { buildVersionCollectionFields, combineQueries, flattenWhereToOperators } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildSortParam } from './queries/buildSortParam.js'\nimport { buildJoinAggregation } from './utilities/buildJoinAggregation.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getSession } from './utilities/getSession.js'\nimport { sanitizeInternalFields } from './utilities/sanitizeInternalFields.js'\n\nexport const queryDrafts: QueryDrafts = async function queryDrafts(\n this: MongooseAdapter,\n { collection, joins, limit, locale, page, pagination, req, select, sort: sortArg, where },\n) {\n const VersionModel = this.versions[collection]\n const collectionConfig = this.payload.collections[collection].config\n const options: QueryOptions = {\n session: await getSession(this, req),\n }\n\n let hasNearConstraint\n let sort\n\n if (where) {\n const constraints = flattenWhereToOperators(where)\n hasNearConstraint = constraints.some((prop) => Object.keys(prop).some((key) => key === 'near'))\n }\n\n if (!hasNearConstraint) {\n sort = buildSortParam({\n config: this.payload.config,\n fields: collectionConfig.flattenedFields,\n locale,\n sort: sortArg || collectionConfig.defaultSort,\n timestamps: true,\n })\n }\n\n const combinedWhere = combineQueries({ latest: { equals: true } }, where)\n\n const fields = buildVersionCollectionFields(this.payload.config, collectionConfig, true)\n const versionQuery = await buildQuery({\n adapter: this,\n fields,\n locale,\n where: combinedWhere,\n })\n\n const projection = buildProjectionFromSelect({\n adapter: this,\n fields,\n select,\n })\n // useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters.\n const useEstimatedCount =\n hasNearConstraint || !versionQuery || Object.keys(versionQuery).length === 0\n const paginationOptions: PaginateOptions = {\n lean: true,\n leanWithId: true,\n options,\n page,\n pagination,\n projection,\n sort,\n useEstimatedCount,\n }\n\n if (this.collation) {\n const defaultLocale = 'en'\n paginationOptions.collation = {\n locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,\n ...this.collation,\n }\n }\n\n if (\n !useEstimatedCount &&\n Object.keys(versionQuery).length === 0 &&\n this.disableIndexHints !== true\n ) {\n // Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding\n // a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,\n // which makes queries very slow. This only happens when no query (filter) is provided. If one is provided, it uses\n // the correct indexed field\n paginationOptions.useCustomCountFn = () => {\n return Promise.resolve(\n VersionModel.countDocuments(versionQuery, {\n hint: { _id: 1 },\n }),\n )\n }\n }\n\n if (limit > 0) {\n paginationOptions.limit = limit\n // limit must also be set here, it's ignored when pagination is false\n paginationOptions.options.limit = limit\n }\n\n let result\n\n const aggregate = await buildJoinAggregation({\n adapter: this,\n collection,\n collectionConfig,\n joins,\n locale,\n projection,\n query: versionQuery,\n versions: true,\n })\n\n // build join aggregation\n if (aggregate) {\n result = await VersionModel.aggregatePaginate(\n VersionModel.aggregate(aggregate),\n paginationOptions,\n )\n } else {\n result = await VersionModel.paginate(versionQuery, paginationOptions)\n }\n\n const docs = JSON.parse(JSON.stringify(result.docs))\n\n return {\n ...result,\n docs: docs.map((doc) => {\n doc = {\n _id: doc.parent,\n id: doc.parent,\n ...doc.version,\n }\n\n return sanitizeInternalFields(doc)\n }),\n }\n}\n"],"names":["buildVersionCollectionFields","combineQueries","flattenWhereToOperators","buildQuery","buildSortParam","buildJoinAggregation","buildProjectionFromSelect","getSession","sanitizeInternalFields","queryDrafts","collection","joins","limit","locale","page","pagination","req","select","sort","sortArg","where","VersionModel","versions","collectionConfig","payload","collections","config","options","session","hasNearConstraint","constraints","some","prop","Object","keys","key","fields","flattenedFields","defaultSort","timestamps","combinedWhere","latest","equals","versionQuery","adapter","projection","useEstimatedCount","length","paginationOptions","lean","leanWithId","collation","defaultLocale","disableIndexHints","useCustomCountFn","Promise","resolve","countDocuments","hint","_id","result","aggregate","query","aggregatePaginate","paginate","docs","JSON","parse","stringify","map","doc","parent","id","version"],"mappings":"AAGA,SAASA,4BAA4B,EAAEC,cAAc,EAAEC,uBAAuB,QAAQ,UAAS;AAI/F,S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E,SAAS,CAACR,OAAOK,IAAI;IAElD,OAAO;QACL,GAAGL,MAAM;QACTK,MAAMA,KAAKI,GAAG,CAAC,CAACC;YACdA,MAAM;gBACJX,KAAKW,IAAIC,MAAM;gBACfC,IAAIF,IAAIC,MAAM;gBACd,GAAGD,IAAIG,OAAO;YAChB;YAEA,OAAOjE,uBAAuB8D;QAChC;IACF;AACF,EAAC"}
1
+ {"version":3,"sources":["../src/queryDrafts.ts"],"sourcesContent":["import type { PaginateOptions, QueryOptions } from 'mongoose'\nimport type { QueryDrafts } from 'payload'\n\nimport { buildVersionCollectionFields, combineQueries, flattenWhereToOperators } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildSortParam } from './queries/buildSortParam.js'\nimport { buildJoinAggregation } from './utilities/buildJoinAggregation.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getSession } from './utilities/getSession.js'\nimport { transform } from './utilities/transform.js'\n\nexport const queryDrafts: QueryDrafts = async function queryDrafts(\n this: MongooseAdapter,\n { collection, joins, limit, locale, page, pagination, req, select, sort: sortArg, where },\n) {\n const VersionModel = this.versions[collection]\n const collectionConfig = this.payload.collections[collection].config\n const options: QueryOptions = {\n session: await getSession(this, req),\n }\n\n let hasNearConstraint\n let sort\n\n if (where) {\n const constraints = flattenWhereToOperators(where)\n hasNearConstraint = constraints.some((prop) => Object.keys(prop).some((key) => key === 'near'))\n }\n\n if (!hasNearConstraint) {\n sort = buildSortParam({\n config: this.payload.config,\n fields: collectionConfig.flattenedFields,\n locale,\n sort: sortArg || collectionConfig.defaultSort,\n timestamps: true,\n })\n }\n\n const combinedWhere = combineQueries({ latest: { equals: true } }, where)\n\n const fields = buildVersionCollectionFields(this.payload.config, collectionConfig, true)\n const versionQuery = await buildQuery({\n adapter: this,\n fields,\n locale,\n where: combinedWhere,\n })\n\n const projection = buildProjectionFromSelect({\n adapter: this,\n fields,\n select,\n })\n // useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters.\n const useEstimatedCount =\n hasNearConstraint || !versionQuery || Object.keys(versionQuery).length === 0\n const paginationOptions: PaginateOptions = {\n lean: true,\n leanWithId: true,\n options,\n page,\n pagination,\n projection,\n sort,\n useEstimatedCount,\n }\n\n if (this.collation) {\n const defaultLocale = 'en'\n paginationOptions.collation = {\n locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,\n ...this.collation,\n }\n }\n\n if (\n !useEstimatedCount &&\n Object.keys(versionQuery).length === 0 &&\n this.disableIndexHints !== true\n ) {\n // Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding\n // a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,\n // which makes queries very slow. This only happens when no query (filter) is provided. If one is provided, it uses\n // the correct indexed field\n paginationOptions.useCustomCountFn = () => {\n return Promise.resolve(\n VersionModel.countDocuments(versionQuery, {\n hint: { _id: 1 },\n }),\n )\n }\n }\n\n if (limit > 0) {\n paginationOptions.limit = limit\n // limit must also be set here, it's ignored when pagination is false\n paginationOptions.options.limit = limit\n }\n\n let result\n\n const aggregate = await buildJoinAggregation({\n adapter: this,\n collection,\n collectionConfig,\n joins,\n locale,\n projection,\n query: versionQuery,\n versions: true,\n })\n\n // build join aggregation\n if (aggregate) {\n result = await VersionModel.aggregatePaginate(\n VersionModel.aggregate(aggregate),\n paginationOptions,\n )\n } else {\n result = await VersionModel.paginate(versionQuery, paginationOptions)\n }\n\n transform({\n adapter: this,\n data: result.docs,\n fields: buildVersionCollectionFields(this.payload.config, collectionConfig),\n operation: 'read',\n })\n\n for (let i = 0; i < result.docs.length; i++) {\n const id = result.docs[i].parent\n result.docs[i] = result.docs[i].version\n result.docs[i].id = id\n }\n\n return result\n}\n"],"names":["buildVersionCollectionFields","combineQueries","flattenWhereToOperators","buildQuery","buildSortParam","buildJoinAggregation","buildProjectionFromSelect","getSession","transform","queryDrafts","collection","joins","limit","locale","page","pagination","req","select","sort","sortArg","where","VersionModel","versions","collectionConfig","payload","collections","config","options","session","hasNearConstraint","constraints","some","prop","Object","keys","key","fields","flattenedFields","defaultSort","timestamps","combinedWhere","latest","equals","versionQuery","adapter","projection","useEstimatedCount","length","paginationOptions","lean","leanWithId","collation","defaultLocale","disableIndexHints","useCustomCountFn","Promise","resolve","countDocuments","hint","_id","result","aggregate","query","aggregatePaginate","paginate","data","docs","operation","i","id","parent","version"],"mappings":"AAGA,SAASA,4BAA4B,EAAEC,cAAc,EAAEC,uBAAuB,QAAQ,UAAS;AAI/F,SAASC,UAAU,QAAQ,0BAAyB;AACpD,SAASC,cAAc,QAAQ,8BAA6B;AAC5D,SAASC,oBAAoB,QAAQ,sCAAqC;AAC1E,SAASC,yBAAyB,QAAQ,2CAA0C;AACpF,SAASC,UAAU,QAAQ,4BAA2B;AACtD,SAASC,SAAS,QAAQ,2BAA0B;AAEpD,OAAO,MAAMC,cAA2B,eAAeA,YAErD,EAAEC,UAAU,EAAEC,KAAK,EAAEC,KAAK,EAAEC,MAAM,EAAEC,IAAI,EAAEC,UAAU,EAAEC,GAAG,EAAEC,MAAM,EAAEC,MAAMC,OAAO,EAAEC,KAAK,EAAE;IAEzF,MAAMC,eAAe,IAAI,CAACC,QAAQ,CAACZ,WAAW;IAC9C,MAAMa,mBAAmB,IAAI,CAACC,OAAO,CAACC,WAAW,CAACf,WAAW,CAACgB,MAAM;IACpE,MAAMC,UAAwB;QAC5BC,SAAS,MAAMrB,WAAW,IAAI,EAAES;IAClC;IAEA,IAAIa;IACJ,IAAIX;IAEJ,IAAIE,OAAO;QACT,MAAMU,cAAc5B,wBAAwBkB;QAC5CS,oBAAoBC,YAAYC,IAAI,CAAC,CAACC,OAASC,OAAOC,IAAI,CAACF,MAAMD,IAAI,CAAC,CAACI,MAAQA,QAAQ;IACzF;IAEA,IAAI,CAACN,mBAAmB;QACtBX,OAAOd,eAAe;YACpBsB,QAAQ,IAAI,CAACF,OAAO,CAACE,MAAM;YAC3BU,QAAQb,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AAMC,YAAY,MAAMxD,qBAAqB;QAC3CuC,SAAS,IAAI;QACblC;QACAa;QACAZ;QACAE;QACAgC;QACAiB,OAAOnB;QACPrB,UAAU;IACZ;IAEA,yBAAyB;IACzB,IAAIuC,WAAW;QACbD,SAAS,MAAMvC,aAAa0C,iBAAiB,CAC3C1C,aAAawC,SAAS,CAACA,YACvBb;IAEJ,OAAO;QACLY,SAAS,MAAMvC,aAAa2C,QAAQ,CAACrB,cAAcK;IACrD;IAEAxC,UAAU;QACRoC,SAAS,IAAI;QACbqB,MAAML,OAAOM,IAAI;QACjB9B,QAAQpC,6BAA6B,IAAI,CAACwB,OAAO,CAACE,MAAM,EAAEH;QAC1D4C,WAAW;IACb;IAEA,IAAK,IAAIC,IAAI,GAAGA,IAAIR,OAAOM,IAAI,CAACnB,MAAM,EAAEqB,IAAK;QAC3C,MAAMC,KAAKT,OAAOM,IAAI,CAACE,EAAE,CAACE,MAAM;QAChCV,OAAOM,IAAI,CAACE,EAAE,GAAGR,OAAOM,IAAI,CAACE,EAAE,CAACG,OAAO;QACvCX,OAAOM,IAAI,CAACE,EAAE,CAACC,EAAE,GAAGA;IACtB;IAEA,OAAOT;AACT,EAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"updateGlobal.d.ts","sourceRoot":"","sources":["../src/updateGlobal.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAS3C,eAAO,MAAM,YAAY,EAAE,YAwC1B,CAAA"}
1
+ {"version":3,"file":"updateGlobal.d.ts","sourceRoot":"","sources":["../src/updateGlobal.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAQ3C,eAAO,MAAM,YAAY,EAAE,YA0B1B,CAAA"}