0

I want to have my application's admin code hosted on a completely different app that shares the same database. However, that means that my collections are defined, at least in the code, in the global namespace of my main application and not my admin application. How can I access my collections, that are in the database, without having the global variables defined in a file shared between the meteor server/client? For reference, I am using this article as the idea to set up my admin tools this way. admin article

TDmoneybanks
  • 478
  • 1
  • 7
  • 20
  • IIRC, when doing `new Mongo.Collection('db_name')` you are actually creating a collection named `'db_name'` in MongoDB, so maybe simply sharing names would be enough (which you can do by copy-pasting them or if you want to be real clean use a config file). Not entirely sure about this though. – Kyll Dec 22 '15 at 23:46

3 Answers3

2

To simplify the problem, let's say you have:

  • two applications: A and B
  • one shared collection: Posts
  • one shared database via MONGO_URL

Quick and Dirty

There's nothing complex about this solution - just copy the collection definition from one app to the next:

A/lib/collections.js

Posts = new Mongo.Collection('posts');

B/lib/collections.js

Posts = new Mongo.Collection('posts');

This works well in cases where you just need the collection name.


More Work but Maintainable

Create a shared local package for your Posts collection. In each app: meteor add posts.

This is a little more complex because you'll need to create a pacakge, but it's better for cases where your collection has a model or other extra code that needs to be shared between the applications. Additionally, you'll get the benefits of creating a package, like testing dependency management, etc.

David Weldon
  • 63,632
  • 11
  • 148
  • 146
0

Each application will have its own code but will share the same mongo db. You'll need to define the same collections (or a subset or even a superset) for the admin app. You can rsync certain directories between the two apps if that makes that process either but there isn't anything in Meteor that will do this for you afaik.

Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
0

Or you could share data between the two servers using DDP:

var conn = DDP.connect('http://admin-server');

Tracker.autorun(function() {
  var status = conn.status();

  if(status.connection) {
    var messages = new Mongo.Collection('messages', {connection: conn});
    conn.subscribe('messages', function() { console.log('I haz messages'); });
  }
});

This creates a local collection named messages that pulls data from the "admin server" over DDP. This collection only exists in memory - nothing is created in mongo. You can do this on the server or client. Definitely not the best choice for large datasets. Limit the data transfer with publications.

ejb
  • 304
  • 2
  • 3