Cache-Sync
Usage:
First configure restpoint and collection:
1var urlPrefix = 'http://test/v1/foo'; 2 3var name = 'foo'; 4 5foo = new Mongo.Collection('test_' + name); 6 7fooCache = new CacheSync({ 8 // Set target collection 9 collection: foo, 10 11 // Return headers authenticating the http call 12 headers: function() { 13 return { 14 auth: 'set' 15 } 16 }, 17 18 // Return paginated url for loading 19 paginatedUrl: function(offset, limit) { 20 return urlPrefix + '?sort=id+desc&limit=' + limit + '&offset=' + offset; 21 }, 22 23 // Return url for a single document 24 singleUrl: function(id) { 25 return urlPrefix + '?filter[id]=' + id; 26 }, 27 28 // Return updates from date url for synchronization 29 updatedAtUrl: function(updatedAt) { 30 return urlPrefix + '?filter[updated_at_gt]=' + updatedAt; 31 }, 32 33 // Check data before it's stored in the database 34 check: { 35 id: Number, 36 name: String, 37 updatedAt: Date 38 } 39});
Then call methods:
1 fooCache.load(function(err, status) { 2 // load is done 3 }); 4 5 fooCache.sync(function(err, status) { 6 // sync is done 7 }); 8 9 fooCache.loadOne(id, function(err, status) { 10 // loadOne is done 11 }); 12
The status collection
CacheSync.status
collection format:
1 _id, 2 initialized: false, 3 loading: true, 4 page: 0, 5 count: { 6 inserted: 0, 7 updated: 0, 8 removed: 0, 9 issues: 0 10 }, 11 createdAt: new Date(), 12 updatedAt: null, 13 syncAt: null, 14 loadAt: null, 15 loadedAt: null
Events
Events:
loadCalled
- Emitted when a load is calledsyncCalled
- Emitted when a sync is calledsync
- Emittet when a sync startssynchronized
- Emittet when a collection is synchronized[collection name].synchronized
inserted
- Emittet when a document is insertedupdated
- Emittet when a doucment is updatedremoved
- Emittet when a document is removedloading
- Emittet when load startsloaded
- Emittet when collection has just been fully loaded[collection name].loaded
load
- Emittet on every page load of a collection[collection name].load
initialized
- Called when collection is loaded the first time[collection name].initialized
error
- Emitted on errorswaiting
- Emitted if CacheSync is paused and "load" or "sync" can't runpaused
- Emitted when sync is pausedresumed
- Emitted when sync is resumedbefore.auto.remove
- Emitted before auto remove is done - useful if needing to unset the__old_cache_document
flag on documents
Error event
1 { 2 name: 'collection name', 3 type: 'http request', 4 message: 'Error message' 5 }
Error event types:
http-missing-header
http-request
http-denied
transform
schema
after
load-not-supported
sync-not-supported
loadOne-not-supported