datasquirel/package-shared/utils/grab-sql-key-name.ts
2026-01-03 05:34:24 +01:00

31 lines
839 B
TypeScript

type Param = {
type: "foreign_key" | "index" | "user" | "unique_constraint";
userId?: string | number;
addDate?: boolean;
};
/**
* # Grab Key Names
* @description Grab key names for foreign keys and indexes
*/
export default function grabSQLKeyName({ type, userId, addDate }: Param) {
let prefixParadigm = (() => {
if (type == "unique_constraint") return "unq";
if (type == "foreign_key") return "fk";
if (type == "index") return "indx";
if (type == "user") return "user";
return null;
})();
const uuid = crypto.randomUUID();
const uidPrefx = uuid.split("-")[0];
let key = `dsql`;
if (prefixParadigm) key += `_${prefixParadigm}`;
if (userId) key += `_${userId}`;
if (addDate) key += `_${uidPrefx}`;
return key;
}