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

findorcreate-promise

v1.2.0

Published

Useful plugin "findOrCreate" for Mongoose, which was coded in ES6.

Downloads

174

Readme

findOrCreate-Promise

Build Status npm license

Useful mongoose plugin "findOrCreate-Promise", which was made using es6 Promises.

Installation

npm i findorcreate-promise

Import plugin

const import findOrCreate from 'findorcreate-promise';

Connecting to MongoDB

First, we need to define a connection and add static method on the schema.

const import mongoose from 'mongoose';
const Schema = mongoose.Schema;

const TestSchema = new Schema({
    name: { type: String },
    password: { type: String },
});

TestSchema.plugin(findOrCreate);

const Test = mongoose.model('Test', TestSchema);

Usage

Basic Usage

findOrCreate takes at most three arguments.

  • query : The values to be searched
  • data : If a document is created, these values will be defined.
  • options: Decides different behavior in the vent an object is found or created.

The function returns in all cases the object in question, whether it was found or created, and a boolean. The boolean is true if it created a new document and false if it found and or updated one.

Schema.findOrCreate({ query }, { data }, { options })
    .then((doc) => {
       /**
        * doc.created is a boolean
        * doc.result is an object
        **/
    })
    .catch(done);

Create or Find a new document

You can create a new document by searching for values. If a matching document is found, it is returned. Otherwise a new document with the searched values is created and returned.

Test.findOrCreate({ name: 'mongoose' })
    .then((doc) => {
       /**
        * doc.created = true
        * doc.result = new document
        **/
    })
    .catch(done);

You can create a new document by searching values and in the second parameter, define values in the new document.

Test.findOrCreate({ name: 'mongoose' }, { password: 'nosql' })
    .then((doc) => {
       /**
        * doc.created = true
        * doc.result = new document
        **/
    })
    .catch(done);

Update a document

The option upsert dictates how to handle found objects. If it is true, the values passed in data are updated in the object. By default it's false.

Test.findOrCreate({ name: 'mongoose' }, { name: 'mongoDB' }, { upsert: true })
    .then((doc) => {
       /**
        * doc.created = false
        * doc.result = document update
        **/
    })
    .catch(done);

Create option

An object is created by default if it is not found. However, if create is set to false, the function performs only a find and no new object will be created.

Test.findOrCreate({ name: 'mongoose' }, {}, { create: false })
    .then((doc) => {
       /**
        * doc.created = false
        * doc.result = null
        **/
    })
    .catch(done);

Dev Dependencies

Test

npm test

Bugs

When you find issues, please report them:

License