New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

mongoose-transfer

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-transfer

mongoose plugin to simplify the reference transfer in case of deletion

latest
Source
npmnpm
Version
1.3.0
Version published
Maintainers
1
Created
Source

mongoose-transfer

Build Status npm npm

Mongoose plugin to transfer references to a new document by calling [document].transfer(newID)

Installation

npm install --save mongoose-transfer

Usage

const mongoose = require('mongoose')
const Schema = mongoose.Schema
const mongooseTransfer = require('mongoose-transfer')

let YourSchema = new Schema({
  title: String,
  description: String,
  author: String,
})

YourSchema.plugin(mongooseTransfer, {
  relations: [{ model: 'SomeOtherModel', key: 'author', condition: { company: '...' } }],
  debug: true, // Default: false -> If true operations are logged out in your console
})

let Model = mongoose.model('YourSchema', YourSchema)

After setting up all relations, you can call .transfer(NEWID, OPTIONS) on your documents which will transfer all related documents to a new entity.

document.transfer(NEWID, OPTIONS)

Methods

transfer

This plugin adds a method .transfer to the schema. Call .transfer to run a reference transfer

This function takes 2 parameters:

Parameter no.TypeDescription
1ObjectIdObjectId which matching documents should be transfered to
2ObjectOptions (see info below)

Example

user.transfer('NEWID', {
  condition: { company: '...' },
})

Options

condition

Define under which conditions transfer can take place. Accepts an object (mongoose query) or a function executed on each document

{
  condition: { "name": { $ne: "Mark" } }
}
{
  condition: function(doc, model) {
    console.log(model) // -> e.g. "User"
    return doc.name !== 'Mark'
  }
}

relations

Define all relations this model has to other models. relations is an Array and takes Objects like this:

PropTypeDescription
modelStringName of the registered mongoose model
conditionSee condition aboveOverrides the condition on document level
keyString/Object/ArrayDefine which props that contain references. See relations.key below

Examples

{
  model: 'SomeModel',
  key: 'reference',
  condition: { company: '...' }
}
{
  model: 'SomeModel',
  key: ['something.nested', { key: 'reference', options: { remove: true } }]
  condition: function(doc, model) {
    return doc.company === '...'
  }
}

relations.key

Define where the reference is located inside the document. Allows dotnotation ("some.deep.key").

relations.key can be an Array for multiple references inside one document or String/Object for single references inside one document.

Key can be defined in the following ways:

TypeExampleDescription
Stringsome.deep.keySimpel string selector (dotnotation)
Object{ key: "some.deep.key", options: {...} }Defining it as objects allow for options

Key options

Keys allow the following options:

PropTypeDescription
removeBooleanSet to true if you want to remove the relation instead of transfering
{
  model: 'SomeModel',
  key: [
    'something.nested',
    { key: 'reference', options: { remove: true } }
  ]
}

debug

You can enable logging of all operations by setting debug to true

License

The MIT License Copyright (c) Carsten Jacobsen

Keywords

mongoose

FAQs

Package last updated on 27 Nov 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