Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Updating the state of cartRecipes table (holds cartId, recipeId and amount).
- * Shows the creation of dynamic insert statement by using a reduce function.
- *
- * Note: This was taken out of context for demonstational purposes therefore
- * some imports might be missing.
- */
- function updateCartRecipes(cartId, recipes) {
- if (cartId) {
- try {
- // First delete all recipes with the given cart id
- await databaseService.query(`DELETE FROM cartRecipes WHERE cartId = ${cartId}`);
- if (recipes.length > 0) {
- // Create a dynamic insert statement to insert multiple recipes at once
- const insertQuery = await databaseService.query(`
- INSERT INTO cartRecipes (recipeId, cartId, amount)
- VALUES ${payload.recipes.reduce((prev, cur) => `${prev ? (prev + ',') : prev} (${cur.id}, ${cartId}, ${cur.amount})`, '')}`);
- return insertQuery.success;
- }
- return true;
- } catch (e) {
- logger.error(e);
- return false;
- }
- }
- }
Add Comment
Please, Sign In to add comment