View profile

What Mongoose 6 Means for JavaScript

What Mongoose 6 Means for JavaScript
By Mastering JS Weekly • Issue #70 • View online
First, a quick note from our sponsor, Vue School:
The Nuxt Nation Conference 2021 is happening on 15th - 16th September. Join the first-ever Nuxt Conference for 100% free!
Watch live industry leaders and speakers including the creator of the Nuxt Framework, Sébastien Chopin, and the creator of Vue.js, Evan You.
Mongoose 6 was released on August 24, with over 50 (mostly minor) breaking changes and improvements. Mongoose 6 finally dropped support for Node.js 4.x-10.x, which let Mongoose take advantage of several improvements and new features in JavaScript. Here’s a couple of the highlights and what they mean for the future of JavaScript.

Async/Await First
When Mongoose 5 was released in early 2018, async await had just been added to the JavaScript language spec. Node.js did not support async await yet. Mongoose had to rely on co and generators for testing, and Mongoose’s docs still relied on callbacks and promise chaining.
Generators are a decent alternative to async await, but async/await is a better paradigm for most developers because it doesn’t require any outside libraries.
The fact that async/await is built in to the language itself is huge for documentation. As a docs writer, using an outside library like co for your docs is a tradeoff, because it may imply that you must use co to developers that aren’t experts on co.
With async/await, there’s less of a tradeoff, which is why Mongoose’s quick start now uses async/await. Also, given that Mongoose no longer needs to support Node 4.x, Mongoose’s test suite is now moving to async/await as well.
Getting started with Mongoose now uses async/await, not callbacks
Getting started with Mongoose now uses async/await, not callbacks
Going forward, I expect to see more libraries use async/await in their docs and their tests. The readability benefits of async/await are just too good to ignore. Not only does async/await make code more concise, but it also makes JavaScript easier for developers that specialize in non-event-loop languages, like Swift or Kotlin.
A Vote for Proxies
Proxies often seem like the lost ES6 feature. Although they were added to the JavaScript language spec in 2015, they saw very little adoption. At first, proxies were seen as the underappreciated predecessor to Object.observe(), but then the Angular team pulled `Object.observe()` TC39 proposal at the last minute because they were switching to React-style diffing.
Even after `Object.observe()` was pulled, proxies still didn’t see much adoption. Proxies are impossible to polyfill in ES5, browser support was lacking, Node.js didn’t handle proxies well from a performance standpoint, and the overall developer experience was still lacking. As of 2019, less than 20% of JavaScript developers had used proxies.
Vue 3 was arguably the major turning point. With Vue 3 using proxies for change tracking across the board, proxies are now a core skill for every Vue developer.
Following in Vue’s footsteps, Mongoose switched to proxies for arrays. In Mongoose 5, arrays in Mongoose documents were an instance of Mongoose’s `MongooseArray` class, which had two major limitations.
First, Mongoose couldn’t track changes when developers set array indexes directly, as shown below. Second, some deep equality checks, like Node’s `assert.deepStrictEqual()`, would fail because they would pick up on MongooseArray internals.
Mongoose 6 can track changes when you set array indexes
Mongoose 6 can track changes when you set array indexes
Proxies are growing in adoption and are starting to look like the successor to `Object.observe()`. Expect to see more libraries taking advantage of proxies for change tracking, and less libraries relying on immutability and React-style diffing. Hopefully now JavaScript developers can stop pretending that cloning large objects repeatedly is somehow “good for performance.”
Change tracking is easier when you can intercept individual sets
Change tracking is easier when you can intercept individual sets
MongoDB is #1
Mongoose continues to be the most downloaded database framework for Node.js. And the MongoDB Node driver’s downloads continue to outpace every database-related project on npm.
Mongoose vs several SQL-based ORMs
Mongoose vs several SQL-based ORMs
MongoDB was listed as the most wanted database on Stack Overflow’s Developer Survey for the 4th year in a row. And that’s not just idle wanting. MongoDB’s quarterly revenues have more than 5x-ed since their IPO in 2017.
Surveys are one thing, but MDB users are putting their money where their mouth is
Surveys are one thing, but MDB users are putting their money where their mouth is
MongoDB adoption isn’t going anywhere but up. And given MongoDB’s long history as the de facto database for Node.js developers, we’re confident in our stance that MongoDB is the right choice for modern full stack JavaScript applications.
And check out Nuxt Nation!
And check out Nuxt Nation!
Most Recent Tutorials
How to Handle Window Resize Events in Vue - Mastering JS
How to Use JavaScript's Object.values() Function - Mastering JS
How to Wait 1 Second in JavaScript - Mastering JS
What We're Reading
Play Flex Box Adventure – CSS Game to Learn Flexbox
I Built A $300K/Year Vue.js Component Library - Starter Story
Detached window memory leaks
Did you enjoy this issue?
Mastering JS Weekly

Pragmatic web development. No bloatware allowed!

In order to unsubscribe, click here.
If you were forwarded this newsletter and you like it, you can subscribe here.
Powered by Revue