Possible to create a non-database collection to publish to?


#1

I was wondering if it was possible to create a Meteor collection that I could publish to, but where there is no collection created in my Mongo DB. I just want something that I can do custom publications to like:

Meteor.publish('something', function () {
    this.unblock();
    const response = HTTP.get('url');
    const things = response.data;

    lodash.each(things, thing => {
        this.added('collection', Random.id(), thing);
    });

    this.ready();
});

I tried something like

Collection = new Mongo.Collection('collection', { connection: null });

The code ran fine, but nothing was actually published. To get it to work, I had to

Collection = new Mongo.Collection('collection');

This works, but it will create a collection in my database, which is something that I don’t want to happen.

Is what I want to to possible?

Thanks.


#2

Yes it is possible. The Guide talks about this in the Custom publications with the low level API and Loading data from a REST endpoint with a publication sections. If you’re looking for a more concrete example, check out the source of the okgrow:rest2ddp package.


#3

What is described in the guide is what I am doing. I have it all working. But with those examples, a collection in my database is created. What I want to know is how to create a Meteor collection (via Mongo.Collection) without having a collection created in my database, while also being able to do a custom publication with it.


#4

More specifically, this:

will create a collection in my database. I want to know how (if possible) to create a collection that my app is aware of, but not my database.

I know you can do
Collection = new Mongo.Collection(null);

but you can’t do a custom publication on that.


#5

Got it, sorry I missed that. I don’t think you can get away from creating the Collection (unless you’re creating a local/unmanaged collection). You might want to consider replacing your pub/sub approach with a Method call (that pulls from your external API) that feeds into a local Collection. Another option is to consider using Apollo.


#6

We considered using a method, but we want to be able to use fast render for this. We also just like the idea of keeping our data access APIs consistent.

Apollo will have to wait, unfortunately. We don’t have the time to do it now.

Guess we will just have to leave an empty collection in our DB… not really a big deal, just feels wrong.

Thanks for the input.


#7

If you make sure the collection constructor only runs on the client then you won’t get a collection in your database.