How to use GroundDB?

I am trying to follow tutorial and using documentation but when I stop meteor or put Chrome in Offline mode data is gone. Here is my app.

App.html

<body>
  {{#each people}}
    <li>{{name}} is {{age}} years old</li>
  {{/each}}
</body>

App.js

//CREATE COLLECTION.
PeopleList = new Ground.Collection('people');       

//CLIENT. 
if (Meteor.isClient) {
  
  //INSERT DOCUMENTS.
  if(1==1) {
    PeopleList.insert({ name : "John", age : 20 });
    PeopleList.insert({ name : "Bill", age : 30 });
    PeopleList.insert({ name : "Lucy", age : 40 });
  }
  
  //HELPERS.
  Template.body.helpers({   
    people: function() {
      return PeopleList.find(); 
    }  
  });  
  
}
4 Likes

Anyone?
I just need an initial working example so that I can start experimenting.

Did you follow the full example with the warning about subscriptions:

subscribe to the data you want offline on the application startup. For instance, in the file lib/router.coffee you can have something like the following:

if Meteor.isClient
  subscribed = false
  Tracker.autorun () ->
    if Meteor.user() && !subscribed
      Meteor.subscribe 'users'
      Meteor.subscribe 'trips'
      Meteor.subscribe 'expenses'
      Meteor.subscribe 'notifications'
      subscribed = true

This way the data will always be on localStorage when the user goes offline.

You may also want to place your PeopleList.insert() commands in a Meteor.startup() clause.

@serkandurusoy
Which example is that?
How would that look in JavaScript?
Do I need to subscribe if I have autopublish and insecure which is OK if I am making fully offline client app?

@awatson1978
Why?

I made changes as suggested but it still doesn’t work?

App.html

<body>
  {{#each people}}
    <li>{{name}} is {{age}} years old</li>
  {{/each}}
</body>

App.js

var people = new Ground.Collection('people', { connection: null });

//CLIENT. 
if (Meteor.isClient) {
  
  //STARTUP.
  Meteor.startup(function() {  
    
    //INSERT DOCUMENTS.
    if(1==1) {
      people.insert({ name : "John", age : 20 });
      people.insert({ name : "Bill", age : 30 });
      people.insert({ name : "Lucy", age : 40 });
    }
    
  });  
  
  //HELPERS.
  Template.body.helpers({   
    people: function() {
      return people.find(); 
    }  
  });  
  
}

lib/router.js

//CLIENT. 
if (Meteor.isClient) {

  //TRACKER.
  subscribed = false
  Tracker.autorun(function () {
    if (!subscribed) {
      Meteor.subscribe('people');
      subscribed = true;  
    }
  });
  
}

I seems that you have to use
meteor add appcache
But shouldn’t this package be automatically included if it is needed?

Here’s an example of the Todos app using GroundDB.

Sorry, did not realize it was in coffeescript. I just copied that over from the link you’ve posted, to make a note of the subscription requirement but autopublish should already solve that as you’ve pointed out.

So, sorry, can’t really help much :frowning:

It’s OK.
I appreciate your help.
I think it is working now that I have added “meteor add appcache”.
But this wasn’t mentioned anywhere.

It’s interesting, and nice to know.

I wonder, did you try (without appcache) to see what’s in localstorage? Perhaps the data may still be there, but the problem may be the lack of an app component to actually fetch it for you.

Its unfortunate the above thread makes little sense to me. Perhaps, I was looking for a magic button and couldn’t find it.

According to the docs: https://atmospherejs.com/ground/db

// Return a grounded Meteor.Collection var list = new Ground.Collection('list'); //or // Get the groundDB of existing Meteor.Collection var list = new Meteor.Collection('list'); var groundList = new Ground.Collection(list); //or // Ground an existing Meteor.Collection var list = new Meteor.Collection('list'); // just ground the database: Ground.Collection(list);

So, let’s say, I do this:
Ground.Collection(list);
…then what?

Does it automatically think for me or I have to put in some here-and-theres to get it actually working?

I think the docs is pretty empty when it comes to how to use grounddb. It only tells me how to create grounddb, but absolutely nothing as to how to use it for real.

The example page too, I was expecting to see some code, rather grounddb in action.

Some resources about the topic: