OK! I finally got it working:
I copy over the fullDocument every time… I know I know only the changed fields. But the point of this is to only run when a blog post is update, rare, so I’d rather make sure everything copied will than care about that level of efficiency?
exports = async function(changeEvent) {
const post = changeEvent.fullDocument;
if (post === undefined) { return; }
const mdId = post._id;
const htmlId = post.hasOwnProperty("htmlId") ? post.htmlId : undefined;
// convert to html post
delete post._id;
delete post.htmlId;
post.contentType = "html";
const htmlCollection = context.services.get("Cluster0").db("database").collection("posts_html");
if (htmlId === undefined) {
const res = await htmlCollection.insertOne(post);
const mdCollection = context.services.get("Cluster0").db("database").collection("posts_md");
mdCollection.updateOne({_id: mdId}, { $set: {htmlId: res.insertedId} });
} else {
return htmlCollection.updateOne({_id: htmlId}, { $set: post}, {upsert: true});
}
};
NEXT STEP: Get the markdown library working with a trigger… Twilio is a special third party service?
How do I add a node library for markdown?