1

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.

Community
  • 1
  • 1
Gevious
  • 3,214
  • 3
  • 21
  • 42

1 Answers1

3

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);
Ibuprofen
  • 73
  • 3
  • 1
    Thanks. The fact that it has to go through racer is a bit of a concern for me. thanks for clearing it up – Gevious Jun 15 '13 at 18:27
  • Sure. I suppose it might be possible to go below racer and modify redis & mongo directly but I don't know how it works. I'd be curious to know what you find if you go down that route. – Ibuprofen Jun 15 '13 at 18:56
  • Meteor works that way. Its mongo driver reports back changes. I haven't checked the details, though. – Gevious Jun 15 '13 at 19:01
  • 2
    The reason Derby.js requires the requests to run through Racer is because there is a focus on Operation Transforms in Derby v0.5 and OT has a hard requirement that oplogs get incremented atomically. There is a control flow mechanism in Racer/ShareJS (using Redis/MongoDB) that ensures commits occur atomically. The upside is that you get Operational Transforms, however, you cannot (and should not modify) the database directly because you can break OT by doing this. Good discussion on this topic here... [Link](https://groups.google.com/forum/?fromgroups#!topic/derbyjs/C3Nja_UtQx8) – erichrusch Jun 18 '13 at 17:53