web / lib/utils/relation-id
lib/utils/relation-id
Utility for extracting IDs from Payload CMS relationship fields.
Payload relationship fields can be either the full related document (object with id) or just the ID (number/string), depending on depth and population settings. This helper normalizes both cases to a plain ID.
Functions
extractRelationId()
extractRelationId<
TId>(value):TId|undefined
Extract the ID from a Payload relationship field value.
Handles both populated (object) and unpopulated (id) relationship values.
Type Parameters
TId
TId = number
Parameters
value
TId | { id: TId; } | null | undefined
Returns
TId | undefined
Example
// Works with both populated and unpopulated relationships:
const userId = extractRelationId(doc.createdBy); // number | string | undefined
const datasetId = extractRelationId(job.dataset); // number | string | undefinedLast updated on