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

v0.0.8

Published

Add tenant ref field and helpers function to mongoose models.

Downloads

36

Readme

mongoose-tenant

Add tenant ref attribute and helpers function to mongoose models.

Build Status Coverage Status

Installation

npm install --save mongoose-tenant

API

The mongoose-tenant module exposes a single function that you can pass to the mongoose.Schema.prototype.plugin() function.

Suppose you have two collections, "users" and "companies". The User model looks like this:

var tenant = require('mongoose-tenant');

var companySchema = new Schema({ name: String });
Company = mongoose.model('Company', companySchema, 'companies');

var userSchema = new Schema({ name: String });
userSchema.plugin(tenant, {
  tenant: 'company'
});
User = mongoose.model('User', userSchema, 'users');

Suppose your "companies" collection has one document:

{
  name: 'Mario Inc.',
  vat: 'IT1234567889'
  _id: '10ab3f375559dcaa649a3abc'
};

And your "users" collection has one document:

{
  _id: '54ef3d123456abcd244a3abd',
  name: "Mario Rossi",
  company: '10ab3f375559dcaa649a3abc'
}

It can't get a single doc without the tenant attr in conditions object

if you use findOneByTenant or findByTenant functions you have to pass tenant attr in conditions object or you got an error

    

    Customer.findOneByTenant({}, function(err, result) {
      should.exist(err);
      done();
    });

  

It gets a single doc by tenant

You can get a single doc by tenant

    

    Tenant.findOne({
      name: "Mario Inc."
    }, function(err, tenant) {
      assert.ifError(err);
      Customer.findOneByTenant({
        tenant: tenant._id
      }, function(err, result) {
        should.not.exist(err);
        result.should.have.property('name');
        done();
      });

    });

  

It gets 2 docs by tenant

You can limit docs

    

    Tenant.findOne({
      name: "Mario Inc."
    }, function(err, tenant) {
      assert.ifError(err);

      Customer.findByTenant({
        tenant: tenant._id
      }, {}, {
        limit: 2
      }, function(err, result) {
        should.not.exist(err);
        result.should.have.length(2);
        for (var i = 0; i < 2; ++i) {
          result[i].should.have.property('name');
        }
        done();
      });

    });

  

It can get tenant field in schemas

You can get the tenant field from Model

    

    Customer
      .findOne({
        name: "Mario Rossi"
      })
      .populate('tenant')
      .exec(function(err, doc) {
        assert.ifError(err);
        assert.equal('Mario Inc.', doc.tenant.name);
        done();
      });

  

It can populate

You can populate refs in schemas

    

    Tenant.findOne({
      name: "Mario Inc."
    }, function(err, tenant) {
      assert.ifError(err);

      Customer.findOneByTenant({
        tenant: tenant._id,
        name: "Mario Rossi"
      }, {}, {
        populate: 'tenant'
      }, function(err, doc) {
        should.not.exist(err);
        assert.equal('Mario Inc.', doc.tenant.name);
        assert.equal('IT123456789', doc.tenant.vat);

        done();
      });

    });

  

It can settings populated fields

you can manually populate a field

    

    Tenant.findOne({
      name: "Mario Inc."
    }, function(err, tenant) {
      assert.ifError(err);

      Customer.findOneByTenant({
        tenant: tenant._id,
        name: "Mario Rossi"
      }, {}, {
        populate: [{model:'tenant', fileds: '_id name'}]
      }, function(err, doc) {
        should.not.exist(err);
        doc.should.have.property('_id');

        assert.equal('Mario Inc.', doc.tenant.name);
        assert.equal(undefined, doc.tenant.vat);

        done();
      });

    });
  

It supports custom tenant field

mongoose-tenant also works on custom tenant field.

    

    var customerSchema = new Schema({
      name: String
    });
    var companySchema = new Schema({
      name: String,
      vat: String
    });

    customerSchema.plugin(tenant_plugin, {
      tenant: 'company'
    });
    var Company = mongoose.model('Company', companySchema, 'companies');
    var GoodCustomer = mongoose.model('GoodCustomer', customerSchema, 'customers');

    Company.create({
      'name': 'Mario Company Srl'
    }, function(error, doc) {
      assert.ifError(error);
      assert.ok(doc);

      GoodCustomer.create({
        name: 'Mario Reds',
        company: doc._id
      }, function(error, doc) {
        assert.ifError(error);
        assert.ok(doc);

        GoodCustomer
          .findOne({
            name: "Mario Reds"
          })
          .populate('company')
          .exec(function(err, doc) {
            assert.ifError(err);
            assert.equal('Mario Company Srl', doc.company.name);
            done();
          });

      });
    });

  

Author

Palmabit Srl

License

The MIT License (MIT). Please see License File for more information.