npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongoose-async

v1.0.1

Published

Little mongoose plugin that mocks getters/setters and accepts promises (async)

Downloads

10

Readme

Mongoose Async

Little mongoose plugin that mocks getters/setters and accepts promises (async)

Preface (skip to installation)

Let's face it, Mongoose works great until it does not™. Searching for another way to make my code more elegant, I decided that most of my schema's pre and post hooks should go into getters and setters. However, most hooks rely on asynchronous functions or promises and Mongoose does not currently support that. (To be fair, this is acually a JavaScript limitation, but Mongoose could've implemented getter/setter functionality in a different way.)

That said, I started thinkering with ways to get around this limitation, and this plugin is what I've come up with. It's certainly not the only way to accomplish asynchronous getters and setters, probably not even the best way, but it does what I had in mind.

Feel free to improve, comment or critique this plugin and make sure to follow #4227 because the functionality this plugin is trying to "polyfill" may or may not land in the core of Mongoose soon.

Installation

npm install mongoose-async

or

yarn add mongoose-async

Usage

Activate the plugin

const mongoose = require('mongoose')
const mongooseAsync = require('mongoose-async')

// mongoose schema that needs async getters/setters
const schema = new mongoose.Schema({...})

// add plugin to schema (options see below)
schema.plugin(mongooseAsync, options)

Alternatively (add to every schema):

const mongoose = require('mongoose')
const mongooseAsync = require('mongoose-async')

mongoose.plugin(mongooseAsync, options)

Mongoose plugin docs

Add getters/setters

To avoid conflicts, you can define a getter using the read property (instead of get) and a setter using the write property (instead of set).

const schema = new mongoose.Schema({
  somePath: {
    type: String,
    // ...
    read: async (value, schemaType, document) => {
      // this === document (like mongoose getter)
      // do something async
      return 'value that your code sees'
    },
    write: async (value, schemaType, document) => {
      // this === document (like mongoose setter)
      // do something async
      return 'value that is stored in database'
    }
  }
})

Note synchronous functions work too.

Although not tested, you should still be able to use Mongoose native getters and setters without complications.

Also remember the getters of this plugin get always applied. Native getters do not get applied by default when doc.toJSON or doc.toObject are called. You can change this behavior as done below:

schema.set('toJSON', {getters: true})
schema.set('toObject', {getters: true})

Options

This plugin currently has only one option: setters.applyOn.

Below is the defaults object for reference:

const defaults = {
  getters: {},
  setters: {
    applyOn: 'save',
    // (enum) When setters should be applied
    // change: only execute when path was modified (behaves like an ordinary setter)
    // save: execute every time before saving
  }
}

How it works

  • Getter logic is located in ./src/applyGetters.js.
  • Setter logic is located in ./src/applySetters.js.
  • applyGetters.js is executed on schema post init (obtaining a document from db) and post save (modifies return value of doc.save()).
  • applySetters.js is executed on schema pre save (saving).
    • Should possibly be hooked to pre validate so validation is done on result of setter. (?)
  • On initialization, plugin looks for schema paths that have a read and/or write property set (should be a function), and stores result in object.
    • Objects are passed to applyGetters.js and applySetters.js.