Last active
March 7, 2022 14:41
-
-
Save evankirkiles/23fac76fef5687dbc1f0df9cf546ba22 to your computer and use it in GitHub Desktop.
MEDIUM: AWS Amplify Cascading Deletion Clothe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const GraphQLClient = require("/opt/graphQLClient.js"); | |
const Q = require("/opt/graphql/queries.js"); | |
const M = require("/opt/graphql/mutations.js"); | |
/* -------------------------------------------------------------------------- */ | |
/* EXPORTS */ | |
/* -------------------------------------------------------------------------- */ | |
/** | |
* Deletes a clothe, its clothe images, and any corresponding outfit components. | |
* @param {*} clotheId The ID of the clothe to delete. | |
* @param {*} authHeader The Bearer header from Lambda. | |
*/ | |
const deleteClotheCascading = async (clotheId, authHeader) => { | |
const collaterals = { | |
clotheImages: new Set(), | |
outfitComponents: new Set(), | |
outfits: new Set(), | |
}; | |
const GQLClient = new GraphQLClient(authHeader); | |
// 1. Get all collateral clothe images and outfit components | |
const clotheImages = await GQLClient.runWithPaginate( | |
Q.listClotheImages, | |
{ | |
filter: { | |
clotheImagesId: { | |
eq: clotheId, | |
}, | |
}, | |
}, | |
"listClotheImages" | |
); | |
for (const clotheImage of clotheImages) { | |
collaterals.clotheImages.add(clotheImage.id); | |
const outfitComponents = await GQLClient.runWithPaginate( | |
Q.listOutfitComponents, | |
{ | |
filter: { | |
clotheImageComponentsId: { | |
eq: clotheImage.id, | |
}, | |
}, | |
}, | |
"listOutfitComponents" | |
); | |
for (const outfitComponent of outfitComponents) { | |
collaterals.outfitComponents.add(outfitComponent.id); | |
collaterals.outfits.add(outfitComponent.outfitComponentsId); | |
} | |
} | |
// 2. Delete all the collaterals (clothe, clotheImages, outfitComponents) | |
await GQLClient.run(M.deleteClothe, { | |
input: { | |
id: clotheId, | |
}, | |
}); | |
for (const clotheImageId of collaterals.clotheImages) { | |
await GQLClient.run(M.deleteClotheImage, { | |
input: { | |
id: clotheImageId, | |
}, | |
}); | |
} | |
for (const outfitComponentId of collaterals.outfitComponents) { | |
await GQLClient.run(M.deleteOutfitComponent, { | |
input: { | |
id: outfitComponentId, | |
}, | |
}); | |
} | |
// Now return the clothe id and the updated outfit ids | |
return { | |
clothe: clotheId, | |
outfits: Array.from(collaterals.outfits), | |
}; | |
}; | |
module.exports = { | |
deleteCascading: deleteClotheCascading, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment