Provide non-numeric unique item ids (UUID, cuid2, Ulid, NanoId) out of the box #13054
MurzNN
started this conversation in
Feature Requests & Ideas
Replies: 3 comments
-
Here is an example of how to make this manually in the current version, using the "cuid2" id generator: import { init as cuid2Init } from '@paralleldrive/cuid2';
import { FieldHook } from '_temp/payload/packages/payload/src';
import { CollectionConfig } from 'payload';
export const MyCollection: CollectionConfig = {
slug: 'my-collection',
fields: [
{
name: 'id',
type: 'text',
hooks: {
beforeChange: [
({ operation, value }): FieldHook => {
if (operation === 'create') {
return cuid2Init({ length: 12 })();
}
return value;
},
],
},
},
{
name: 'myField',
type: 'text',
},
],
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
And here https://medium.com/@dinesharney/understanding-uuid-v4-uuid-v7-snowflake-id-and-nano-id-in-simple-terms-c50acf185b00 is a good article explaining why we have many text ID generators. |
Beta Was this translation helpful? Give feedback.
0 replies
-
So far, you can use UUID by setting the db adapter with the following setting // payload.config.ts
export default buildConfig({
// ...
db: postgresAdapter({
idType: "uuid",
//...
}),
// or
db: sqliteAdapter({
idType: "uuid",
//...
}),
// ...
}) btw, I am also looking for more ID options / able to customise ID |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Now out of the box Payload seems provides only numeric autoincrement id values for items, which is good and classic, but not for all modern cases.
In many cases, we need unpredictable IDs to make things better. Yes, we can override the ID field behavior, as described in the documentation, but it requires writing custom hooks, which is not good for performance, and managing this manually.
So, would be great to provide custom id generation functions directly in the PayloadAPI, maybe something like this?
And maybe something like configuring the default id function for all collections, to not list it in each collection explicitly.
This should allow simple usage as ID any technique as the developer wants: UUID, cuid, cuid2, Ulid, NanoId, ULID, KSUID, etc.
Beta Was this translation helpful? Give feedback.
All reactions