diff --git a/src/assets/helper.ts b/src/assets/helper.ts index 91b9bdd..b56fb28 100644 --- a/src/assets/helper.ts +++ b/src/assets/helper.ts @@ -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)} + } + }`); +}