Search code examples
derbyjs

Derby live update


I asked a question about meteor updating the view if a 3rd party updates the database directly. I've been playing with the derby examples and noticed that the view doesn't get updated if I change the data in mongo.

Is derby able to change the view based on the data changing in the database (ie editing tables directly). If so, why isn't it working in the examples.


Solution

  • Derby will update the view if you make changes via racer. So for example when using the default app which is created with derby new project, I was able to update the list of items on the derby app's list page with the following code. You don't want to copy/paste this code directly. You'll need to look up your _userId from the users collection and make sure you're connecting to the correct redis and mongo databases, meaning the same ones your derby app is using.

    var liveDbMongo = require('livedb-mongo');
    var redis = require('redis').createClient();
    var racer = require('racer');
    
    redis.select(process.env.REDIS_DB || 1);
    var store = racer.createStore({
      db: liveDbMongo('localhost:27017/project?auto_reconnect', {safe: true}),
      redis: redis
    });
    
    var model = store.createModel();
    
    model.on('change', function(value) {
      console.log('change: ' + value);
    });
    
    var obj = {
      'name': 'Updated from outside of derby',
      'note': new Date().toDateString(),
      'userId': 'fix-me'
    };
    
    model.add('items', obj);