0
Well, I need to build a generic code that builds a mysql query, I want to find out the best way to do it. Through a JSON I will send the information, it is they:
"Group"(TAG, EMPRESA, CARRO);
"Group_value"(id's das tags, empresas, carros);
"Function"(INSERT, REMOVE, DELETE);
"Condition"(seria o meu WHERE).
At the moment I am recognizing the group and through a switch case I define the table and column that will be edited, and the column that will be applied to Where.
When "Function" is DELETE the only thing you can do is delete the selected clients, so I don’t have big problems with this condition.
Follows draft code:
function (event, context) => {
var table, column, condition;
switch (event.group) {
case 'Tags':
table = 'client_tags'
column = 'client_tag_id'
condition = 'client_id'
break;
case 'Cars':
table = 'clients'
column = 'car_id'
condition = 'id'
break;
case 'Empresas':
table = 'clients'
column = 'company_id'
condition = 'id'
break;
// default:
//
}
var query = "UPDATE " +table+ " SET " +column+ " = " +event.group_values_id+ " WHERE " +condition+ " = " +event.condition
return query
};
Basically the Update is built in the right way, but when the group is TAG will be applied DELETE or INSERT, and there is the possibility of a client having several tags, (Tags have a table the part linked to Clients through foreign key, already COMPANIES and CARS are columns in table clients).
I would like a north to build the best code that will treat these conditions!
Thank you, your comment was of great help!
– Vinícius Carvalho