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-transfer

v1.3.0

Published

mongoose plugin to simplify the reference transfer in case of deletion

Downloads

12

Readme

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. | Type | Description | | ------------- | -------- | --------------------------------------------------------- | | 1 | ObjectId | ObjectId which matching documents should be transfered to | | 2 | Object | Options (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:

| Prop | Type | Description | | ----------- | ------------------- | --------------------------------------------------------------------- | | model | String | Name of the registered mongoose model | | condition | See condition above | Overrides the condition on document level | | key | String/Object/Array | Define 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:

| Type | Example | Description | | ------ | ------------------------------------------ | ---------------------------------------- | | String | some.deep.key | Simpel string selector (dotnotation) | | Object | { key: "some.deep.key", options: {...} } | Defining it as objects allow for options |

Key options

Keys allow the following options:

| Prop | Type | Description | | ------ | --------- | --------------------------------------------------------------------- | | remove | Boolean | Set 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