From efe36fc60a69e2c8b9908360367c49ca6067a79c Mon Sep 17 00:00:00 2001 From: Spacelord Date: Tue, 27 Jun 2023 20:52:20 +0200 Subject: [PATCH] Aded helper func to parse int for prisma relations --- src/assets/helper.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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)} + } + }`); +}