Aded helper func to parse int for prisma relations

This commit is contained in:
Leon Meier 2023-06-27 20:52:20 +02:00
parent 9ab12118a0
commit efe36fc60a

View File

@ -57,3 +57,22 @@ function returnAllModelFieldData() {
});
return modelData;
}
/**
* Helper function for parsing a string into a prisma connect object
*
* @export
* @param {string} data
* @param {string} [relation_name='id']
* @returns {undefined || object} undefined or prisma connect object
*/
export function parseIntRelation(data: string, relation_name: string = 'id') {
// This function is perfect. If data is not a valid number, return `undefined`
// If it is a valid number return `{connect: {relation_name: yourNumber}}}`
// This can be used by prisma to connect relations
return isNaN(parseInt(data)) ? undefined : JSON.parse(`{
"connect": {
"${relation_name}": ${parseInt(data)}
}
}`);
}