Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor.Collections : What the different between new Collection(null) and new Collection({connection:null})

Tags:

meteor

In Meteor.js, if I put code in both client and server as:

var col = new Collection(null);

What's the difference between:

var col = new Collection('someName',{connection:null});

From the documentation:

new Meteor.Collection(name, [options])

name String: The name of the collection. If null, creates an unmanaged (unsynchronized) local collection.

connection Object The server connection that will manage this collection. Uses the default connection if not specified. Pass the return value of calling DDP. Connect to specify a different server. Pass null to specify no connection.

From what it says, it seems like the code above are the same: both have two non connected collections on client and server. But why does it need to have two different way to produce the same result. My view is the name doesn't matter since they're not connected ( there is no need to send DDP messages, which needs to specify the collection name. ) Am I missing something? Thanks.

like image 547
Josh Wang Avatar asked Mar 13 '14 12:03

Josh Wang


1 Answers

My understanding is that new Meteor.Collection( null ) is for a local collection that you don't want to publish. You can still publish it but you will have to use publish's internal 'added', 'removed', and 'changed' functions to specify which collection on the client gets the data. The client will need to create a named collection to receive the data but all the db methods like 'remove' or 'update' will error since they do not exist on the server.

On the server new Meteor.Collection( 'someName', {connection: null} ) also only exists in memory but can be used in a publish function exactly like a collection backed by the db. The client collection receiving the data is created with new Meteor.Collection( 'someName' ) as normal and there will be no way for the client to know that this collection is only in server memory.

On the client I think 'null'-named and 'null'-connection are both ways to get collections that can not receive / send data from / to the server.

Some discussion here.


update: Collections on the server with {connection: null} do not get methods set up for the client to access. These methods can be set up by temporarily creating a connection for the collection and defining the methods. Like the following:

//server js
var serverOnly = new Meteor.Collection( 'serverOnly', {connection: null} );
serverOnly._connection = Meteor.server;
serverOnly._defineMutationMethods();
serverOnly._connection = null;

You would still need to use allow / deny rules on the collection to allow the client methods to work. If you find yourself using this hack you should comment on the pull request that makes these methods available by default.

like image 147
user728291 Avatar answered Sep 28 '22 12:09

user728291