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

monogram

v0.2.5

Published

TAO (aspect-oriented) modeling for MongoDB

Downloads

149

Readme

monogram

Action-based anti-ODM for MongoDB and Node.js

Read the intro blog post here.

Usage

const { connect } = require('monogram');
const db = await connect('mongodb://localhost:27017/test');

Usage

Actions

From an end developer perspective, monogram behaves just like the MongoDB Node.js driver. The key difference is that monogram converts collection functions into actions under the hood. Actions are an object representation of a function call.


    const Test = db.collection('Test');

    let called = 0;
    Test.pre(action => {
      ++called;
      // An _action_ is an object representation of a function call.
      // It has an `_id` property to uniquely identify it, and
      // some other properties:
      assert.deepEqual(_.omit(action, ['_id']), {
        collection: 'Test', // The name of the collection
        name: 'insertOne', // The name of the function called
        params: [{
          hello: 'world'
        }], // The parameters passed to the function
        chained: [] // Function calls chained onto this one
      });
    });

    await Test.insertOne({ hello: 'world' });

    assert.equal(called, 1);
  

Motivation: Logging

Monogram isn't an ODM/ORM like its uncle mongoose, It's a new abstraction entirely. You can call it an AOM, "action-object mapping". Why is this abstraction better? Consider the problem of logging all database operations to the console in an ODM. In mongoose, this is hard, because there's a lot of different types of middleware. In monogram, this is trivial, because all database operations are represented in a common form, actions, and all actions go through one pipeline.


    const Test = db.collection('Test');

    let called = 0;

    Test.action$.subscribe(action => {
      ++called;
      const params = action.params.
        map(p => util.inspect(p, { depth: 5 })).
        join(', ');
      const msg = `${action.collection}.${action.name}(${params})`

      assert.equal(msg,
        `Test.updateOne({ _id: 1 }, { '$set': { hello: 'world' } })`);
    });

    await Test.updateOne({ _id: 1 }, {
      $set: { hello: 'world' }
    });

    assert.equal(called, 1);
  

Enforcing Internal Best Practices

The purpose of monogram is to allow you to enforce best practices, not to prescribe best practices. Beginners are best served using a tool like mongoose, which has a lot of baked-in best practices to prevent you from shooting yourself in the foot. Monogram is more for advanced users who have established best practices they want to enforce. For example, here's how you would prevent users from calling updateOne() or updateMany() without any update operators, which would overwrite the document.


    const Test = db.collection('Test');

    let called = 0;

    // Will catch `updateOne()`, `updateMany()`, and `findOneAndUpdate()`
    // actions
    Test.pre(/update/i, action => {
      const update = action.params[1] || {};
      const keys = Object.keys(update);
      if (keys.length > 0 && !keys[0].startsWith('$')) {
        throw new Error('Not allowed to overwrite document ' +
          'using `updateOne()`, use `replaceOne() instead`');
      }
    });

    let threw = false;
    try {
      // Normally this would delete all properties on the document
      // other than `_id` and `overwrite`. This is expected behavior,
      // but you might want to disallow it. Monogram gives you a
      // framework to do so.
      await Test.updateOne({ _id: 1 }, { overwrite: 'woops!' });
    } catch (error) {
      threw = true;
      assert.equal(error.message, 'Not allowed to overwrite document ' +
        'using `updateOne()`, use `replaceOne() instead`');
    }

    assert.ok(threw);