Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot set document id on .create() #10662

Open
cgilly2fast opened this issue Jan 18, 2025 · 2 comments
Open

Cannot set document id on .create() #10662

cgilly2fast opened this issue Jan 18, 2025 · 2 comments
Assignees
Labels
db: mongodb @payloadcms/db-mongodb

Comments

@cgilly2fast
Copy link

cgilly2fast commented Jan 18, 2025

Describe the Bug

I have bunch of code that now doesn't work because the id param in .create() is ignored and new id is generated:

await payload.create({
    collection: 'auth-tokens',
    data: {
        id: <mongo_id>
    }
})

In payload 2.0 I would have to use _id as the property to set the id, after 3.0, I had to switch using 'id'

Now it not working at all.

A simple example of my use of this is in a beforeChange hook for a Users collection.

On a create operation:

export const accountSetup: CollectionBeforeChangeHook<User> = async ({ operation, req, data }) => {

    if(operation === 'create') {
        const id = new ObjectId().toHexString()
    
        // fire and forget
        payload.create({
            collection: 'calendar-settings',
            data: {
                 id,
            }
        })
    
        data.calendar_settings = id
        
    }

    return data
}

In this simple example I could just await the creating the calendar-settings but my real use case I can not do this.

Like I said this used to work fine and payload's type for says I can pass in an id field.

Its removed as required and added back in to the data type as optional.

(property) data: Omit<Billing, "sizes" | "createdAt" | "updatedAt" | "id"> & Partial<Pick<Billing, "sizes" | "createdAt" | "updatedAt" | "id">>

I used the mongo db adapter.

I am pretty sure the _id change to id in payload 3.0 was due to Payload starting to actually use ObjectID objects.

Also I can hack this to work if in the calendar-settings before change hook I do this:

export const setId: CollectionBeforeChangeHook<CalendarSettings> = ({ data, operation }) => {
  if (operation === 'create' && data.id) {
    ;(data as any)['_id'] = data.id
  }
  return data
}

From the payload code it appears the issue is that is because data is passed directly in the db adapter and mongo db needs _id: https://github.com/payloadcms/payload/blob/main/packages/payload/src/collections/operations/create.ts#L268

Link to the code that reproduces this issue

https://github.com/cgilly2fast/payload/tree/id-issue

Reproduction Steps

Create new user

Which area(s) are affected? (Select all that apply)

area: core

Environment Info

bson-objectid: ^2.0.4
next": 15.1.0
payload": 3.16.0
react": 19.0.0
node: v18.20.4
@cgilly2fast cgilly2fast added status: needs-triage Possible bug which hasn't been reproduced yet validate-reproduction labels Jan 18, 2025
@cbratschi
Copy link

I tried to set the id value too but the following code only supports this if customIDType is set:

https://github.com/payloadcms/payload/blob/main/packages/db-mongodb/src/create.ts#L28

It would be useful if this could be supported for all documents having an id value set. For instance cross-referenced collection documents could be created without having to wait for the IDs and an update operation afterwards.

@AlessioGr AlessioGr added the db: mongodb @payloadcms/db-mongodb label Jan 19, 2025
@github-actions github-actions bot removed the status: needs-triage Possible bug which hasn't been reproduced yet label Jan 19, 2025
@cgilly2fast
Copy link
Author

@cbratschi Yes exactly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
db: mongodb @payloadcms/db-mongodb
Projects
None yet
Development

No branches or pull requests

5 participants