How to have a if statement in data queries?

I have some WIX Data Queries in my backend code and I now want to add three if statements and depending on which execute different data queries. What is best practise doing this?

if (statement === value) {
run query1
} else {
run query2
}

Do I have to create a function that will only execute a data query and return resulsts after the then and make a new function for each different query?

Sometimes I want to chain eq but in some cases lt and I want to change these with code.

Hey,

This is a matter of design. I recommend splitting the queries to different functions so that in case there’s an issue with a certain scenario, it’ll be easier to fix it. Note that you can use also a switch statement instead of multiple if else statements for that matter.

Best,
Tal.

Hey
I just made it work with that design pattern.

if (eventAccuracy === "week") {
				createLogEntry("WEEK---" ,facebookUserId);
					return queryEventsFromDateNoCategoryWeek(eventDate,city,country,facebookUserId).then((resultingItems) => {
				 	
				 		createLogEntry("Events back:" + JSON.stringify(resultingItems) ,facebookUserId);	
				 	
				 		if (resultingItems.length > 0){
			 				var appCards = [];
  							appCards = createCards(resultingItems.items);
			 		
			  				returnJSON = { "cards" : appCards };				
						}
						else {
							returnJSON = { "message" : "We could not find any events in "+locationName+", try another location or narrow your search like find "+type+"s in "+locationName+" next sunday instead of just "+type+ "s",
							"quickReplies" : ['Search again','Add Event', 'Restart']};
						}
						
						return ok({body: JSON.stringify(returnJSON)});
				
					});
				
				} 

and then the query in a function:

function queryEventsFromDateNoCategoryWeek(eventdate, eventcity, eventcountry,facebookuserid) {
	createLogEntry("GET EVENTS BETWEEN " + getMonday(eventdate) + " AND " + getSunday(eventdate), facebookuserid);
	return wixData.query("Events")
		 .between("eventDate", getMonday(eventdate), getSunday(eventdate))
		 .eq("city", eventcity)
		 .eq("country",eventcountry)
		 .find()
		 .then( (results) => {
			
	 			return results;
			} )
			 .catch( (err) => {
			 	createLogEntry("Error in query: " + err, facebookuserid);
			 	return { "error" : err };
			 } );
}

Adn it works perfect and I must say you are so right, all the functions and different queries make more sense in separate functions. Easier to maintain and modify as well.