Meteor-Community-Packages / ground-db

GroundDB is a thin layer providing Meteor offline database and methods
https://atmospherejs.com/ground/db
MIT License
572 stars 77 forks source link

Unclear on how to use version 2 #198

Closed MastaBaba closed 4 weeks ago

MastaBaba commented 7 years ago

I'm not clear on how I can get ground:db 2 to work. The version that's on Atmosphere works for me, to the extent that not all my grounded collections have persistency. I very much want to move to the current release candidate. However, I can not get it to work.

So, how to set up ground:db in its current version?

Below is what I understand.

In, say, /imports/api/ I define the collections that need publishing, so, e.g.

Cities = new Ground.Collection('cities');

if (Meteor.isServer) {
    Meteor.publish('cities', function citiesPublication() {
        return Cities.find();
    });
}

In /server/main.js, I include the above, like so:

import '../imports/api/cities.js'

I should now be able to manipulate Cities in /server/main.js.

In /client/main.js, I want to have a grounded version of Cities. For example, like so:

Meteor.startup(function () {
    GroundedCities = new Ground.Collection('groundedCities');
    GroundedCities.observeSource(Cities.find());
});

However, now, querying GroundedCities (for example with GroundedCities.find().fetch()) generates a client side error, as if the collection is not defined. How to resolve this?

Additionally, any methods for Cities are not defined for GroundedCities. So, does this imply that I should write my code such that, on the client, writes are performed on Cities, while reads are performed on GroundedCities? Does that mean the client also needs to subscribe to Cities, besides GroundedCities having been defined, client-side?

s7dhansh commented 7 years ago

Ground is only available to you on the client. You should use Meteor.Collection everywhere in general. Ground.Collection should be used only to instantiate grounddb collections on client after which you will need to use .observeSource and .keep.

Example:

Cities = new Meteor.Collection('cities');

if (Meteor.isClient) {
    GDCities = new Ground.Collection('local.users');
    GDCities.users.observeSource(Cities.find());
        Meteor.subscribe('cities', {
             onReady() {
                   GDCities.keep(Cities.find({}, {reactive: false}));
             }
       });
       GDCities.once('loaded', () => { console.log('loaded'); });
}
ghost commented 6 years ago

This really should be the first page of the readme lol