import type { BasePayload } from 'payload' /** * Site Settings → Title separator: stored value → the character in the title. * * Stored values are names, not the characters: select values become GraphQL * enum names, and '|', '–', '·' are not valid ones ('-' and '/' both turn into * '_'), which made /api/graphql answer 500 for every site up to 1.14.4. Those * releases stored the character itself; it is still understood everywhere. */ export const TITLE_SEPARATORS = { endash: '–', hyphen: '-', middot: '·', pipe: '|', slash: '/', } as const export type TitleSeparatorName = keyof typeof TITLE_SEPARATORS const NAME_OF_CHAR: Record = Object.fromEntries( Object.entries(TITLE_SEPARATORS).map(([name, char]) => [char, name as TitleSeparatorName]), ) /** * Stored value → separator character. Names map to their character, a legacy * stored character is returned as is, and any other value (a project's own * option) is used literally. Empty → undefined. */ export function titleSeparatorChar(value: unknown): string | undefined { if (typeof value !== 'string' || !value.trim()) {return undefined} return (TITLE_SEPARATORS as Record)[value] ?? value } /** * Legacy stored character → its name; everything else unchanged. The field's * afterRead/beforeChange hooks use it, so documents saved before the rename open * and save without "invalid selection" before anyone touches them. */ export function titleSeparatorName(value: unknown): unknown { return typeof value === 'string' && NAME_OF_CHAR[value] ? NAME_OF_CHAR[value] : value } type MongoGlobals = { collection?: { updateOne: (filter: Record, update: Record) => Promise<{ modifiedCount?: number }> } } /** * MongoDB: rewrites a legacy stored character to its name, once per start (a * no-op once done). SQL adapters change the column in a migration — see * TITLE_SEPARATOR_SQL and docs/seo.md. */ export async function migrateTitleSeparator(payload: BasePayload, settingsSlug = 'site-settings'): Promise { if (payload.db.name !== 'mongoose') {return} const globals = (payload.db as unknown as { globals?: MongoGlobals }).globals if (!globals?.collection) {return} try { for (const [char, name] of Object.entries(NAME_OF_CHAR)) { const result = await globals.collection.updateOne({ globalType: settingsSlug, titleSeparator: char }, { $set: { titleSeparator: name } }) if (result.modifiedCount) { payload.logger.info(`[iapl] Site Settings: title separator '${char}' stored as '${name}' (GraphQL-safe value).`) } } } catch (error) { payload.logger.error({ err: error, msg: '[iapl] Site Settings: could not migrate the title separator' }) } } /** * SQL that rewrites legacy separator characters to names (`direction: 'up'`) or * back (`'down'`), for a Postgres or SQLite migration. Postgres: the generated * migration turns the column into text, recreates the enum and casts back — * insert this right after `SET DATA TYPE text;`, or the cast fails on '|': * * ALTER TABLE "site_settings" ALTER COLUMN "title_separator" SET DATA TYPE text; * ${sql.raw(titleSeparatorSql())} * * Table/column default to Payload's names for the plugin's Site Settings. */ export function titleSeparatorSql({ column = 'title_separator', direction = 'up', table = 'site_settings', }: { column?: string; direction?: 'down' | 'up'; table?: string } = {}): string { const pairs = Object.entries(NAME_OF_CHAR).map(([char, name]) => (direction === 'up' ? [char, name] : [name, char])) const cases = pairs.map(([from, to]) => `WHEN '${from}' THEN '${to}'`).join(' ') const from = pairs.map(([f]) => `'${f}'`).join(', ') return `UPDATE "${table}" SET "${column}" = CASE "${column}" ${cases} ELSE "${column}" END WHERE "${column}" IN (${from});` }