@payloadcms/db-mongodb 3.8.1-canary.84084b6 → 3.10.0
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/count.d.ts.map +1 -1
- package/dist/count.js +20 -22
- package/dist/count.js.map +1 -1
- package/dist/countGlobalVersions.d.ts.map +1 -1
- package/dist/countGlobalVersions.js +20 -22
- package/dist/countGlobalVersions.js.map +1 -1
- package/dist/countVersions.d.ts.map +1 -1
- package/dist/countVersions.js +20 -22
- package/dist/countVersions.js.map +1 -1
- package/dist/create.d.ts.map +1 -1
- package/dist/create.js +23 -24
- package/dist/create.js.map +1 -1
- package/dist/createGlobal.d.ts.map +1 -1
- package/dist/createGlobal.js +22 -20
- package/dist/createGlobal.js.map +1 -1
- package/dist/createGlobalVersion.d.ts.map +1 -1
- package/dist/createGlobalVersion.js +36 -31
- package/dist/createGlobalVersion.js.map +1 -1
- package/dist/createVersion.d.ts.map +1 -1
- package/dist/createVersion.js +37 -32
- package/dist/createVersion.js.map +1 -1
- package/dist/deleteMany.d.ts.map +1 -1
- package/dist/deleteMany.js +7 -7
- package/dist/deleteMany.js.map +1 -1
- package/dist/deleteOne.d.ts.map +1 -1
- package/dist/deleteOne.js +18 -14
- package/dist/deleteOne.js.map +1 -1
- package/dist/deleteVersions.d.ts.map +1 -1
- package/dist/deleteVersions.js +7 -7
- package/dist/deleteVersions.js.map +1 -1
- package/dist/find.d.ts.map +1 -1
- package/dist/find.js +41 -74
- package/dist/find.js.map +1 -1
- package/dist/findGlobal.d.ts.map +1 -1
- package/dist/findGlobal.js +20 -19
- package/dist/findGlobal.js.map +1 -1
- package/dist/findGlobalVersions.d.ts.map +1 -1
- package/dist/findGlobalVersions.js +34 -63
- package/dist/findGlobalVersions.js.map +1 -1
- package/dist/findOne.d.ts.map +1 -1
- package/dist/findOne.js +33 -21
- package/dist/findOne.js.map +1 -1
- package/dist/findVersions.d.ts.map +1 -1
- package/dist/findVersions.js +34 -62
- package/dist/findVersions.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/migrateFresh.d.ts.map +1 -1
- package/dist/migrateFresh.js.map +1 -1
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.d.ts.map +1 -1
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js +20 -16
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js.map +1 -1
- package/dist/predefinedMigrations/migrateVersionsV1_V2.js +4 -4
- package/dist/predefinedMigrations/migrateVersionsV1_V2.js.map +1 -1
- package/dist/queries/buildAndOrConditions.d.ts +3 -1
- package/dist/queries/buildAndOrConditions.d.ts.map +1 -1
- package/dist/queries/buildAndOrConditions.js +2 -1
- package/dist/queries/buildAndOrConditions.js.map +1 -1
- package/dist/queries/buildQuery.d.ts +2 -0
- package/dist/queries/buildQuery.d.ts.map +1 -1
- package/dist/queries/buildQuery.js +2 -6
- package/dist/queries/buildQuery.js.map +1 -1
- package/dist/queries/buildSearchParams.d.ts +3 -1
- package/dist/queries/buildSearchParams.d.ts.map +1 -1
- package/dist/queries/buildSearchParams.js +15 -10
- package/dist/queries/buildSearchParams.js.map +1 -1
- package/dist/queries/buildSortParam.d.ts +1 -7
- package/dist/queries/buildSortParam.d.ts.map +1 -1
- package/dist/queries/buildSortParam.js +2 -2
- package/dist/queries/buildSortParam.js.map +1 -1
- package/dist/queries/getLocalizedSortProperty.js +2 -2
- package/dist/queries/getLocalizedSortProperty.js.map +1 -1
- package/dist/queries/parseParams.d.ts +3 -1
- package/dist/queries/parseParams.d.ts.map +1 -1
- package/dist/queries/parseParams.js +2 -1
- package/dist/queries/parseParams.js.map +1 -1
- package/dist/queries/sanitizeQueryValue.d.ts +2 -1
- package/dist/queries/sanitizeQueryValue.d.ts.map +1 -1
- package/dist/queries/sanitizeQueryValue.js +33 -6
- package/dist/queries/sanitizeQueryValue.js.map +1 -1
- package/dist/queryDrafts.d.ts.map +1 -1
- package/dist/queryDrafts.js +42 -66
- package/dist/queryDrafts.js.map +1 -1
- package/dist/updateGlobal.d.ts.map +1 -1
- package/dist/updateGlobal.js +27 -24
- package/dist/updateGlobal.js.map +1 -1
- package/dist/updateGlobalVersion.d.ts.map +1 -1
- package/dist/updateGlobalVersion.js +30 -28
- package/dist/updateGlobalVersion.js.map +1 -1
- package/dist/updateOne.d.ts.map +1 -1
- package/dist/updateOne.js +30 -25
- package/dist/updateOne.js.map +1 -1
- package/dist/updateVersion.d.ts.map +1 -1
- package/dist/updateVersion.js +30 -27
- package/dist/updateVersion.js.map +1 -1
- package/dist/upsert.d.ts.map +1 -1
- package/dist/upsert.js +1 -1
- package/dist/upsert.js.map +1 -1
- package/dist/utilities/buildJoinAggregation.d.ts +4 -4
- package/dist/utilities/buildJoinAggregation.d.ts.map +1 -1
- package/dist/utilities/buildJoinAggregation.js +13 -30
- package/dist/utilities/buildJoinAggregation.js.map +1 -1
- package/dist/utilities/buildProjectionFromSelect.d.ts.map +1 -1
- package/dist/utilities/buildProjectionFromSelect.js +18 -1
- package/dist/utilities/buildProjectionFromSelect.js.map +1 -1
- package/dist/utilities/findMany.d.ts +20 -0
- package/dist/utilities/findMany.d.ts.map +1 -0
- package/dist/utilities/findMany.js +84 -0
- package/dist/utilities/findMany.js.map +1 -0
- package/dist/utilities/getHasNearConstraint.d.ts +3 -0
- package/dist/utilities/getHasNearConstraint.d.ts.map +1 -0
- package/dist/utilities/getHasNearConstraint.js +26 -0
- package/dist/utilities/getHasNearConstraint.js.map +1 -0
- package/dist/utilities/getSession.d.ts +9 -0
- package/dist/utilities/getSession.d.ts.map +1 -0
- package/dist/{withSession.js → utilities/getSession.js} +7 -6
- package/dist/utilities/getSession.js.map +1 -0
- package/dist/utilities/handleError.d.ts +3 -2
- package/dist/utilities/handleError.d.ts.map +1 -1
- package/dist/utilities/handleError.js +8 -7
- package/dist/utilities/handleError.js.map +1 -1
- package/dist/utilities/transform.d.ts +25 -0
- package/dist/utilities/transform.d.ts.map +1 -0
- package/dist/utilities/transform.js +254 -0
- package/dist/utilities/transform.js.map +1 -0
- package/dist/utilities/{sanitizeRelationshipIDs.spec.js → transform.spec.js} +23 -7
- package/dist/utilities/transform.spec.js.map +1 -0
- package/package.json +4 -4
- package/dist/utilities/sanitizeInternalFields.d.ts +0 -2
- package/dist/utilities/sanitizeInternalFields.d.ts.map +0 -1
- package/dist/utilities/sanitizeInternalFields.js +0 -20
- package/dist/utilities/sanitizeInternalFields.js.map +0 -1
- package/dist/utilities/sanitizeRelationshipIDs.d.ts +0 -9
- package/dist/utilities/sanitizeRelationshipIDs.d.ts.map +0 -1
- package/dist/utilities/sanitizeRelationshipIDs.js +0 -125
- package/dist/utilities/sanitizeRelationshipIDs.js.map +0 -1
- package/dist/utilities/sanitizeRelationshipIDs.spec.js.map +0 -1
- package/dist/withSession.d.ts +0 -11
- package/dist/withSession.d.ts.map +0 -1
- package/dist/withSession.js.map +0 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { buildVersionCollectionFields, buildVersionGlobalFields } from 'payload';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
const migrateModelWithBatching = async ({
|
|
2
|
+
import { getSession } from '../utilities/getSession.js';
|
|
3
|
+
import { transform } from '../utilities/transform.js';
|
|
4
|
+
const migrateModelWithBatching = async ({ adapter, batchSize, fields, Model, session })=>{
|
|
5
5
|
let hasNext = true;
|
|
6
6
|
let skip = 0;
|
|
7
7
|
while(hasNext){
|
|
@@ -19,10 +19,12 @@ const migrateModelWithBatching = async ({ batchSize, config, fields, Model, sess
|
|
|
19
19
|
docs.pop();
|
|
20
20
|
}
|
|
21
21
|
for (const doc of docs){
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
transform({
|
|
23
|
+
adapter,
|
|
24
24
|
data: doc,
|
|
25
|
-
fields
|
|
25
|
+
fields,
|
|
26
|
+
operation: 'update',
|
|
27
|
+
validateRelationships: false
|
|
26
28
|
});
|
|
27
29
|
}
|
|
28
30
|
await Model.collection.bulkWrite(docs.map((doc)=>({
|
|
@@ -77,13 +79,13 @@ export async function migrateRelationshipsV2_V3({ batchSize, req }) {
|
|
|
77
79
|
const { payload } = req;
|
|
78
80
|
const db = payload.db;
|
|
79
81
|
const config = payload.config;
|
|
80
|
-
const
|
|
82
|
+
const session = await getSession(db, req);
|
|
81
83
|
for (const collection of payload.config.collections.filter(hasRelationshipOrUploadField)){
|
|
82
84
|
payload.logger.info(`Migrating collection "${collection.slug}"`);
|
|
83
85
|
await migrateModelWithBatching({
|
|
86
|
+
adapter: db,
|
|
84
87
|
batchSize,
|
|
85
|
-
|
|
86
|
-
fields: collection.fields,
|
|
88
|
+
fields: collection.flattenedFields,
|
|
87
89
|
Model: db.collections[collection.slug],
|
|
88
90
|
session
|
|
89
91
|
});
|
|
@@ -91,9 +93,9 @@ export async function migrateRelationshipsV2_V3({ batchSize, req }) {
|
|
|
91
93
|
if (collection.versions) {
|
|
92
94
|
payload.logger.info(`Migrating collection versions "${collection.slug}"`);
|
|
93
95
|
await migrateModelWithBatching({
|
|
96
|
+
adapter: db,
|
|
94
97
|
batchSize,
|
|
95
|
-
config,
|
|
96
|
-
fields: buildVersionCollectionFields(config, collection),
|
|
98
|
+
fields: buildVersionCollectionFields(config, collection, true),
|
|
97
99
|
Model: db.versions[collection.slug],
|
|
98
100
|
session
|
|
99
101
|
});
|
|
@@ -113,10 +115,12 @@ export async function migrateRelationshipsV2_V3({ batchSize, req }) {
|
|
|
113
115
|
});
|
|
114
116
|
// in case if the global doesn't exist in the database yet (not saved)
|
|
115
117
|
if (doc) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
+
transform({
|
|
119
|
+
adapter: db,
|
|
118
120
|
data: doc,
|
|
119
|
-
fields: global.
|
|
121
|
+
fields: global.flattenedFields,
|
|
122
|
+
operation: 'update',
|
|
123
|
+
validateRelationships: false
|
|
120
124
|
});
|
|
121
125
|
await GlobalsModel.collection.updateOne({
|
|
122
126
|
globalType: global.slug
|
|
@@ -130,9 +134,9 @@ export async function migrateRelationshipsV2_V3({ batchSize, req }) {
|
|
|
130
134
|
if (global.versions) {
|
|
131
135
|
payload.logger.info(`Migrating global versions "${global.slug}"`);
|
|
132
136
|
await migrateModelWithBatching({
|
|
137
|
+
adapter: db,
|
|
133
138
|
batchSize,
|
|
134
|
-
config,
|
|
135
|
-
fields: buildVersionGlobalFields(config, global),
|
|
139
|
+
fields: buildVersionGlobalFields(config, global, true),
|
|
136
140
|
Model: db.versions[global.slug],
|
|
137
141
|
session
|
|
138
142
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"sourcesContent":["import type { ClientSession, Model } from 'mongoose'\nimport type { Field,
|
|
1
|
+
{"version":3,"sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"sourcesContent":["import type { ClientSession, Model } from 'mongoose'\nimport type { Field, FlattenedField, PayloadRequest } 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 adapter,\n batchSize,\n fields,\n Model,\n session,\n}: {\n adapter: MongooseAdapter\n batchSize: number\n fields: FlattenedField[]\n Model: Model<any>\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, data: doc, fields, operation: 'update', validateRelationships: false })\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 (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.filter(hasRelationshipOrUploadField)) {\n payload.logger.info(`Migrating collection \"${collection.slug}\"`)\n\n await migrateModelWithBatching({\n adapter: db,\n batchSize,\n fields: collection.flattenedFields,\n Model: db.collections[collection.slug],\n session,\n })\n\n payload.logger.info(`Migrated collection \"${collection.slug}\"`)\n\n if (collection.versions) {\n payload.logger.info(`Migrating collection versions \"${collection.slug}\"`)\n\n await migrateModelWithBatching({\n adapter: db,\n batchSize,\n fields: buildVersionCollectionFields(config, collection, true),\n Model: db.versions[collection.slug],\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.filter(hasRelationshipOrUploadField)) {\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.flattenedFields,\n operation: 'update',\n validateRelationships: false,\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 if (global.versions) {\n payload.logger.info(`Migrating global versions \"${global.slug}\"`)\n\n await migrateModelWithBatching({\n adapter: db,\n batchSize,\n fields: buildVersionGlobalFields(config, global, true),\n Model: db.versions[global.slug],\n session,\n })\n\n payload.logger.info(`Migrated global versions \"${global.slug}\"`)\n }\n }\n}\n"],"names":["buildVersionCollectionFields","buildVersionGlobalFields","getSession","transform","migrateModelWithBatching","adapter","batchSize","fields","Model","session","hasNext","skip","docs","find","lean","limit","length","pop","doc","data","operation","validateRelationships","collection","bulkWrite","map","updateOne","filter","_id","update","$set","hasRelationshipOrUploadField","field","type","block","blocks","tab","tabs","migrateRelationshipsV2_V3","req","payload","db","config","collections","logger","info","slug","flattenedFields","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,OAAO,EACPC,SAAS,EACTC,MAAM,EACNC,KAAK,EACLC,OAAO,EAOR;IACC,IAAIC,UAAU;IACd,IAAIC,OAAO;IAEX,MAAOD,QAAS;QACd,MAAME,OAAO,MAAMJ,MAAMK,IAAI,CAC3B,CAAC,GACD,CAAC,GACD;YACEC,MAAM;YACNC,OAAOT,YAAY;YACnBG;YACAE;QACF;QAGF,IAAIC,KAAKI,MAAM,KAAK,GAAG;YACrB;QACF;QAEAN,UAAUE,KAAKI,MAAM,GAAGV;QAExB,IAAII,SAAS;YACXE,KAAKK,GAAG;QACV;QAEA,KAAK,MAAMC,OAAON,KAAM;YACtBT,UAAU;gBAAEE;gBAASc,MAAMD;gBAAKX;gBAAQa,WAAW;gBAAUC,uBAAuB;YAAM;QAC5F;QAEA,MAAMb,MAAMc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YAAYF,OAAOL,IAAI;YACzB,GACA;gBAAEhB,MAAMX;YAAI,GACZ;gBAAET;YAAQ;QAEd;QAEA8B,QAAQI,MAAM,CAACC,IAAI,CAAC,CAAC,iBAAiB,EAAEM,OAAOL,IAAI,CAAC,CAAC,CAAC;QAEtD,IAAIK,OAAOH,QAAQ,EAAE;YACnBR,QAAQI,MAAM,CAACC,IAAI,CAAC,CAAC,2BAA2B,EAAEM,OAAOL,IAAI,CAAC,CAAC,CAAC;YAEhE,MAAMzC,yBAAyB;gBAC7BC,SAASmC;gBACTlC;gBACAC,QAAQN,yBAAyBwC,QAAQS,QAAQ;gBACjD1C,OAAOgC,GAAGO,QAAQ,CAACG,OAAOL,IAAI,CAAC;gBAC/BpC;YACF;YAEA8B,QAAQI,MAAM,CAACC,IAAI,CAAC,CAAC,0BAA0B,EAAEM,OAAOL,IAAI,CAAC,CAAC,CAAC;QACjE;IACF;AACF"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { getSession } from '../utilities/getSession.js';
|
|
2
2
|
export async function migrateVersionsV1_V2({ req }) {
|
|
3
3
|
const { payload } = req;
|
|
4
|
-
const
|
|
4
|
+
const session = await getSession(payload.db, req);
|
|
5
5
|
// For each collection
|
|
6
6
|
for (const { slug, versions } of payload.config.collections){
|
|
7
7
|
if (versions?.drafts) {
|
|
@@ -87,10 +87,10 @@ async function migrateCollectionDocs({ slug, docsAtATime = 100, payload, session
|
|
|
87
87
|
}
|
|
88
88
|
return;
|
|
89
89
|
}
|
|
90
|
-
const
|
|
90
|
+
const remainingDocIDs = remainingDocs.map((doc)=>doc._versionID);
|
|
91
91
|
await VersionsModel.updateMany({
|
|
92
92
|
_id: {
|
|
93
|
-
$in:
|
|
93
|
+
$in: remainingDocIDs
|
|
94
94
|
}
|
|
95
95
|
}, {
|
|
96
96
|
latest: true
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/predefinedMigrations/migrateVersionsV1_V2.ts"],"sourcesContent":["import type { ClientSession } from 'mongoose'\nimport type { Payload, PayloadRequest } from 'payload'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport {
|
|
1
|
+
{"version":3,"sources":["../../src/predefinedMigrations/migrateVersionsV1_V2.ts"],"sourcesContent":["import type { ClientSession } from 'mongoose'\nimport type { Payload, PayloadRequest } from 'payload'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { getSession } from '../utilities/getSession.js'\n\nexport async function migrateVersionsV1_V2({ req }: { req: PayloadRequest }) {\n const { payload } = req\n\n const session = await getSession(payload.db as MongooseAdapter, req)\n\n // For each collection\n\n for (const { slug, versions } of payload.config.collections) {\n if (versions?.drafts) {\n await migrateCollectionDocs({ slug, payload, session })\n\n payload.logger.info(`Migrated the \"${slug}\" collection.`)\n }\n }\n\n // For each global\n for (const { slug, versions } of payload.config.globals) {\n if (versions) {\n const VersionsModel = payload.db.versions[slug]\n\n await VersionsModel.findOneAndUpdate(\n {},\n { latest: true },\n {\n session,\n sort: { updatedAt: -1 },\n },\n ).exec()\n\n payload.logger.info(`Migrated the \"${slug}\" global.`)\n }\n }\n}\n\nasync function migrateCollectionDocs({\n slug,\n docsAtATime = 100,\n payload,\n session,\n}: {\n docsAtATime?: number\n payload: Payload\n session: ClientSession\n slug: string\n}) {\n const VersionsModel = payload.db.versions[slug]\n const remainingDocs = await VersionsModel.aggregate(\n [\n // Sort so that newest are first\n {\n $sort: {\n updatedAt: -1,\n },\n },\n // Group by parent ID\n // take the $first of each\n {\n $group: {\n _id: '$parent',\n _versionID: { $first: '$_id' },\n createdAt: { $first: '$createdAt' },\n latest: { $first: '$latest' },\n updatedAt: { $first: '$updatedAt' },\n version: { $first: '$version' },\n },\n },\n {\n $match: {\n latest: { $eq: null },\n },\n },\n {\n $limit: docsAtATime,\n },\n ],\n {\n allowDiskUse: true,\n session,\n },\n ).exec()\n\n if (!remainingDocs || remainingDocs.length === 0) {\n const newVersions = await VersionsModel.find(\n {\n latest: {\n $eq: true,\n },\n },\n undefined,\n { session },\n )\n\n if (newVersions?.length) {\n payload.logger.info(\n `Migrated ${newVersions.length} documents in the \"${slug}\" versions collection.`,\n )\n }\n\n return\n }\n\n const remainingDocIDs = remainingDocs.map((doc) => doc._versionID)\n\n await VersionsModel.updateMany(\n {\n _id: {\n $in: remainingDocIDs,\n },\n },\n {\n latest: true,\n },\n {\n session,\n },\n )\n\n await migrateCollectionDocs({ slug, payload, session })\n}\n"],"names":["getSession","migrateVersionsV1_V2","req","payload","session","db","slug","versions","config","collections","drafts","migrateCollectionDocs","logger","info","globals","VersionsModel","findOneAndUpdate","latest","sort","updatedAt","exec","docsAtATime","remainingDocs","aggregate","$sort","$group","_id","_versionID","$first","createdAt","version","$match","$eq","$limit","allowDiskUse","length","newVersions","find","undefined","remainingDocIDs","map","doc","updateMany","$in"],"mappings":"AAKA,SAASA,UAAU,QAAQ,6BAA4B;AAEvD,OAAO,eAAeC,qBAAqB,EAAEC,GAAG,EAA2B;IACzE,MAAM,EAAEC,OAAO,EAAE,GAAGD;IAEpB,MAAME,UAAU,MAAMJ,WAAWG,QAAQE,EAAE,EAAqBH;IAEhE,sBAAsB;IAEtB,KAAK,MAAM,EAAEI,IAAI,EAAEC,QAAQ,EAAE,IAAIJ,QAAQK,MAAM,CAACC,WAAW,CAAE;QAC3D,IAAIF,UAAUG,QAAQ;YACpB,MAAMC,sBAAsB;gBAAEL;gBAAMH;gBAASC;YAAQ;YAErDD,QAAQS,MAAM,CAACC,IAAI,CAAC,CAAC,cAAc,EAAEP,KAAK,aAAa,CAAC;QAC1D;IACF;IAEA,kBAAkB;IAClB,KAAK,MAAM,EAAEA,IAAI,EAAEC,QAAQ,EAAE,IAAIJ,QAAQK,MAAM,CAACM,OAAO,CAAE;QACvD,IAAIP,UAAU;YACZ,MAAMQ,gBAAgBZ,QAAQE,EAAE,CAACE,QAAQ,CAACD,KAAK;YAE/C,MAAMS,cAAcC,gBAAgB,CAClC,CAAC,GACD;gBAAEC,QAAQ;YA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,cAAc2B,UAAU,CAC5B;QACEhB,KAAK;YACHiB,KAAKJ;QACP;IACF,GACA;QACEtB,QAAQ;IACV,GACA;QACEb;IACF;IAGF,MAAMO,sBAAsB;QAAEL;QAAMH;QAASC;IAAQ;AACvD"}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import type { ClientSession } from 'mongodb';
|
|
1
2
|
import type { FlattenedField, Payload, Where } from 'payload';
|
|
2
|
-
export declare function buildAndOrConditions({ collectionSlug, fields, globalSlug, locale, payload, where, }: {
|
|
3
|
+
export declare function buildAndOrConditions({ collectionSlug, fields, globalSlug, locale, payload, session, where, }: {
|
|
3
4
|
collectionSlug?: string;
|
|
4
5
|
fields: FlattenedField[];
|
|
5
6
|
globalSlug?: string;
|
|
6
7
|
locale?: string;
|
|
7
8
|
payload: Payload;
|
|
9
|
+
session?: ClientSession;
|
|
8
10
|
where: Where[];
|
|
9
11
|
}): Promise<Record<string, unknown>[]>;
|
|
10
12
|
//# 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;AAI7D,wBAAsB,oBAAoB,CAAC,EACzC,cAAc,EACd,MAAM,EACN,UAAU,EACV,MAAM,EACN,OAAO,EACP,KAAK,GACN,EAAE;IACD,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,MAAM,EAAE,cAAc,EAAE,CAAA;IACxB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,OAAO,CAAA;IAChB,KAAK,EAAE,KAAK,EAAE,CAAA;CACf,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,
|
|
1
|
+
{"version":3,"file":"buildAndOrConditions.d.ts","sourceRoot":"","sources":["../../src/queries/buildAndOrConditions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAC5C,OAAO,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAI7D,wBAAsB,oBAAoB,CAAC,EACzC,cAAc,EACd,MAAM,EACN,UAAU,EACV,MAAM,EACN,OAAO,EACP,OAAO,EACP,KAAK,GACN,EAAE;IACD,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,MAAM,EAAE,cAAc,EAAE,CAAA;IACxB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,OAAO,CAAA;IAChB,OAAO,CAAC,EAAE,aAAa,CAAA;IACvB,KAAK,EAAE,KAAK,EAAE,CAAA;CACf,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,CAuBrC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { parseParams } from './parseParams.js';
|
|
2
|
-
export async function buildAndOrConditions({ collectionSlug, fields, globalSlug, locale, payload, where }) {
|
|
2
|
+
export async function buildAndOrConditions({ collectionSlug, fields, globalSlug, locale, payload, session, 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
|
|
@@ -12,6 +12,7 @@ export async function buildAndOrConditions({ collectionSlug, fields, globalSlug,
|
|
|
12
12
|
globalSlug,
|
|
13
13
|
locale,
|
|
14
14
|
payload,
|
|
15
|
+
session,
|
|
15
16
|
where: condition
|
|
16
17
|
});
|
|
17
18
|
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 payload,\n where,\n}: {\n collectionSlug?: string\n fields: FlattenedField[]\n globalSlug?: string\n locale?: string\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 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","payload","where","completedConditions","condition","result","Object","keys","length","push"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/queries/buildAndOrConditions.ts"],"sourcesContent":["import type { ClientSession } from 'mongodb'\nimport 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 payload,\n session,\n where,\n}: {\n collectionSlug?: string\n fields: FlattenedField[]\n globalSlug?: string\n locale?: string\n payload: Payload\n session?: ClientSession\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 payload,\n session,\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","payload","session","where","completedConditions","condition","result","Object","keys","length","push"],"mappings":"AAGA,SAASA,WAAW,QAAQ,mBAAkB;AAE9C,OAAO,eAAeC,qBAAqB,EACzCC,cAAc,EACdC,MAAM,EACNC,UAAU,EACVC,MAAM,EACNC,OAAO,EACPC,OAAO,EACPC,KAAK,EASN;IACC,MAAMC,sBAAsB,EAAE;IAC9B,6EAA6E;IAC7E,6CAA6C;IAE7C,KAAK,MAAMC,aAAaF,MAAO;QAC7B,sDAAsD;QACtD,IAAI,OAAOE,cAAc,UAAU;YACjC,MAAMC,SAAS,MAAMX,YAAY;gBAC/BE;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"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ClientSession } from 'mongodb';
|
|
1
2
|
import type { FlattenedField, Payload, Where } from 'payload';
|
|
2
3
|
type GetBuildQueryPluginArgs = {
|
|
3
4
|
collectionSlug?: string;
|
|
@@ -7,6 +8,7 @@ export type BuildQueryArgs = {
|
|
|
7
8
|
globalSlug?: string;
|
|
8
9
|
locale?: string;
|
|
9
10
|
payload: Payload;
|
|
11
|
+
session?: ClientSession;
|
|
10
12
|
where: Where;
|
|
11
13
|
};
|
|
12
14
|
export declare const getBuildQueryPlugin: ({ collectionSlug, versionsFields, }?: GetBuildQueryPluginArgs) => (schema: any) => void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildQuery.d.ts","sourceRoot":"","sources":["../../src/queries/buildQuery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,OAAO,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,aAAa,EAAE,MAAM,SAAS,CAAA;AAC5C,OAAO,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAI7D,KAAK,uBAAuB,GAAG;IAC7B,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,cAAc,CAAC,EAAE,cAAc,EAAE,CAAA;CAClC,CAAA;AAED,MAAM,MAAM,cAAc,GAAG;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,OAAO,CAAA;IAChB,OAAO,CAAC,EAAE,aAAa,CAAA;IACvB,KAAK,EAAE,KAAK,CAAA;CACb,CAAA;AAID,eAAO,MAAM,mBAAmB,yCAG7B,uBAAuB,0BAoCzB,CAAA"}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { QueryError } from 'payload';
|
|
2
1
|
import { parseParams } from './parseParams.js';
|
|
3
2
|
// This plugin asynchronously builds a list of Mongoose query constraints
|
|
4
3
|
// which can then be used in subsequent Mongoose queries.
|
|
5
4
|
export const getBuildQueryPlugin = ({ collectionSlug, versionsFields } = {})=>{
|
|
6
5
|
return function buildQueryPlugin(schema) {
|
|
7
6
|
const modifiedSchema = schema;
|
|
8
|
-
async function buildQuery({ globalSlug, locale, payload, where }) {
|
|
7
|
+
async function buildQuery({ globalSlug, locale, payload, session, where }) {
|
|
9
8
|
let fields = versionsFields;
|
|
10
9
|
if (!fields) {
|
|
11
10
|
if (globalSlug) {
|
|
@@ -17,18 +16,15 @@ export const getBuildQueryPlugin = ({ collectionSlug, versionsFields } = {})=>{
|
|
|
17
16
|
fields = collectionConfig.flattenedFields;
|
|
18
17
|
}
|
|
19
18
|
}
|
|
20
|
-
const errors = [];
|
|
21
19
|
const result = await parseParams({
|
|
22
20
|
collectionSlug,
|
|
23
21
|
fields,
|
|
24
22
|
globalSlug,
|
|
25
23
|
locale,
|
|
26
24
|
payload,
|
|
25
|
+
session,
|
|
27
26
|
where
|
|
28
27
|
});
|
|
29
|
-
if (errors.length > 0) {
|
|
30
|
-
throw new QueryError(errors);
|
|
31
|
-
}
|
|
32
28
|
return result;
|
|
33
29
|
}
|
|
34
30
|
modifiedSchema.statics.buildQuery = buildQuery;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/queries/buildQuery.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"sources":["../../src/queries/buildQuery.ts"],"sourcesContent":["import type { ClientSession } from 'mongodb'\nimport type { FlattenedField, Payload, Where } from 'payload'\n\nimport { parseParams } from './parseParams.js'\n\ntype GetBuildQueryPluginArgs = {\n collectionSlug?: string\n versionsFields?: FlattenedField[]\n}\n\nexport type BuildQueryArgs = {\n globalSlug?: string\n locale?: string\n payload: Payload\n session?: ClientSession\n where: Where\n}\n\n// This plugin asynchronously builds a list of Mongoose query constraints\n// which can then be used in subsequent Mongoose queries.\nexport const getBuildQueryPlugin = ({\n collectionSlug,\n versionsFields,\n}: GetBuildQueryPluginArgs = {}) => {\n return function buildQueryPlugin(schema) {\n const modifiedSchema = schema\n async function buildQuery({\n globalSlug,\n locale,\n payload,\n session,\n where,\n }: BuildQueryArgs): Promise<Record<string, unknown>> {\n let fields = versionsFields\n if (!fields) {\n if (globalSlug) {\n const globalConfig = payload.globals.config.find(({ slug }) => slug === globalSlug)\n fields = globalConfig.flattenedFields\n }\n if (collectionSlug) {\n const collectionConfig = payload.collections[collectionSlug].config\n fields = collectionConfig.flattenedFields\n }\n }\n\n const result = await parseParams({\n collectionSlug,\n fields,\n globalSlug,\n locale,\n payload,\n session,\n where,\n })\n\n return result\n }\n modifiedSchema.statics.buildQuery = buildQuery\n }\n}\n"],"names":["parseParams","getBuildQueryPlugin","collectionSlug","versionsFields","buildQueryPlugin","schema","modifiedSchema","buildQuery","globalSlug","locale","payload","session","where","fields","globalConfig","globals","config","find","slug","flattenedFields","collectionConfig","collections","result","statics"],"mappings":"AAGA,SAASA,WAAW,QAAQ,mBAAkB;AAe9C,yEAAyE;AACzE,yDAAyD;AACzD,OAAO,MAAMC,sBAAsB,CAAC,EAClCC,cAAc,EACdC,cAAc,EACU,GAAG,CAAC,CAAC;IAC7B,OAAO,SAASC,iBAAiBC,MAAM;QACrC,MAAMC,iBAAiBD;QACvB,eAAeE,WAAW,EACxBC,UAAU,EACVC,MAAM,EACNC,OAAO,EACPC,OAAO,EACPC,KAAK,EACU;YACf,IAAIC,SAASV;YACb,IAAI,CAACU,QAAQ;gBACX,IAAIL,YAAY;oBACd,MAAMM,eAAeJ,QAAQK,OAAO,CAACC,MAAM,CAACC,IAAI,CAAC,CAAC,EAAEC,IAAI,EAAE,GAAKA,SAASV;oBACxEK,SAASC,aAAaK,eAAe;gBACvC;gBACA,IAAIjB,gBAAgB;oBAClB,MAAMkB,mBAAmBV,QAAQW,WAAW,CAACnB,eAAe,CAACc,MAAM;oBACnEH,SAASO,iBAAiBD,eAAe;gBAC3C;YACF;YAEA,MAAMG,SAAS,MAAMtB,YAAY;gBAC/BE;gBACAW;gBACAL;gBACAC;gBACAC;gBACAC;gBACAC;YACF;YAEA,OAAOU;QACT;QACAhB,eAAeiB,OAAO,CAAChB,UAAU,GAAGA;IACtC;AACF,EAAC"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ClientSession } from 'mongodb';
|
|
1
2
|
import type { FlattenedField, Payload } from 'payload';
|
|
2
3
|
type SearchParam = {
|
|
3
4
|
path?: string;
|
|
@@ -7,7 +8,7 @@ type SearchParam = {
|
|
|
7
8
|
/**
|
|
8
9
|
* Convert the Payload key / value / operator into a MongoDB query
|
|
9
10
|
*/
|
|
10
|
-
export declare function buildSearchParam({ collectionSlug, fields, globalSlug, incomingPath, locale, operator, payload, val, }: {
|
|
11
|
+
export declare function buildSearchParam({ collectionSlug, fields, globalSlug, incomingPath, locale, operator, payload, session, val, }: {
|
|
11
12
|
collectionSlug?: string;
|
|
12
13
|
fields: FlattenedField[];
|
|
13
14
|
globalSlug?: string;
|
|
@@ -15,6 +16,7 @@ export declare function buildSearchParam({ collectionSlug, fields, globalSlug, i
|
|
|
15
16
|
locale?: string;
|
|
16
17
|
operator: string;
|
|
17
18
|
payload: Payload;
|
|
19
|
+
session?: ClientSession;
|
|
18
20
|
val: unknown;
|
|
19
21
|
}): Promise<SearchParam>;
|
|
20
22
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildSearchParams.d.ts","sourceRoot":"","sources":["../../src/queries/buildSearchParams.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAyB,OAAO,EAAE,MAAM,SAAS,CAAA;AAW7E,KAAK,WAAW,GAAG;IACjB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB,CAAA;
|
|
1
|
+
{"version":3,"file":"buildSearchParams.d.ts","sourceRoot":"","sources":["../../src/queries/buildSearchParams.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAe,MAAM,SAAS,CAAA;AACzD,OAAO,KAAK,EAAE,cAAc,EAAyB,OAAO,EAAE,MAAM,SAAS,CAAA;AAW7E,KAAK,WAAW,GAAG;IACjB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,KAAK,CAAC,EAAE,OAAO,CAAA;CAChB,CAAA;AASD;;GAEG;AACH,wBAAsB,gBAAgB,CAAC,EACrC,cAAc,EACd,MAAM,EACN,UAAU,EACV,YAAY,EACZ,MAAM,EACN,QAAQ,EACR,OAAO,EACP,OAAO,EACP,GAAG,GACJ,EAAE;IACD,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,MAAM,CAAA;IAChB,OAAO,EAAE,OAAO,CAAA;IAChB,OAAO,CAAC,EAAE,aAAa,CAAA;IACvB,GAAG,EAAE,OAAO,CAAA;CACb,GAAG,OAAO,CAAC,WAAW,CAAC,CAgOvB"}
|
|
@@ -4,12 +4,14 @@ import { validOperators } from 'payload/shared';
|
|
|
4
4
|
import { operatorMap } from './operatorMap.js';
|
|
5
5
|
import { sanitizeQueryValue } from './sanitizeQueryValue.js';
|
|
6
6
|
const subQueryOptions = {
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
limit: 50,
|
|
8
|
+
projection: {
|
|
9
|
+
_id: true
|
|
10
|
+
}
|
|
9
11
|
};
|
|
10
12
|
/**
|
|
11
13
|
* Convert the Payload key / value / operator into a MongoDB query
|
|
12
|
-
*/ export async function buildSearchParam({ collectionSlug, fields, globalSlug, incomingPath, locale, operator, payload, val }) {
|
|
14
|
+
*/ export async function buildSearchParam({ collectionSlug, fields, globalSlug, incomingPath, locale, operator, payload, session, val }) {
|
|
13
15
|
// Replace GraphQL nested field double underscore formatting
|
|
14
16
|
let sanitizedPath = incomingPath.replace(/__/g, '.');
|
|
15
17
|
if (sanitizedPath === 'id') {
|
|
@@ -48,6 +50,7 @@ const subQueryOptions = {
|
|
|
48
50
|
const sanitizedQueryValue = sanitizeQueryValue({
|
|
49
51
|
field,
|
|
50
52
|
hasCustomID,
|
|
53
|
+
locale,
|
|
51
54
|
operator,
|
|
52
55
|
path,
|
|
53
56
|
payload,
|
|
@@ -86,14 +89,13 @@ const subQueryOptions = {
|
|
|
86
89
|
}
|
|
87
90
|
}
|
|
88
91
|
});
|
|
89
|
-
const result = await SubModel.find(subQuery,
|
|
92
|
+
const result = await SubModel.collection.find(subQuery, {
|
|
93
|
+
session,
|
|
94
|
+
...subQueryOptions
|
|
95
|
+
}).toArray();
|
|
90
96
|
const $in = [];
|
|
91
97
|
result.forEach((doc)=>{
|
|
92
|
-
|
|
93
|
-
$in.push(stringID);
|
|
94
|
-
if (Types.ObjectId.isValid(stringID)) {
|
|
95
|
-
$in.push(doc._id);
|
|
96
|
-
}
|
|
98
|
+
$in.push(doc._id);
|
|
97
99
|
});
|
|
98
100
|
if (pathsToQuery.length === 1) {
|
|
99
101
|
return {
|
|
@@ -113,7 +115,10 @@ const subQueryOptions = {
|
|
|
113
115
|
};
|
|
114
116
|
}
|
|
115
117
|
const subQuery = priorQueryResult.value;
|
|
116
|
-
const result = await SubModel.find(subQuery,
|
|
118
|
+
const result = await SubModel.collection.find(subQuery, {
|
|
119
|
+
session,
|
|
120
|
+
...subQueryOptions
|
|
121
|
+
}).toArray();
|
|
117
122
|
const $in = result.map((doc)=>doc._id);
|
|
118
123
|
// If it is the last recursion
|
|
119
124
|
// then pass through the search param
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/queries/buildSearchParams.ts"],"sourcesContent":["import type { FlattenedField, Operator, PathToQuery, Payload } from 'payload'\n\nimport { Types } from 'mongoose'\nimport { getLocalizedPaths } from 'payload'\nimport { validOperators } from 'payload/shared'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { operatorMap } from './operatorMap.js'\nimport { sanitizeQueryValue } from './sanitizeQueryValue.js'\n\ntype SearchParam = {\n path?: string\n rawQuery?: unknown\n value?: unknown\n}\n\nconst subQueryOptions = {\n lean: true,\n limit: 50,\n}\n\n/**\n * Convert the Payload key / value / operator into a MongoDB query\n */\nexport async function buildSearchParam({\n collectionSlug,\n fields,\n globalSlug,\n incomingPath,\n locale,\n operator,\n payload,\n val,\n}: {\n collectionSlug?: string\n fields: FlattenedField[]\n globalSlug?: string\n incomingPath: string\n locale?: string\n operator: string\n payload: Payload\n val: unknown\n}): Promise<SearchParam> {\n // Replace GraphQL nested field double underscore formatting\n let sanitizedPath = incomingPath.replace(/__/g, '.')\n if (sanitizedPath === 'id') {\n sanitizedPath = '_id'\n }\n\n let paths: PathToQuery[] = []\n\n let hasCustomID = false\n\n if (sanitizedPath === '_id') {\n const customIDFieldType = payload.collections[collectionSlug]?.customIDType\n\n let idFieldType: 'number' | 'text' = 'text'\n\n if (customIDFieldType) {\n idFieldType = customIDFieldType\n hasCustomID = true\n }\n\n paths.push({\n collectionSlug,\n complete: true,\n field: {\n name: 'id',\n type: idFieldType,\n } as FlattenedField,\n path: '_id',\n })\n } else {\n paths = getLocalizedPaths({\n collectionSlug,\n fields,\n globalSlug,\n incomingPath: sanitizedPath,\n locale,\n payload,\n })\n }\n\n const [{ field, path }] = paths\n if (path) {\n const sanitizedQueryValue = sanitizeQueryValue({\n field,\n hasCustomID,\n operator,\n path,\n payload,\n val,\n })\n\n if (!sanitizedQueryValue) {\n return undefined\n }\n\n const { operator: formattedOperator, rawQuery, val: formattedValue } = sanitizedQueryValue\n\n if (rawQuery) {\n return { value: rawQuery }\n }\n\n // If there are multiple collections to search through,\n // Recursively build up a list of query constraints\n if (paths.length > 1) {\n // Remove top collection and reverse array\n // to work backwards from top\n const pathsToQuery = paths.slice(1).reverse()\n\n const initialRelationshipQuery = {\n value: {},\n } as SearchParam\n\n const relationshipQuery = await pathsToQuery.reduce(\n async (priorQuery, { collectionSlug: slug, path: subPath }, i) => {\n const priorQueryResult = await priorQuery\n\n const SubModel = (payload.db as MongooseAdapter).collections[slug]\n\n // On the \"deepest\" collection,\n // Search on the value passed through the query\n if (i === 0) {\n const subQuery = await SubModel.buildQuery({\n locale,\n payload,\n where: {\n [subPath]: {\n [formattedOperator]: val,\n },\n },\n })\n\n const result = await SubModel.find(subQuery, subQueryOptions)\n\n const $in: unknown[] = []\n\n result.forEach((doc) => {\n const stringID = doc._id.toString()\n $in.push(stringID)\n\n if (Types.ObjectId.isValid(stringID)) {\n $in.push(doc._id)\n }\n })\n\n if (pathsToQuery.length === 1) {\n return {\n path,\n value: { $in },\n }\n }\n\n const nextSubPath = pathsToQuery[i + 1].path\n\n return {\n value: { [nextSubPath]: { $in } },\n }\n }\n\n const subQuery = priorQueryResult.value\n const result = await SubModel.find(subQuery, subQueryOptions)\n\n const $in = result.map((doc) => doc._id)\n\n // If it is the last recursion\n // then pass through the search param\n if (i + 1 === pathsToQuery.length) {\n return {\n path,\n value: { $in },\n }\n }\n\n return {\n value: {\n _id: { $in },\n },\n }\n },\n Promise.resolve(initialRelationshipQuery),\n )\n\n return relationshipQuery\n }\n\n if (formattedOperator && validOperators.includes(formattedOperator as Operator)) {\n const operatorKey = operatorMap[formattedOperator]\n\n if (field.type === 'relationship' || field.type === 'upload') {\n let hasNumberIDRelation\n let multiIDCondition = '$or'\n if (operatorKey === '$ne') {\n multiIDCondition = '$and'\n }\n\n const result = {\n value: {\n [multiIDCondition]: [{ [path]: { [operatorKey]: formattedValue } }],\n },\n }\n\n if (typeof formattedValue === 'string') {\n if (Types.ObjectId.isValid(formattedValue)) {\n result.value[multiIDCondition].push({\n [path]: { [operatorKey]: new Types.ObjectId(formattedValue) },\n })\n } else {\n ;(Array.isArray(field.relationTo) ? field.relationTo : [field.relationTo]).forEach(\n (relationTo) => {\n const isRelatedToCustomNumberID =\n payload.collections[relationTo]?.customIDType === 'number'\n\n if (isRelatedToCustomNumberID) {\n hasNumberIDRelation = true\n }\n },\n )\n\n if (hasNumberIDRelation) {\n result.value[multiIDCondition].push({\n [path]: { [operatorKey]: parseFloat(formattedValue) },\n })\n }\n }\n }\n\n if (result.value[multiIDCondition].length > 1) {\n return result\n }\n }\n\n if (formattedOperator === 'like' && typeof formattedValue === 'string') {\n const words = formattedValue.split(' ')\n\n const result = {\n value: {\n $and: words.map((word) => ({\n [path]: {\n $options: 'i',\n $regex: word.replace(/[\\\\^$*+?.()|[\\]{}]/g, '\\\\$&'),\n },\n })),\n },\n }\n\n return result\n }\n\n // Some operators like 'near' need to define a full query\n // so if there is no operator key, just return the value\n if (!operatorKey) {\n return {\n path,\n value: formattedValue,\n }\n }\n\n return {\n path,\n value: { [operatorKey]: formattedValue },\n }\n }\n }\n return undefined\n}\n"],"names":["Types","getLocalizedPaths","validOperators","operatorMap","sanitizeQueryValue","subQueryOptions","lean","limit","buildSearchParam","collectionSlug","fields","globalSlug","incomingPath","locale","operator","payload","val","sanitizedPath","replace","paths","hasCustomID","customIDFieldType","collections","customIDType","idFieldType","push","complete","field","name","type","path","sanitizedQueryValue","undefined","formattedOperator","rawQuery","formattedValue","value","length","pathsToQuery","slice","reverse","initialRelationshipQuery","relationshipQuery","reduce","priorQuery","slug","subPath","i","priorQueryResult","SubModel","db","subQuery","buildQuery","where","result","find","$in","forEach","doc","stringID","_id","toString","ObjectId","isValid","nextSubPath","map","Promise","resolve","includes","operatorKey","hasNumberIDRelation","multiIDCondition","Array","isArray","relationTo","isRelatedToCustomNumberID","parseFloat","words","split","$and","word","$options","$regex"],"mappings":"AAEA,SAASA,KAAK,QAAQ,WAAU;AAChC,SAASC,iBAAiB,QAAQ,UAAS;AAC3C,SAASC,cAAc,QAAQ,iBAAgB;AAI/C,SAASC,WAAW,QAAQ,mBAAkB;AAC9C,SAASC,kBAAkB,QAAQ,0BAAyB;AAQ5D,MAAMC,kBAAkB;IACtBC,MAAM;IACNC,OAAO;AACT;AAEA;;CAEC,GACD,OAAO,eAAeC,iBAAiB,EACrCC,cAAc,EACdC,MAAM,EACNC,UAAU,EACVC,YAAY,EACZC,MAAM,EACNC,QAAQ,EACRC,OAAO,EACPC,GAAG,EAUJ;IACC,4DAA4D;IAC5D,IAAIC,gBAAgBL,aAAaM,OAAO,CAAC,OAAO;IAChD,IAAID,kBAAkB,MAAM;QAC1BA,gBAAgB;IAClB;IAEA,IAAIE,QAAuB,EAAE;IAE7B,IAAIC,cAAc;IAElB,IAAIH,kBAAkB,OAAO;QAC3B,MAAMI,oBAAoBN,QAAQO,WAAW,CAACb,eAAe,EAAEc;QAE/D,IAAIC,cAAiC;QAErC,IAAIH,mBAAmB;YACrBG,cAAcH;YACdD,cAAc;QAChB;QAEAD,MAAMM,IAAI,CAAC;YACThB;YACAiB,UAAU;YACVC,OAAO;gBACLC,MAAM;gBACNC,MAAML;YACR;YACAM,MAAM;QACR;IACF,OAAO;QACLX,QAAQlB,kBAAkB;YACxBQ;YACAC;YACAC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gBACAM,OAAO;oBAAE,CAACiC,YAAY,EAAElC;gBAAe;YACzC;QACF;IACF;IACA,OAAOH;AACT"}
|
|
1
|
+
{"version":3,"sources":["../../src/queries/buildSearchParams.ts"],"sourcesContent":["import type { ClientSession, FindOptions } from 'mongodb'\nimport type { FlattenedField, Operator, PathToQuery, Payload } from 'payload'\n\nimport { Types } from 'mongoose'\nimport { getLocalizedPaths } from 'payload'\nimport { validOperators } from 'payload/shared'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { operatorMap } from './operatorMap.js'\nimport { sanitizeQueryValue } from './sanitizeQueryValue.js'\n\ntype SearchParam = {\n path?: string\n rawQuery?: unknown\n value?: unknown\n}\n\nconst subQueryOptions: FindOptions = {\n limit: 50,\n projection: {\n _id: true,\n },\n}\n\n/**\n * Convert the Payload key / value / operator into a MongoDB query\n */\nexport async function buildSearchParam({\n collectionSlug,\n fields,\n globalSlug,\n incomingPath,\n locale,\n operator,\n payload,\n session,\n val,\n}: {\n collectionSlug?: string\n fields: FlattenedField[]\n globalSlug?: string\n incomingPath: string\n locale?: string\n operator: string\n payload: Payload\n session?: ClientSession\n val: unknown\n}): Promise<SearchParam> {\n // Replace GraphQL nested field double underscore formatting\n let sanitizedPath = incomingPath.replace(/__/g, '.')\n if (sanitizedPath === 'id') {\n sanitizedPath = '_id'\n }\n\n let paths: PathToQuery[] = []\n\n let hasCustomID = false\n\n if (sanitizedPath === '_id') {\n const customIDFieldType = payload.collections[collectionSlug]?.customIDType\n\n let idFieldType: 'number' | 'text' = 'text'\n\n if (customIDFieldType) {\n idFieldType = customIDFieldType\n hasCustomID = true\n }\n\n paths.push({\n collectionSlug,\n complete: true,\n field: {\n name: 'id',\n type: idFieldType,\n } as FlattenedField,\n path: '_id',\n })\n } else {\n paths = getLocalizedPaths({\n collectionSlug,\n fields,\n globalSlug,\n incomingPath: sanitizedPath,\n locale,\n payload,\n })\n }\n\n const [{ field, path }] = paths\n if (path) {\n const sanitizedQueryValue = sanitizeQueryValue({\n field,\n hasCustomID,\n locale,\n operator,\n path,\n payload,\n val,\n })\n\n if (!sanitizedQueryValue) {\n return undefined\n }\n\n const { operator: formattedOperator, rawQuery, val: formattedValue } = sanitizedQueryValue\n\n if (rawQuery) {\n return { value: rawQuery }\n }\n\n // If there are multiple collections to search through,\n // Recursively build up a list of query constraints\n if (paths.length > 1) {\n // Remove top collection and reverse array\n // to work backwards from top\n const pathsToQuery = paths.slice(1).reverse()\n\n const initialRelationshipQuery = {\n value: {},\n } as SearchParam\n\n const relationshipQuery = await pathsToQuery.reduce(\n async (priorQuery, { collectionSlug: slug, path: subPath }, i) => {\n const priorQueryResult = await priorQuery\n\n const SubModel = (payload.db as MongooseAdapter).collections[slug]\n\n // On the \"deepest\" collection,\n // Search on the value passed through the query\n if (i === 0) {\n const subQuery = await SubModel.buildQuery({\n locale,\n payload,\n where: {\n [subPath]: {\n [formattedOperator]: val,\n },\n },\n })\n\n const result = await SubModel.collection\n .find(subQuery, { session, ...subQueryOptions })\n .toArray()\n\n const $in: unknown[] = []\n\n result.forEach((doc) => {\n $in.push(doc._id)\n })\n\n if (pathsToQuery.length === 1) {\n return {\n path,\n value: { $in },\n }\n }\n\n const nextSubPath = pathsToQuery[i + 1].path\n\n return {\n value: { [nextSubPath]: { $in } },\n }\n }\n\n const subQuery = priorQueryResult.value\n const result = await SubModel.collection\n .find(subQuery, { session, ...subQueryOptions })\n .toArray()\n\n const $in = result.map((doc) => doc._id)\n\n // If it is the last recursion\n // then pass through the search param\n if (i + 1 === pathsToQuery.length) {\n return {\n path,\n value: { $in },\n }\n }\n\n return {\n value: {\n _id: { $in },\n },\n }\n },\n Promise.resolve(initialRelationshipQuery),\n )\n\n return relationshipQuery\n }\n\n if (formattedOperator && validOperators.includes(formattedOperator as Operator)) {\n const operatorKey = operatorMap[formattedOperator]\n\n if (field.type === 'relationship' || field.type === 'upload') {\n let hasNumberIDRelation\n let multiIDCondition = '$or'\n if (operatorKey === '$ne') {\n multiIDCondition = '$and'\n }\n\n const result = {\n value: {\n [multiIDCondition]: [{ [path]: { [operatorKey]: formattedValue } }],\n },\n }\n\n if (typeof formattedValue === 'string') {\n if (Types.ObjectId.isValid(formattedValue)) {\n result.value[multiIDCondition].push({\n [path]: { [operatorKey]: new Types.ObjectId(formattedValue) },\n })\n } else {\n ;(Array.isArray(field.relationTo) ? field.relationTo : [field.relationTo]).forEach(\n (relationTo) => {\n const isRelatedToCustomNumberID =\n payload.collections[relationTo]?.customIDType === 'number'\n\n if (isRelatedToCustomNumberID) {\n hasNumberIDRelation = true\n }\n },\n )\n\n if (hasNumberIDRelation) {\n result.value[multiIDCondition].push({\n [path]: { [operatorKey]: parseFloat(formattedValue) },\n })\n }\n }\n }\n\n if (result.value[multiIDCondition].length > 1) {\n return result\n }\n }\n\n if (formattedOperator === 'like' && typeof formattedValue === 'string') {\n const words = formattedValue.split(' ')\n\n const result = {\n value: {\n $and: words.map((word) => ({\n [path]: {\n $options: 'i',\n $regex: word.replace(/[\\\\^$*+?.()|[\\]{}]/g, '\\\\$&'),\n },\n })),\n },\n }\n\n return result\n }\n\n // Some operators like 'near' need to define a full query\n // so if there is no operator key, just return the value\n if (!operatorKey) {\n return {\n path,\n value: formattedValue,\n }\n }\n\n return {\n path,\n value: { [operatorKey]: formattedValue },\n }\n }\n }\n return undefined\n}\n"],"names":["Types","getLocalizedPaths","validOperators","operatorMap","sanitizeQueryValue","subQueryOptions","limit","projection","_id","buildSearchParam","collectionSlug","fields","globalSlug","incomingPath","locale","operator","payload","session","val","sanitizedPath","replace","paths","hasCustomID","customIDFieldType","collections","customIDType","idFieldType","push","complete","field","name","type","path","sanitizedQueryValue","undefined","formattedOperator","rawQuery","formattedValue","value","length","pathsToQuery","slice","reverse","initialRelationshipQuery","relationshipQuery","reduce","priorQuery","slug","subPath","i","priorQueryResult","SubModel","db","subQuery","buildQuery","where","result","collection","find","toArray","$in","forEach","doc","nextSubPath","map","Promise","resolve","includes","operatorKey","hasNumberIDRelation","multiIDCondition","ObjectId","isValid","Array","isArray","relationTo","isRelatedToCustomNumberID","parseFloat","words","split","$and","word","$options","$regex"],"mappings":"AAGA,SAASA,KAAK,QAAQ,WAAU;AAChC,SAASC,iBAAiB,QAAQ,UAAS;AAC3C,SAASC,cAAc,QAAQ,iBAAgB;AAI/C,SAASC,WAAW,QAAQ,mBAAkB;AAC9C,SAASC,kBAAkB,QAAQ,0BAAyB;AAQ5D,MAAMC,kBAA+B;IACnCC,OAAO;IACPC,YAAY;QACVC,KAAK;IACP;AACF;AAEA;;CAEC,GACD,OAAO,eAAeC,iBAAiB,EACrCC,cAAc,EACdC,MAAM,EACNC,UAAU,EACVC,YAAY,EACZC,MAAM,EACNC,QAAQ,EACRC,OAAO,EACPC,OAAO,EACPC,GAAG,EAWJ;IACC,4DAA4D;IAC5D,IAAIC,gBAAgBN,aAAaO,OAAO,CAAC,OAAO;IAChD,IAAID,kBAAkB,MAAM;QAC1BA,gBAAgB;IAClB;IAEA,IAAIE,QAAuB,EAAE;IAE7B,IAAIC,cAAc;IAElB,IAAIH,kBAAkB,OAAO;QAC3B,MAAMI,oBAAoBP,QAAQQ,WAAW,CAACd,eAAe,EAAEe;QAE/D,IAAIC,cAAiC;QAErC,IAAIH,mBAAmB;YACrBG,cAAcH;YACdD,cAAc;QAChB;QAEAD,MAAMM,IAAI,CAAC;YACTjB;YACAkB,UAAU;YACVC,OAAO;gBACLC,MAAM;gBACNC,MAAML;YACR;YACAM,MAAM;QACR;IACF,OAAO;QACLX,QAAQpB,kBAAkB;YACxBS;YACAC;YACAC;YACAC,cAAcM;YACdL;YACAE;QACF;IACF;IAEA,MAAM,CAAC,EAAEa,KAAK,EAAEG,IAAI,EAAE,CAAC,GAAGX;IAC1B,IAAIW,MAAM;QACR,MAAMC,sBAAsB7B,mBAAmB;YAC7CyB;YACAP;YACAR;YACAC;YACAiB;YACAhB;YACAE;QACF;QAEA,IAAI,CAACe,qBAAqB;YACxB,OAAOC;QACT;QAEA,MAAM,EAAEnB,UAAUoB,i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AAM8C,UAAU,IAAI9C,MAAM8C,UAAU,GAAG;4BAAC9C,MAAM8C,UAAU;yBAAC,AAAD,EAAGd,OAAO,CAChF,CAACc;4BACC,MAAMC,4BACJ5D,QAAQQ,WAAW,CAACmD,WAAW,EAAElD,iBAAiB;4BAEpD,IAAImD,2BAA2B;gCAC7BP,sBAAsB;4BACxB;wBACF;wBAGF,IAAIA,qBAAqB;4BACvBb,OAAOlB,KAAK,CAACgC,iBAAiB,CAAC3C,IAAI,CAAC;gCAClC,CAACK,KAAK,EAAE;oCAAE,CAACoC,YAAY,EAAES,WAAWxC;gCAAgB;4BACtD;wBACF;oBACF;gBACF;gBAEA,IAAImB,OAAOlB,KAAK,CAACgC,iBAAiB,CAAC/B,MAAM,GAAG,GAAG;oBAC7C,OAAOiB;gBACT;YACF;YAEA,IAAIrB,sBAAsB,UAAU,OAAOE,mBAAmB,UAAU;gBACtE,MAAMyC,QAAQzC,eAAe0C,KAAK,CAAC;gBAEnC,MAAMvB,SAAS;oBACblB,OAAO;wBACL0C,MAAMF,MAAMd,GAAG,CAAC,CAACiB,OAAU,CAAA;gCACzB,CAACjD,KAAK,EAAE;oCACNkD,UAAU;oCACVC,QAAQF,KAAK7D,OAAO,CAAC,uBAAuB;gCAC9C;4BACF,CAAA;oBACF;gBACF;gBAEA,OAAOoC;YACT;YAEA,yDAAyD;YACzD,wDAAwD;YACxD,IAAI,CAACY,aAAa;gBAChB,OAAO;oBACLpC;oBACAM,OAAOD;gBACT;YACF;YAEA,OAAO;gBACLL;gBACAM,OAAO;oBAAE,CAAC8B,YAAY,EAAE/B;gBAAe;YACzC;QACF;IACF;IACA,OAAOH;AACT"}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { PaginateOptions } from 'mongoose';
|
|
2
1
|
import type { FlattenedField, SanitizedConfig, Sort } from 'payload';
|
|
3
2
|
type Args = {
|
|
4
3
|
config: SanitizedConfig;
|
|
@@ -7,11 +6,6 @@ type Args = {
|
|
|
7
6
|
sort: Sort;
|
|
8
7
|
timestamps: boolean;
|
|
9
8
|
};
|
|
10
|
-
export
|
|
11
|
-
direction: SortDirection;
|
|
12
|
-
property: string;
|
|
13
|
-
}[];
|
|
14
|
-
export type SortDirection = 'asc' | 'desc';
|
|
15
|
-
export declare const buildSortParam: ({ config, fields, locale, sort, timestamps, }: Args) => PaginateOptions["sort"];
|
|
9
|
+
export declare const buildSortParam: ({ config, fields, locale, sort, timestamps, }: Args) => Record<string, -1 | 1>;
|
|
16
10
|
export {};
|
|
17
11
|
//# sourceMappingURL=buildSortParam.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildSortParam.d.ts","sourceRoot":"","sources":["../../src/queries/buildSortParam.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"buildSortParam.d.ts","sourceRoot":"","sources":["../../src/queries/buildSortParam.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAIpE,KAAK,IAAI,GAAG;IACV,MAAM,EAAE,eAAe,CAAA;IACvB,MAAM,EAAE,cAAc,EAAE,CAAA;IACxB,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,IAAI,CAAA;IACV,UAAU,EAAE,OAAO,CAAA;CACpB,CAAA;AAED,eAAO,MAAM,cAAc,kDAMxB,IAAI,KAAG,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC,CAsC9B,CAAA"}
|
|
@@ -17,10 +17,10 @@ export const buildSortParam = ({ config, fields, locale, sort, timestamps })=>{
|
|
|
17
17
|
let sortDirection;
|
|
18
18
|
if (item.indexOf('-') === 0) {
|
|
19
19
|
sortProperty = item.substring(1);
|
|
20
|
-
sortDirection =
|
|
20
|
+
sortDirection = -1;
|
|
21
21
|
} else {
|
|
22
22
|
sortProperty = item;
|
|
23
|
-
sortDirection =
|
|
23
|
+
sortDirection = 1;
|
|
24
24
|
}
|
|
25
25
|
if (sortProperty === 'id') {
|
|
26
26
|
acc['_id'] = sortDirection;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/queries/buildSortParam.ts"],"sourcesContent":["import type { PaginateOptions } from 'mongoose'\nimport type { FlattenedField, SanitizedConfig, Sort } from 'payload'\n\nimport { getLocalizedSortProperty } from './getLocalizedSortProperty.js'\n\ntype Args = {\n config: SanitizedConfig\n fields: FlattenedField[]\n locale: string\n sort: Sort\n timestamps: boolean\n}\n\nexport
|
|
1
|
+
{"version":3,"sources":["../../src/queries/buildSortParam.ts"],"sourcesContent":["import type { PaginateOptions } from 'mongoose'\nimport type { FlattenedField, SanitizedConfig, Sort } from 'payload'\n\nimport { getLocalizedSortProperty } from './getLocalizedSortProperty.js'\n\ntype Args = {\n config: SanitizedConfig\n fields: FlattenedField[]\n locale: string\n sort: Sort\n timestamps: boolean\n}\n\nexport const buildSortParam = ({\n config,\n fields,\n locale,\n sort,\n timestamps,\n}: Args): Record<string, -1 | 1> => {\n if (!sort) {\n if (timestamps) {\n sort = '-createdAt'\n } else {\n sort = '-id'\n }\n }\n\n if (typeof sort === 'string') {\n sort = [sort]\n }\n\n const sorting = sort.reduce<Record<string, -1 | 1>>((acc, item) => {\n let sortProperty: string\n let sortDirection: -1 | 1\n if (item.indexOf('-') === 0) {\n sortProperty = item.substring(1)\n sortDirection = -1\n } else {\n sortProperty = item\n sortDirection = 1\n }\n if (sortProperty === 'id') {\n acc['_id'] = sortDirection\n return acc\n }\n const localizedProperty = getLocalizedSortProperty({\n config,\n fields,\n locale,\n segments: sortProperty.split('.'),\n })\n acc[localizedProperty] = sortDirection\n return acc\n }, {})\n\n return sorting\n}\n"],"names":["getLocalizedSortProperty","buildSortParam","config","fields","locale","sort","timestamps","sorting","reduce","acc","item","sortProperty","sortDirection","indexOf","substring","localizedProperty","segments","split"],"mappings":"AAGA,SAASA,wBAAwB,QAAQ,gCAA+B;AAUxE,OAAO,MAAMC,iBAAiB,CAAC,EAC7BC,MAAM,EACNC,MAAM,EACNC,MAAM,EACNC,IAAI,EACJC,UAAU,EACL;IACL,IAAI,CAACD,MAAM;QACT,IAAIC,YAAY;YACdD,OAAO;QACT,OAAO;YACLA,OAAO;QACT;IACF;IAEA,IAAI,OAAOA,SAAS,UAAU;QAC5BA,OAAO;YAACA;SAAK;IACf;IAEA,MAAME,UAAUF,KAAKG,MAAM,CAAyB,CAACC,KAAKC;QACxD,IAAIC;QACJ,IAAIC;QACJ,IAAIF,KAAKG,OAAO,CAAC,SAAS,GAAG;YAC3BF,eAAeD,KAAKI,SAAS,CAAC;YAC9BF,gBAAgB,CAAC;QACnB,OAAO;YACLD,eAAeD;YACfE,gBAAgB;QAClB;QACA,IAAID,iBAAiB,MAAM;YACzBF,GAAG,CAAC,MAAM,GAAGG;YACb,OAAOH;QACT;QACA,MAAMM,oBAAoBf,yBAAyB;YACjDE;YACAC;YACAC;YACAY,UAAUL,aAAaM,KAAK,CAAC;QAC/B;QACAR,GAAG,CAACM,kBAAkB,GAAGH;QACzB,OAAOH;IACT,GAAG,CAAC;IAEJ,OAAOF;AACT,EAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { fieldAffectsData
|
|
1
|
+
import { fieldAffectsData } from 'payload/shared';
|
|
2
2
|
export const getLocalizedSortProperty = ({ config, fields, locale, result: incomingResult, segments: incomingSegments })=>{
|
|
3
3
|
// If localization is not enabled, accept exactly
|
|
4
4
|
// what is sent in
|
|
@@ -12,7 +12,7 @@ export const getLocalizedSortProperty = ({ config, fields, locale, result: incom
|
|
|
12
12
|
const firstSegment = segments.shift();
|
|
13
13
|
// Attempt to find a matched field
|
|
14
14
|
const matchedField = fields.find((field)=>fieldAffectsData(field) && field.name === firstSegment);
|
|
15
|
-
if (matchedField
|
|
15
|
+
if (matchedField) {
|
|
16
16
|
let nextFields;
|
|
17
17
|
const remainingSegments = [
|
|
18
18
|
...segments
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/queries/getLocalizedSortProperty.ts"],"sourcesContent":["import type { FlattenedField, SanitizedConfig } from 'payload'\n\nimport { fieldAffectsData
|
|
1
|
+
{"version":3,"sources":["../../src/queries/getLocalizedSortProperty.ts"],"sourcesContent":["import type { FlattenedField, SanitizedConfig } from 'payload'\n\nimport { fieldAffectsData } from 'payload/shared'\n\ntype Args = {\n config: SanitizedConfig\n fields: FlattenedField[]\n locale: string\n result?: string\n segments: string[]\n}\n\nexport const getLocalizedSortProperty = ({\n config,\n fields,\n locale,\n result: incomingResult,\n segments: incomingSegments,\n}: Args): string => {\n // If localization is not enabled, accept exactly\n // what is sent in\n if (!config.localization) {\n return incomingSegments.join('.')\n }\n\n const segments = [...incomingSegments]\n\n // Retrieve first segment, and remove from segments\n const firstSegment = segments.shift()\n\n // Attempt to find a matched field\n const matchedField = fields.find(\n (field) => fieldAffectsData(field) && field.name === firstSegment,\n )\n\n if (matchedField) {\n let nextFields: FlattenedField[]\n const remainingSegments = [...segments]\n let localizedSegment = matchedField.name\n\n if (matchedField.localized) {\n // Check to see if next segment is a locale\n if (segments.length > 0) {\n const nextSegmentIsLocale = config.localization.localeCodes.includes(remainingSegments[0])\n\n // If next segment is locale, remove it from remaining segments\n // and use it to localize the current segment\n if (nextSegmentIsLocale) {\n const nextSegment = remainingSegments.shift()\n localizedSegment = `${matchedField.name}.${nextSegment}`\n }\n } else {\n // If no more segments, but field is localized, use default locale\n localizedSegment = `${matchedField.name}.${locale}`\n }\n }\n\n // If there are subfields, pass them through\n if (\n matchedField.type === 'tab' ||\n matchedField.type === 'group' ||\n matchedField.type === 'array'\n ) {\n nextFields = matchedField.flattenedFields\n }\n\n if (matchedField.type === 'blocks') {\n nextFields = matchedField.blocks.reduce((flattenedBlockFields, block) => {\n return [\n ...flattenedBlockFields,\n ...block.flattenedFields.filter(\n (blockField) =>\n (fieldAffectsData(blockField) &&\n blockField.name !== 'blockType' &&\n blockField.name !== 'blockName') ||\n !fieldAffectsData(blockField),\n ),\n ]\n }, [])\n }\n\n const result = incomingResult ? `${incomingResult}.${localizedSegment}` : localizedSegment\n\n if (nextFields) {\n return getLocalizedSortProperty({\n config,\n fields: nextFields,\n locale,\n result,\n segments: remainingSegments,\n })\n }\n\n return result\n }\n\n return incomingSegments.join('.')\n}\n"],"names":["fieldAffectsData","getLocalizedSortProperty","config","fields","locale","result","incomingResult","segments","incomingSegments","localization","join","firstSegment","shift","matchedField","find","field","name","nextFields","remainingSegments","localizedSegment","localized","length","nextSegmentIsLocale","localeCodes","includes","nextSegment","type","flattenedFields","blocks","reduce","flattenedBlockFields","block","filter","blockField"],"mappings":"AAEA,SAASA,gBAAgB,QAAQ,iBAAgB;AAUjD,OAAO,MAAMC,2BAA2B,CAAC,EACvCC,MAAM,EACNC,MAAM,EACNC,MAAM,EACNC,QAAQC,cAAc,EACtBC,UAAUC,gBAAgB,EACrB;IACL,iDAAiD;IACjD,kBAAkB;IAClB,IAAI,CAACN,OAAOO,YAAY,EAAE;QACxB,OAAOD,iBAAiBE,IAAI,CAAC;IAC/B;IAEA,MAAMH,WAAW;WAAIC;KAAiB;IAEtC,mDAAmD;IACnD,MAAMG,eAAeJ,SAASK,KAAK;IAEnC,kCAAkC;IAClC,MAAMC,eAAeV,OAAOW,IAAI,CAC9B,CAACC,QAAUf,iBAAiBe,UAAUA,MAAMC,IAAI,KAAKL;IAGvD,IAAIE,cAAc;QAChB,IAAII;QACJ,MAAMC,oBAAoB;eAAIX;SAAS;QACvC,IAAIY,mBAAmBN,aAAaG,IAAI;QAExC,IAAIH,aAAaO,SAAS,EAAE;YAC1B,2CAA2C;YAC3C,IAAIb,SAASc,MAAM,GAAG,GAAG;gBACvB,MAAMC,sBAAsBpB,OAAOO,YAAY,CAACc,WAAW,CAACC,QAAQ,CAACN,iBAAiB,CAAC,EAAE;gBAEzF,+DAA+D;gBAC/D,6CAA6C;gBAC7C,IAAII,qBAAqB;oBACvB,MAAMG,cAAcP,kBAAkBN,KAAK;oBAC3CO,mBAAmB,GAAGN,aAAaG,IAAI,CAAC,CAAC,EAAES,aAAa;gBAC1D;YACF,OAAO;gBACL,kEAAkE;gBAClEN,mBAAmB,GAAGN,aAAaG,IAAI,CAAC,CAAC,EAAEZ,QAAQ;YACrD;QACF;QAEA,4CAA4C;QAC5C,IACES,aAAaa,IAAI,KAAK,SACtBb,aAAaa,IAAI,KAAK,WACtBb,aAAaa,IAAI,KAAK,SACtB;YACAT,aAAaJ,aAAac,eAAe;QAC3C;QAEA,IAAId,aAAaa,IAAI,KAAK,UAAU;YAClCT,aAAaJ,aAAae,MAAM,CAACC,MAAM,CAAC,CAACC,sBAAsBC;gBAC7D,OAAO;uBACFD;uBACAC,MAAMJ,eAAe,CAACK,MAAM,CAC7B,CAACC,aACC,AAACjC,iBAAiBiC,eAChBA,WAAWjB,IAAI,KAAK,eACpBiB,WAAWjB,IAAI,KAAK,eACtB,CAAChB,iBAAiBiC;iBAEvB;YACH,GAAG,EAAE;QACP;QAEA,MAAM5B,SAASC,iBAAiB,GAAGA,eAAe,CAAC,EAAEa,kBAAkB,GAAGA;QAE1E,IAAIF,YAAY;YACd,OAAOhB,yBAAyB;gBAC9BC;gBACAC,QAAQc;gBACRb;gBACAC;gBACAE,UAAUW;YACZ;QACF;QAEA,OAAOb;IACT;IAEA,OAAOG,iBAAiBE,IAAI,CAAC;AAC/B,EAAC"}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import type { ClientSession } from 'mongodb';
|
|
1
2
|
import type { FlattenedField, Payload, Where } from 'payload';
|
|
2
|
-
export declare function parseParams({ collectionSlug, fields, globalSlug, locale, payload, where, }: {
|
|
3
|
+
export declare function parseParams({ collectionSlug, fields, globalSlug, locale, payload, session, where, }: {
|
|
3
4
|
collectionSlug?: string;
|
|
4
5
|
fields: FlattenedField[];
|
|
5
6
|
globalSlug?: string;
|
|
6
7
|
locale: string;
|
|
7
8
|
payload: Payload;
|
|
9
|
+
session?: ClientSession;
|
|
8
10
|
where: Where;
|
|
9
11
|
}): Promise<Record<string, unknown>>;
|
|
10
12
|
//# sourceMappingURL=parseParams.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parseParams.d.ts","sourceRoot":"","sources":["../../src/queries/parseParams.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"parseParams.d.ts","sourceRoot":"","sources":["../../src/queries/parseParams.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAE5C,OAAO,KAAK,EAAE,cAAc,EAAY,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAQvE,wBAAsB,WAAW,CAAC,EAChC,cAAc,EACd,MAAM,EACN,UAAU,EACV,MAAM,EACN,OAAO,EACP,OAAO,EACP,KAAK,GACN,EAAE;IACD,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,MAAM,EAAE,cAAc,EAAE,CAAA;IACxB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,OAAO,CAAA;IAChB,OAAO,CAAC,EAAE,aAAa,CAAA;IACvB,KAAK,EAAE,KAAK,CAAA;CACb,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAgEnC"}
|
|
@@ -2,7 +2,7 @@ import { deepMergeWithCombinedArrays } from 'payload';
|
|
|
2
2
|
import { validOperators } from 'payload/shared';
|
|
3
3
|
import { buildAndOrConditions } from './buildAndOrConditions.js';
|
|
4
4
|
import { buildSearchParam } from './buildSearchParams.js';
|
|
5
|
-
export async function parseParams({ collectionSlug, fields, globalSlug, locale, payload, where }) {
|
|
5
|
+
export async function parseParams({ collectionSlug, fields, globalSlug, locale, payload, session, where }) {
|
|
6
6
|
let result = {};
|
|
7
7
|
if (typeof where === 'object') {
|
|
8
8
|
// We need to determine if the whereKey is an AND, OR, or a schema path
|
|
@@ -42,6 +42,7 @@ export async function parseParams({ collectionSlug, fields, globalSlug, locale,
|
|
|
42
42
|
locale,
|
|
43
43
|
operator,
|
|
44
44
|
payload,
|
|
45
|
+
session,
|
|
45
46
|
val: pathOperators[operator]
|
|
46
47
|
});
|
|
47
48
|
if (searchParam?.value && searchParam?.path) {
|