ground:db
GroundDB is a fast and thin layer providing Meteor offline database - Taking cloud data to the ground.
Features
This version of GroundDB is a caching only storage - meaning it does not support resuming of method calls/cross tab updates etc. But it's faster and async supporting local storages like:
- localstorage
- indexeddb
- websql
* SQLite (on cordova)
It's using localforage with some minor modifications - hopefully we can use localForage via npm in the future
Notes about migration to GroundDB II
Usage
A pure offline collection
1 foo = new Ground.Collection('test');
Ground.Collection is client-side only and depends on LocalCollection
for now
Get documents and updates from a Meteor Mongo Collection via DDP
1 foo = new Ground.Collection('test'); 2 3 foo.observeSource(bar.find()); 4 5 Meteor.setTimeout(() => { 6 // Stop observing - keeping all documents as is 7 foo.stopObserver(); 8 }, 1000);
Limiting the stored data
If you want to clean up the storage and eg. have it match the current subscription, now you can:
1 foo.keep(bar.find());
This will discard all documents not in the subscribed data
Limit the data stored locally
1 foo.keep(bar.find({}, { limit: 30 }));
This will discard all but 30 documents
Limit the data stored locall using multiple cursors
1 foo.keep(bar.find({ type: 'a' }, { limit: 30 }), bar.find({ type: 'b' }, { limit: 30 }));
This will keep at max 60 documents 30 documents of each type "a"/"b"
Clear the storage
1 foo.clear();
This will empty the in memory and the local storage
Need a near backwards compatible solution?
This example behaves much like the previous version of ground db regarding caching a Mongo.Collection
- This class inforces a manual clean up. Calling removeLocalOnly()
will keep only the documents in the Mongo.Collection
.
1GroundLegacy = { 2 Collection: class GroundLegacy extends Ground.Collection { 3 constructor(collection, options) { 4 if (!(collection instanceof Mongo.Collection)) { 5 throw new Error('GroundLegacy requires a Mongo.Collection'); 6 } 7 if (options.cleanupLocalData !== false) { 8 throw new Error('GroundLegacy requires cleanupLocalData to be false'); 9 } 10 11 // Create an instance of ground db 12 super(collection._name); 13 14 this.mongoCollection = collection; 15 16 collection.grounddb = this; 17 18 // Observe on the whole collection 19 this.observeSource(collection.find()); 20 21 // Store super 22 collection.orgFind = collection.find; 23 collection.orgFindOne = collection.findOne; 24 25 // Overwrite collection finds using the grounded data 26 collection.find = (...args) => { 27 return this.find(...args); 28 }; 29 30 collection.findOne = (...args) => { 31 return this.findOne(...args); 32 }; 33 } 34 35 removeLocalOnly() { 36 // Remove all documents not in current subscription 37 this.keep(this.mongoCollection.orgFind()); 38 } 39 }, 40};
More
Read about:
- Events in Ground DB
Contributions
Feel free to send issues, pull requests all is wellcome
Kind regards Morten