Skip to content

Instantly share code, notes, and snippets.

@sahilrajput03
Created December 9, 2022 10:47
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save sahilrajput03/c367e716809f25888ccb4d80d83a7961 to your computer and use it in GitHub Desktop.
// 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