feat: add jsonParse flag to mongooseAdapter that preserves existing, untracked MongoDB data types (#7338)

## Description

Preserves external data structures stored in MongoDB by avoiding the use
of `JSON.parse(JSON.stringify(mongooseDoc))`.

- [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] New feature (non-breaking change which adds functionality)

## Checklist:

- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] Existing test suite passes locally with my changes
This commit is contained in:
James Mikrut
2024-07-24 14:02:06 -04:00
committed by GitHub
parent 8259611ce6
commit f96cf593ce
49 changed files with 146 additions and 102 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

View File

@@ -187,6 +187,8 @@ const createDatabaseTestConfig = async () => {
configWithDefaults.db = mongooseAdapter({
migrationDir,
url: 'mongodb://127.0.0.1/payloadtests',
// Disable JSON parsing to retain existing data shape(s)
jsonParse: false,
// Disable strict mode for Mongoose
schemaOptions: {
strict: false,

View File

@@ -353,39 +353,45 @@ describe('database', () => {
if (payload.db.name === 'mongoose') {
const Model = payload.db.collections['custom-schema']
const [doc] = await Model.create([
{
text: 'hello',
localizedText: {
en: 'goodbye',
const [doc] = await Model.create(
[
{
text: 'hello',
localizedText: {
en: 'goodbye',
},
noFieldDefined: 'hi',
isoDate: new Date('2024-07-24T12:00:00Z'),
array: [
{
id: uuid(),
noFieldDefined: 'hi',
isoDate: new Date('2024-07-24T12:00:00Z'),
text: 'hello',
localizedText: {
en: 'goodbye',
},
},
],
blocks: [
{
id: uuid(),
blockType: 'block',
noFieldDefined: 'hi',
isoDate: new Date('2024-07-24T12:00:00Z'),
text: 'hello',
localizedText: {
en: 'goodbye',
},
},
],
},
noFieldDefined: 'hi',
array: [
{
id: uuid(),
noFieldDefined: 'hi',
text: 'hello',
localizedText: {
en: 'goodbye',
},
},
],
blocks: [
{
id: uuid(),
blockType: 'block',
noFieldDefined: 'hi',
text: 'hello',
localizedText: {
en: 'goodbye',
},
},
],
},
])
],
{ lean: true },
)
const result = JSON.parse(JSON.stringify(doc))
result.id = result._id
const result = doc.toObject()
result.id = result._id.toString()
existingDataDoc = result
}
})
@@ -406,6 +412,10 @@ describe('database', () => {
expect(docWithExistingData.noFieldDefined).toStrictEqual('hi')
expect(docWithExistingData.array[0].noFieldDefined).toStrictEqual('hi')
expect(docWithExistingData.blocks[0].noFieldDefined).toStrictEqual('hi')
expect(docWithExistingData.isoDate instanceof Date).toBeTruthy()
expect(docWithExistingData.array[0].isoDate instanceof Date).toBeTruthy()
expect(docWithExistingData.blocks[0].isoDate instanceof Date).toBeTruthy()
}
})
@@ -447,6 +457,10 @@ describe('database', () => {
expect(result.noFieldDefined).toStrictEqual('hi')
expect(result.array[0].noFieldDefined).toStrictEqual('hi')
expect(result.blocks[0].noFieldDefined).toStrictEqual('hi')
expect(result.isoDate instanceof Date).toBeTruthy()
expect(result.array[0].isoDate instanceof Date).toBeTruthy()
expect(result.blocks[0].isoDate instanceof Date).toBeTruthy()
}
})
})

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 50 KiB