Issue using RemoteCollectionDriver and exposing collection to common code


#1

I just started using meteor and I’m using the RemoteCollectionDriver to access a preexisting MongoDB Collection and trying to display the data using aldeed:tabular package.

I have the following code:

root/lib/hello.js

if (Meteor.isServer) {
    Meteor.startup(function () {
        // code to run on server at startup

        database = new MongoInternals.RemoteCollectionDriver("mongodb://localhost:27017/mydb");
        idpool = new Mongo.Collection("idpool", {_driver:database});

    Meteor.publish('idpool', function () {
    return idpool.find();
   });
 });
}

Notice above I declare the collection. To use Tabular I need to declare this in the common code, not server-side code…continuing on in the same file I have.

TabularTables = {};

 Meteor.isClient && Template.registerHelper('TabularTables', TabularTables);
    TabularTables.idpool = new Tabular.Table({
        name: "idpool",
        collection: idpool,
        columns: [
            {data: "_id", title: "id"},
            {data:"created", title: "created"}
        ]
    });

books = new Mongo.Collection("books");

Meteor.isClient && Template.registerHelper('TabularTables', TabularTables);
TabularTables.books = new Tabular.Table({
    name: "Books",
    collection: books,
    columns: [
        {data: "_id", title: "id"}]
});

Now above is where I put the Tabular code in place. I setup two tables for testing. In the second table i declare the “books” collection in the common code. And the “books” table displays fine in the HTML page. However, I cannot re-declare the “idpool” collection since it would throw a “Error: A method named ‘/idpool/insert’ is already defined”

The most logical approach would be to declare the “idpool” collection in the common code, as I did with the “books” collection. However, this does not work since the “idpool” declaration is relying on this code which is not in scope of the common code

database = new MongoInternals.RemoteCollectionDriver("mongodb://localhost:27017/mydb");

Now one might think why not move the entire declaration down to common code (which is obviously bad to expose to the client, but just trying to make it work atm) but that is server-side specific code and causes the “idpool” collection not to get published.

TabularTables = {};

  database = new MongoInternals.RemoteCollectionDriver("mongodb://localhost:27017/mydb");
 idpool = new Mongo.Collection("idpool", {_driver:database});

 Meteor.isClient && Template.registerHelper('TabularTables', TabularTables);
    TabularTables.idpool = new Tabular.Table({
        name: "idpool",
        collection: idpool,
        columns: [
            {data: "_id", title: "id"},
            {data:"created", title: "created"}
        ]
    });

books = new Mongo.Collection("books");

Meteor.isClient && Template.registerHelper('TabularTables', TabularTables);
TabularTables.books = new Tabular.Table({
    name: "Books",
    collection: books,
    columns: [
        {data: "_id", title: "id"}]
});

I’m a bit perplexed. So I’m wondering what the correct way to do this is?


#2

If I understand correctly you need a collection instance with the same name on the client and server but the server instance needs to be declared with different options than the client?

Like this (in lib/*.js so it is common code):

if (Meteor.isServer)
  database = MongoInternals.defaultRemoteCollectionDriver();

var options = Meteor.isClient ? null : {_driver: database};
idpool = new Mongo.Collection('idpool', options);

I have dummied the collection driver to the default so I could test in Meteorpad. No experience with aldeed:tabular or passing _driver option to a collection but I think the above would work.