@payloadcms/db-mongodb 4.0.0-internal.934870d → 4.0.0-internal.952ed66
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/connect.d.ts.map +1 -1
- package/dist/connect.js +1 -0
- package/dist/connect.js.map +1 -1
- package/dist/count.d.ts.map +1 -1
- package/dist/count.js +33 -4
- package/dist/count.js.map +1 -1
- package/dist/countGlobalVersions.d.ts.map +1 -1
- package/dist/countGlobalVersions.js +10 -2
- package/dist/countGlobalVersions.js.map +1 -1
- package/dist/countVersions.d.ts.map +1 -1
- package/dist/countVersions.js +36 -4
- package/dist/countVersions.js.map +1 -1
- package/dist/createGlobalVersion.d.ts +1 -1
- package/dist/createGlobalVersion.d.ts.map +1 -1
- package/dist/createGlobalVersion.js +24 -1
- package/dist/createGlobalVersion.js.map +1 -1
- package/dist/createVersion.d.ts.map +1 -1
- package/dist/createVersion.js +16 -3
- package/dist/createVersion.js.map +1 -1
- package/dist/deleteBranchGlobal.d.ts +10 -0
- package/dist/deleteBranchGlobal.d.ts.map +1 -0
- package/dist/deleteBranchGlobal.js +26 -0
- package/dist/deleteBranchGlobal.js.map +1 -0
- package/dist/deleteMany.d.ts.map +1 -1
- package/dist/deleteMany.js +1 -0
- package/dist/deleteMany.js.map +1 -1
- package/dist/deleteOne.d.ts.map +1 -1
- package/dist/deleteOne.js +20 -1
- package/dist/deleteOne.js.map +1 -1
- package/dist/deleteVersions.d.ts.map +1 -1
- package/dist/deleteVersions.js +1 -0
- package/dist/deleteVersions.js.map +1 -1
- package/dist/exports/migration-utils.d.ts +1 -0
- package/dist/exports/migration-utils.d.ts.map +1 -1
- package/dist/exports/migration-utils.js +1 -0
- package/dist/exports/migration-utils.js.map +1 -1
- package/dist/find.d.ts.map +1 -1
- package/dist/find.js +44 -8
- package/dist/find.js.map +1 -1
- package/dist/findDistinct.d.ts.map +1 -1
- package/dist/findDistinct.js +70 -11
- package/dist/findDistinct.js.map +1 -1
- package/dist/findGlobal.d.ts.map +1 -1
- package/dist/findGlobal.js +48 -9
- package/dist/findGlobal.js.map +1 -1
- package/dist/findGlobalVersions.d.ts.map +1 -1
- package/dist/findGlobalVersions.js +10 -2
- package/dist/findGlobalVersions.js.map +1 -1
- package/dist/findOne.d.ts.map +1 -1
- package/dist/findOne.js +43 -5
- package/dist/findOne.js.map +1 -1
- package/dist/findVersions.d.ts.map +1 -1
- package/dist/findVersions.js +49 -5
- package/dist/findVersions.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/models/buildGlobalModel.d.ts.map +1 -1
- package/dist/models/buildGlobalModel.js +30 -1
- package/dist/models/buildGlobalModel.js.map +1 -1
- package/dist/models/buildSchema.d.ts.map +1 -1
- package/dist/models/buildSchema.js +36 -10
- package/dist/models/buildSchema.js.map +1 -1
- package/dist/predefinedMigrations/branching.d.ts +4 -0
- package/dist/predefinedMigrations/branching.d.ts.map +1 -0
- package/dist/predefinedMigrations/branching.js +9 -0
- package/dist/predefinedMigrations/branching.js.map +1 -0
- package/dist/predefinedMigrations/migrateBranching.d.ts +17 -0
- package/dist/predefinedMigrations/migrateBranching.d.ts.map +1 -0
- package/dist/predefinedMigrations/migrateBranching.js +90 -0
- package/dist/predefinedMigrations/migrateBranching.js.map +1 -0
- package/dist/predefinedMigrations/migrateSizesToVariants.js.map +1 -1
- package/dist/queries/buildAndOrConditions.d.ts +4 -2
- package/dist/queries/buildAndOrConditions.d.ts.map +1 -1
- package/dist/queries/buildAndOrConditions.js +3 -1
- package/dist/queries/buildAndOrConditions.js.map +1 -1
- package/dist/queries/buildBranchVisibility.d.ts +11 -0
- package/dist/queries/buildBranchVisibility.d.ts.map +1 -0
- package/dist/queries/buildBranchVisibility.js +204 -0
- package/dist/queries/buildBranchVisibility.js.map +1 -0
- package/dist/queries/buildQuery.d.ts +4 -2
- package/dist/queries/buildQuery.d.ts.map +1 -1
- package/dist/queries/buildQuery.js +3 -1
- package/dist/queries/buildQuery.js.map +1 -1
- package/dist/queries/buildRelatedQueryPipeline.d.ts +7 -0
- package/dist/queries/buildRelatedQueryPipeline.d.ts.map +1 -0
- package/dist/queries/buildRelatedQueryPipeline.js +56 -0
- package/dist/queries/buildRelatedQueryPipeline.js.map +1 -0
- package/dist/queries/buildSearchParams.d.ts +4 -2
- package/dist/queries/buildSearchParams.d.ts.map +1 -1
- package/dist/queries/buildSearchParams.js +177 -45
- package/dist/queries/buildSearchParams.js.map +1 -1
- package/dist/queries/getBuildQueryPlugin.d.ts +3 -1
- package/dist/queries/getBuildQueryPlugin.d.ts.map +1 -1
- package/dist/queries/getBuildQueryPlugin.js +3 -1
- package/dist/queries/getBuildQueryPlugin.js.map +1 -1
- package/dist/queries/parseParams.d.ts +4 -2
- package/dist/queries/parseParams.d.ts.map +1 -1
- package/dist/queries/parseParams.js +5 -1
- package/dist/queries/parseParams.js.map +1 -1
- package/dist/queryDrafts.d.ts.map +1 -1
- package/dist/queryDrafts.js +39 -7
- package/dist/queryDrafts.js.map +1 -1
- package/dist/transactions/commitTransaction.d.ts.map +1 -1
- package/dist/transactions/commitTransaction.js +69 -7
- package/dist/transactions/commitTransaction.js.map +1 -1
- package/dist/transactions/rollbackTransaction.d.ts.map +1 -1
- package/dist/transactions/rollbackTransaction.js +10 -9
- package/dist/transactions/rollbackTransaction.js.map +1 -1
- package/dist/updateGlobal.d.ts.map +1 -1
- package/dist/updateGlobal.js +89 -7
- package/dist/updateGlobal.js.map +1 -1
- package/dist/updateGlobalVersion.d.ts.map +1 -1
- package/dist/updateGlobalVersion.js +1 -0
- package/dist/updateGlobalVersion.js.map +1 -1
- package/dist/updateMany.d.ts.map +1 -1
- package/dist/updateMany.js +3 -1
- package/dist/updateMany.js.map +1 -1
- package/dist/updateOne.d.ts.map +1 -1
- package/dist/updateOne.js +28 -2
- package/dist/updateOne.js.map +1 -1
- package/dist/updateVersion.d.ts.map +1 -1
- package/dist/updateVersion.js +1 -0
- package/dist/updateVersion.js.map +1 -1
- package/dist/upsertBranchGlobalChange.d.ts +3 -0
- package/dist/upsertBranchGlobalChange.d.ts.map +1 -0
- package/dist/upsertBranchGlobalChange.js +28 -0
- package/dist/upsertBranchGlobalChange.js.map +1 -0
- package/dist/utilities/aggregatePaginate.d.ts +2 -1
- package/dist/utilities/aggregatePaginate.d.ts.map +1 -1
- package/dist/utilities/aggregatePaginate.js +18 -4
- package/dist/utilities/aggregatePaginate.js.map +1 -1
- package/dist/utilities/buildJoinAggregation.d.ts +4 -2
- package/dist/utilities/buildJoinAggregation.d.ts.map +1 -1
- package/dist/utilities/buildJoinAggregation.js +158 -24
- package/dist/utilities/buildJoinAggregation.js.map +1 -1
- package/dist/utilities/resolveJoins.d.ts +6 -2
- package/dist/utilities/resolveJoins.d.ts.map +1 -1
- package/dist/utilities/resolveJoins.js +115 -26
- package/dist/utilities/resolveJoins.js.map +1 -1
- package/package.json +16 -19
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/predefinedMigrations/migrateSizesToVariants.ts"],"sourcesContent":["import type {\n Collection,\n CreateIndexesOptions,\n Document,\n IndexDescriptionInfo,\n IndexDirection,\n} from 'mongodb'\nimport type { Payload, PayloadRequest } from 'payload'\n\nimport { isDeepStrictEqual } from 'node:util'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { getSession } from '../utilities/getSession.js'\n\n/** Mongo error codes for an index that conflicts with an existing index. */\nconst INDEX_CONFLICT_CODES = new Set([85, 86])\n/** Mongo error code for a collection that hasn't been created yet. */\nconst NAMESPACE_NOT_FOUND_CODE = 26\nconst GENERATED_VARIANT_METADATA_FIELDS = new Set([\n 'filename',\n 'filesize',\n 'height',\n 'mimeType',\n 'url',\n 'width',\n])\n\n/**\n * Moves every upload collection's stored image sizes between the legacy `sizes` field and\n * `variants`, on each collection and its versions collection (`version.sizes`). Uses `$rename`,\n * so no document is rewritten beyond the renamed key, and re-keys the indexes on those fields.\n * Documents that are already renamed are left as they are, so it's safe to re-run.\n */\nexport async function migrateSizesToVariants({\n direction = 'up',\n payload,\n req,\n}: {\n direction?: 'down' | 'up'\n payload: Payload\n req?: Partial<PayloadRequest>\n}): Promise<void> {\n const adapter = payload.db as unknown as MongooseAdapter\n const session = await getSession(adapter, req)\n const [from, to] = direction === 'up' ? ['sizes', 'variants'] : ['variants', 'sizes']\n\n const targets: {\n collection: Collection\n expectedVariantNames: string[]\n fromPath: string\n toPath: string\n }[] = []\n\n for (const collection of payload.config.collections) {\n if (!collection.upload) {\n continue\n }\n\n const upload = typeof collection.upload === 'object' ? collection.upload : undefined\n const expectedVariantNames = (upload?.variants ?? []).map(({ name }) => name)\n\n targets.push({\n collection: adapter.collections[collection.slug]!.collection,\n expectedVariantNames,\n fromPath: from,\n toPath: to,\n })\n\n if (collection.versions && adapter.versions[collection.slug]) {\n targets.push({\n collection: adapter.versions[collection.slug]!.collection,\n expectedVariantNames,\n fromPath: `version.${from}`,\n toPath: `version.${to}`,\n })\n }\n }\n\n for (const { collection, expectedVariantNames, fromPath, toPath } of targets) {\n const [sourceData, directCollision] = await Promise.all([\n collection.findOne({ [fromPath]: { $exists: true } }, { projection: { _id: 1 } }),\n collection.findOne(\n { [fromPath]: { $exists: true }, [toPath]: { $exists: true } },\n { projection: { _id: 1 } },\n ),\n ])\n\n if (\n sourceData &&\n (directCollision ||\n (await findUnexpectedDestinationData({ collection, expectedVariantNames, toPath })))\n ) {\n throw new Error(\n `Cannot run the sizes-to-variants migration because collection \"${collection.collectionName}\" contains both \"${fromPath}\" and \"${toPath}\" data. Move or rename the existing destination data before running this migration.`,\n )\n }\n }\n\n // Index changes can't be part of a multi-document transaction, and one made after the\n // migration's transaction has written to a collection aborts it. A transaction's snapshot starts\n // at its first operation, so re-keying every index before any transactional write is safe.\n for (const target of targets) {\n await rekeyIndexes(target)\n }\n\n for (const { collection, fromPath, toPath } of targets) {\n const result = await collection.updateMany(\n { [fromPath]: { $exists: true }, [toPath]: { $exists: false } },\n { $rename: { [fromPath]: toPath } },\n { session },\n )\n\n payload.logger.info({\n msg: `sizes-to-variants (${direction}): renamed ${fromPath} on ${result.modifiedCount} document(s) in \"${collection.collectionName}\"`,\n })\n }\n}\n\nasync function findUnexpectedDestinationData({\n collection,\n expectedVariantNames,\n toPath,\n}: {\n collection: Collection\n expectedVariantNames: string[]\n toPath: string\n}): Promise<Document | null> {\n const expectedVariantNameSet = new Set(expectedVariantNames)\n const cursor = collection.find(\n { [toPath]: { $exists: true } },\n { projection: { _id: 1, [toPath]: 1 } },\n )\n\n try {\n for await (const document of cursor) {\n const destinationData = getDocumentPath({ document, path: toPath })\n\n if (hasUnexpectedVariantData({ destinationData, expectedVariantNameSet })) {\n return document\n }\n }\n } finally {\n await cursor.close()\n }\n\n return null\n}\n\nfunction getDocumentPath({ document, path }: { document: Document; path: string }): unknown {\n return path.split('.').reduce<unknown>((value, pathSegment) => {\n if (!isRecord(value)) {\n return undefined\n }\n\n return value[pathSegment]\n }, document)\n}\n\nfunction hasUnexpectedVariantData({\n destinationData,\n expectedVariantNameSet,\n}: {\n destinationData: unknown\n expectedVariantNameSet: Set<string>\n}): boolean {\n if (!isRecord(destinationData)) {\n return destinationData !== null && destinationData !== undefined\n }\n\n for (const [variantName, variantData] of Object.entries(destinationData)) {\n if (!expectedVariantNameSet.has(variantName)) {\n return true\n }\n\n if (variantData === null || variantData === undefined) {\n continue\n }\n\n if (\n !isRecord(variantData) ||\n Object.keys(variantData).some(\n (metadataFieldName) => !GENERATED_VARIANT_METADATA_FIELDS.has(metadataFieldName),\n )\n ) {\n return true\n }\n }\n\n return false\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return value !== null && typeof value === 'object' && !Array.isArray(value)\n}\n\nasync function rekeyIndexes({\n collection,\n fromPath,\n toPath,\n}: {\n collection: Collection\n fromPath: string\n toPath: string\n}): Promise<void> {\n let indexes: IndexDescriptionInfo[]\n\n try {\n indexes = await collection.indexes()\n } catch (err) {\n if ((err as { code?: number }).code === NAMESPACE_NOT_FOUND_CODE) {\n return\n }\n\n throw err\n }\n\n for (const index of indexes) {\n const sourceKeys = getIndexKeysForCreate({ fromPath, index, toPath: fromPath })\n\n if (!Object.keys(sourceKeys).some((key) => isPathAtOrBelow({ key, path: fromPath }))) {\n continue\n }\n\n const rekeyedKeys = getIndexKeysForCreate({ fromPath, index, toPath })\n const destinationOptions = getDestinationIndexOptions({ fromPath, index, toPath })\n\n if (index.weights && '_fts' in index.key && '_ftsx' in index.key) {\n await rekeyTextIndex({\n collection,\n destinationOptions,\n index,\n rekeyedKeys,\n sourceKeys,\n })\n continue\n }\n\n try {\n await collection.createIndex(rekeyedKeys, destinationOptions)\n } catch (err) {\n if (!INDEX_CONFLICT_CODES.has((err as { code?: number }).code ?? 0)) {\n throw err\n }\n\n const destinationIndex = (await collection.indexes()).find((candidate) =>\n isDeepStrictEqual(\n getIndexKeysForCreate({ fromPath: toPath, index: candidate, toPath }),\n rekeyedKeys,\n ),\n )\n const existingDestinationOptions = destinationIndex\n ? getDestinationIndexOptions({\n fromPath: toPath,\n index: destinationIndex,\n toPath,\n })\n : undefined\n\n if (!destinationIndex || !isDeepStrictEqual(existingDestinationOptions, destinationOptions)) {\n throw err\n }\n }\n\n await collection.dropIndex(index.name!)\n }\n}\n\nasync function rekeyTextIndex({\n collection,\n destinationOptions,\n index,\n rekeyedKeys,\n sourceKeys,\n}: {\n collection: Collection\n destinationOptions: CreateIndexesOptions\n index: IndexDescriptionInfo\n rekeyedKeys: Record<string, IndexDirection>\n sourceKeys: Record<string, IndexDirection>\n}): Promise<void> {\n if (!index.name) {\n throw new Error('Cannot re-key an unnamed MongoDB text index.')\n }\n\n const sourceOptions: CreateIndexesOptions = {\n ...getDestinationIndexOptions({ fromPath: '', index, toPath: '' }),\n name: index.name,\n }\n\n await collection.dropIndex(index.name)\n\n try {\n await collection.createIndex(rekeyedKeys, destinationOptions)\n } catch (err) {\n try {\n await collection.createIndex(sourceKeys, sourceOptions)\n } catch (restoreErr) {\n throw new AggregateError(\n [err, restoreErr],\n `Failed to create the replacement for MongoDB text index \"${index.name}\" and failed to restore the source index.`,\n )\n }\n\n throw err\n }\n}\n\nfunction getIndexKeysForCreate({\n fromPath,\n index,\n toPath,\n}: {\n fromPath: string\n index: IndexDescriptionInfo\n toPath: string\n}): Record<string, IndexDirection> {\n const keys: [string, IndexDirection][] = []\n const hasInternalTextKeys = '_fts' in index.key && '_ftsx' in index.key\n\n for (const [key, value] of Object.entries(index.key)) {\n if (hasInternalTextKeys && key === '_fts') {\n for (const textPath of Object.keys(index.weights ?? {})) {\n keys.push([rekeyPath({ fromPath, path: textPath, toPath }), 'text'])\n }\n continue\n }\n\n if (hasInternalTextKeys && key === '_ftsx') {\n continue\n }\n\n keys.push([rekeyPath({ fromPath, path: key, toPath }), value])\n }\n\n return Object.fromEntries(keys)\n}\n\nfunction getDestinationIndexOptions({\n fromPath,\n index,\n toPath,\n}: {\n fromPath: string\n index: IndexDescriptionInfo\n toPath: string\n}): CreateIndexesOptions {\n const options: CreateIndexesOptions = {\n '2dsphereIndexVersion': index['2dsphereIndexVersion'],\n background: index.background,\n bits: index.bits,\n bucketSize: index.bucketSize,\n collation: index.collation,\n default_language: index.default_language,\n expireAfterSeconds: index.expireAfterSeconds,\n hidden: index.hidden,\n language_override: index.language_override,\n max: index.max,\n min: index.min,\n partialFilterExpression: index.partialFilterExpression\n ? rekeyDocumentPaths({ document: index.partialFilterExpression, fromPath, toPath })\n : undefined,\n sparse: index.sparse,\n storageEngine: index.storageEngine,\n textIndexVersion: index.textIndexVersion,\n unique: index.unique,\n weights: index.weights\n ? rekeyDocumentPaths({ document: index.weights, fromPath, toPath })\n : undefined,\n wildcardProjection: index.wildcardProjection\n ? rekeyDocumentPaths({ document: index.wildcardProjection, fromPath, toPath })\n : undefined,\n }\n\n return Object.fromEntries(\n Object.entries(options).filter(([, value]) => value !== undefined),\n ) as CreateIndexesOptions\n}\n\nfunction rekeyDocumentPaths({\n document,\n fromPath,\n toPath,\n}: {\n document: Document\n fromPath: string\n toPath: string\n}): Document {\n return Object.fromEntries(\n Object.entries(document).map(([key, value]) => [\n rekeyPath({ fromPath, path: key, toPath }),\n rekeyDocumentValue({ fromPath, toPath, value }),\n ]),\n )\n}\n\nfunction isPathAtOrBelow({ key, path }: { key: string; path: string }): boolean {\n return key === path || key.startsWith(`${path}.`)\n}\n\nfunction rekeyPath({\n fromPath,\n path,\n toPath,\n}: {\n fromPath: string\n path: string\n toPath: string\n}): string {\n return isPathAtOrBelow({ key: path, path: fromPath })\n ? `${toPath}${path.slice(fromPath.length)}`\n : path\n}\n\nfunction rekeyDocumentValue({\n fromPath,\n toPath,\n value,\n}: {\n fromPath: string\n toPath: string\n value: unknown\n}): unknown {\n if (Array.isArray(value)) {\n return value.map((item) => rekeyDocumentValue({ fromPath, toPath, value: item }))\n }\n\n if (\n value !== null &&\n typeof value === 'object' &&\n (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null)\n ) {\n return rekeyDocumentPaths({ document: value as Document, fromPath, toPath })\n }\n\n return value\n}\n"],"names":["isDeepStrictEqual","getSession","INDEX_CONFLICT_CODES","Set","NAMESPACE_NOT_FOUND_CODE","GENERATED_VARIANT_METADATA_FIELDS","migrateSizesToVariants","direction","payload","req","adapter","db","session","from","to","targets","collection","config","collections","upload","undefined","expectedVariantNames","variants","map","name","push","slug","fromPath","toPath","versions","sourceData","directCollision","Promise","all","findOne","$exists","projection","_id","findUnexpectedDestinationData","Error","collectionName","target","rekeyIndexes","result","updateMany","$rename","logger","info","msg","modifiedCount","expectedVariantNameSet","cursor","find","document","destinationData","getDocumentPath","path","hasUnexpectedVariantData","close","split","reduce","value","pathSegment","isRecord","variantName","variantData","Object","entries","has","keys","some","metadataFieldName","Array","isArray","indexes","err","code","index","sourceKeys","getIndexKeysForCreate","key","isPathAtOrBelow","rekeyedKeys","destinationOptions","getDestinationIndexOptions","weights","rekeyTextIndex","createIndex","destinationIndex","candidate","existingDestinationOptions","dropIndex","sourceOptions","restoreErr","AggregateError","hasInternalTextKeys","textPath","rekeyPath","fromEntries","options","background","bits","bucketSize","collation","default_language","expireAfterSeconds","hidden","language_override","max","min","partialFilterExpression","rekeyDocumentPaths","sparse","storageEngine","textIndexVersion","unique","wildcardProjection","filter","rekeyDocumentValue","startsWith","slice","length","item","getPrototypeOf","prototype"],"mappings":"AASA,SAASA,iBAAiB,QAAQ,YAAW;AAI7C,SAASC,UAAU,QAAQ,6BAA4B;AAEvD,0EAA0E,GAC1E,MAAMC,uBAAuB,IAAIC,IAAI;IAAC;IAAI;CAAG;AAC7C,oEAAoE,GACpE,MAAMC,2BAA2B;AACjC,MAAMC,oCAAoC,IAAIF,IAAI;IAChD;IACA;IACA;IACA;IACA;IACA;CACD;AAED;;;;;CAKC,GACD,OAAO,eAAeG,uBAAuB,EAC3CC,YAAY,IAAI,EAChBC,OAAO,EACPC,GAAG,EAKJ;IACC,MAAMC,UAAUF,QAAQG,EAAE;IAC1B,MAAMC,UAAU,MAAMX,WAAWS,SAASD;IAC1C,MAAM,CAACI,MAAMC,GAAG,GAAGP,cAAc,OAAO;QAAC;QAAS;KAAW,GAAG;QAAC;QAAY;KAAQ;IAErF,MAAMQ,UAKA,EAAE;IAER,KAAK,MAAMC,cAAcR,QAAQS,MAAM,CAACC,WAAW,CAAE;QACnD,IAAI,CAACF,WAAWG,MAAM,EAAE;YACtB;QACF;QAEA,MAAMA,SAAS,OAAOH,WAAWG,MAAM,KAAK,WAAWH,WAAWG,MAAM,GAAGC;QAC3E,MAAMC,uBAAuB,AAACF,CAAAA,QAAQG,YAAY,EAAE,AAAD,EAAGC,GAAG,CAAC,CAAC,EAAEC,IAAI,EAAE,GAAKA;QAExET,QAAQU,IAAI,CAAC;YACXT,YAAYN,QAAQQ,WAAW,CAACF,WAAWU,IAAI,CAAC,CAAEV,UAAU;YAC5DK;YACAM,UAAUd;YACVe,QAAQd;QACV;QAEA,IAAIE,WAAWa,QAAQ,IAAInB,QAAQmB,QAAQ,CAACb,WA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AC,CAACmG,OAASJ,mBAAmB;gBAAE3F;gBAAUC;gBAAQiC,OAAO6D;YAAK;IAChF;IAEA,IACE7D,UAAU,QACV,OAAOA,UAAU,YAChBK,CAAAA,OAAOyD,cAAc,CAAC9D,WAAWK,OAAO0D,SAAS,IAAI1D,OAAOyD,cAAc,CAAC9D,WAAW,IAAG,GAC1F;QACA,OAAOkD,mBAAmB;YAAE1D,UAAUQ;YAAmBlC;YAAUC;QAAO;IAC5E;IAEA,OAAOiC;AACT"}
|
|
1
|
+
{"version":3,"sources":["../../src/predefinedMigrations/migrateSizesToVariants.ts"],"sourcesContent":["import type {\n Collection,\n CreateIndexesOptions,\n Document,\n IndexDescriptionInfo,\n IndexDirection,\n} from 'mongodb'\nimport type { Payload, PayloadRequest } from 'payload'\n\nimport { isDeepStrictEqual } from 'node:util'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { getSession } from '../utilities/getSession.js'\n\n/** Mongo error codes for an index that conflicts with an existing index. */\nconst INDEX_CONFLICT_CODES = new Set([85, 86])\n/** Mongo error code for a collection that hasn't been created yet. */\nconst NAMESPACE_NOT_FOUND_CODE = 26\nconst GENERATED_VARIANT_METADATA_FIELDS = new Set([\n 'filename',\n 'filesize',\n 'height',\n 'mimeType',\n 'url',\n 'width',\n])\n\n/**\n * Moves every upload collection's stored image sizes between the legacy `sizes` field and\n * `variants`, on each collection and its versions collection (`version.sizes`). Uses `$rename`,\n * so no document is rewritten beyond the renamed key, and re-keys the indexes on those fields.\n * Documents that are already renamed are left as they are, so it's safe to re-run.\n */\nexport async function migrateSizesToVariants({\n direction = 'up',\n payload,\n req,\n}: {\n direction?: 'down' | 'up'\n payload: Payload\n req?: Partial<PayloadRequest>\n}): Promise<void> {\n const adapter = payload.db as unknown as MongooseAdapter\n const session = await getSession(adapter, req)\n const [from, to] = direction === 'up' ? ['sizes', 'variants'] : ['variants', 'sizes']\n\n const targets: {\n collection: Collection\n expectedVariantNames: string[]\n fromPath: string\n toPath: string\n }[] = []\n\n for (const collection of payload.config.collections) {\n if (!collection.upload) {\n continue\n }\n\n const upload = typeof collection.upload === 'object' ? collection.upload : undefined\n const expectedVariantNames = (upload?.variants ?? []).map(({ name }) => name)\n\n targets.push({\n collection: adapter.collections[collection.slug]!.collection,\n expectedVariantNames,\n fromPath: from,\n toPath: to,\n })\n\n if (collection.versions && adapter.versions[collection.slug]) {\n targets.push({\n collection: adapter.versions[collection.slug]!.collection,\n expectedVariantNames,\n fromPath: `version.${from}`,\n toPath: `version.${to}`,\n })\n }\n }\n\n for (const { collection, expectedVariantNames, fromPath, toPath } of targets) {\n const [sourceData, directCollision] = await Promise.all([\n collection.findOne({ [fromPath]: { $exists: true } }, { projection: { _id: 1 } }),\n collection.findOne(\n { [fromPath]: { $exists: true }, [toPath]: { $exists: true } },\n { projection: { _id: 1 } },\n ),\n ])\n\n if (\n sourceData &&\n (directCollision ||\n (await findUnexpectedDestinationData({ collection, expectedVariantNames, toPath })))\n ) {\n throw new Error(\n `Cannot run the sizes-to-variants migration because collection \"${collection.collectionName}\" contains both \"${fromPath}\" and \"${toPath}\" data. Move or rename the existing destination data before running this migration.`,\n )\n }\n }\n\n // Index changes can't be part of a multi-document transaction, and one made after the\n // migration's transaction has written to a collection aborts it. A transaction's snapshot starts\n // at its first operation, so re-keying every index before any transactional write is safe.\n for (const target of targets) {\n await rekeyIndexes(target)\n }\n\n for (const { collection, fromPath, toPath } of targets) {\n const result = await collection.updateMany(\n { [fromPath]: { $exists: true }, [toPath]: { $exists: false } },\n { $rename: { [fromPath]: toPath } },\n { session },\n )\n\n payload.logger.info({\n msg: `sizes-to-variants (${direction}): renamed ${fromPath} on ${result.modifiedCount} document(s) in \"${collection.collectionName}\"`,\n })\n }\n}\n\nasync function findUnexpectedDestinationData({\n collection,\n expectedVariantNames,\n toPath,\n}: {\n collection: Collection\n expectedVariantNames: string[]\n toPath: string\n}): Promise<Document | null> {\n const expectedVariantNameSet = new Set(expectedVariantNames)\n const cursor = collection.find(\n { [toPath]: { $exists: true } },\n { projection: { _id: 1, [toPath]: 1 } },\n )\n\n try {\n for await (const document of cursor) {\n const destinationData = getDocumentPath({ document, path: toPath })\n\n if (hasUnexpectedVariantData({ destinationData, expectedVariantNameSet })) {\n return document\n }\n }\n } finally {\n await cursor.close()\n }\n\n return null\n}\n\nfunction getDocumentPath({ document, path }: { document: Document; path: string }): unknown {\n return path.split('.').reduce<unknown>((value, pathSegment) => {\n if (!isRecord(value)) {\n return undefined\n }\n\n return value[pathSegment]\n }, document)\n}\n\nfunction hasUnexpectedVariantData({\n destinationData,\n expectedVariantNameSet,\n}: {\n destinationData: unknown\n expectedVariantNameSet: Set<string>\n}): boolean {\n if (!isRecord(destinationData)) {\n return destinationData !== null && destinationData !== undefined\n }\n\n for (const [variantName, variantData] of Object.entries(destinationData)) {\n if (!expectedVariantNameSet.has(variantName)) {\n return true\n }\n\n if (variantData === null || variantData === undefined) {\n continue\n }\n\n if (\n !isRecord(variantData) ||\n Object.keys(variantData).some(\n (metadataFieldName) => !GENERATED_VARIANT_METADATA_FIELDS.has(metadataFieldName),\n )\n ) {\n return true\n }\n }\n\n return false\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return value !== null && typeof value === 'object' && !Array.isArray(value)\n}\n\nasync function rekeyIndexes({\n collection,\n fromPath,\n toPath,\n}: {\n collection: Collection\n fromPath: string\n toPath: string\n}): Promise<void> {\n let indexes: IndexDescriptionInfo[]\n\n try {\n indexes = await collection.indexes()\n } catch (err) {\n if ((err as { code?: number }).code === NAMESPACE_NOT_FOUND_CODE) {\n return\n }\n\n throw err\n }\n\n for (const index of indexes) {\n const sourceKeys = getIndexKeysForCreate({ fromPath, index, toPath: fromPath })\n\n if (!Object.keys(sourceKeys).some((key) => isPathAtOrBelow({ key, path: fromPath }))) {\n continue\n }\n\n const rekeyedKeys = getIndexKeysForCreate({ fromPath, index, toPath })\n const destinationOptions = getDestinationIndexOptions({ fromPath, index, toPath })\n\n if (index.weights && '_fts' in index.key && '_ftsx' in index.key) {\n await rekeyTextIndex({\n collection,\n destinationOptions,\n index,\n rekeyedKeys,\n sourceKeys,\n })\n continue\n }\n\n try {\n await collection.createIndex(rekeyedKeys, destinationOptions)\n } catch (err) {\n if (!INDEX_CONFLICT_CODES.has((err as { code?: number }).code ?? 0)) {\n throw err\n }\n\n const destinationIndex = (await collection.indexes()).find((candidate) =>\n isDeepStrictEqual(\n getIndexKeysForCreate({ fromPath: toPath, index: candidate, toPath }),\n rekeyedKeys,\n ),\n )\n const existingDestinationOptions = destinationIndex\n ? getDestinationIndexOptions({\n fromPath: toPath,\n index: destinationIndex,\n toPath,\n })\n : undefined\n\n if (!destinationIndex || !isDeepStrictEqual(existingDestinationOptions, destinationOptions)) {\n throw err\n }\n }\n\n await collection.dropIndex(index.name!)\n }\n}\n\nasync function rekeyTextIndex({\n collection,\n destinationOptions,\n index,\n rekeyedKeys,\n sourceKeys,\n}: {\n collection: Collection\n destinationOptions: CreateIndexesOptions\n index: IndexDescriptionInfo\n rekeyedKeys: Record<string, IndexDirection>\n sourceKeys: Record<string, IndexDirection>\n}): Promise<void> {\n if (!index.name) {\n throw new Error('Cannot re-key an unnamed MongoDB text index.')\n }\n\n const sourceOptions: CreateIndexesOptions = {\n ...getDestinationIndexOptions({ fromPath: '', index, toPath: '' }),\n name: index.name,\n }\n\n await collection.dropIndex(index.name)\n\n try {\n await collection.createIndex(rekeyedKeys, destinationOptions)\n } catch (err) {\n try {\n await collection.createIndex(sourceKeys, sourceOptions)\n } catch (restoreErr) {\n throw new AggregateError(\n [err, restoreErr],\n `Failed to create the replacement for MongoDB text index \"${index.name}\" and failed to restore the source index.`,\n )\n }\n\n throw err\n }\n}\n\nfunction getIndexKeysForCreate({\n fromPath,\n index,\n toPath,\n}: {\n fromPath: string\n index: IndexDescriptionInfo\n toPath: string\n}): Record<string, IndexDirection> {\n const keys: [string, IndexDirection][] = []\n const hasInternalTextKeys = '_fts' in index.key && '_ftsx' in index.key\n\n for (const [key, value] of Object.entries(index.key)) {\n if (hasInternalTextKeys && key === '_fts') {\n for (const textPath of Object.keys(index.weights ?? {})) {\n keys.push([rekeyPath({ fromPath, path: textPath, toPath }), 'text'])\n }\n continue\n }\n\n if (hasInternalTextKeys && key === '_ftsx') {\n continue\n }\n\n keys.push([rekeyPath({ fromPath, path: key, toPath }), value])\n }\n\n return Object.fromEntries(keys)\n}\n\nfunction getDestinationIndexOptions({\n fromPath,\n index,\n toPath,\n}: {\n fromPath: string\n index: IndexDescriptionInfo\n toPath: string\n}): CreateIndexesOptions {\n const options: CreateIndexesOptions = {\n '2dsphereIndexVersion': index['2dsphereIndexVersion'],\n background: index.background,\n bits: index.bits,\n bucketSize: index.bucketSize,\n collation: index.collation,\n default_language: index.default_language,\n expireAfterSeconds: index.expireAfterSeconds,\n hidden: index.hidden,\n language_override: index.language_override,\n max: index.max,\n min: index.min,\n partialFilterExpression: index.partialFilterExpression\n ? rekeyDocumentPaths({ document: index.partialFilterExpression, fromPath, toPath })\n : undefined,\n sparse: index.sparse,\n storageEngine: index.storageEngine,\n textIndexVersion: index.textIndexVersion,\n unique: index.unique,\n weights: index.weights\n ? rekeyDocumentPaths({ document: index.weights, fromPath, toPath })\n : undefined,\n wildcardProjection: index.wildcardProjection\n ? rekeyDocumentPaths({ document: index.wildcardProjection, fromPath, toPath })\n : undefined,\n }\n\n return Object.fromEntries(Object.entries(options).filter(([, value]) => value !== undefined))\n}\n\nfunction rekeyDocumentPaths({\n document,\n fromPath,\n toPath,\n}: {\n document: Document\n fromPath: string\n toPath: string\n}): Document {\n return Object.fromEntries(\n Object.entries(document).map(([key, value]) => [\n rekeyPath({ fromPath, path: key, toPath }),\n rekeyDocumentValue({ fromPath, toPath, value }),\n ]),\n )\n}\n\nfunction isPathAtOrBelow({ key, path }: { key: string; path: string }): boolean {\n return key === path || key.startsWith(`${path}.`)\n}\n\nfunction rekeyPath({\n fromPath,\n path,\n toPath,\n}: {\n fromPath: string\n path: string\n toPath: string\n}): string {\n return isPathAtOrBelow({ key: path, path: fromPath })\n ? `${toPath}${path.slice(fromPath.length)}`\n : path\n}\n\nfunction rekeyDocumentValue({\n fromPath,\n toPath,\n value,\n}: {\n fromPath: string\n toPath: string\n value: unknown\n}): unknown {\n if (Array.isArray(value)) {\n return value.map((item) => rekeyDocumentValue({ fromPath, toPath, value: item }))\n }\n\n if (\n value !== null &&\n typeof value === 'object' &&\n (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null)\n ) {\n return rekeyDocumentPaths({ document: value, fromPath, toPath })\n }\n\n return value\n}\n"],"names":["isDeepStrictEqual","getSession","INDEX_CONFLICT_CODES","Set","NAMESPACE_NOT_FOUND_CODE","GENERATED_VARIANT_METADATA_FIELDS","migrateSizesToVariants","direction","payload","req","adapter","db","session","from","to","targets","collection","config","collections","upload","undefined","expectedVariantNames","variants","map","name","push","slug","fromPath","toPath","versions","sourceData","directCollision","Promise","all","findOne","$exists","projection","_id","findUnexpectedDestinationData","Error","collectionName","target","rekeyIndexes","result","updateMany","$rename","logger","info","msg","modifiedCount","expectedVariantNameSet","cursor","find","document","destinationData","getDocumentPath","path","hasUnexpectedVariantData","close","split","reduce","value","pathSegment","isRecord","variantName","variantData","Object","entries","has","keys","some","metadataFieldName","Array","isArray","indexes","err","code","index","sourceKeys","getIndexKeysForCreate","key","isPathAtOrBelow","rekeyedKeys","destinationOptions","getDestinationIndexOptions","weights","rekeyTextIndex","createIndex","destinationIndex","candidate","existingDestinationOptions","dropIndex","sourceOptions","restoreErr","AggregateError","hasInternalTextKeys","textPath","rekeyPath","fromEntries","options","background","bits","bucketSize","collation","default_language","expireAfterSeconds","hidden","language_override","max","min","partialFilterExpression","rekeyDocumentPaths","sparse","storageEngine","textIndexVersion","unique","wildcardProjection","filter","rekeyDocumentValue","startsWith","slice","length","item","getPrototypeOf","prototype"],"mappings":"AASA,SAASA,iBAAiB,QAAQ,YAAW;AAI7C,SAASC,UAAU,QAAQ,6BAA4B;AAEvD,0EAA0E,GAC1E,MAAMC,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 -1,11 +1,13 @@
|
|
|
1
|
-
import type { FlattenedField, Payload, Where } from 'payload';
|
|
2
|
-
export declare function buildAndOrConditions({ collectionSlug, fields, globalSlug, locale, parentIsLocalized, payload, where, }: {
|
|
1
|
+
import type { FlattenedField, Payload, PayloadRequest, Where } from 'payload';
|
|
2
|
+
export declare function buildAndOrConditions({ branch, collectionSlug, fields, globalSlug, locale, parentIsLocalized, payload, req, where, }: {
|
|
3
|
+
branch?: false | string;
|
|
3
4
|
collectionSlug?: string;
|
|
4
5
|
fields: FlattenedField[];
|
|
5
6
|
globalSlug?: string;
|
|
6
7
|
locale?: string;
|
|
7
8
|
parentIsLocalized: boolean;
|
|
8
9
|
payload: Payload;
|
|
10
|
+
req?: Partial<PayloadRequest>;
|
|
9
11
|
where: Where[];
|
|
10
12
|
}): Promise<Record<string, unknown>[]>;
|
|
11
13
|
//# sourceMappingURL=buildAndOrConditions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildAndOrConditions.d.ts","sourceRoot":"","sources":["../../src/queries/buildAndOrConditions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"buildAndOrConditions.d.ts","sourceRoot":"","sources":["../../src/queries/buildAndOrConditions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAI7E,wBAAsB,oBAAoB,CAAC,EACzC,MAAM,EACN,cAAc,EACd,MAAM,EACN,UAAU,EACV,MAAM,EACN,iBAAiB,EACjB,OAAO,EACP,GAAG,EACH,KAAK,GACN,EAAE;IACD,MAAM,CAAC,EAAE,KAAK,GAAG,MAAM,CAAA;IACvB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,MAAM,EAAE,cAAc,EAAE,CAAA;IACxB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,iBAAiB,EAAE,OAAO,CAAA;IAC1B,OAAO,EAAE,OAAO,CAAA;IAChB,GAAG,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,CAAA;IAC7B,KAAK,EAAE,KAAK,EAAE,CAAA;CACf,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,CAyBrC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { parseParams } from './parseParams.js';
|
|
2
|
-
export async function buildAndOrConditions({ collectionSlug, fields, globalSlug, locale, parentIsLocalized, payload, where }) {
|
|
2
|
+
export async function buildAndOrConditions({ branch, collectionSlug, fields, globalSlug, locale, parentIsLocalized, payload, req, where }) {
|
|
3
3
|
const completedConditions = [];
|
|
4
4
|
// Loop over all AND / OR operations and add them to the AND / OR query param
|
|
5
5
|
// Operations should come through as an array
|
|
@@ -7,12 +7,14 @@ export async function buildAndOrConditions({ collectionSlug, fields, globalSlug,
|
|
|
7
7
|
// If the operation is properly formatted as an object
|
|
8
8
|
if (typeof condition === 'object') {
|
|
9
9
|
const result = await parseParams({
|
|
10
|
+
branch,
|
|
10
11
|
collectionSlug,
|
|
11
12
|
fields,
|
|
12
13
|
globalSlug,
|
|
13
14
|
locale,
|
|
14
15
|
parentIsLocalized,
|
|
15
16
|
payload,
|
|
17
|
+
req,
|
|
16
18
|
where: condition
|
|
17
19
|
});
|
|
18
20
|
if (Object.keys(result).length > 0) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/queries/buildAndOrConditions.ts"],"sourcesContent":["import type { FlattenedField, Payload, Where } from 'payload'\n\nimport { parseParams } from './parseParams.js'\n\nexport async function buildAndOrConditions({\n collectionSlug,\n fields,\n globalSlug,\n locale,\n parentIsLocalized,\n payload,\n where,\n}: {\n collectionSlug?: string\n fields: FlattenedField[]\n globalSlug?: string\n locale?: string\n parentIsLocalized: boolean\n payload: Payload\n where: Where[]\n}): Promise<Record<string, unknown>[]> {\n const completedConditions = []\n // Loop over all AND / OR operations and add them to the AND / OR query param\n // Operations should come through as an array\n\n for (const condition of where) {\n // If the operation is properly formatted as an object\n if (typeof condition === 'object') {\n const result = await parseParams({\n collectionSlug,\n fields,\n globalSlug,\n locale,\n parentIsLocalized,\n payload,\n where: condition,\n })\n if (Object.keys(result).length > 0) {\n completedConditions.push(result)\n }\n }\n }\n return completedConditions\n}\n"],"names":["parseParams","buildAndOrConditions","collectionSlug","fields","globalSlug","locale","parentIsLocalized","payload","where","completedConditions","condition","result","Object","keys","length","push"],"mappings":"AAEA,SAASA,WAAW,QAAQ,mBAAkB;AAE9C,OAAO,eAAeC,qBAAqB,EACzCC,cAAc,EACdC,MAAM,EACNC,UAAU,EACVC,MAAM,EACNC,iBAAiB,EACjBC,OAAO,EACPC,KAAK,
|
|
1
|
+
{"version":3,"sources":["../../src/queries/buildAndOrConditions.ts"],"sourcesContent":["import type { FlattenedField, Payload, PayloadRequest, Where } from 'payload'\n\nimport { parseParams } from './parseParams.js'\n\nexport async function buildAndOrConditions({\n branch,\n collectionSlug,\n fields,\n globalSlug,\n locale,\n parentIsLocalized,\n payload,\n req,\n where,\n}: {\n branch?: false | string\n collectionSlug?: string\n fields: FlattenedField[]\n globalSlug?: string\n locale?: string\n parentIsLocalized: boolean\n payload: Payload\n req?: Partial<PayloadRequest>\n where: Where[]\n}): Promise<Record<string, unknown>[]> {\n const completedConditions = []\n // Loop over all AND / OR operations and add them to the AND / OR query param\n // Operations should come through as an array\n\n for (const condition of where) {\n // If the operation is properly formatted as an object\n if (typeof condition === 'object') {\n const result = await parseParams({\n branch,\n collectionSlug,\n fields,\n globalSlug,\n locale,\n parentIsLocalized,\n payload,\n req,\n where: condition,\n })\n if (Object.keys(result).length > 0) {\n completedConditions.push(result)\n }\n }\n }\n return completedConditions\n}\n"],"names":["parseParams","buildAndOrConditions","branch","collectionSlug","fields","globalSlug","locale","parentIsLocalized","payload","req","where","completedConditions","condition","result","Object","keys","length","push"],"mappings":"AAEA,SAASA,WAAW,QAAQ,mBAAkB;AAE9C,OAAO,eAAeC,qBAAqB,EACzCC,MAAM,EACNC,cAAc,EACdC,MAAM,EACNC,UAAU,EACVC,MAAM,EACNC,iBAAiB,EACjBC,OAAO,EACPC,GAAG,EACHC,KAAK,EAWN;IACC,MAAMC,sBAAsB,EAAE;IAC9B,6EAA6E;IAC7E,6CAA6C;IAE7C,KAAK,MAAMC,aAAaF,MAAO;QAC7B,sDAAsD;QACtD,IAAI,OAAOE,cAAc,UAAU;YACjC,MAAMC,SAAS,MAAMb,YAAY;gBAC/BE;gBACAC;gBACAC;gBACAC;gBACAC;gBACAC;gBACAC;gBACAC;gBACAC,OAAOE;YACT;YACA,IAAIE,OAAOC,IAAI,CAACF,QAAQG,MAAM,GAAG,GAAG;gBAClCL,oBAAoBM,IAAI,CAACJ;YAC3B;QACF;IACF;IACA,OAAOF;AACT"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { PipelineStage } from 'mongoose';
|
|
2
|
+
import type { MongooseAdapter } from '../index.js';
|
|
3
|
+
type BranchVisibilityStage = Exclude<PipelineStage, PipelineStage.Merge | PipelineStage.Out>;
|
|
4
|
+
export declare const buildBranchVisibilityStages: ({ adapter, branch, collectionSlug, mode, }: {
|
|
5
|
+
adapter: MongooseAdapter;
|
|
6
|
+
branch: string;
|
|
7
|
+
collectionSlug: string;
|
|
8
|
+
mode?: 'documents' | 'drafts' | 'history';
|
|
9
|
+
}) => BranchVisibilityStage[];
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=buildBranchVisibility.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buildBranchVisibility.d.ts","sourceRoot":"","sources":["../../src/queries/buildBranchVisibility.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAI7C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAIlD,KAAK,qBAAqB,GAAG,OAAO,CAAC,aAAa,EAAE,aAAa,CAAC,KAAK,GAAG,aAAa,CAAC,GAAG,CAAC,CAAA;AAE5F,eAAO,MAAM,2BAA2B,+CAKrC;IACD,OAAO,EAAE,eAAe,CAAA;IACxB,MAAM,EAAE,MAAM,CAAA;IACd,cAAc,EAAE,MAAM,CAAA;IACtB,IAAI,CAAC,EAAE,WAAW,GAAG,QAAQ,GAAG,SAAS,CAAA;CAC1C,KAAG,qBAAqB,EAmGxB,CAAA"}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { branchChangesCollectionSlug, MAIN_BRANCH } from 'payload';
|
|
2
|
+
const lookupField = '__payloadBranchChange';
|
|
3
|
+
export const buildBranchVisibilityStages = ({ adapter, branch, collectionSlug, mode = 'documents' })=>{
|
|
4
|
+
const branchChangesModel = adapter.collections[branchChangesCollectionSlug];
|
|
5
|
+
if (!branchChangesModel) {
|
|
6
|
+
throw new Error(`Collection model "${branchChangesCollectionSlug}" is not initialized.`);
|
|
7
|
+
}
|
|
8
|
+
const canonicalIDExpression = mode === 'documents' ? {
|
|
9
|
+
$ifNull: [
|
|
10
|
+
'$_branchDocID',
|
|
11
|
+
'$_id'
|
|
12
|
+
]
|
|
13
|
+
} : {
|
|
14
|
+
$ifNull: [
|
|
15
|
+
'$_branchParent',
|
|
16
|
+
'$parent'
|
|
17
|
+
]
|
|
18
|
+
};
|
|
19
|
+
const hasNoMatchingChange = {
|
|
20
|
+
$eq: [
|
|
21
|
+
{
|
|
22
|
+
$size: `$${lookupField}`
|
|
23
|
+
},
|
|
24
|
+
0
|
|
25
|
+
]
|
|
26
|
+
};
|
|
27
|
+
const operation = {
|
|
28
|
+
$arrayElemAt: [
|
|
29
|
+
`$${lookupField}.operation`,
|
|
30
|
+
0
|
|
31
|
+
]
|
|
32
|
+
};
|
|
33
|
+
const mainVisibility = mode === 'history' ? {
|
|
34
|
+
$or: [
|
|
35
|
+
hasNoMatchingChange,
|
|
36
|
+
{
|
|
37
|
+
$and: [
|
|
38
|
+
{
|
|
39
|
+
$ne: [
|
|
40
|
+
operation,
|
|
41
|
+
'create'
|
|
42
|
+
]
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
$or: [
|
|
46
|
+
{
|
|
47
|
+
$and: [
|
|
48
|
+
{
|
|
49
|
+
$eq: [
|
|
50
|
+
{
|
|
51
|
+
$arrayElemAt: [
|
|
52
|
+
`$${lookupField}.baseVersionID`,
|
|
53
|
+
0
|
|
54
|
+
]
|
|
55
|
+
},
|
|
56
|
+
null
|
|
57
|
+
]
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
$eq: [
|
|
61
|
+
{
|
|
62
|
+
$arrayElemAt: [
|
|
63
|
+
`$${lookupField}.baseVersionUpdatedAt`,
|
|
64
|
+
0
|
|
65
|
+
]
|
|
66
|
+
},
|
|
67
|
+
null
|
|
68
|
+
]
|
|
69
|
+
}
|
|
70
|
+
]
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
$eq: [
|
|
74
|
+
{
|
|
75
|
+
$toString: '$_id'
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
$arrayElemAt: [
|
|
79
|
+
`$${lookupField}.baseVersionID`,
|
|
80
|
+
0
|
|
81
|
+
]
|
|
82
|
+
}
|
|
83
|
+
]
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
$lte: [
|
|
87
|
+
'$updatedAt',
|
|
88
|
+
{
|
|
89
|
+
$arrayElemAt: [
|
|
90
|
+
`$${lookupField}.baseVersionUpdatedAt`,
|
|
91
|
+
0
|
|
92
|
+
]
|
|
93
|
+
}
|
|
94
|
+
]
|
|
95
|
+
}
|
|
96
|
+
]
|
|
97
|
+
}
|
|
98
|
+
]
|
|
99
|
+
}
|
|
100
|
+
]
|
|
101
|
+
} : hasNoMatchingChange;
|
|
102
|
+
const branchVisibility = mode === 'history' ? true : {
|
|
103
|
+
$ne: [
|
|
104
|
+
operation,
|
|
105
|
+
'delete'
|
|
106
|
+
]
|
|
107
|
+
};
|
|
108
|
+
return [
|
|
109
|
+
{
|
|
110
|
+
$match: {
|
|
111
|
+
_branch: {
|
|
112
|
+
$in: [
|
|
113
|
+
MAIN_BRANCH,
|
|
114
|
+
branch
|
|
115
|
+
]
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
{
|
|
120
|
+
$lookup: {
|
|
121
|
+
as: lookupField,
|
|
122
|
+
from: branchChangesModel.collection.name,
|
|
123
|
+
let: {
|
|
124
|
+
canonicalDocumentID: {
|
|
125
|
+
$toString: canonicalIDExpression
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
pipeline: [
|
|
129
|
+
{
|
|
130
|
+
$match: {
|
|
131
|
+
$expr: {
|
|
132
|
+
$and: [
|
|
133
|
+
{
|
|
134
|
+
$eq: [
|
|
135
|
+
'$branch',
|
|
136
|
+
branch
|
|
137
|
+
]
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
$eq: [
|
|
141
|
+
'$collectionSlug',
|
|
142
|
+
collectionSlug
|
|
143
|
+
]
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
$eq: [
|
|
147
|
+
'$documentID',
|
|
148
|
+
'$$canonicalDocumentID'
|
|
149
|
+
]
|
|
150
|
+
}
|
|
151
|
+
]
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
{
|
|
156
|
+
$project: {
|
|
157
|
+
baseVersionID: 1,
|
|
158
|
+
baseVersionUpdatedAt: 1,
|
|
159
|
+
operation: 1
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
$limit: 1
|
|
164
|
+
}
|
|
165
|
+
]
|
|
166
|
+
}
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
$match: {
|
|
170
|
+
$expr: {
|
|
171
|
+
$or: [
|
|
172
|
+
{
|
|
173
|
+
$and: [
|
|
174
|
+
{
|
|
175
|
+
$eq: [
|
|
176
|
+
'$_branch',
|
|
177
|
+
MAIN_BRANCH
|
|
178
|
+
]
|
|
179
|
+
},
|
|
180
|
+
mainVisibility
|
|
181
|
+
]
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
$and: [
|
|
185
|
+
{
|
|
186
|
+
$eq: [
|
|
187
|
+
'$_branch',
|
|
188
|
+
branch
|
|
189
|
+
]
|
|
190
|
+
},
|
|
191
|
+
branchVisibility
|
|
192
|
+
]
|
|
193
|
+
}
|
|
194
|
+
]
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
},
|
|
198
|
+
{
|
|
199
|
+
$unset: lookupField
|
|
200
|
+
}
|
|
201
|
+
];
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
//# sourceMappingURL=buildBranchVisibility.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/queries/buildBranchVisibility.ts"],"sourcesContent":["import type { PipelineStage } from 'mongoose'\n\nimport { branchChangesCollectionSlug, MAIN_BRANCH } from 'payload'\n\nimport type { MongooseAdapter } from '../index.js'\n\nconst lookupField = '__payloadBranchChange'\n\ntype BranchVisibilityStage = Exclude<PipelineStage, PipelineStage.Merge | PipelineStage.Out>\n\nexport const buildBranchVisibilityStages = ({\n adapter,\n branch,\n collectionSlug,\n mode = 'documents',\n}: {\n adapter: MongooseAdapter\n branch: string\n collectionSlug: string\n mode?: 'documents' | 'drafts' | 'history'\n}): BranchVisibilityStage[] => {\n const branchChangesModel = adapter.collections[branchChangesCollectionSlug]\n\n if (!branchChangesModel) {\n throw new Error(`Collection model \"${branchChangesCollectionSlug}\" is not initialized.`)\n }\n\n const canonicalIDExpression =\n mode === 'documents'\n ? { $ifNull: ['$_branchDocID', '$_id'] }\n : { $ifNull: ['$_branchParent', '$parent'] }\n const hasNoMatchingChange = { $eq: [{ $size: `$${lookupField}` }, 0] }\n const operation = { $arrayElemAt: [`$${lookupField}.operation`, 0] }\n const mainVisibility =\n mode === 'history'\n ? {\n $or: [\n hasNoMatchingChange,\n {\n $and: [\n { $ne: [operation, 'create'] },\n {\n $or: [\n {\n $and: [\n {\n $eq: [{ $arrayElemAt: [`$${lookupField}.baseVersionID`, 0] }, null],\n },\n {\n $eq: [\n { $arrayElemAt: [`$${lookupField}.baseVersionUpdatedAt`, 0] },\n null,\n ],\n },\n ],\n },\n {\n $eq: [\n { $toString: '$_id' },\n { $arrayElemAt: [`$${lookupField}.baseVersionID`, 0] },\n ],\n },\n {\n $lte: [\n '$updatedAt',\n { $arrayElemAt: [`$${lookupField}.baseVersionUpdatedAt`, 0] },\n ],\n },\n ],\n },\n ],\n },\n ],\n }\n : hasNoMatchingChange\n const branchVisibility = mode === 'history' ? true : { $ne: [operation, 'delete'] }\n\n return [\n { $match: { _branch: { $in: [MAIN_BRANCH, branch] } } },\n {\n $lookup: {\n as: lookupField,\n from: branchChangesModel.collection.name,\n let: { canonicalDocumentID: { $toString: canonicalIDExpression } },\n pipeline: [\n {\n $match: {\n $expr: {\n $and: [\n { $eq: ['$branch', branch] },\n { $eq: ['$collectionSlug', collectionSlug] },\n { $eq: ['$documentID', '$$canonicalDocumentID'] },\n ],\n },\n },\n },\n {\n $project: {\n baseVersionID: 1,\n baseVersionUpdatedAt: 1,\n operation: 1,\n },\n },\n { $limit: 1 },\n ],\n },\n },\n {\n $match: {\n $expr: {\n $or: [\n { $and: [{ $eq: ['$_branch', MAIN_BRANCH] }, mainVisibility] },\n { $and: [{ $eq: ['$_branch', branch] }, branchVisibility] },\n ],\n },\n },\n },\n { $unset: lookupField },\n ]\n}\n"],"names":["branchChangesCollectionSlug","MAIN_BRANCH","lookupField","buildBranchVisibilityStages","adapter","branch","collectionSlug","mode","branchChangesModel","collections","Error","canonicalIDExpression","$ifNull","hasNoMatchingChange","$eq","$size","operation","$arrayElemAt","mainVisibility","$or","$and","$ne","$toString","$lte","branchVisibility","$match","_branch","$in","$lookup","as","from","collection","name","let","canonicalDocumentID","pipeline","$expr","$project","baseVersionID","baseVersionUpdatedAt","$limit","$unset"],"mappings":"AAEA,SAASA,2BAA2B,EAAEC,WAAW,QAAQ,UAAS;AAIlE,MAAMC,cAAc;AAIpB,OAAO,MAAMC,8BAA8B,CAAC,EAC1CC,OAAO,EACPC,MAAM,EACNC,cAAc,EACdC,OAAO,WAAW,EAMnB;IACC,MAAMC,qBAAqBJ,QAAQK,WAAW,CAACT,4BAA4B;IAE3E,IAAI,CAACQ,oBAAoB;QACvB,MAAM,IAAIE,MAAM,CAAC,kBAAkB,EAAEV,4BAA4B,qBAAqB,CAAC;IACzF;IAEA,MAAMW,wBACJJ,SAAS,cACL;QAAEK,SAAS;YAAC;YAAiB;SAAO;IAAC,IACrC;QAAEA,SAAS;YAAC;YAAkB;SAAU;IAAC;IAC/C,MAAMC,sBAAsB;QAAEC,KAAK;YAAC;gBAAEC,OAAO,CAAC,CAAC,EAAEb,aAAa;YAAC;YAAG;SAAE;IAAC;IACrE,MAAMc,YAAY;QAAEC,cAAc;YAAC,CAAC,CAAC,EAAEf,YAAY,UAAU,CAAC;YAAE;SAAE;IAAC;IACnE,MAAMgB,iBACJX,SAAS,YACL;QACEY,KAAK;YACHN;YACA;gBACEO,MAAM;oBACJ;wBAAEC,KAAK;4BAACL;4BAAW;yBAAS;oBAAC;oBAC7B;wBACEG,KAAK;4BACH;gCACEC,MAAM;oCACJ;wCACEN,KAAK;4CAAC;gDAAEG,cAAc;oDAAC,CAAC,CAAC,EAAEf,YAAY,cAAc,CAAC;oDAAE;iDAAE;4CAAC;4CAAG;yCAAK;oCACrE;oCACA;wCACEY,KAAK;4CACH;gDAAEG,cAAc;oDAAC,CAAC,CAAC,EAAEf,YAAY,qBAAqB,CAAC;oDAAE;iDAAE;4CAAC;4CAC5D;yCACD;oCACH;iCACD;4BACH;4BACA;gCACEY,KAAK;oCACH;wCAAEQ,WAAW;oCAAO;oCACpB;wCAAEL,cAAc;4CAAC,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;KACvB;AACH,EAAC"}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
-
import type { FlattenedField, Where } from 'payload';
|
|
1
|
+
import type { FlattenedField, PayloadRequest, Where } from 'payload';
|
|
2
2
|
import type { MongooseAdapter } from '../index.js';
|
|
3
|
-
export declare const buildQuery: ({ adapter, collectionSlug, fields, globalSlug, locale, where, }: {
|
|
3
|
+
export declare const buildQuery: ({ adapter, branch, collectionSlug, fields, globalSlug, locale, req, where, }: {
|
|
4
4
|
adapter: MongooseAdapter;
|
|
5
|
+
branch?: false | string;
|
|
5
6
|
collectionSlug?: string;
|
|
6
7
|
fields: FlattenedField[];
|
|
7
8
|
globalSlug?: string;
|
|
8
9
|
locale?: string;
|
|
10
|
+
req?: Partial<PayloadRequest>;
|
|
9
11
|
where: Where;
|
|
10
12
|
}) => Promise<Record<string, unknown>>;
|
|
11
13
|
//# sourceMappingURL=buildQuery.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildQuery.d.ts","sourceRoot":"","sources":["../../src/queries/buildQuery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"buildQuery.d.ts","sourceRoot":"","sources":["../../src/queries/buildQuery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAEpE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAIlD,eAAO,MAAM,UAAU,iFASpB;IACD,OAAO,EAAE,eAAe,CAAA;IACxB,MAAM,CAAC,EAAE,KAAK,GAAG,MAAM,CAAA;IACvB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,MAAM,EAAE,cAAc,EAAE,CAAA;IACxB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,GAAG,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,CAAA;IAC7B,KAAK,EAAE,KAAK,CAAA;CACb,qCAcA,CAAA"}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import { parseParams } from './parseParams.js';
|
|
2
|
-
export const buildQuery = async ({ adapter, collectionSlug, fields, globalSlug, locale, where })=>{
|
|
2
|
+
export const buildQuery = async ({ adapter, branch, collectionSlug, fields, globalSlug, locale, req, where })=>{
|
|
3
3
|
const result = await parseParams({
|
|
4
|
+
branch,
|
|
4
5
|
collectionSlug,
|
|
5
6
|
fields,
|
|
6
7
|
globalSlug,
|
|
7
8
|
locale,
|
|
8
9
|
parentIsLocalized: false,
|
|
9
10
|
payload: adapter.payload,
|
|
11
|
+
req,
|
|
10
12
|
where
|
|
11
13
|
});
|
|
12
14
|
return result;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/queries/buildQuery.ts"],"sourcesContent":["import type { FlattenedField, Where } from 'payload'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { parseParams } from './parseParams.js'\n\nexport const buildQuery = async ({\n adapter,\n collectionSlug,\n fields,\n globalSlug,\n locale,\n where,\n}: {\n adapter: MongooseAdapter\n collectionSlug?: string\n fields: FlattenedField[]\n globalSlug?: string\n locale?: string\n where: Where\n}) => {\n const result = await parseParams({\n collectionSlug,\n fields,\n globalSlug,\n locale,\n parentIsLocalized: false,\n payload: adapter.payload,\n where,\n })\n\n return result\n}\n"],"names":["parseParams","buildQuery","adapter","collectionSlug","fields","globalSlug","locale","where","result","parentIsLocalized","payload"],"mappings":"AAIA,SAASA,WAAW,QAAQ,mBAAkB;AAE9C,OAAO,MAAMC,aAAa,OAAO,EAC/BC,OAAO,EACPC,cAAc,EACdC,MAAM,EACNC,UAAU,EACVC,MAAM,EACNC,KAAK,
|
|
1
|
+
{"version":3,"sources":["../../src/queries/buildQuery.ts"],"sourcesContent":["import type { FlattenedField, PayloadRequest, Where } from 'payload'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { parseParams } from './parseParams.js'\n\nexport const buildQuery = async ({\n adapter,\n branch,\n collectionSlug,\n fields,\n globalSlug,\n locale,\n req,\n where,\n}: {\n adapter: MongooseAdapter\n branch?: false | string\n collectionSlug?: string\n fields: FlattenedField[]\n globalSlug?: string\n locale?: string\n req?: Partial<PayloadRequest>\n where: Where\n}) => {\n const result = await parseParams({\n branch,\n collectionSlug,\n fields,\n globalSlug,\n locale,\n parentIsLocalized: false,\n payload: adapter.payload,\n req,\n where,\n })\n\n return result\n}\n"],"names":["parseParams","buildQuery","adapter","branch","collectionSlug","fields","globalSlug","locale","req","where","result","parentIsLocalized","payload"],"mappings":"AAIA,SAASA,WAAW,QAAQ,mBAAkB;AAE9C,OAAO,MAAMC,aAAa,OAAO,EAC/BC,OAAO,EACPC,MAAM,EACNC,cAAc,EACdC,MAAM,EACNC,UAAU,EACVC,MAAM,EACNC,GAAG,EACHC,KAAK,EAUN;IACC,MAAMC,SAAS,MAAMV,YAAY;QAC/BG;QACAC;QACAC;QACAC;QACAC;QACAI,mBAAmB;QACnBC,SAASV,QAAQU,OAAO;QACxBJ;QACAC;IACF;IAEA,OAAOC;AACT,EAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { PipelineStage } from 'mongoose';
|
|
2
|
+
/** Keep geospatial filtering first, before branch visibility introduces lookups. */
|
|
3
|
+
export declare const buildRelatedQueryPipeline: ({ branchVisibility, query, }: {
|
|
4
|
+
branchVisibility: PipelineStage[];
|
|
5
|
+
query: Record<string, unknown>;
|
|
6
|
+
}) => PipelineStage[];
|
|
7
|
+
//# sourceMappingURL=buildRelatedQueryPipeline.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buildRelatedQueryPipeline.d.ts","sourceRoot":"","sources":["../../src/queries/buildRelatedQueryPipeline.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAI7C,oFAAoF;AACpF,eAAO,MAAM,yBAAyB,iCAGnC;IACD,gBAAgB,EAAE,aAAa,EAAE,CAAA;IACjC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CAC/B,KAAG,aAAa,EAiDhB,CAAA"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { APIError } from 'payload';
|
|
2
|
+
/** Keep geospatial filtering first, before branch visibility introduces lookups. */ export const buildRelatedQueryPipeline = ({ branchVisibility, query })=>{
|
|
3
|
+
let geoNear;
|
|
4
|
+
const extractNear = ({ query })=>{
|
|
5
|
+
const remainingQuery = {};
|
|
6
|
+
for (const [key, value] of Object.entries(query)){
|
|
7
|
+
if (key === '$and' && Array.isArray(value)) {
|
|
8
|
+
remainingQuery[key] = value.map((query)=>extractNear({
|
|
9
|
+
query
|
|
10
|
+
}));
|
|
11
|
+
} else if (value && typeof value === 'object' && '$near' in value) {
|
|
12
|
+
if (geoNear) {
|
|
13
|
+
throw new APIError('Only one near constraint is supported per related query.', 400);
|
|
14
|
+
}
|
|
15
|
+
const { $near, ...remainingOperators } = value;
|
|
16
|
+
const near = $near;
|
|
17
|
+
geoNear = {
|
|
18
|
+
distanceField: '__payloadRelatedDistance',
|
|
19
|
+
key,
|
|
20
|
+
near: near.$geometry,
|
|
21
|
+
spherical: true,
|
|
22
|
+
...near.$maxDistance !== undefined ? {
|
|
23
|
+
maxDistance: near.$maxDistance
|
|
24
|
+
} : {},
|
|
25
|
+
...near.$minDistance !== undefined ? {
|
|
26
|
+
minDistance: near.$minDistance
|
|
27
|
+
} : {}
|
|
28
|
+
};
|
|
29
|
+
if (Object.keys(remainingOperators).length) {
|
|
30
|
+
remainingQuery[key] = remainingOperators;
|
|
31
|
+
}
|
|
32
|
+
} else {
|
|
33
|
+
remainingQuery[key] = value;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return remainingQuery;
|
|
37
|
+
};
|
|
38
|
+
// MongoDB permits $near at the top level or in $and, but not inside $or/$nor.
|
|
39
|
+
// Leave those unsupported forms intact so MongoDB still rejects invalid queries.
|
|
40
|
+
const remainingQuery = extractNear({
|
|
41
|
+
query
|
|
42
|
+
});
|
|
43
|
+
return [
|
|
44
|
+
geoNear ? {
|
|
45
|
+
$geoNear: {
|
|
46
|
+
...geoNear,
|
|
47
|
+
query: remainingQuery
|
|
48
|
+
}
|
|
49
|
+
} : {
|
|
50
|
+
$match: query
|
|
51
|
+
},
|
|
52
|
+
...branchVisibility
|
|
53
|
+
];
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
//# sourceMappingURL=buildRelatedQueryPipeline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/queries/buildRelatedQueryPipeline.ts"],"sourcesContent":["import type { PipelineStage } from 'mongoose'\n\nimport { APIError } from 'payload'\n\n/** Keep geospatial filtering first, before branch visibility introduces lookups. */\nexport const buildRelatedQueryPipeline = ({\n branchVisibility,\n query,\n}: {\n branchVisibility: PipelineStage[]\n query: Record<string, unknown>\n}): PipelineStage[] => {\n let geoNear: PipelineStage.GeoNear['$geoNear'] | undefined\n\n const extractNear = ({ query }: { query: Record<string, unknown> }): Record<string, unknown> => {\n const remainingQuery: Record<string, unknown> = {}\n\n for (const [key, value] of Object.entries(query)) {\n if (key === '$and' && Array.isArray(value)) {\n remainingQuery[key] = value.map((query) => extractNear({ query }))\n } else if (value && typeof value === 'object' && '$near' in value) {\n if (geoNear) {\n throw new APIError('Only one near constraint is supported per related query.', 400)\n }\n\n const { $near, ...remainingOperators } = value\n const near = $near as {\n $geometry: { coordinates: [number, number]; type: 'Point' }\n $maxDistance?: number\n $minDistance?: number\n }\n\n geoNear = {\n distanceField: '__payloadRelatedDistance',\n key,\n near: near.$geometry,\n spherical: true,\n ...(near.$maxDistance !== undefined ? { maxDistance: near.$maxDistance } : {}),\n ...(near.$minDistance !== undefined ? { minDistance: near.$minDistance } : {}),\n }\n\n if (Object.keys(remainingOperators).length) {\n remainingQuery[key] = remainingOperators\n }\n } else {\n remainingQuery[key] = value\n }\n }\n\n return remainingQuery\n }\n\n // MongoDB permits $near at the top level or in $and, but not inside $or/$nor.\n // Leave those unsupported forms intact so MongoDB still rejects invalid queries.\n const remainingQuery = extractNear({ query })\n\n return [\n geoNear ? { $geoNear: { ...geoNear, query: remainingQuery } } : { $match: query },\n ...branchVisibility,\n ]\n}\n"],"names":["APIError","buildRelatedQueryPipeline","branchVisibility","query","geoNear","extractNear","remainingQuery","key","value","Object","entries","Array","isArray","map","$near","remainingOperators","near","distanceField","$geometry","spherical","$maxDistance","undefined","maxDistance","$minDistance","minDistance","keys","length","$geoNear","$match"],"mappings":"AAEA,SAASA,QAAQ,QAAQ,UAAS;AAElC,kFAAkF,GAClF,OAAO,MAAMC,4BAA4B,CAAC,EACxCC,gBAAgB,EAChBC,KAAK,EAIN;IACC,IAAIC;IAEJ,MAAMC,cAAc,CAAC,EAAEF,KAAK,EAAsC;QAChE,MAAMG,iBAA0C,CAAC;QAEjD,KAAK,MAAM,CAACC,KAAKC,MAAM,IAAIC,OAAOC,OAAO,CAACP,OAAQ;YAChD,IAAII,QAAQ,UAAUI,MAAMC,OAAO,CAACJ,QAAQ;gBAC1CF,cAAc,CAACC,IAAI,GAAGC,MAAMK,GAAG,CAAC,CAACV,QAAUE,YAAY;wBAAEF;oBAAM;YACjE,OAAO,IAAIK,SAAS,OAAOA,UAAU,YAAY,WAAWA,OAAO;gBACjE,IAAIJ,SAAS;oBACX,MAAM,IAAIJ,SAAS,4DAA4D;gBACjF;gBAEA,MAAM,EAAEc,KAAK,EAAE,GAAGC,oBAAoB,GAAGP;gBACzC,MAAMQ,OAAOF;gBAMbV,UAAU;oBACRa,eAAe;oBACfV;oBACAS,MAAMA,KAAKE,SAAS;oBACpBC,WAAW;oBACX,GAAIH,KAAKI,YAAY,KAAKC,YAAY;wBAAEC,aAAaN,KAAKI,YAAY;oBAAC,IAAI,CAAC,CAAC;oBAC7E,GAAIJ,KAAKO,YAAY,KAAKF,YAAY;wBAAEG,aAAaR,KAAKO,YAAY;oBAAC,IAAI,CAAC,CAAC;gBAC/E;gBAEA,IAAId,OAAOgB,IAAI,CAACV,oBAAoBW,MAAM,EAAE;oBAC1CpB,cAAc,CAACC,IAAI,GAAGQ;gBACxB;YACF,OAAO;gBACLT,cAAc,CAACC,IAAI,GAAGC;YACxB;QACF;QAEA,OAAOF;IACT;IAEA,8EAA8E;IAC9E,iFAAiF;IACjF,MAAMA,iBAAiBD,YAAY;QAAEF;IAAM;IAE3C,OAAO;QACLC,UAAU;YAAEuB,UAAU;gBAAE,GAAGvB,OAAO;gBAAED,OAAOG;YAAe;QAAE,IAAI;YAAEsB,QAAQzB;QAAM;WAC7ED;KACJ;AACH,EAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { FlattenedField, Operator, Payload } from 'payload';
|
|
1
|
+
import type { FlattenedField, Operator, Payload, PayloadRequest } from 'payload';
|
|
2
2
|
type SearchParam = {
|
|
3
3
|
path?: string;
|
|
4
4
|
rawQuery?: unknown;
|
|
@@ -7,7 +7,8 @@ type SearchParam = {
|
|
|
7
7
|
/**
|
|
8
8
|
* Convert the Payload key / value / operator into a MongoDB query
|
|
9
9
|
*/
|
|
10
|
-
export declare function buildSearchParam({ collectionSlug, fields, globalSlug, incomingPath, locale, operator, parentIsLocalized, payload, val, }: {
|
|
10
|
+
export declare function buildSearchParam({ branch, collectionSlug, fields, globalSlug, incomingPath, locale, operator, parentIsLocalized, payload, req, val, }: {
|
|
11
|
+
branch?: false | string;
|
|
11
12
|
collectionSlug?: string;
|
|
12
13
|
fields: FlattenedField[];
|
|
13
14
|
globalSlug?: string;
|
|
@@ -16,6 +17,7 @@ export declare function buildSearchParam({ collectionSlug, fields, globalSlug, i
|
|
|
16
17
|
operator: Operator;
|
|
17
18
|
parentIsLocalized: boolean;
|
|
18
19
|
payload: Payload;
|
|
20
|
+
req?: Partial<PayloadRequest>;
|
|
19
21
|
val: unknown;
|
|
20
22
|
}): Promise<SearchParam | undefined>;
|
|
21
23
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildSearchParams.d.ts","sourceRoot":"","sources":["../../src/queries/buildSearchParams.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,cAAc,EAEd,QAAQ,EAER,OAAO,
|
|
1
|
+
{"version":3,"file":"buildSearchParams.d.ts","sourceRoot":"","sources":["../../src/queries/buildSearchParams.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,cAAc,EAEd,QAAQ,EAER,OAAO,EACP,cAAc,EAEf,MAAM,SAAS,CAAA;AA6BhB,KAAK,WAAW,GAAG;IACjB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB,CAAA;AAED;;GAEG;AACH,wBAAsB,gBAAgB,CAAC,EACrC,MAAM,EACN,cAAc,EACd,MAAM,EACN,UAAU,EACV,YAAY,EACZ,MAAM,EACN,QAAQ,EACR,iBAAiB,EACjB,OAAO,EACP,GAAG,EACH,GAAG,GACJ,EAAE;IACD,MAAM,CAAC,EAAE,KAAK,GAAG,MAAM,CAAA;IACvB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,MAAM,EAAE,cAAc,EAAE,CAAA;IACxB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,YAAY,EAAE,MAAM,CAAA;IACpB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,QAAQ,CAAA;IAClB,iBAAiB,EAAE,OAAO,CAAA;IAC1B,OAAO,EAAE,OAAO,CAAA;IAChB,GAAG,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,CAAA;IAC7B,GAAG,EAAE,OAAO,CAAA;CACb,GAAG,OAAO,CAAC,WAAW,GAAG,SAAS,CAAC,CA2bnC"}
|