mondora / asteroid

An alternative client for a Meteor backend
MIT License
734 stars 101 forks source link

Asteroid & Subscriptions #116

Open aadamsx opened 7 years ago

aadamsx commented 7 years ago

Before switching to React or Vue, I'm trying out Blaze with Asteroid (since my existing code base is Blaze). Most all examples I see are Asteroid + React/Redux, where the contents of the Subscription are piped into the Redux store.

The only issue I see so far with Asteroid is, when using it with Blaze (as a test), I don't see a way to pipe in the subscription into mini-mongo.

For example, I inside the ddp.on('added' ...) I cannot do a People.findOne({userId: ID}), like I could with mini-mongo. So at least with Blaze, there seems to be no way to get the data out from the subscription...

// asteroid.js
asteriod.subscrib('People');

asteroid.ddp.on('added', (doc) => {
  if (doc.collection === 'People') {
    // I see the data
    // wish I could query like let ppl = People.findOne({userId: ID});
  }
}); 

// names.js
Template.namesOverview.onCreated(function() {
   // let ppl = People.find({}); // doesn't work!
});

I get an Object inside 'added' like so:

collection: "People"
fields: Object
  createdAt: Object
  firstName: 'Test'
  lastName: 'Test'
id: "asdfWkfasedf"
msg: "added"

My question, is there a way to query the subscription like I do in mini-mongo?

davidebianchi commented 7 years ago

Hi @aadamsx, I'm not sure to have understood. To make query to mongo, you should have a specific publication on meteor server. The added event on asteroid return an object with the keys collection, fields and id and include the element added to collection and not the entire collection.

You should save the collections somewhere in your application as plain JS object.

If you want to save in another structure, you can make your own mixin (like the asteroid-immutable-collections-mixin which convert the collections in Immutable.js).

aadamsx commented 7 years ago

To make query to mongo, you should have a specific publication on meteor server

Yes, done that

The added event on asteroid return an object with the keys collection, fields and id and include the element added to collection and not the entire collection.

Yes, I've seen that, I get the following object for example:

collection: "People"
fields: Object
  createdAt: Object
  firstName: 'Test'
  lastName: 'Test'
id: "asdfWkfasedf"
msg: "added"

You should save the collections somewhere in your application as plain JS object.

OK, this is the part were I fell down. So save the object in a Session variable for example?

If you want to save in another structure, you can make your own mixin (like the asteroid-immutable-collections-mixin which convert the collections in Immutable.js).

Thank you, I'll check this out.

davidebianchi commented 7 years ago

OK, this is the part were I fell down. So save the object in a Session variable for example?

Yes, something like this. For example in React application we use the redux store to save the collections.

aadamsx commented 7 years ago

For example in React application we use the redux store to save the collections.

Right, and in Blaze we use mini-mongo. So I played with this more, and it's going to be difficult doing anything with that return object, in part I guess because I don't have a redux store.

What would be great is if I could do something like what we can do with a straight DDP connection, for example. This makes it so easy to get Collections in the proper format from the remote server!

http://stackoverflow.com/a/18360441/2547360:

var remote = DDP.connect('http://server1.com/');
Items = new Meteor.Collection('items', remote); 

remote.subscribe('items', function() {
  var items = Items.find();
  console.log(items.count());  // get 1         
});

Notice how I can take the connection and pass it to the Meteor.Collection, and from there query it acts like mini-mongo on the client (with a .find())? Is there a way to do this in Asteroid?

It seems there use to be some equivalent to this before with Asteroid:

https://www.npmjs.com/package/asteroid.browser

// Connect to a Meteor backend 
var ceres = new Asteroid("localhost:3000");

// Use real-time collections 
ceres.subscribe("tasksPublication");
var tasks = ceres.getCollection("tasks");
tasks.insert({
  description: "Do the laundry"
});

But I can't find .getCollection()

patrickcneuhaus commented 5 years ago

Hello guys!

I have the exact same issue as @aadamsx . Were you able to solve it?