Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

find-or-create-mongoose

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

find-or-create-mongoose

Atomic find-or-create plugin for mongoose

latest
Source
npmnpm
Version
0.1.2
Version published
Maintainers
1
Created
Source

Mongoose findOrCreate plugin CircleCI

A findOrCreate plugin that's atomic. A necessary characteristic for use in scaled, distributed systems. Achieved by reducing the findOrCreate determination to a single mongo operation.

How To Use

You can set the plugin on the mongoose instance:

// app.js
const mongoose = require('mongoose');
const findOrCreate = requir ('find-or-create-mongoose');
mongoose.plugin(findOrCreate);

Or on individual schemas:

// people.model.js

const findOrCreate = require('find-or-create-mongoose');

const schema = new mongoose.Schema({
  name: { type: String },
  age: { type: Number }
});

schema.plugin(findOrCreate);

module.exports = mongoose.model('People', schema, 'people');

Then use it ...

// people.controller.js

const findBy = { name: 'John' };
const onCreate = { name: 'John', age: 29 };

const result = await model.findOrCreate(findBy, onCreate);

/*
 * result === {
 *	writeResult: 'create' / 'find',
 *	document: { THE_MONGO_RECORD }
 * }
**/

Keywords

mongoose

FAQs

Package last updated on 29 Sep 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts