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-recursive-upsert

v0.8.1

Published

For upserting Mongoose objects while preserving Middleware and defaults.

Downloads

30

Readme

Plugin Overview

Adds a recursive upsert capability to a mongoose model.

The capability is enabled as a mongoose plugin, and can be used as a static method against the schema.

Motivation

While you can use findOneAndUpdate with the upsert option, unfortunately it doesn't trigger mongoose Middleware.

I created the plugin because upsert is something I use a lot, but I rely heavily on Mongoose middleware for things like generating short id's, generating the virtual id field, generating other virtual fields etc.

Note this performs 2 separate, non-atomic DB operations:

  1. findOne to retrieve the document.
  2. Separately saves to update or create the document.

Installation

npm install mongoose-recursive-upsert

To use

First set up the plugin on your schema. Refer to the mongoose instructions how to do this.

const mongooseRecursiveUpsert = require('mongoose-recursive-upsert');

const MySchema = new Schema({...});

MySchema.plugin(mongooseRecursiveUpsert);

Alternatively set it up for all schemas

const mongooseRecursiveUpsert = require('mongoose-recursive-upsert')
const mongoose = require('mongoose');

mongoose.plugin(mongooseRecursiveUpsert);

With the plugin set up you can now use upsert static method set up on your Schema. The method returns a Mongoose Promise. Refer to the Mongoose documentation for setting up an alternative promise library.

const Model = mongoose.model('MyModel');
const newObject = {title: 'hello world'};
Model.upsert({_id: newObject.id}, newObject)
  .then((object) => {
    console.log('saved object');
  })

Or an existing object:

const Model = mongoose.model('MyModel');

const update = {
    _id: '12345'
    title: 'Update'
}

Model.upsert({_id: update._id}, update)
  .then((updatedDoc) => {
    // here is the document
  })
  .catch((err) => {
    // handle any save errors here
  });

Note you do not need to use id for the query, as long as it resolves a single document any query will be fine. The query is passed verbatim to Mongoose's findOne query.

e.g. Model.upsert({'identifier': 'UniqueIdent'}, upsertDoc); // will work

Change Log

Version 0.8 (19/2/2017)

  • Initial Release
  • Tested Version 4

Dependencies

Dependency | Description ----------- | --------------------- Lodash | Used for iterating and merging the object properties. Mongoose | Required as a peer dependency. Tested with v4 of Mongoose.

Testing and Development

Unit test dependencies:

  1. Mongoose
  2. Should.js
  3. Mocha
  4. MongoDB

Set up ./conf.js with your Mongo details.

Run npm test or mocha ./tests/upsert.tests.js directly.

Need changes? Submit an issue or better yet a pull-request on the Github repository:

License

MIT License