Meteor Remote MongoDB

I don’t like to use big repeating command and I was searching for a solution where I will be setting a variable embedded with something so every time I start my meteor app; the MONGO_URL will set to environment automatically. So this what I did:

In the package.json file I replaced the start parameter as below:

"scripts": {
    "start": "MONGO_URL=mongodb://username:password@host_url:portnumber/dbname meteor run"
  },

 

Credits: https://stackoverflow.com/questions/38713003/how-to-connect-to-external-mongodb-instance-in-meteor

Fix: Error: Cannot find module ‘@babel/runtime/helpers/builtin/objectSpread’

Another common error:

Error: Cannot find module ‘@babel/runtime/helpers/builtin/interopRequireDefault’

How to fix:

This has been traced in the forums as an incompatibility with the latest version of @babel/runtime. Change the corresponding line in the package.json file

  "dependencies": {
    "@babel/runtime": "7.0.0-beta.55",

and then do

meteor npm install

Credits:

https://stackoverflow.com/questions/51684918/meteor-error-cannot-find-module-babel-runtime-helpers-builtin-interoprequired

 

 

Meteor template default events

Whenever you create a new template you can use these boilerplate events and rename them to fit your needs.

Template.templateName.onDestroyed(function (){

});

Template.templateName.onRendered(function () {

});

Template.templateName.events({

});

Template.templateName.helpers({

});

How to import from JSON file into Meteor

    // import data only when Products collection is empty

    if (Products.find().count() === 0) {
        console.log("Importing private/products.json to db")

        var data = JSON.parse(Assets.getText("products.json"));

        data.forEach(function (item, index, array) {
            Products.insert(item);
        })
    }

 

Credits:
https://stackoverflow.com/questions/25370332/import-json-file-into-collection-in-server-code-on-startup

 

 

Meteor multi-tenancy patterns and package

Packages that can be used to create multi-tenant apps.

https://github.com/flipace/meteor-tenantify

https://github.com/mizzao/meteor-partitioner

This is a ripped down version of what’s in my main.js file in the client:

if (!Session.get('tenantId')) {
  var hostnameArray = document.location.hostname.split('.'), subdomain;
  if (hostnameArray[1] === 'mydomainname' && hostnameArray[2] === 'com') {
    subdomain = hostnameArray[0];  
  }
  if (subdomain) {
    Meteor.call('findTenantBySubdomain', subdomain, function(err, res) {
      var tenantId = res;
      if (tenantId) {
        Session.set('tenantId', tenantId); 
      }
    });
  }
}

It’s not pretty, but it works. Note: subdomains are unique to tenants, so we can get the tenant _id value by finding the right document by subdomain:

Meteor.methods({
  "findTenantBySubdomain" : function (subdomain) {
    check(subdomain, String);
    var tenant = Tenants.findOne({subdomain: subdomain});
    if (tenant) {
      return tenant._id;    
    }
  }
});

source: https://forums.meteor.com/t/multitenancy-and-meteor/5653/14