Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
37 lines
1.5 KiB
Plaintext
37 lines
1.5 KiB
Plaintext
import { APIError } from 'payload';
|
|
import toSnakeCase from 'to-snake-case';
|
|
/**
|
|
* Used to name database enums and tables
|
|
* Returns the table or enum name for a given entity
|
|
*/ export const createTableName = ({ adapter, config: { name, slug }, config, parentTableName, prefix = '', target = 'dbName', throwValidationError = false, versions = false, versionsCustomName = false })=>{
|
|
let customNameDefinition = config[target];
|
|
let defaultTableName = `${prefix}${toSnakeCase(name ?? slug)}`;
|
|
if (versions) {
|
|
defaultTableName = `_${defaultTableName}${adapter.versionsSuffix}`;
|
|
}
|
|
let customTableNameResult;
|
|
if (!customNameDefinition && target === 'enumName') {
|
|
customNameDefinition = config['dbName'];
|
|
}
|
|
if (customNameDefinition) {
|
|
customTableNameResult = typeof customNameDefinition === 'function' ? customNameDefinition({
|
|
tableName: parentTableName
|
|
}) : customNameDefinition;
|
|
if (versionsCustomName) {
|
|
customTableNameResult = `_${customTableNameResult}${adapter.versionsSuffix}`;
|
|
}
|
|
}
|
|
const result = customTableNameResult || defaultTableName;
|
|
adapter.tableNameMap.set(defaultTableName, result);
|
|
if (!throwValidationError) {
|
|
return result;
|
|
}
|
|
if (result.length > 63) {
|
|
throw new APIError(`Exceeded max identifier length for table or enum name of 63 characters. Invalid name: ${result}.
|
|
Tip: You can use the dbName property to reduce the table name length.
|
|
`);
|
|
}
|
|
return result;
|
|
};
|
|
|
|
//# sourceMappingURL=createTableName.js.map |