@payloadcms/db-mongodb 3.0.0-canary.c27a334 → 3.0.0-canary.c96fa61
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +1 -1
- package/dist/connect.d.ts +1 -1
- package/dist/connect.d.ts.map +1 -1
- package/dist/connect.js +13 -4
- package/dist/connect.js.map +1 -1
- package/dist/count.d.ts +1 -1
- package/dist/count.d.ts.map +1 -1
- package/dist/count.js +2 -2
- package/dist/count.js.map +1 -1
- package/dist/countGlobalVersions.d.ts +3 -0
- package/dist/countGlobalVersions.d.ts.map +1 -0
- package/dist/countGlobalVersions.js +33 -0
- package/dist/countGlobalVersions.js.map +1 -0
- package/dist/countVersions.d.ts +3 -0
- package/dist/countVersions.d.ts.map +1 -0
- package/dist/countVersions.js +33 -0
- package/dist/countVersions.js.map +1 -0
- package/dist/create.d.ts +1 -1
- package/dist/create.d.ts.map +1 -1
- package/dist/create.js +17 -4
- package/dist/create.js.map +1 -1
- package/dist/createGlobal.d.ts +1 -1
- package/dist/createGlobal.d.ts.map +1 -1
- package/dist/createGlobal.js +11 -6
- package/dist/createGlobal.js.map +1 -1
- package/dist/createGlobalVersion.d.ts +1 -1
- package/dist/createGlobalVersion.d.ts.map +1 -1
- package/dist/createGlobalVersion.js +14 -5
- package/dist/createGlobalVersion.js.map +1 -1
- package/dist/createMigration.d.ts +1 -1
- package/dist/createMigration.d.ts.map +1 -1
- package/dist/createMigration.js +9 -7
- package/dist/createMigration.js.map +1 -1
- package/dist/createVersion.d.ts +1 -1
- package/dist/createVersion.d.ts.map +1 -1
- package/dist/createVersion.js +32 -10
- package/dist/createVersion.js.map +1 -1
- package/dist/deleteMany.d.ts +1 -1
- package/dist/deleteMany.d.ts.map +1 -1
- package/dist/deleteMany.js +1 -1
- package/dist/deleteMany.js.map +1 -1
- package/dist/deleteOne.d.ts +1 -1
- package/dist/deleteOne.d.ts.map +1 -1
- package/dist/deleteOne.js +12 -4
- package/dist/deleteOne.js.map +1 -1
- package/dist/deleteVersions.d.ts +1 -1
- package/dist/deleteVersions.d.ts.map +1 -1
- package/dist/deleteVersions.js +1 -1
- package/dist/deleteVersions.js.map +1 -1
- package/dist/destroy.d.ts +1 -1
- package/dist/destroy.d.ts.map +1 -1
- package/dist/destroy.js.map +1 -1
- package/dist/find.d.ts +1 -1
- package/dist/find.d.ts.map +1 -1
- package/dist/find.js +36 -6
- package/dist/find.js.map +1 -1
- package/dist/findGlobal.d.ts +1 -1
- package/dist/findGlobal.d.ts.map +1 -1
- package/dist/findGlobal.js +11 -5
- package/dist/findGlobal.js.map +1 -1
- package/dist/findGlobalVersions.d.ts +1 -1
- package/dist/findGlobalVersions.d.ts.map +1 -1
- package/dist/findGlobalVersions.js +19 -8
- package/dist/findGlobalVersions.js.map +1 -1
- package/dist/findOne.d.ts +1 -1
- package/dist/findOne.d.ts.map +1 -1
- package/dist/findOne.js +28 -4
- package/dist/findOne.js.map +1 -1
- package/dist/findVersions.d.ts +1 -1
- package/dist/findVersions.d.ts.map +1 -1
- package/dist/findVersions.js +17 -5
- package/dist/findVersions.js.map +1 -1
- package/dist/index.d.ts +52 -35
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +17 -7
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts +1 -1
- package/dist/init.d.ts.map +1 -1
- package/dist/init.js +11 -8
- package/dist/init.js.map +1 -1
- package/dist/migrateFresh.d.ts.map +1 -1
- package/dist/migrateFresh.js +1 -1
- package/dist/migrateFresh.js.map +1 -1
- package/dist/models/buildCollectionSchema.d.ts +2 -29
- package/dist/models/buildCollectionSchema.d.ts.map +1 -1
- package/dist/models/buildCollectionSchema.js +16 -4
- package/dist/models/buildCollectionSchema.js.map +1 -1
- package/dist/models/buildGlobalModel.d.ts +1 -1
- package/dist/models/buildGlobalModel.d.ts.map +1 -1
- package/dist/models/buildGlobalModel.js +2 -2
- package/dist/models/buildGlobalModel.js.map +1 -1
- package/dist/models/buildSchema.d.ts +2 -29
- package/dist/models/buildSchema.d.ts.map +1 -1
- package/dist/models/buildSchema.js +116 -23
- package/dist/models/buildSchema.js.map +1 -1
- package/dist/predefinedMigrations/versions-v1-v2.js.map +1 -1
- package/dist/queries/buildAndOrConditions.d.ts +1 -2
- package/dist/queries/buildAndOrConditions.d.ts.map +1 -1
- package/dist/queries/buildAndOrConditions.js +0 -2
- package/dist/queries/buildAndOrConditions.js.map +1 -1
- package/dist/queries/buildQuery.d.ts +3 -4
- package/dist/queries/buildQuery.d.ts.map +1 -1
- package/dist/queries/buildQuery.js +2 -3
- package/dist/queries/buildQuery.js.map +1 -1
- package/dist/queries/buildSearchParams.d.ts +1 -2
- package/dist/queries/buildSearchParams.d.ts.map +1 -1
- package/dist/queries/buildSearchParams.js +21 -13
- package/dist/queries/buildSearchParams.js.map +1 -1
- package/dist/queries/buildSortParam.d.ts +3 -29
- package/dist/queries/buildSortParam.d.ts.map +1 -1
- package/dist/queries/buildSortParam.js +26 -17
- package/dist/queries/buildSortParam.js.map +1 -1
- package/dist/queries/getLocalizedSortProperty.d.ts +1 -2
- package/dist/queries/getLocalizedSortProperty.d.ts.map +1 -1
- package/dist/queries/getLocalizedSortProperty.js +2 -2
- package/dist/queries/getLocalizedSortProperty.js.map +1 -1
- package/dist/queries/getLocalizedSortProperty.spec.js +44 -44
- package/dist/queries/getLocalizedSortProperty.spec.js.map +1 -1
- package/dist/queries/mock.js.map +1 -1
- package/dist/queries/operatorMap.js.map +1 -1
- package/dist/queries/parseParams.d.ts +1 -3
- package/dist/queries/parseParams.d.ts.map +1 -1
- package/dist/queries/parseParams.js +6 -7
- package/dist/queries/parseParams.js.map +1 -1
- package/dist/queries/sanitizeQueryValue.d.ts +1 -1
- package/dist/queries/sanitizeQueryValue.d.ts.map +1 -1
- package/dist/queries/sanitizeQueryValue.js +146 -27
- package/dist/queries/sanitizeQueryValue.js.map +1 -1
- package/dist/queryDrafts.d.ts +1 -1
- package/dist/queryDrafts.d.ts.map +1 -1
- package/dist/queryDrafts.js +37 -9
- package/dist/queryDrafts.js.map +1 -1
- package/dist/testCredentials.js.map +1 -1
- package/dist/transactions/beginTransaction.d.ts +1 -1
- package/dist/transactions/beginTransaction.d.ts.map +1 -1
- package/dist/transactions/beginTransaction.js +3 -1
- package/dist/transactions/beginTransaction.js.map +1 -1
- package/dist/transactions/commitTransaction.d.ts +1 -1
- package/dist/transactions/commitTransaction.d.ts.map +1 -1
- package/dist/transactions/commitTransaction.js +3 -0
- package/dist/transactions/commitTransaction.js.map +1 -1
- package/dist/transactions/rollbackTransaction.d.ts +1 -1
- package/dist/transactions/rollbackTransaction.d.ts.map +1 -1
- package/dist/transactions/rollbackTransaction.js +13 -7
- package/dist/transactions/rollbackTransaction.js.map +1 -1
- package/dist/types.d.ts +7 -31
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/updateGlobal.d.ts +1 -1
- package/dist/updateGlobal.d.ts.map +1 -1
- package/dist/updateGlobal.js +18 -5
- package/dist/updateGlobal.js.map +1 -1
- package/dist/updateGlobalVersion.d.ts +2 -3
- package/dist/updateGlobalVersion.d.ts.map +1 -1
- package/dist/updateGlobalVersion.js +19 -5
- package/dist/updateGlobalVersion.js.map +1 -1
- package/dist/updateOne.d.ts +1 -1
- package/dist/updateOne.d.ts.map +1 -1
- package/dist/updateOne.js +25 -7
- package/dist/updateOne.js.map +1 -1
- package/dist/updateVersion.d.ts +1 -1
- package/dist/updateVersion.d.ts.map +1 -1
- package/dist/updateVersion.js +18 -4
- package/dist/updateVersion.js.map +1 -1
- package/dist/upsert.d.ts +3 -0
- package/dist/upsert.d.ts.map +1 -0
- package/dist/upsert.js +15 -0
- package/dist/upsert.js.map +1 -0
- package/dist/utilities/buildJoinAggregation.d.ts +18 -0
- package/dist/utilities/buildJoinAggregation.d.ts.map +1 -0
- package/dist/utilities/buildJoinAggregation.js +159 -0
- package/dist/utilities/buildJoinAggregation.js.map +1 -0
- package/dist/utilities/buildProjectionFromSelect.d.ts +8 -0
- package/dist/utilities/buildProjectionFromSelect.d.ts.map +1 -0
- package/dist/utilities/buildProjectionFromSelect.js +171 -0
- package/dist/utilities/buildProjectionFromSelect.js.map +1 -0
- package/dist/utilities/getDBName.d.ts +1 -1
- package/dist/utilities/getDBName.d.ts.map +1 -1
- package/dist/utilities/getDBName.js +3 -1
- package/dist/utilities/getDBName.js.map +1 -1
- package/dist/utilities/handleError.d.ts +6 -2
- package/dist/utilities/handleError.d.ts.map +1 -1
- package/dist/utilities/handleError.js +12 -9
- package/dist/utilities/handleError.js.map +1 -1
- package/dist/utilities/sanitizeInternalFields.d.ts +1 -2
- package/dist/utilities/sanitizeInternalFields.d.ts.map +1 -1
- package/dist/utilities/sanitizeInternalFields.js +1 -2
- package/dist/utilities/sanitizeInternalFields.js.map +1 -1
- package/dist/utilities/sanitizeRelationshipIDs.d.ts +9 -0
- package/dist/utilities/sanitizeRelationshipIDs.d.ts.map +1 -0
- package/dist/utilities/sanitizeRelationshipIDs.js +115 -0
- package/dist/utilities/sanitizeRelationshipIDs.js.map +1 -0
- package/dist/withSession.d.ts +3 -27
- package/dist/withSession.d.ts.map +1 -1
- package/dist/withSession.js +10 -4
- package/dist/withSession.js.map +1 -1
- package/package.json +14 -12
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/queries/buildSearchParams.ts"],"sourcesContent":["import type { Payload } from 'payload'\nimport type { PathToQuery } from 'payload/database'\nimport type { Field } from 'payload/types'\nimport type { Operator } from 'payload/types'\n\nimport ObjectIdImport from 'bson-objectid'\nimport mongoose from 'mongoose'\nimport { getLocalizedPaths } from 'payload/database'\nimport { fieldAffectsData } from 'payload/types'\nimport { validOperators } from 'payload/types'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { operatorMap } from './operatorMap.js'\nimport { sanitizeQueryValue } from './sanitizeQueryValue.js'\n\nconst ObjectId = (ObjectIdImport.default ||\n ObjectIdImport) as unknown as typeof ObjectIdImport.default\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: Field[]\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') sanitizedPath = '_id'\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 Field,\n path: '_id',\n })\n } else {\n paths = await getLocalizedPaths({\n collectionSlug,\n fields,\n globalSlug,\n incomingPath: sanitizedPath,\n locale,\n payload,\n })\n }\n\n const [{ field, path }] = paths\n\n if (path) {\n const {\n operator: formattedOperator,\n rawQuery,\n val: formattedValue,\n } = sanitizeQueryValue({\n field,\n hasCustomID,\n operator,\n path,\n val,\n })\n\n if (rawQuery) return { value: rawQuery }\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 (mongoose.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.toString())\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') multiIDCondition = '$and'\n\n const result = {\n value: {\n [multiIDCondition]: [{ [path]: { [operatorKey]: formattedValue } }],\n },\n }\n\n if (typeof formattedValue === 'string') {\n if (mongoose.Types.ObjectId.isValid(formattedValue)) {\n result.value[multiIDCondition].push({\n [path]: { [operatorKey]: 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 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":["ObjectIdImport","mongoose","getLocalizedPaths","validOperators","operatorMap","sanitizeQueryValue","ObjectId","default","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","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","Types","isValid","nextSubPath","map","Promise","resolve","includes","operatorKey","hasNumberIDRelation","multiIDCondition","Array","isArray","relationTo","isRelatedToCustomNumberID","parseFloat","words","split","$and","word","$options","$regex","undefined"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAKA,OAAOA,oBAAoB,gBAAe;AAC1C,OAAOC,cAAc,WAAU;AAC/B,SAASC,iBAAiB,QAAQ,mBAAkB;AAEpD,SAASC,cAAc,QAAQ,gBAAe;AAI9C,SAASC,WAAW,QAAQ,mBAAkB;AAC9C,SAASC,kBAAkB,QAAQ,0BAAyB;AAE5D,MAAMC,WAAYN,eAAe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AY,EAAEO,WAAWzC;4BAAgB;wBACtD;oBACJ;gBACF;gBAEA,IAAImB,OAAOlB,KAAK,CAACmC,iBAAiB,CAAClC,MAAM,GAAG,GAAG;oBAC7C,OAAOiB;gBACT;YACF;YAEA,IAAIrB,sBAAsB,UAAU,OAAOE,mBAAmB,UAAU;gBACtE,MAAM0C,QAAQ1C,eAAe2C,KAAK,CAAC;gBAEnC,MAAMxB,SAAS;oBACblB,OAAO;wBACL2C,MAAMF,MAAMZ,GAAG,CAAC,CAACe,OAAU,CAAA;gCACzB,CAAChD,KAAK,EAAE;oCACNiD,UAAU;oCACVC,QAAQF,KAAK5D,OAAO,CAAC,uBAAuB;gCAC9C;4BACF,CAAA;oBACF;gBACF;gBAEA,OAAOkC;YACT;YAEA,yDAAyD;YACzD,wDAAwD;YACxD,IAAI,CAACe,aAAa;gBAChB,OAAO;oBACLrC;oBACAI,OAAOD;gBACT;YACF;YAEA,OAAO;gBACLH;gBACAI,OAAO;oBAAE,CAACiC,YAAY,EAAElC;gBAAe;YACzC;QACF;IACF;IACA,OAAOgD;AACT"}
|
1
|
+
{"version":3,"sources":["../../src/queries/buildSearchParams.ts"],"sourcesContent":["import type { Field, Operator, PathToQuery, Payload } from 'payload'\n\nimport ObjectIdImport from 'bson-objectid'\nimport mongoose 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\nconst ObjectId = (ObjectIdImport.default ||\n ObjectIdImport) as unknown as typeof ObjectIdImport.default\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: Field[]\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 Field,\n path: '_id',\n })\n } else {\n paths = await getLocalizedPaths({\n collectionSlug,\n fields,\n globalSlug,\n incomingPath: sanitizedPath,\n locale,\n payload,\n })\n }\n\n const [{ field, path }] = paths\n\n if (path) {\n const {\n operator: formattedOperator,\n rawQuery,\n val: formattedValue,\n } = sanitizeQueryValue({\n field,\n hasCustomID,\n operator,\n path,\n val,\n })\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 (mongoose.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 (mongoose.Types.ObjectId.isValid(formattedValue)) {\n result.value[multiIDCondition].push({\n [path]: { [operatorKey]: 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":["ObjectIdImport","mongoose","getLocalizedPaths","validOperators","operatorMap","sanitizeQueryValue","ObjectId","default","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","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","Types","isValid","nextSubPath","map","Promise","resolve","includes","operatorKey","hasNumberIDRelation","multiIDCondition","Array","isArray","relationTo","isRelatedToCustomNumberID","parseFloat","words","split","$and","word","$options","$regex","undefined"],"mappings":"AAEA,OAAOA,oBAAoB,gBAAe;AAC1C,OAAOC,cAAc,WAAU;AAC/B,SAASC,iBAAiB,QAAQ,UAAS;AAC3C,SAASC,cAAc,QAAQ,iBAAgB;AAI/C,SAASC,WAAW,QAAQ,mBAAkB;AAC9C,SAASC,kBAAkB,QAAQ,0BAAyB;AAE5D,MAAMC,WAAYN,eAAeO,OAAO,IACtCP;AAQF,MAAMQ,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,IAAI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U,CAAA;gCACzB,CAAChD,KAAK,EAAE;oCACNiD,UAAU;oCACVC,QAAQF,KAAK5D,OAAO,CAAC,uBAAuB;gCAC9C;4BACF,CAAA;oBACF;gBACF;gBAEA,OAAOkC;YACT;YAEA,yDAAyD;YACzD,wDAAwD;YACxD,IAAI,CAACe,aAAa;gBAChB,OAAO;oBACLrC;oBACAI,OAAOD;gBACT;YACF;YAEA,OAAO;gBACLH;gBACAI,OAAO;oBAAE,CAACiC,YAAY,EAAElC;gBAAe;YACzC;QACF;IACF;IACA,OAAOgD;AACT"}
|
@@ -1,36 +1,10 @@
|
|
1
|
-
/// <reference types="mongoose/types/aggregate.js" />
|
2
|
-
/// <reference types="mongoose/types/callback.js" />
|
3
|
-
/// <reference types="mongoose/types/collection.js" />
|
4
|
-
/// <reference types="mongoose/types/connection.js" />
|
5
|
-
/// <reference types="mongoose/types/cursor.js" />
|
6
|
-
/// <reference types="mongoose/types/document.js" />
|
7
|
-
/// <reference types="mongoose/types/error.js" />
|
8
|
-
/// <reference types="mongoose/types/expressions.js" />
|
9
|
-
/// <reference types="mongoose/types/helpers.js" />
|
10
|
-
/// <reference types="mongoose/types/middlewares.js" />
|
11
|
-
/// <reference types="mongoose/types/indexes.js" />
|
12
|
-
/// <reference types="mongoose/types/models.js" />
|
13
|
-
/// <reference types="mongoose/types/mongooseoptions.js" />
|
14
|
-
/// <reference types="mongoose/types/pipelinestage.js" />
|
15
|
-
/// <reference types="mongoose/types/populate.js" />
|
16
|
-
/// <reference types="mongoose/types/query.js" />
|
17
|
-
/// <reference types="mongoose/types/schemaoptions.js" />
|
18
|
-
/// <reference types="mongoose/types/schematypes.js" />
|
19
|
-
/// <reference types="mongoose/types/session.js" />
|
20
|
-
/// <reference types="mongoose/types/types.js" />
|
21
|
-
/// <reference types="mongoose/types/utility.js" />
|
22
|
-
/// <reference types="mongoose/types/validation.js" />
|
23
|
-
/// <reference types="mongoose/types/virtuals.js" />
|
24
|
-
/// <reference types="mongoose/types/inferschematype.js" />
|
25
|
-
/// <reference types="mongoose-paginate-v2" />
|
26
1
|
import type { PaginateOptions } from 'mongoose';
|
27
|
-
import type { SanitizedConfig } from 'payload
|
28
|
-
import type { Field } from 'payload/types';
|
2
|
+
import type { Field, SanitizedConfig, Sort } from 'payload';
|
29
3
|
type Args = {
|
30
4
|
config: SanitizedConfig;
|
31
5
|
fields: Field[];
|
32
6
|
locale: string;
|
33
|
-
sort:
|
7
|
+
sort: Sort;
|
34
8
|
timestamps: boolean;
|
35
9
|
};
|
36
10
|
export type SortArgs = {
|
@@ -38,6 +12,6 @@ export type SortArgs = {
|
|
38
12
|
property: string;
|
39
13
|
}[];
|
40
14
|
export type SortDirection = 'asc' | 'desc';
|
41
|
-
export declare const buildSortParam: ({ config, fields, locale, sort, timestamps, }: Args) => PaginateOptions[
|
15
|
+
export declare const buildSortParam: ({ config, fields, locale, sort, timestamps, }: Args) => PaginateOptions["sort"];
|
42
16
|
export {};
|
43
17
|
//# 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":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,UAAU,CAAA;AAC/C,OAAO,KAAK,EAAE,KAAK,EAAE,eAAe,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAI3D,KAAK,IAAI,GAAG;IACV,MAAM,EAAE,eAAe,CAAA;IACvB,MAAM,EAAE,KAAK,EAAE,CAAA;IACf,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,EAAE,IAAI,CAAA;IACV,UAAU,EAAE,OAAO,CAAA;CACpB,CAAA;AAED,MAAM,MAAM,QAAQ,GAAG;IACrB,SAAS,EAAE,aAAa,CAAA;IACxB,QAAQ,EAAE,MAAM,CAAA;CACjB,EAAE,CAAA;AAEH,MAAM,MAAM,aAAa,GAAG,KAAK,GAAG,MAAM,CAAA;AAE1C,eAAO,MAAM,cAAc,kDAMxB,IAAI,KAAG,eAAe,CAAC,MAAM,CAsC/B,CAAA"}
|
@@ -1,32 +1,41 @@
|
|
1
1
|
import { getLocalizedSortProperty } from './getLocalizedSortProperty.js';
|
2
2
|
export const buildSortParam = ({ config, fields, locale, sort, timestamps })=>{
|
3
|
-
let sortProperty;
|
4
|
-
let sortDirection = 'desc';
|
5
3
|
if (!sort) {
|
6
4
|
if (timestamps) {
|
7
|
-
|
5
|
+
sort = '-createdAt';
|
8
6
|
} else {
|
9
|
-
|
7
|
+
sort = '-id';
|
10
8
|
}
|
11
|
-
} else if (sort.indexOf('-') === 0) {
|
12
|
-
sortProperty = sort.substring(1);
|
13
|
-
} else {
|
14
|
-
sortProperty = sort;
|
15
|
-
sortDirection = 'asc';
|
16
9
|
}
|
17
|
-
if (
|
18
|
-
|
19
|
-
|
20
|
-
|
10
|
+
if (typeof sort === 'string') {
|
11
|
+
sort = [
|
12
|
+
sort
|
13
|
+
];
|
14
|
+
}
|
15
|
+
const sorting = sort.reduce((acc, item)=>{
|
16
|
+
let sortProperty;
|
17
|
+
let sortDirection;
|
18
|
+
if (item.indexOf('-') === 0) {
|
19
|
+
sortProperty = item.substring(1);
|
20
|
+
sortDirection = 'desc';
|
21
|
+
} else {
|
22
|
+
sortProperty = item;
|
23
|
+
sortDirection = 'asc';
|
24
|
+
}
|
25
|
+
if (sortProperty === 'id') {
|
26
|
+
acc['_id'] = sortDirection;
|
27
|
+
return acc;
|
28
|
+
}
|
29
|
+
const localizedProperty = getLocalizedSortProperty({
|
21
30
|
config,
|
22
31
|
fields,
|
23
32
|
locale,
|
24
33
|
segments: sortProperty.split('.')
|
25
34
|
});
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
35
|
+
acc[localizedProperty] = sortDirection;
|
36
|
+
return acc;
|
37
|
+
}, {});
|
38
|
+
return sorting;
|
30
39
|
};
|
31
40
|
|
32
41
|
//# sourceMappingURL=buildSortParam.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/queries/buildSortParam.ts"],"sourcesContent":["import type { PaginateOptions } from 'mongoose'\nimport type { SanitizedConfig
|
1
|
+
{"version":3,"sources":["../../src/queries/buildSortParam.ts"],"sourcesContent":["import type { PaginateOptions } from 'mongoose'\nimport type { Field, SanitizedConfig, Sort } from 'payload'\n\nimport { getLocalizedSortProperty } from './getLocalizedSortProperty.js'\n\ntype Args = {\n config: SanitizedConfig\n fields: Field[]\n locale: string\n sort: Sort\n timestamps: boolean\n}\n\nexport type SortArgs = {\n direction: SortDirection\n property: string\n}[]\n\nexport type SortDirection = 'asc' | 'desc'\n\nexport const buildSortParam = ({\n config,\n fields,\n locale,\n sort,\n timestamps,\n}: Args): PaginateOptions['sort'] => {\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<PaginateOptions['sort']>((acc, item) => {\n let sortProperty: string\n let sortDirection: SortDirection\n if (item.indexOf('-') === 0) {\n sortProperty = item.substring(1)\n sortDirection = 'desc'\n } else {\n sortProperty = item\n sortDirection = 'asc'\n }\n if (sortProperty === 'id') {\n acc['_id'] = sortDirection\n return acc\n }\n 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;AAiBxE,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,CAA0B,CAACC,KAAKC;QACzD,IAAIC;QACJ,IAAIC;QACJ,IAAIF,KAAKG,OAAO,CAAC,SAAS,GAAG;YAC3BF,eAAeD,KAAKI,SAAS,CAAC;YAC9BF,gBAAgB;QAClB,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 +1 @@
|
|
1
|
-
{"version":3,"file":"getLocalizedSortProperty.d.ts","sourceRoot":"","sources":["../../src/queries/getLocalizedSortProperty.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
1
|
+
{"version":3,"file":"getLocalizedSortProperty.d.ts","sourceRoot":"","sources":["../../src/queries/getLocalizedSortProperty.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,eAAe,EAAE,MAAM,SAAS,CAAA;AAKrD,KAAK,IAAI,GAAG;IACV,MAAM,EAAE,eAAe,CAAA;IACvB,MAAM,EAAE,KAAK,EAAE,CAAA;IACf,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,MAAM,EAAE,CAAA;CACnB,CAAA;AAED,eAAO,MAAM,wBAAwB,oGAMlC,IAAI,KAAG,MAkFT,CAAA"}
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import {
|
2
|
-
import {
|
1
|
+
import { flattenTopLevelFields } from 'payload';
|
2
|
+
import { fieldAffectsData, fieldIsPresentationalOnly } from 'payload/shared';
|
3
3
|
export const getLocalizedSortProperty = ({ config, fields: incomingFields, locale, result: incomingResult, segments: incomingSegments })=>{
|
4
4
|
// If localization is not enabled, accept exactly
|
5
5
|
// what is sent in
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/queries/getLocalizedSortProperty.ts"],"sourcesContent":["import type { SanitizedConfig } from 'payload
|
1
|
+
{"version":3,"sources":["../../src/queries/getLocalizedSortProperty.ts"],"sourcesContent":["import type { Field, SanitizedConfig } from 'payload'\n\nimport { flattenTopLevelFields } from 'payload'\nimport { fieldAffectsData, fieldIsPresentationalOnly } from 'payload/shared'\n\ntype Args = {\n config: SanitizedConfig\n fields: Field[]\n locale: string\n result?: string\n segments: string[]\n}\n\nexport const getLocalizedSortProperty = ({\n config,\n fields: incomingFields,\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 // Flatten incoming fields (row, etc)\n const fields = flattenTopLevelFields(incomingFields)\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 && !fieldIsPresentationalOnly(matchedField)) {\n let nextFields: Field[]\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.fields\n }\n\n if (matchedField.type === 'blocks') {\n nextFields = matchedField.blocks.reduce((flattenedBlockFields, block) => {\n return [\n ...flattenedBlockFields,\n ...block.fields.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":["flattenTopLevelFields","fieldAffectsData","fieldIsPresentationalOnly","getLocalizedSortProperty","config","fields","incomingFields","locale","result","incomingResult","segments","incomingSegments","localization","join","firstSegment","shift","matchedField","find","field","name","nextFields","remainingSegments","localizedSegment","localized","length","nextSegmentIsLocale","localeCodes","includes","nextSegment","type","blocks","reduce","flattenedBlockFields","block","filter","blockField"],"mappings":"AAEA,SAASA,qBAAqB,QAAQ,UAAS;AAC/C,SAASC,gBAAgB,EAAEC,yBAAyB,QAAQ,iBAAgB;AAU5E,OAAO,MAAMC,2BAA2B,CAAC,EACvCC,MAAM,EACNC,QAAQC,cAAc,EACtBC,MAAM,EACNC,QAAQC,cAAc,EACtBC,UAAUC,gBAAgB,EACrB;IACL,iDAAiD;IACjD,kBAAkB;IAClB,IAAI,CAACP,OAAOQ,YAAY,EAAE;QACxB,OAAOD,iBAAiBE,IAAI,CAAC;IAC/B;IAEA,qCAAqC;IACrC,MAAMR,SAASL,sBAAsBM;IAErC,MAAMI,WAAW;WAAIC;KAAiB;IAEtC,mDAAmD;IACnD,MAAMG,eAAeJ,SAASK,KAAK;IAEnC,kCAAkC;IAClC,MAAMC,eAAeX,OAAOY,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AC9BC;gBACAC,QAAQe;gBACRb;gBACAC;gBACAE,UAAUW;YACZ;QACF;QAEA,OAAOb;IACT;IAEA,OAAOG,iBAAiBE,IAAI,CAAC;AAC/B,EAAC"}
|
@@ -1,24 +1,21 @@
|
|
1
|
-
import { sanitizeConfig } from 'payload
|
1
|
+
import { sanitizeConfig } from 'payload';
|
2
2
|
import { getLocalizedSortProperty } from './getLocalizedSortProperty.js';
|
3
3
|
let config;
|
4
4
|
describe('get localized sort property', ()=>{
|
5
5
|
beforeAll(async ()=>{
|
6
6
|
config = await sanitizeConfig({
|
7
7
|
localization: {
|
8
|
+
defaultLocale: 'en',
|
9
|
+
fallback: true,
|
8
10
|
locales: [
|
9
11
|
'en',
|
10
12
|
'es'
|
11
|
-
]
|
12
|
-
defaultLocale: 'en',
|
13
|
-
fallback: true
|
13
|
+
]
|
14
14
|
}
|
15
15
|
});
|
16
16
|
});
|
17
|
-
it('passes through a non-localized sort property',
|
17
|
+
it('passes through a non-localized sort property', ()=>{
|
18
18
|
const result = getLocalizedSortProperty({
|
19
|
-
segments: [
|
20
|
-
'title'
|
21
|
-
],
|
22
19
|
config,
|
23
20
|
fields: [
|
24
21
|
{
|
@@ -26,15 +23,15 @@ describe('get localized sort property', ()=>{
|
|
26
23
|
type: 'text'
|
27
24
|
}
|
28
25
|
],
|
29
|
-
locale: 'en'
|
26
|
+
locale: 'en',
|
27
|
+
segments: [
|
28
|
+
'title'
|
29
|
+
]
|
30
30
|
});
|
31
31
|
expect(result).toStrictEqual('title');
|
32
32
|
});
|
33
33
|
it('properly localizes an un-localized sort property', ()=>{
|
34
34
|
const result = getLocalizedSortProperty({
|
35
|
-
segments: [
|
36
|
-
'title'
|
37
|
-
],
|
38
35
|
config,
|
39
36
|
fields: [
|
40
37
|
{
|
@@ -43,16 +40,15 @@ describe('get localized sort property', ()=>{
|
|
43
40
|
localized: true
|
44
41
|
}
|
45
42
|
],
|
46
|
-
locale: 'en'
|
43
|
+
locale: 'en',
|
44
|
+
segments: [
|
45
|
+
'title'
|
46
|
+
]
|
47
47
|
});
|
48
48
|
expect(result).toStrictEqual('title.en');
|
49
49
|
});
|
50
50
|
it('keeps specifically asked-for localized sort properties', ()=>{
|
51
51
|
const result = getLocalizedSortProperty({
|
52
|
-
segments: [
|
53
|
-
'title',
|
54
|
-
'es'
|
55
|
-
],
|
56
52
|
config,
|
57
53
|
fields: [
|
58
54
|
{
|
@@ -61,16 +57,16 @@ describe('get localized sort property', ()=>{
|
|
61
57
|
localized: true
|
62
58
|
}
|
63
59
|
],
|
64
|
-
locale: 'en'
|
60
|
+
locale: 'en',
|
61
|
+
segments: [
|
62
|
+
'title',
|
63
|
+
'es'
|
64
|
+
]
|
65
65
|
});
|
66
66
|
expect(result).toStrictEqual('title.es');
|
67
67
|
});
|
68
68
|
it('properly localizes nested sort properties', ()=>{
|
69
69
|
const result = getLocalizedSortProperty({
|
70
|
-
segments: [
|
71
|
-
'group',
|
72
|
-
'title'
|
73
|
-
],
|
74
70
|
config,
|
75
71
|
fields: [
|
76
72
|
{
|
@@ -85,17 +81,16 @@ describe('get localized sort property', ()=>{
|
|
85
81
|
]
|
86
82
|
}
|
87
83
|
],
|
88
|
-
locale: 'en'
|
84
|
+
locale: 'en',
|
85
|
+
segments: [
|
86
|
+
'group',
|
87
|
+
'title'
|
88
|
+
]
|
89
89
|
});
|
90
90
|
expect(result).toStrictEqual('group.title.en');
|
91
91
|
});
|
92
92
|
it('keeps requested locale with nested sort properties', ()=>{
|
93
93
|
const result = getLocalizedSortProperty({
|
94
|
-
segments: [
|
95
|
-
'group',
|
96
|
-
'title',
|
97
|
-
'es'
|
98
|
-
],
|
99
94
|
config,
|
100
95
|
fields: [
|
101
96
|
{
|
@@ -110,15 +105,17 @@ describe('get localized sort property', ()=>{
|
|
110
105
|
]
|
111
106
|
}
|
112
107
|
],
|
113
|
-
locale: 'en'
|
108
|
+
locale: 'en',
|
109
|
+
segments: [
|
110
|
+
'group',
|
111
|
+
'title',
|
112
|
+
'es'
|
113
|
+
]
|
114
114
|
});
|
115
115
|
expect(result).toStrictEqual('group.title.es');
|
116
116
|
});
|
117
117
|
it('properly localizes field within row', ()=>{
|
118
118
|
const result = getLocalizedSortProperty({
|
119
|
-
segments: [
|
120
|
-
'title'
|
121
|
-
],
|
122
119
|
config,
|
123
120
|
fields: [
|
124
121
|
{
|
@@ -132,16 +129,15 @@ describe('get localized sort property', ()=>{
|
|
132
129
|
]
|
133
130
|
}
|
134
131
|
],
|
135
|
-
locale: 'en'
|
132
|
+
locale: 'en',
|
133
|
+
segments: [
|
134
|
+
'title'
|
135
|
+
]
|
136
136
|
});
|
137
137
|
expect(result).toStrictEqual('title.en');
|
138
138
|
});
|
139
139
|
it('properly localizes field within named tab', ()=>{
|
140
140
|
const result = getLocalizedSortProperty({
|
141
|
-
segments: [
|
142
|
-
'tab',
|
143
|
-
'title'
|
144
|
-
],
|
145
141
|
config,
|
146
142
|
fields: [
|
147
143
|
{
|
@@ -160,34 +156,38 @@ describe('get localized sort property', ()=>{
|
|
160
156
|
]
|
161
157
|
}
|
162
158
|
],
|
163
|
-
locale: 'en'
|
159
|
+
locale: 'en',
|
160
|
+
segments: [
|
161
|
+
'tab',
|
162
|
+
'title'
|
163
|
+
]
|
164
164
|
});
|
165
165
|
expect(result).toStrictEqual('tab.title.en');
|
166
166
|
});
|
167
167
|
it('properly localizes field within unnamed tab', ()=>{
|
168
168
|
const result = getLocalizedSortProperty({
|
169
|
-
segments: [
|
170
|
-
'title'
|
171
|
-
],
|
172
169
|
config,
|
173
170
|
fields: [
|
174
171
|
{
|
175
172
|
type: 'tabs',
|
176
173
|
tabs: [
|
177
174
|
{
|
178
|
-
label: 'Tab',
|
179
175
|
fields: [
|
180
176
|
{
|
181
177
|
name: 'title',
|
182
178
|
type: 'text',
|
183
179
|
localized: true
|
184
180
|
}
|
185
|
-
]
|
181
|
+
],
|
182
|
+
label: 'Tab'
|
186
183
|
}
|
187
184
|
]
|
188
185
|
}
|
189
186
|
],
|
190
|
-
locale: 'en'
|
187
|
+
locale: 'en',
|
188
|
+
segments: [
|
189
|
+
'title'
|
190
|
+
]
|
191
191
|
});
|
192
192
|
expect(result).toStrictEqual('title.en');
|
193
193
|
});
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/queries/getLocalizedSortProperty.spec.ts"],"sourcesContent":["import {
|
1
|
+
{"version":3,"sources":["../../src/queries/getLocalizedSortProperty.spec.ts"],"sourcesContent":["import type { Config, SanitizedConfig } from 'payload'\n\nimport { sanitizeConfig } from 'payload'\n\nimport { getLocalizedSortProperty } from './getLocalizedSortProperty.js'\n\nlet config: SanitizedConfig\n\ndescribe('get localized sort property', () => {\n beforeAll(async () => {\n config = await sanitizeConfig({\n localization: {\n defaultLocale: 'en',\n fallback: true,\n locales: ['en', 'es'],\n },\n } as Config)\n })\n it('passes through a non-localized sort property', () => {\n const result = getLocalizedSortProperty({\n config,\n fields: [\n {\n name: 'title',\n type: 'text',\n },\n ],\n locale: 'en',\n segments: ['title'],\n })\n\n expect(result).toStrictEqual('title')\n })\n\n it('properly localizes an un-localized sort property', () => {\n const result = getLocalizedSortProperty({\n config,\n fields: [\n {\n name: 'title',\n type: 'text',\n localized: true,\n },\n ],\n locale: 'en',\n segments: ['title'],\n })\n\n expect(result).toStrictEqual('title.en')\n })\n\n it('keeps specifically asked-for localized sort properties', () => {\n const result = getLocalizedSortProperty({\n config,\n fields: [\n {\n name: 'title',\n type: 'text',\n localized: true,\n },\n ],\n locale: 'en',\n segments: ['title', 'es'],\n })\n\n expect(result).toStrictEqual('title.es')\n })\n\n it('properly localizes nested sort properties', () => {\n const result = getLocalizedSortProperty({\n config,\n fields: [\n {\n name: 'group',\n type: 'group',\n fields: [\n {\n name: 'title',\n type: 'text',\n localized: true,\n },\n ],\n },\n ],\n locale: 'en',\n segments: ['group', 'title'],\n })\n\n expect(result).toStrictEqual('group.title.en')\n })\n\n it('keeps requested locale with nested sort properties', () => {\n const result = getLocalizedSortProperty({\n config,\n fields: [\n {\n name: 'group',\n type: 'group',\n fields: [\n {\n name: 'title',\n type: 'text',\n localized: true,\n },\n ],\n },\n ],\n locale: 'en',\n segments: ['group', 'title', 'es'],\n })\n\n expect(result).toStrictEqual('group.title.es')\n })\n\n it('properly localizes field within row', () => {\n const result = getLocalizedSortProperty({\n config,\n fields: [\n {\n type: 'row',\n fields: [\n {\n name: 'title',\n type: 'text',\n localized: true,\n },\n ],\n },\n ],\n locale: 'en',\n segments: ['title'],\n })\n\n expect(result).toStrictEqual('title.en')\n })\n\n it('properly localizes field within named tab', () => {\n const result = getLocalizedSortProperty({\n config,\n fields: [\n {\n type: 'tabs',\n tabs: [\n {\n name: 'tab',\n fields: [\n {\n name: 'title',\n type: 'text',\n localized: true,\n },\n ],\n },\n ],\n },\n ],\n locale: 'en',\n segments: ['tab', 'title'],\n })\n\n expect(result).toStrictEqual('tab.title.en')\n })\n\n it('properly localizes field within unnamed tab', () => {\n const result = getLocalizedSortProperty({\n config,\n fields: [\n {\n type: 'tabs',\n tabs: [\n {\n fields: [\n {\n name: 'title',\n type: 'text',\n localized: true,\n },\n ],\n label: 'Tab',\n },\n ],\n },\n ],\n locale: 'en',\n segments: ['title'],\n })\n\n expect(result).toStrictEqual('title.en')\n })\n})\n"],"names":["sanitizeConfig","getLocalizedSortProperty","config","describe","beforeAll","localization","defaultLocale","fallback","locales","it","result","fields","name","type","locale","segments","expect","toStrictEqual","localized","tabs","label"],"mappings":"AAEA,SAASA,cAAc,QAAQ,UAAS;AAExC,SAASC,wBAAwB,QAAQ,gCAA+B;AAExE,IAAIC;AAEJC,SAAS,+BAA+B;IACtCC,UAAU;QACRF,SAAS,MAAMF,eAAe;YAC5BK,cAAc;gBACZC,eAAe;gBACfC,UAAU;gBACVC,SAAS;oBAAC;oBAAM;iBAAK;YACvB;QACF;IACF;IACAC,GAAG,gDAAgD;QACjD,MAAMC,SAAST,yBAAyB;YACtCC;YACAS,QAAQ;gBACN;oBACEC,MAAM;oBACNC,MAAM;gBACR;a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,WAAW;wBACb;qBACD;gBACH;aACD;YACDJ,QAAQ;YACRC,UAAU;gBAAC;aAAQ;QACrB;QAEAC,OAAON,QAAQO,aAAa,CAAC;IAC/B;IAEAR,GAAG,6CAA6C;QAC9C,MAAMC,SAAST,yBAAyB;YACtCC;YACAS,QAAQ;gBACN;oBACEE,MAAM;oBACNM,MAAM;wBACJ;4BACEP,MAAM;4BACND,QAAQ;gCACN;oCACEC,MAAM;oCACNC,MAAM;oCACNK,WAAW;gCACb;6BACD;wBACH;qBACD;gBACH;aACD;YACDJ,QAAQ;YACRC,UAAU;gBAAC;gBAAO;aAAQ;QAC5B;QAEAC,OAAON,QAAQO,aAAa,CAAC;IAC/B;IAEAR,GAAG,+CAA+C;QAChD,MAAMC,SAAST,yBAAyB;YACtCC;YACAS,QAAQ;gBACN;oBACEE,MAAM;oBACNM,MAAM;wBACJ;4BACER,QAAQ;gCACN;oCACEC,MAAM;oCACNC,MAAM;oCACNK,WAAW;gCACb;6BACD;4BACDE,OAAO;wBACT;qBACD;gBACH;aACD;YACDN,QAAQ;YACRC,UAAU;gBAAC;aAAQ;QACrB;QAEAC,OAAON,QAAQO,aAAa,CAAC;IAC/B;AACF"}
|
package/dist/queries/mock.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/queries/mock.js"],"names":[],"
|
1
|
+
{"version":3,"sources":["../../src/queries/mock.js"],"names":[],"mappings":""}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/queries/operatorMap.ts"],"sourcesContent":["export const operatorMap = {\n all: '$all',\n equals: '$eq',\n exists: '$exists',\n greater_than: '$gt',\n greater_than_equal: '$gte',\n in: '$in',\n intersects: '$geoIntersects',\n less_than: '$lt',\n less_than_equal: '$lte',\n near: '$near',\n not_equals: '$ne',\n not_in: '$nin',\n within: '$geoWithin',\n}\n"],"names":["operatorMap","all","equals","exists","greater_than","greater_than_equal","in","intersects","less_than","less_than_equal","near","not_equals","not_in","within"],"
|
1
|
+
{"version":3,"sources":["../../src/queries/operatorMap.ts"],"sourcesContent":["export const operatorMap = {\n all: '$all',\n equals: '$eq',\n exists: '$exists',\n greater_than: '$gt',\n greater_than_equal: '$gte',\n in: '$in',\n intersects: '$geoIntersects',\n less_than: '$lt',\n less_than_equal: '$lte',\n near: '$near',\n not_equals: '$ne',\n not_in: '$nin',\n within: '$geoWithin',\n}\n"],"names":["operatorMap","all","equals","exists","greater_than","greater_than_equal","in","intersects","less_than","less_than_equal","near","not_equals","not_in","within"],"mappings":"AAAA,OAAO,MAAMA,cAAc;IACzBC,KAAK;IACLC,QAAQ;IACRC,QAAQ;IACRC,cAAc;IACdC,oBAAoB;IACpBC,IAAI;IACJC,YAAY;IACZC,WAAW;IACXC,iBAAiB;IACjBC,MAAM;IACNC,YAAY;IACZC,QAAQ;IACRC,QAAQ;AACV,EAAC"}
|
@@ -1,6 +1,4 @@
|
|
1
|
-
import type { Payload } from 'payload';
|
2
|
-
import type { Where } from 'payload/types';
|
3
|
-
import type { Field } from 'payload/types';
|
1
|
+
import type { Field, Payload, Where } from 'payload';
|
4
2
|
export declare function parseParams({ collectionSlug, fields, globalSlug, locale, payload, where, }: {
|
5
3
|
collectionSlug?: string;
|
6
4
|
fields: Field[];
|
@@ -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":"AACA,OAAO,KAAK,EAAE,KAAK,EAAY,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAQ9D,wBAAsB,WAAW,CAAC,EAChC,cAAc,EACd,MAAM,EACN,UAAU,EACV,MAAM,EACN,OAAO,EACP,KAAK,GACN,EAAE;IACD,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,MAAM,EAAE,KAAK,EAAE,CAAA;IACf,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,EAAE,OAAO,CAAA;IAChB,KAAK,EAAE,KAAK,CAAA;CACb,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CA4DnC"}
|
@@ -1,6 +1,5 @@
|
|
1
|
-
|
2
|
-
import { validOperators } from 'payload/
|
3
|
-
import { combineMerge } from 'payload/utilities';
|
1
|
+
import { deepMergeWithCombinedArrays } from 'payload';
|
2
|
+
import { validOperators } from 'payload/shared';
|
4
3
|
import { buildAndOrConditions } from './buildAndOrConditions.js';
|
5
4
|
import { buildSearchParam } from './buildSearchParams.js';
|
6
5
|
export async function parseParams({ collectionSlug, fields, globalSlug, locale, payload, where }) {
|
@@ -24,7 +23,9 @@ export async function parseParams({ collectionSlug, fields, globalSlug, locale,
|
|
24
23
|
payload,
|
25
24
|
where: condition
|
26
25
|
});
|
27
|
-
if (builtConditions.length > 0)
|
26
|
+
if (builtConditions.length > 0) {
|
27
|
+
result[conditionOperator] = builtConditions;
|
28
|
+
}
|
28
29
|
} else {
|
29
30
|
// It's a path - and there can be multiple comparisons on a single path.
|
30
31
|
// For example - title like 'test' and title not equal to 'tester'
|
@@ -49,9 +50,7 @@ export async function parseParams({ collectionSlug, fields, globalSlug, locale,
|
|
49
50
|
[searchParam.path]: searchParam.value
|
50
51
|
};
|
51
52
|
} else if (typeof searchParam?.value === 'object') {
|
52
|
-
result =
|
53
|
-
arrayMerge: combineMerge
|
54
|
-
});
|
53
|
+
result = deepMergeWithCombinedArrays(result, searchParam.value);
|
55
54
|
}
|
56
55
|
}
|
57
56
|
}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/queries/parseParams.ts"],"sourcesContent":["
|
1
|
+
{"version":3,"sources":["../../src/queries/parseParams.ts"],"sourcesContent":["import type { FilterQuery } from 'mongoose'\nimport type { Field, Operator, Payload, Where } from 'payload'\n\nimport { deepMergeWithCombinedArrays } from 'payload'\nimport { validOperators } from 'payload/shared'\n\nimport { buildAndOrConditions } from './buildAndOrConditions.js'\nimport { buildSearchParam } from './buildSearchParams.js'\n\nexport async function parseParams({\n collectionSlug,\n fields,\n globalSlug,\n locale,\n payload,\n where,\n}: {\n collectionSlug?: string\n fields: Field[]\n globalSlug?: string\n locale: string\n payload: Payload\n where: Where\n}): Promise<Record<string, unknown>> {\n let result = {} as FilterQuery<any>\n\n if (typeof where === 'object') {\n // We need to determine if the whereKey is an AND, OR, or a schema path\n for (const relationOrPath of Object.keys(where)) {\n const condition = where[relationOrPath]\n let conditionOperator: '$and' | '$or'\n if (relationOrPath.toLowerCase() === 'and') {\n conditionOperator = '$and'\n } else if (relationOrPath.toLowerCase() === 'or') {\n conditionOperator = '$or'\n }\n if (Array.isArray(condition)) {\n const builtConditions = await buildAndOrConditions({\n collectionSlug,\n fields,\n globalSlug,\n locale,\n payload,\n where: condition,\n })\n if (builtConditions.length > 0) {\n result[conditionOperator] = builtConditions\n }\n } else {\n // It's a path - and there can be multiple comparisons on a single path.\n // For example - title like 'test' and title not equal to 'tester'\n // So we need to loop on keys again here to handle each operator independently\n const pathOperators = where[relationOrPath]\n if (typeof pathOperators === 'object') {\n for (const operator of Object.keys(pathOperators)) {\n if (validOperators.includes(operator as Operator)) {\n const searchParam = await buildSearchParam({\n collectionSlug,\n fields,\n globalSlug,\n incomingPath: relationOrPath,\n locale,\n operator,\n payload,\n val: pathOperators[operator],\n })\n\n if (searchParam?.value && searchParam?.path) {\n result = {\n ...result,\n [searchParam.path]: searchParam.value,\n }\n } else if (typeof searchParam?.value === 'object') {\n result = deepMergeWithCombinedArrays(result, searchParam.value)\n }\n }\n }\n }\n }\n }\n }\n\n return result\n}\n"],"names":["deepMergeWithCombinedArrays","validOperators","buildAndOrConditions","buildSearchParam","parseParams","collectionSlug","fields","globalSlug","locale","payload","where","result","relationOrPath","Object","keys","condition","conditionOperator","toLowerCase","Array","isArray","builtConditions","length","pathOperators","operator","includes","searchParam","incoming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,GAAGA,MAAM;oCACT,CAACc,YAAYI,IAAI,CAAC,EAAEJ,YAAYG,KAAK;gCACvC;4BACF,OAAO,IAAI,OAAOH,aAAaG,UAAU,UAAU;gCACjDjB,SAASX,4BAA4BW,QAAQc,YAAYG,KAAK;4BAChE;wBACF;oBACF;gBACF;YACF;QACF;IACF;IAEA,OAAOjB;AACT"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"sanitizeQueryValue.d.ts","sourceRoot":"","sources":["../../src/queries/sanitizeQueryValue.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,
|
1
|
+
{"version":3,"file":"sanitizeQueryValue.d.ts","sourceRoot":"","sources":["../../src/queries/sanitizeQueryValue.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAMhD,KAAK,sBAAsB,GAAG;IAC5B,KAAK,EAAE,KAAK,GAAG,UAAU,CAAA;IACzB,WAAW,EAAE,OAAO,CAAA;IACpB,QAAQ,EAAE,MAAM,CAAA;IAChB,IAAI,EAAE,MAAM,CAAA;IACZ,GAAG,EAAE,GAAG,CAAA;CACT,CAAA;AA4BD,eAAO,MAAM,kBAAkB,iDAM5B,sBAAsB,KAAG;IAC1B,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,GAAG,CAAC,EAAE,OAAO,CAAA;CAsOd,CAAA"}
|