fix(db-mongodb): adds new optional collation feature flag behind mongodb collation option (#7361)

## Description

V2 PR [here](https://github.com/payloadcms/payload/pull/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
This commit is contained in:
Patrik
2024-07-25 12:38:18 -04:00
committed by GitHub
parent f8c79d2f84
commit 8ba39aa5ca
7 changed files with 53 additions and 9 deletions

View File

@@ -37,6 +37,7 @@ export default buildConfig({
| `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 |
| `migrationDir` | Customize the directory that migrations are stored. |
| `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. |
| `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/). |
## Access to Mongoose models

View File

@@ -54,8 +54,12 @@ export const find: Find = async function find(
useEstimatedCount,
}
if (locale && locale !== 'all' && locale !== '*') {
paginationOptions.collation = { locale, strength: 1 }
if (this.collation) {
const defaultLocale = 'en'
paginationOptions.collation = {
locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,
...this.collation,
}
}
if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {

View File

@@ -72,8 +72,12 @@ export const findGlobalVersions: FindGlobalVersions = async function findGlobalV
useEstimatedCount,
}
if (locale && locale !== 'all' && locale !== '*') {
paginationOptions.collation = { locale, strength: 1 }
if (this.collation) {
const defaultLocale = 'en'
paginationOptions.collation = {
locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,
...this.collation,
}
}
if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {

View File

@@ -69,8 +69,12 @@ export const findVersions: FindVersions = async function findVersions(
useEstimatedCount,
}
if (locale && locale !== 'all' && locale !== '*') {
paginationOptions.collation = { locale, strength: 1 }
if (this.collation) {
const defaultLocale = 'en'
paginationOptions.collation = {
locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,
...this.collation,
}
}
if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {

View File

@@ -1,4 +1,4 @@
import type { TransactionOptions } from 'mongodb'
import type { CollationOptions, TransactionOptions } from 'mongodb'
import type { MongoMemoryReplSet } from 'mongodb-memory-server'
import type { ClientSession, ConnectOptions, Connection } from 'mongoose'
import type { BaseDatabaseAdapter, DatabaseAdapterObj, Payload } from 'payload'
@@ -42,6 +42,30 @@ export type { MigrateDownArgs, MigrateUpArgs } from './types.js'
export interface Args {
/** Set to false to disable auto-pluralization of collection names, Defaults to true */
autoPluralization?: boolean
/**
* If enabled, collation allows for language-specific rules for string comparison.
* This configuration can include the following options:
*
* - `strength` (number): Comparison level (1: Primary, 2: Secondary, 3: Tertiary (default), 4: Quaternary, 5: Identical)
* - `caseLevel` (boolean): Include case comparison at strength level 1 or 2.
* - `caseFirst` (string): Sort order of case differences during tertiary level comparisons ("upper", "lower", "off").
* - `numericOrdering` (boolean): Compare numeric strings as numbers.
* - `alternate` (string): Consider whitespace and punctuation as base characters ("non-ignorable", "shifted").
* - `maxVariable` (string): Characters considered ignorable when `alternate` is "shifted" ("punct", "space").
* - `backwards` (boolean): Sort strings with diacritics from back of the string.
* - `normalization` (boolean): Check if text requires normalization and perform normalization.
*
* Available on MongoDB version 3.4 and up.
* The locale that gets passed is your current project's locale but defaults to "en".
*
* Example:
* {
* strength: 3
* }
*
* Defaults to disabled.
*/
collation?: Omit<CollationOptions, 'locale'>
/** Extra configuration options */
connectOptions?: {
/** Set false to disable $facet aggregation in non-supporting databases, Defaults to true */

View File

@@ -57,8 +57,12 @@ export const queryDrafts: QueryDrafts = async function queryDrafts(
useEstimatedCount,
}
if (locale && locale !== 'all' && locale !== '*') {
paginationOptions.collation = { locale, strength: 1 }
if (this.collation) {
const defaultLocale = 'en'
paginationOptions.collation = {
locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,
...this.collation,
}
}
if (

View File

@@ -50,6 +50,9 @@ export async function buildConfigWithDefaults(
process.env.MONGODB_MEMORY_SERVER_URI ||
process.env.DATABASE_URI ||
'mongodb://127.0.0.1/payloadtests',
collation: {
strength: 1,
},
}),
postgres: postgresAdapter({
pool: {