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

mongodbext-relations

v2.1.0

Published

mongodbext plugin that bring relations between collections

Downloads

7

Readme

mongodbext-relations

mongodbext plugin that bring relations between collections

Starting from [email protected] supports mongodb 3.6+ only

options:

  • relations (required) - hash of fields that should be embedded, should be specified in format: {field1: field1Options, field2: fieldOptions, ...}. Each field options could have properties:
    • collection (required) - instance of mongodbext collection, that should be embedded
    • projection - fields projection to embed ({_id: 1} by default`)
    • key - "primary key" field (_id by default)
    • embedded - function that used to create EmbeddedDocument instance (autogenerated by default)
    • onDelete - action that should be done on embedded document deletion, available actions: 'restrict', 'cascade', 'unset', 'pull', null ('restrict' by default)
    • onUpdate - action that should be done on embedded document updation, available actions: cascade, null ('cascade' by default)

Example:

var MongoClient = require('mongodb').MongoClient;
var mongodbext = require('mongodbext');
var mongodbextRelations = require('mongodbext-relations');

MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
    var authorsCol = new mongodbext.Collection(db, 'authors');
    var chaptersCol = new mongodbext.Collection(db, 'chapters');

    var booksCol = new mongodbext.Collection(db, 'books');
    booksCol.addPlugin(mongodbextRelations, {
        relations: {
            'author': {
                collection: authorsCol,
                projection: {_id: 1, name: 1}
            },
            'chapters.$': {
                collection: chaptersCol,
                projection: {_id: 1, title: 1}
            }
        }
    });

    var author = {
        _id: 1,
        name: 'John Doe',
        age: 42
    };

    var chapters = [{
        _id: 1,
        title: 'Chapter 1',
        page: 6
    }, {
        _id: 2,
        title: 'Chapter 2',
        page: 100
    }];

    var book = {
        _id: 1,
        title: 'Book',
        author: 1,
        chapters: [1, 2]
    };

    authorsCol.insertOne(author, function() {
        chaptersCol.insertMany(chapters, function() {
            booksCol.insertOne(book, function(err, book) {
                console.log(book);
                // {
                //   _id: 1,
                //   title: 'Book',
                //   author: { _id: 1, name: 'John Doe' },
                //   chapters: [
                //     { _id: 1, title: 'Chapter 1' },
                //     { _id: 2, title: 'Chapter 2' }
                //   ]
                // }

                chaptersCol.deleteOne({_id: 1}, function(err) {
                    console.log(err);
                    // [Error: Could not delete document from collection `chapters` because it is embedded to related collection `books` in the field `chapters` of document with _id=1]
                });
            });
        });
    });
});

skipping additional db requests with trustEmbeddedValue flag:

Relations plugin adds new trustEmbeddedValue option to the following methods:

  • insertOne
  • insertMany
  • updateOne
  • updateMany
  • upsertOne
  • replaceOne
  • findOneAndUpdate
  • findOneAndReplace
  • findOneAndUpsert

Passing trustEmbeddedValue: true will skip any additional mongodb requests for relations containig plain objects instead of identifiers. projection is still applied, though.

This logic has two important pitfalls:

  • Some complex projections are not supported. Using trustEmbeddedValue: true with unsupported projection will cause an error to be thrown.
  • Provided object is used "as is", which may result in saving outdated data. Missing fields are not fetched, but any fields not matching projection are dropped.