By Aileen Agricola | July 7, 2015
Originally posted on Medium.com
Meteor is closely integrated with Mongo, which gives developers exceptional out-of-the-box functionality that you don’t find with other frameworks. The built-in reactivity and latency compensation are nothing short of magical.
For databases that do not support Cursor.observeChanges, we can still make them reactive by rerunning queries and diff’ing the results. Luckily, Meteor provides us with an efficient implementation to diff two collections of documents and call the appropriate observer methods.
Using this system, all one needs to do to make a datasource reactive is set a dependency on which a change would trigger the query to rerun. You can set these dependency keys in each publication, and trigger them to rerun after a database write.
This is how the new package ccorcos:any-db provides reactivity for anydatabase or datasource. It currently works with Mongo, Neo4j, RethinkDB, and REST APIs, but it can in theory work with any database. I’ve been told that Chet, the package author, met with Ben Green — author of numtel:pgand numtel:mysql — in Los Angeles on July 6th during Ben’s Santa-Cruz-to-Baja-and-back bike trip to discuss the possibility of collaborating to get SQL working with this package.
This package breaks Meteor’s “Database Everywhere” principle and settles with a Subscription-Cursor object on the client. That said, this pattern comes with several benefits:
- You do not need to specify your queries both on the client and on the server, which means you no longer need to specify the same query in both Meteor.publish and Template.helpers.
- This package separates the results of individual subscriptions without sending repetitive data. Sometimes with more complicated queries, such as aggregations, it is not possible to separate the results with a client-side query after all the documents are mixed up in a minimongo collection.
This package also implements latency compensation — another important principle of Meteor. This is accomplished using observer methods.
Here’s how you can use it in your next project. We’re going to create a chatroom with Neo4j, in which we will specify subscription dependencies so that it is reactive.
This app will have chatrooms. Each of these chatrooms will have messages that are unique to that room. In the UI, you can select a room on the left, and it will display all the messages associated with that room.Read more here