Skip to content

Commit

Permalink
fix(db-mongodb): adds new optional collation feature flag behind mo…
Browse files Browse the repository at this point in the history
…ngodb collation option (#7359)

## Description

Fixes #7349 

Adds new `collation` prop to the mongodb adapter config to allow for
enabling the `mongodb` collation feature.

- [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
  • Loading branch information
PatrikKozak committed Jul 25, 2024
1 parent 468e544 commit 9750bc2
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 22 deletions.
27 changes: 14 additions & 13 deletions docs/database/mongodb.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ export default buildConfig({

### Options

| Option | Description |
|----------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `autoPluralization` | Tell Mongoose to auto-pluralize any collection names if it encounters any singular words used as collection `slug`s. |
| `schemaOptions` | Customize schema options for all Mongoose schemas created internally. |
| `jsonParse` | Set to false to disable the automatic JSON stringify/parse of data queried by MongoDB. For example, if you have data not tracked by Payload such as `Date` fields and similar, you can use this option to ensure that existing `Date` properties remain as `Date` and not strings. |
| `collections` | Options on a collection-by-collection basis. [More](#collections-options) |
| `globals` | Options for the Globals collection created by Payload. [More](#globals-options) |
| `connectOptions` | Customize MongoDB connection options. Payload will connect to your MongoDB database using default options which you can override and extend to include all the [options](https://mongoosejs.com/docs/connections.html#options) available to mongoose. |
| `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. | |
| Option | Description |
|----------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `autoPluralization` | Tell Mongoose to auto-pluralize any collection names if it encounters any singular words used as collection `slug`s. |
| `schemaOptions` | Customize schema options for all Mongoose schemas created internally. |
| `jsonParse` | Set to false to disable the automatic JSON stringify/parse of data queried by MongoDB. For example, if you have data not tracked by Payload such as `Date` fields and similar, you can use this option to ensure that existing `Date` properties remain as `Date` and not strings. |
| `collections` | Options on a collection-by-collection basis. [More](#collections-options) |
| `globals` | Options for the Globals collection created by Payload. [More](#globals-options) |
| `connectOptions` | Customize MongoDB connection options. Payload will connect to your MongoDB database using default options which you can override and extend to include all the [options](https://mongoosejs.com/docs/connections.html#options) available to mongoose. |
| `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

Expand All @@ -64,7 +65,7 @@ const db = mongooseAdapter({
url: 'your-url-here',
collections: {
users: {
//
//
schemaOptions: {
strict: false,
}
Expand Down Expand Up @@ -99,4 +100,4 @@ mongooseAdapter({
strict: false,
},
})
```
```
8 changes: 6 additions & 2 deletions packages/db-mongodb/src/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,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) {
Expand Down
8 changes: 6 additions & 2 deletions packages/db-mongodb/src/findGlobalVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,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) {
Expand Down
8 changes: 6 additions & 2 deletions packages/db-mongodb/src/findVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,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) {
Expand Down
26 changes: 25 additions & 1 deletion packages/db-mongodb/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TransactionOptions } from 'mongodb'
import type { CollationOptions, TransactionOptions } from 'mongodb'
import type { ClientSession, ConnectOptions, Connection, SchemaOptions } from 'mongoose'
import type { Payload } from 'payload'
import type { BaseDatabaseAdapter } from 'payload/database'
Expand Down Expand Up @@ -42,6 +42,30 @@ export type { MigrateDownArgs, MigrateUpArgs } from './types'
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'>
/** Define Mongoose options on a collection-by-collection basis.
*/
collections?: {
Expand Down
8 changes: 6 additions & 2 deletions packages/db-mongodb/src/queryDrafts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,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 (
Expand Down
3 changes: 3 additions & 0 deletions test/buildConfigWithDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const databaseAdapters = {
mongoose: mongooseAdapter({
migrationDir,
url: 'mongodb://127.0.0.1/payloadtests',
collation: {
strength: 1,
},
}),
postgres: postgresAdapter({
migrationDir,
Expand Down

0 comments on commit 9750bc2

Please sign in to comment.