@payloadcms/db-mongodb 3.63.0-canary.5 → 3.63.0-canary.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"findDistinct.d.ts","sourceRoot":"","sources":["../src/findDistinct.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAkB,MAAM,SAAS,CAAA;AAW3D,eAAO,MAAM,YAAY,EAAE,YA4P1B,CAAA"}
1
+ {"version":3,"file":"findDistinct.d.ts","sourceRoot":"","sources":["../src/findDistinct.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAkB,MAAM,SAAS,CAAA;AAW3D,eAAO,MAAM,YAAY,EAAE,YAqQ1B,CAAA"}
@@ -182,19 +182,27 @@ export const findDistinct = async function(args) {
182
182
  pipeline.push({
183
183
  $limit: args.limit
184
184
  });
185
- const totalDocs = await Model.aggregate([
185
+ // Build count pipeline with the same structure as the main pipeline
186
+ // to ensure relationship lookups are included
187
+ const countPipeline = [
186
188
  {
187
189
  $match: query
188
190
  },
189
- {
190
- $group: {
191
- _id: `$${fieldPath}`
191
+ ...sortAggregation.length > 0 ? sortAggregation : [],
192
+ ...relationLookup?.length ? relationLookup : [],
193
+ ...$unwind ? [
194
+ {
195
+ $unwind
192
196
  }
197
+ ] : [],
198
+ {
199
+ $group
193
200
  },
194
201
  {
195
202
  $count: 'count'
196
203
  }
197
- ], {
204
+ ];
205
+ const totalDocs = await Model.aggregate(countPipeline, {
198
206
  session
199
207
  }).then((res)=>res[0]?.count ?? 0);
200
208
  const totalPages = Math.ceil(totalDocs / args.limit);
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/findDistinct.ts"],"sourcesContent":["import type { PipelineStage } from 'mongoose'\nimport type { FindDistinct, FlattenedField } from 'payload'\n\nimport { getFieldByPath } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildSortParam } from './queries/buildSortParam.js'\nimport { getCollection } from './utilities/getEntity.js'\nimport { getSession } from './utilities/getSession.js'\n\nexport const findDistinct: FindDistinct = async function (this: MongooseAdapter, args) {\n const { collectionConfig, Model } = getCollection({\n adapter: this,\n collectionSlug: args.collection,\n })\n\n const session = await getSession(this, args.req)\n\n const { where = {} } = args\n\n let sortAggregation: PipelineStage[] = []\n\n const sort = buildSortParam({\n adapter: this,\n config: this.payload.config,\n fields: collectionConfig.flattenedFields,\n locale: args.locale,\n sort: args.sort ?? args.field,\n sortAggregation,\n timestamps: true,\n })\n\n const query = await buildQuery({\n adapter: this,\n collectionSlug: args.collection,\n fields: collectionConfig.flattenedFields,\n locale: args.locale,\n where,\n })\n\n const fieldPathResult = getFieldByPath({\n config: this.payload.config,\n fields: collectionConfig.flattenedFields,\n includeRelationships: true,\n path: args.field,\n })\n let fieldPath = args.field\n if (fieldPathResult?.pathHasLocalized && args.locale) {\n fieldPath = fieldPathResult.localizedPath.replace('<locale>', args.locale)\n }\n\n const page = args.page || 1\n\n let sortProperty = Object.keys(sort)[0]! // assert because buildSortParam always returns at least 1 key.\n const sortDirection = sort[sortProperty] === 'asc' ? 1 : -1\n\n let currentFields = collectionConfig.flattenedFields\n let foundField: FlattenedField | null = null\n\n let rels: {\n fieldPath: string\n relationTo: string\n }[] = []\n\n let tempPath = ''\n let insideRelation = false\n\n const segments = args.field.split('.')\n\n for (let i = 0; i < segments.length; i++) {\n const segment = segments[i]\n const field = currentFields.find((e) => e.name === segment)\n if (rels.length) {\n insideRelation = true\n }\n\n if (!field) {\n break\n }\n\n if (tempPath) {\n tempPath = `${tempPath}.${field.name}`\n } else {\n tempPath = field.name\n }\n\n if ('flattenedFields' in field) {\n currentFields = field.flattenedFields\n continue\n }\n\n if (\n (field.type === 'relationship' || field.type === 'upload') &&\n typeof field.relationTo === 'string'\n ) {\n if (i === segments.length - 2 && segments[i + 1] === 'id') {\n foundField = field\n fieldPath = tempPath\n break\n }\n rels.push({ fieldPath: tempPath, relationTo: field.relationTo })\n currentFields = this.payload.collections[field.relationTo]?.config\n .flattenedFields as FlattenedField[]\n continue\n }\n foundField = field\n }\n\n const resolvedField = foundField || fieldPathResult?.field\n const isHasManyValue = resolvedField && 'hasMany' in resolvedField && resolvedField\n\n let relationLookup: null | PipelineStage[] = null\n\n if (!insideRelation) {\n rels = []\n }\n\n if (rels.length) {\n if (sortProperty.startsWith('_')) {\n const sortWithoutRelationPrefix = sortProperty.replace(/^_+/, '')\n const lastFieldPath = rels.at(-1)?.fieldPath as string\n if (sortWithoutRelationPrefix.startsWith(lastFieldPath)) {\n sortProperty = sortWithoutRelationPrefix\n }\n }\n relationLookup = rels.reduce<PipelineStage[]>((acc, { fieldPath, relationTo }) => {\n sortAggregation = sortAggregation.filter((each) => {\n if ('$lookup' in each && each.$lookup.as.replace(/^_+/, '') === fieldPath) {\n return false\n }\n\n return true\n })\n const { Model: foreignModel } = getCollection({ adapter: this, collectionSlug: relationTo })\n acc.push({\n $lookup: {\n as: fieldPath,\n foreignField: '_id',\n from: foreignModel.collection.name,\n localField: fieldPath,\n },\n })\n acc.push({ $unwind: `$${fieldPath}` })\n return acc\n }, [])\n }\n\n let $unwind: any = ''\n let $group: any = null\n if (\n isHasManyValue &&\n sortAggregation.length &&\n sortAggregation[0] &&\n '$lookup' in sortAggregation[0]\n ) {\n $unwind = { path: `$${sortAggregation[0].$lookup.as}`, preserveNullAndEmptyArrays: true }\n $group = {\n _id: {\n _field: `$${sortAggregation[0].$lookup.as}._id`,\n _sort: `$${sortProperty}`,\n },\n }\n } else if (isHasManyValue) {\n $unwind = { path: `$${args.field}`, preserveNullAndEmptyArrays: true }\n }\n\n if (!$group) {\n $group = {\n _id: {\n _field: `$${fieldPath}`,\n ...(sortProperty === fieldPath\n ? {}\n : {\n _sort: `$${sortProperty}`,\n }),\n },\n }\n }\n\n const pipeline: PipelineStage[] = [\n {\n $match: query,\n },\n ...(sortAggregation.length > 0 ? sortAggregation : []),\n ...(relationLookup?.length ? relationLookup : []),\n ...($unwind\n ? [\n {\n $unwind,\n },\n ]\n : []),\n {\n $group,\n },\n {\n $sort: {\n [sortProperty === fieldPath ? '_id._field' : '_id._sort']: sortDirection,\n },\n },\n ]\n\n const getValues = async () => {\n return Model.aggregate(pipeline, { session }).then((res) =>\n res.map((each) => ({\n [args.field]: JSON.parse(JSON.stringify(each._id._field)),\n })),\n )\n }\n\n if (args.limit) {\n pipeline.push({\n $skip: (page - 1) * args.limit,\n })\n pipeline.push({ $limit: args.limit })\n const totalDocs = await Model.aggregate(\n [\n {\n $match: query,\n },\n {\n $group: {\n _id: `$${fieldPath}`,\n },\n },\n { $count: 'count' },\n ],\n {\n session,\n },\n ).then((res) => res[0]?.count ?? 0)\n const totalPages = Math.ceil(totalDocs / args.limit)\n const hasPrevPage = page > 1\n const hasNextPage = totalPages > page\n const pagingCounter = (page - 1) * args.limit + 1\n\n return {\n hasNextPage,\n hasPrevPage,\n limit: args.limit,\n nextPage: hasNextPage ? page + 1 : null,\n page,\n pagingCounter,\n prevPage: hasPrevPage ? page - 1 : null,\n totalDocs,\n totalPages,\n values: await getValues(),\n }\n }\n\n const values = await getValues()\n\n return {\n hasNextPage: false,\n hasPrevPage: false,\n limit: 0,\n page: 1,\n pagingCounter: 1,\n totalDocs: values.length,\n totalPages: 1,\n values,\n }\n}\n"],"names":["getFieldByPath","buildQuery","buildSortParam","getCollection","getSession","findDistinct","args","collectionConfig","Model","adapter","collectionSlug","collection","session","req","where","sortAggregation","sort","config","payload","fields","flattenedFields","locale","field","timestamps","query","fieldPathResult","includeRelationships","path","fieldPath","pathHasLocalized","localizedPath","replace","page","sortProperty","Object","keys","sortDirection","currentFields","foundField","rels","tempPath","insideRelation","segments","split","i","length","segment","find","e","name","type","relationTo","push","collections","resolvedField","isHasManyValue","relationLookup","startsWith","sortWithoutRelationPrefix","lastFieldPath","at","reduce","acc","filter","each","$lookup","as","foreignModel","foreignField","from","localField","$unwind","$group","preserveNullAndEmptyArrays","_id","_field","_sort","pipeline","$match","$sort","getValues","aggregate","then","res","map","JSON","parse","stringify","limit","$skip","$limit","totalDocs","$count","count","totalPages","Math","ceil","hasPrevPage","hasNextPage","pagingCounter","nextPage","prevPage","values"],"mappings":"AAGA,SAASA,cAAc,QAAQ,UAAS;AAIxC,SAASC,UAAU,QAAQ,0BAAyB;AACpD,SAASC,cAAc,QAAQ,8BAA6B;AAC5D,SAASC,aAAa,QAAQ,2BAA0B;AACxD,SAASC,UAAU,QAAQ,4BAA2B;AAEtD,OAAO,MAAMC,eAA6B,eAAuCC,IAAI;IACnF,MAAM,EAAEC,gBAAgB,EAAEC,KAAK,EAAE,GAAGL,cAAc;QAChDM,SAAS,IAAI;QACbC,gBAAgBJ,KAAKK,UAAU;IACjC;IAEA,MAAMC,UAAU,MAAMR,WAAW,IAAI,EAAEE,KAAKO,GAAG;IAE/C,MAAM,EAAEC,QAAQ,CAAC,CAAC,EAAE,GAAGR;IAEvB,IAAIS,kBAAmC,EAAE;IAEzC,MAAMC,OAAOd,eAAe;QAC1BO,SAAS,IAAI;QACbQ,QAAQ,IAAI,CAACC,OAAO,CAACD,MAAM;QAC3BE,QAAQZ,iBAAiBa,eAAe;QACxCC,QAAQf,KAAKe,MAAM;QACnBL,MAAMV,KAAKU,IAAI,IAAIV,KAAKgB,KAAK;QAC7BP;QACAQ,YAAY;IACd;IAEA,MAAMC,QAAQ,MAAMvB,WAAW;QAC7BQ,SAAS,IAAI;QACbC,gBAAgBJ,KAAKK,UAAU;QAC/BQ,QAAQZ,iBAAiBa,eAAe;QACxCC,QAAQf,KAAKe,MAAM;QACnBP;IACF;IAEA,MAAMW,kBAAkBzB,eAAe;QACrCiB,QAAQ,IAAI,CAACC,OAAO,CAACD,MAAM;QAC3BE,QAAQZ,iBAAiBa,eAAe;QACxCM,sBAAsB;QACtBC,MAAMrB,KAAKgB,KAAK;IAClB;IACA,IAAIM,YAAYtB,KAAKgB,KAAK;IAC1B,IAAIG,iBAAiBI,oBAAoB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AAKU,GAAG,CAACC,MAAM;gBACzD,CAAA;IAEJ;IAEA,IAAIrE,KAAKkF,KAAK,EAAE;QACdX,SAASzB,IAAI,CAAC;YACZqC,OAAO,AAACzD,CAAAA,OAAO,CAAA,IAAK1B,KAAKkF,KAAK;QAChC;QACAX,SAASzB,IAAI,CAAC;YAAEsC,QAAQpF,KAAKkF,KAAK;QAAC;QACnC,MAAMG,YAAY,MAAMnF,MAAMyE,SAAS,CACrC;YACE;gBACEH,QAAQtD;YACV;YACA;gBACEgD,QAAQ;oBACNE,KAAK,CAAC,CAAC,EAAE9C,WAAW;gBACtB;YACF;YACA;gBAAEgE,QAAQ;YAAQ;SACnB,EACD;YACEhF;QACF,GACAsE,IAAI,CAAC,CAACC,MAAQA,GAAG,CAAC,EAAE,EAAEU,SAAS;QACjC,MAAMC,aAAaC,KAAKC,IAAI,CAACL,YAAYrF,KAAKkF,KAAK;QACnD,MAAMS,cAAcjE,OAAO;QAC3B,MAAMkE,cAAcJ,aAAa9D;QACjC,MAAMmE,gBAAgB,AAACnE,CAAAA,OAAO,CAAA,IAAK1B,KAAKkF,KAAK,GAAG;QAEhD,OAAO;YACLU;YACAD;YACAT,OAAOlF,KAAKkF,KAAK;YACjBY,UAAUF,cAAclE,OAAO,IAAI;YACnCA;YACAmE;YACAE,UAAUJ,cAAcjE,OAAO,IAAI;YACnC2D;YACAG;YACAQ,QAAQ,MAAMtB;QAChB;IACF;IAEA,MAAMsB,SAAS,MAAMtB;IAErB,OAAO;QACLkB,aAAa;QACbD,aAAa;QACbT,OAAO;QACPxD,MAAM;QACNmE,eAAe;QACfR,WAAWW,OAAOzD,MAAM;QACxBiD,YAAY;QACZQ;IACF;AACF,EAAC"}
1
+ {"version":3,"sources":["../src/findDistinct.ts"],"sourcesContent":["import type { PipelineStage } from 'mongoose'\nimport type { FindDistinct, FlattenedField } from 'payload'\n\nimport { getFieldByPath } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildSortParam } from './queries/buildSortParam.js'\nimport { getCollection } from './utilities/getEntity.js'\nimport { getSession } from './utilities/getSession.js'\n\nexport const findDistinct: FindDistinct = async function (this: MongooseAdapter, args) {\n const { collectionConfig, Model } = getCollection({\n adapter: this,\n collectionSlug: args.collection,\n })\n\n const session = await getSession(this, args.req)\n\n const { where = {} } = args\n\n let sortAggregation: PipelineStage[] = []\n\n const sort = buildSortParam({\n adapter: this,\n config: this.payload.config,\n fields: collectionConfig.flattenedFields,\n locale: args.locale,\n sort: args.sort ?? args.field,\n sortAggregation,\n timestamps: true,\n })\n\n const query = await buildQuery({\n adapter: this,\n collectionSlug: args.collection,\n fields: collectionConfig.flattenedFields,\n locale: args.locale,\n where,\n })\n\n const fieldPathResult = getFieldByPath({\n config: this.payload.config,\n fields: collectionConfig.flattenedFields,\n includeRelationships: true,\n path: args.field,\n })\n let fieldPath = args.field\n if (fieldPathResult?.pathHasLocalized && args.locale) {\n fieldPath = fieldPathResult.localizedPath.replace('<locale>', args.locale)\n }\n\n const page = args.page || 1\n\n let sortProperty = Object.keys(sort)[0]! // assert because buildSortParam always returns at least 1 key.\n const sortDirection = sort[sortProperty] === 'asc' ? 1 : -1\n\n let currentFields = collectionConfig.flattenedFields\n let foundField: FlattenedField | null = null\n\n let rels: {\n fieldPath: string\n relationTo: string\n }[] = []\n\n let tempPath = ''\n let insideRelation = false\n\n const segments = args.field.split('.')\n\n for (let i = 0; i < segments.length; i++) {\n const segment = segments[i]\n const field = currentFields.find((e) => e.name === segment)\n if (rels.length) {\n insideRelation = true\n }\n\n if (!field) {\n break\n }\n\n if (tempPath) {\n tempPath = `${tempPath}.${field.name}`\n } else {\n tempPath = field.name\n }\n\n if ('flattenedFields' in field) {\n currentFields = field.flattenedFields\n continue\n }\n\n if (\n (field.type === 'relationship' || field.type === 'upload') &&\n typeof field.relationTo === 'string'\n ) {\n if (i === segments.length - 2 && segments[i + 1] === 'id') {\n foundField = field\n fieldPath = tempPath\n break\n }\n rels.push({ fieldPath: tempPath, relationTo: field.relationTo })\n currentFields = this.payload.collections[field.relationTo]?.config\n .flattenedFields as FlattenedField[]\n continue\n }\n foundField = field\n }\n\n const resolvedField = foundField || fieldPathResult?.field\n const isHasManyValue = resolvedField && 'hasMany' in resolvedField && resolvedField\n\n let relationLookup: null | PipelineStage[] = null\n\n if (!insideRelation) {\n rels = []\n }\n\n if (rels.length) {\n if (sortProperty.startsWith('_')) {\n const sortWithoutRelationPrefix = sortProperty.replace(/^_+/, '')\n const lastFieldPath = rels.at(-1)?.fieldPath as string\n if (sortWithoutRelationPrefix.startsWith(lastFieldPath)) {\n sortProperty = sortWithoutRelationPrefix\n }\n }\n relationLookup = rels.reduce<PipelineStage[]>((acc, { fieldPath, relationTo }) => {\n sortAggregation = sortAggregation.filter((each) => {\n if ('$lookup' in each && each.$lookup.as.replace(/^_+/, '') === fieldPath) {\n return false\n }\n\n return true\n })\n const { Model: foreignModel } = getCollection({ adapter: this, collectionSlug: relationTo })\n acc.push({\n $lookup: {\n as: fieldPath,\n foreignField: '_id',\n from: foreignModel.collection.name,\n localField: fieldPath,\n },\n })\n acc.push({ $unwind: `$${fieldPath}` })\n return acc\n }, [])\n }\n\n let $unwind: any = ''\n let $group: any = null\n if (\n isHasManyValue &&\n sortAggregation.length &&\n sortAggregation[0] &&\n '$lookup' in sortAggregation[0]\n ) {\n $unwind = { path: `$${sortAggregation[0].$lookup.as}`, preserveNullAndEmptyArrays: true }\n $group = {\n _id: {\n _field: `$${sortAggregation[0].$lookup.as}._id`,\n _sort: `$${sortProperty}`,\n },\n }\n } else if (isHasManyValue) {\n $unwind = { path: `$${args.field}`, preserveNullAndEmptyArrays: true }\n }\n\n if (!$group) {\n $group = {\n _id: {\n _field: `$${fieldPath}`,\n ...(sortProperty === fieldPath\n ? {}\n : {\n _sort: `$${sortProperty}`,\n }),\n },\n }\n }\n\n const pipeline: PipelineStage[] = [\n {\n $match: query,\n },\n ...(sortAggregation.length > 0 ? sortAggregation : []),\n ...(relationLookup?.length ? relationLookup : []),\n ...($unwind\n ? [\n {\n $unwind,\n },\n ]\n : []),\n {\n $group,\n },\n {\n $sort: {\n [sortProperty === fieldPath ? '_id._field' : '_id._sort']: sortDirection,\n },\n },\n ]\n\n const getValues = async () => {\n return Model.aggregate(pipeline, { session }).then((res) =>\n res.map((each) => ({\n [args.field]: JSON.parse(JSON.stringify(each._id._field)),\n })),\n )\n }\n\n if (args.limit) {\n pipeline.push({\n $skip: (page - 1) * args.limit,\n })\n pipeline.push({ $limit: args.limit })\n\n // Build count pipeline with the same structure as the main pipeline\n // to ensure relationship lookups are included\n const countPipeline: PipelineStage[] = [\n {\n $match: query,\n },\n ...(sortAggregation.length > 0 ? sortAggregation : []),\n ...(relationLookup?.length ? relationLookup : []),\n ...($unwind\n ? [\n {\n $unwind,\n },\n ]\n : []),\n {\n $group,\n },\n { $count: 'count' },\n ]\n\n const totalDocs = await Model.aggregate(countPipeline, {\n session,\n }).then((res) => res[0]?.count ?? 0)\n const totalPages = Math.ceil(totalDocs / args.limit)\n const hasPrevPage = page > 1\n const hasNextPage = totalPages > page\n const pagingCounter = (page - 1) * args.limit + 1\n\n return {\n hasNextPage,\n hasPrevPage,\n limit: args.limit,\n nextPage: hasNextPage ? page + 1 : null,\n page,\n pagingCounter,\n prevPage: hasPrevPage ? page - 1 : null,\n totalDocs,\n totalPages,\n values: await getValues(),\n }\n }\n\n const values = await getValues()\n\n return {\n hasNextPage: false,\n hasPrevPage: false,\n limit: 0,\n page: 1,\n pagingCounter: 1,\n totalDocs: values.length,\n totalPages: 1,\n values,\n }\n}\n"],"names":["getFieldByPath","buildQuery","buildSortParam","getCollection","getSession","findDistinct","args","collectionConfig","Model","adapter","collectionSlug","collection","session","req","where","sortAggregation","sort","config","payload","fields","flattenedFields","locale","field","timestamps","query","fieldPathResult","includeRelationships","path","fieldPath","pathHasLocalized","localizedPath","replace","page","sortProperty","Object","keys","sortDirection","currentFields","foundField","rels","tempPath","insideRelation","segments","split","i","length","segment","find","e","name","type","relationTo","push","collections","resolvedField","isHasManyValue","relationLookup","startsWith","sortWithoutRelationPrefix","lastFieldPath","at","reduce","acc","filter","each","$lookup","as","foreignModel","foreignField","from","localField","$unwind","$group","preserveNullAndEmptyArrays","_id","_field","_sort","pipeline","$match","$sort","getValues","aggregate","then","res","map","JSON","parse","stringify","limit","$skip","$limit","countPipeline","$count","totalDocs","count","totalPages","Math","ceil","hasPrevPage","hasNextPage","pagingCounter","nextPage","prevPage","values"],"mappings":"AAGA,SAASA,cAAc,QAAQ,UAAS;AAIxC,SAASC,UAAU,QAAQ,0BAAyB;AACpD,SAASC,cAAc,QAAQ,8BAA6B;AAC5D,SAASC,aAAa,QAAQ,2BAA0B;AACxD,SAASC,UAAU,QAAQ,4BAA2B;AAEtD,OAAO,MAAMC,eAA6B,eAAuCC,IAAI;IACnF,MAAM,EAAEC,gBAAgB,EAAEC,KAAK,EAAE,GAAGL,cAAc;QAChDM,SAAS,IAAI;QACbC,gBAAgBJ,KAAKK,UAAU;IACjC;IAEA,MAAMC,UAAU,MAAMR,WAAW,IAAI,EAAEE,KAAKO,GAAG;IAE/C,MAAM,EAAEC,QAAQ,CAAC,CAAC,EAAE,GAAGR;IAEvB,IAAIS,kBAAmC,EAAE;IAEzC,MAAMC,OAAOd,eAAe;QAC1BO,SAAS,IAAI;QACbQ,QAAQ,IAAI,CAACC,OAAO,CAACD,MAAM;QAC3BE,QAAQZ,iBAAiBa,eAAe;QACxCC,QAAQf,KAAKe,MAAM;QACnBL,MAAMV,KAAKU,IAAI,IAAIV,KAAKgB,KAAK;QAC7BP;QACAQ,YAAY;IACd;IAEA,MAAMC,QAAQ,MAAMvB,WAAW;QAC7BQ,SAAS,IAAI;QACbC,gBAAgBJ,KAAKK,UAAU;QAC/BQ,QAAQZ,iBAAiBa,eAAe;QACxCC,QAAQf,KAAKe,MAAM;QACnBP;IACF;IAEA,MAAMW,kBAAkBzB,eAAe;QACrCiB,QAAQ,IAAI,CAACC,OAAO,C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,aAAa;QACbD,aAAa;QACbV,OAAO;QACPxD,MAAM;QACNoE,eAAe;QACfP,WAAWU,OAAO1D,MAAM;QACxBkD,YAAY;QACZQ;IACF;AACF,EAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@payloadcms/db-mongodb",
3
- "version": "3.63.0-canary.5",
3
+ "version": "3.63.0-canary.7",
4
4
  "description": "The officially supported MongoDB database adapter for Payload",
5
5
  "homepage": "https://payloadcms.com",
6
6
  "repository": {
@@ -50,10 +50,10 @@
50
50
  "mongodb": "6.16.0",
51
51
  "mongodb-memory-server": "10.1.4",
52
52
  "@payloadcms/eslint-config": "3.28.0",
53
- "payload": "3.63.0-canary.5"
53
+ "payload": "3.63.0-canary.7"
54
54
  },
55
55
  "peerDependencies": {
56
- "payload": "3.63.0-canary.5"
56
+ "payload": "3.63.0-canary.7"
57
57
  },
58
58
  "scripts": {
59
59
  "build": "pnpm build:types && pnpm build:swc",