-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(db-mongodb): mongodb optimizations (#10120)
There are few issues introduced in #9594 that we need to look into with these changes.
- Loading branch information
Showing
46 changed files
with
941 additions
and
1,339 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,48 @@ | ||
import type { Create } from 'payload' | ||
import type { CreateOptions } from 'mongoose' | ||
import type { Create, Document } from 'payload' | ||
|
||
import type { MongooseAdapter } from './index.js' | ||
|
||
import { getSession } from './utilities/getSession.js' | ||
import { handleError } from './utilities/handleError.js' | ||
import { transform } from './utilities/transform.js' | ||
import { sanitizeRelationshipIDs } from './utilities/sanitizeRelationshipIDs.js' | ||
|
||
export const create: Create = async function create( | ||
this: MongooseAdapter, | ||
{ collection, data, req }, | ||
) { | ||
const Model = this.collections[collection] | ||
const session = await getSession(this, req) | ||
|
||
const fields = this.payload.collections[collection].config.flattenedFields | ||
|
||
if (this.payload.collections[collection].customIDType) { | ||
data._id = data.id | ||
const options: CreateOptions = { | ||
session: await getSession(this, req), | ||
} | ||
|
||
transform({ | ||
adapter: this, | ||
let doc | ||
|
||
const sanitizedData = sanitizeRelationshipIDs({ | ||
config: this.payload.config, | ||
data, | ||
fields, | ||
operation: 'create', | ||
fields: this.payload.collections[collection].config.fields, | ||
}) | ||
|
||
try { | ||
const { insertedId: insertedID } = await Model.collection.insertOne(data, { session }) | ||
data._id = insertedID | ||
|
||
transform({ | ||
adapter: this, | ||
data, | ||
fields, | ||
operation: 'read', | ||
}) | ||
if (this.payload.collections[collection].customIDType) { | ||
sanitizedData._id = sanitizedData.id | ||
} | ||
|
||
return data | ||
try { | ||
;[doc] = await Model.create([sanitizedData], options) | ||
} catch (error) { | ||
handleError({ collection, error, req }) | ||
} | ||
|
||
// doc.toJSON does not do stuff like converting ObjectIds to string, or date strings to date objects. That's why we use JSON.parse/stringify here | ||
const result: Document = JSON.parse(JSON.stringify(doc)) | ||
const verificationToken = doc._verificationToken | ||
|
||
// custom id type reset | ||
result.id = result._id | ||
if (verificationToken) { | ||
result._verificationToken = verificationToken | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,38 @@ | ||
import type { CreateOptions } from 'mongoose' | ||
import type { CreateGlobal } from 'payload' | ||
|
||
import type { MongooseAdapter } from './index.js' | ||
|
||
import { getSession } from './utilities/getSession.js' | ||
import { transform } from './utilities/transform.js' | ||
import { sanitizeInternalFields } from './utilities/sanitizeInternalFields.js' | ||
import { sanitizeRelationshipIDs } from './utilities/sanitizeRelationshipIDs.js' | ||
|
||
export const createGlobal: CreateGlobal = async function createGlobal( | ||
this: MongooseAdapter, | ||
{ slug, data, req }, | ||
) { | ||
const Model = this.globals | ||
|
||
const fields = this.payload.config.globals.find( | ||
(globalConfig) => globalConfig.slug === slug, | ||
).flattenedFields | ||
|
||
transform({ | ||
adapter: this, | ||
data, | ||
fields, | ||
globalSlug: slug, | ||
operation: 'create', | ||
const global = sanitizeRelationshipIDs({ | ||
config: this.payload.config, | ||
data: { | ||
globalType: slug, | ||
...data, | ||
}, | ||
fields: this.payload.config.globals.find((globalConfig) => globalConfig.slug === slug).fields, | ||
}) | ||
|
||
const session = await getSession(this, req) | ||
const options: CreateOptions = { | ||
session: await getSession(this, req), | ||
} | ||
|
||
const { insertedId: insertedID } = await Model.collection.insertOne(data, { session }) | ||
;(data as any)._id = insertedID | ||
let [result] = (await Model.create([global], options)) as any | ||
|
||
transform({ | ||
adapter: this, | ||
data, | ||
fields, | ||
operation: 'read', | ||
}) | ||
result = JSON.parse(JSON.stringify(result)) | ||
|
||
// custom id type reset | ||
result.id = result._id | ||
result = sanitizeInternalFields(result) | ||
|
||
return data | ||
return result | ||
} |
Oops, something went wrong.