Skip to content

Commit 8ba39aa

Browse files
authored
fix(db-mongodb): adds new optional collation feature flag behind mongodb collation option (#7361)
## Description V2 PR [here](#7359) - [x] I have read and understand the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository. ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) - [x] New feature (non-breaking change which adds functionality) - [x] This change requires a documentation update ## Checklist: - [x] Existing test suite passes locally with my changes - [x] I have made corresponding changes to the documentation
1 parent f8c79d2 commit 8ba39aa

File tree

7 files changed

+53
-9
lines changed

7 files changed

+53
-9
lines changed

docs/database/mongodb.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export default buildConfig({
3737
| `disableIndexHints` | Set to true to disable hinting to MongoDB to use 'id' as index. This is currently done when counting documents for pagination, as it increases the speed of the count function used in that query. Disabling this optimization might fix some problems with AWS DocumentDB. Defaults to false |
3838
| `migrationDir` | Customize the directory that migrations are stored. |
3939
| `transactionOptions` | An object with configuration properties used in [transactions](https://www.mongodb.com/docs/manual/core/transactions/) or `false` which will disable the use of transactions. |
40+
| `collation` | Enable language-specific string comparison with customizable options. Available on MongoDB 3.4+. Defaults locale to "en". Example: `{ strength: 3 }`. For a full list of collation options and their definitions, see the [MongoDB documentation](https://www.mongodb.com/docs/manual/reference/collation/). |
4041

4142
## Access to Mongoose models
4243

packages/db-mongodb/src/find.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,12 @@ export const find: Find = async function find(
5454
useEstimatedCount,
5555
}
5656

57-
if (locale && locale !== 'all' && locale !== '*') {
58-
paginationOptions.collation = { locale, strength: 1 }
57+
if (this.collation) {
58+
const defaultLocale = 'en'
59+
paginationOptions.collation = {
60+
locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,
61+
...this.collation,
62+
}
5963
}
6064

6165
if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {

packages/db-mongodb/src/findGlobalVersions.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@ export const findGlobalVersions: FindGlobalVersions = async function findGlobalV
7272
useEstimatedCount,
7373
}
7474

75-
if (locale && locale !== 'all' && locale !== '*') {
76-
paginationOptions.collation = { locale, strength: 1 }
75+
if (this.collation) {
76+
const defaultLocale = 'en'
77+
paginationOptions.collation = {
78+
locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,
79+
...this.collation,
80+
}
7781
}
7882

7983
if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {

packages/db-mongodb/src/findVersions.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,12 @@ export const findVersions: FindVersions = async function findVersions(
6969
useEstimatedCount,
7070
}
7171

72-
if (locale && locale !== 'all' && locale !== '*') {
73-
paginationOptions.collation = { locale, strength: 1 }
72+
if (this.collation) {
73+
const defaultLocale = 'en'
74+
paginationOptions.collation = {
75+
locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,
76+
...this.collation,
77+
}
7478
}
7579

7680
if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {

packages/db-mongodb/src/index.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { TransactionOptions } from 'mongodb'
1+
import type { CollationOptions, TransactionOptions } from 'mongodb'
22
import type { MongoMemoryReplSet } from 'mongodb-memory-server'
33
import type { ClientSession, ConnectOptions, Connection } from 'mongoose'
44
import type { BaseDatabaseAdapter, DatabaseAdapterObj, Payload } from 'payload'
@@ -42,6 +42,30 @@ export type { MigrateDownArgs, MigrateUpArgs } from './types.js'
4242
export interface Args {
4343
/** Set to false to disable auto-pluralization of collection names, Defaults to true */
4444
autoPluralization?: boolean
45+
/**
46+
* If enabled, collation allows for language-specific rules for string comparison.
47+
* This configuration can include the following options:
48+
*
49+
* - `strength` (number): Comparison level (1: Primary, 2: Secondary, 3: Tertiary (default), 4: Quaternary, 5: Identical)
50+
* - `caseLevel` (boolean): Include case comparison at strength level 1 or 2.
51+
* - `caseFirst` (string): Sort order of case differences during tertiary level comparisons ("upper", "lower", "off").
52+
* - `numericOrdering` (boolean): Compare numeric strings as numbers.
53+
* - `alternate` (string): Consider whitespace and punctuation as base characters ("non-ignorable", "shifted").
54+
* - `maxVariable` (string): Characters considered ignorable when `alternate` is "shifted" ("punct", "space").
55+
* - `backwards` (boolean): Sort strings with diacritics from back of the string.
56+
* - `normalization` (boolean): Check if text requires normalization and perform normalization.
57+
*
58+
* Available on MongoDB version 3.4 and up.
59+
* The locale that gets passed is your current project's locale but defaults to "en".
60+
*
61+
* Example:
62+
* {
63+
* strength: 3
64+
* }
65+
*
66+
* Defaults to disabled.
67+
*/
68+
collation?: Omit<CollationOptions, 'locale'>
4569
/** Extra configuration options */
4670
connectOptions?: {
4771
/** Set false to disable $facet aggregation in non-supporting databases, Defaults to true */

packages/db-mongodb/src/queryDrafts.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ export const queryDrafts: QueryDrafts = async function queryDrafts(
5757
useEstimatedCount,
5858
}
5959

60-
if (locale && locale !== 'all' && locale !== '*') {
61-
paginationOptions.collation = { locale, strength: 1 }
60+
if (this.collation) {
61+
const defaultLocale = 'en'
62+
paginationOptions.collation = {
63+
locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,
64+
...this.collation,
65+
}
6266
}
6367

6468
if (

test/buildConfigWithDefaults.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ export async function buildConfigWithDefaults(
5050
process.env.MONGODB_MEMORY_SERVER_URI ||
5151
process.env.DATABASE_URI ||
5252
'mongodb://127.0.0.1/payloadtests',
53+
collation: {
54+
strength: 1,
55+
},
5356
}),
5457
postgres: postgresAdapter({
5558
pool: {

0 commit comments

Comments
 (0)