-
-
Save sahilrajput03/c367e716809f25888ccb4d80d83a7961 to your computer and use it in GitHub Desktop.
This file contains 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
// post hooks for inserMany | |
schema.post<MovieDocument[]>('insertMany', async function (docs, next) { | |
console.log('docs POST =', docs); | |
if (Array.isArray(docs) && docs.length) { | |
const correctData = docs.map((singleDoc) => { | |
console.log('singelDoc post before added =', JSON.stringify(singleDoc)); | |
// If id AND name are present, then we can use them to generate the sort_name | |
if (singleDoc.id?.length > 0 && singleDoc.name) { | |
singleDoc.sort_name = generateSortName(singleDoc.name, singleDoc.id); | |
} else { | |
// Otherwise set sort_name to null (potentially clearing out an existing value) | |
singleDoc.sort_name = null; | |
} | |
// If id AND name AND releaseDate are present, then we can use them to generate the sortReleaseName | |
if (singleDoc.id?.length > 0 && singleDoc.name && singleDoc.releaseDate) { | |
singleDoc.sortReleaseDate = generateSortReleaseDate(singleDoc.releaseDate, singleDoc.sort_name, singleDoc.id); | |
} else { | |
// Otherwise set sortReleaseDate to null (potentially clearing out an existing value) | |
singleDoc.sortReleaseDate = null; | |
} | |
// If id AND rating are present, then we can use them to generate the sortRating | |
if (singleDoc.id?.length > 0 && singleDoc.rating) { | |
singleDoc.sortRating = generateSortRating(singleDoc.rating, singleDoc.id); | |
} else { | |
// Otherwise set sortRating to null (potentially clearing out an existing value) | |
singleDoc.sortRating = null; | |
} | |
console.log('singelDoc post after added =', JSON.stringify(singleDoc)); | |
// singleDoc.save(); | |
return singleDoc; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment