ShareDB - Run Database queries in middleware

161 views Asked by At

Currently i am dealing with the following situation:

I have a ShareDB backend up and running in order to realize real time collaboration (text writing).

Every time a user connects i would like to check if the document the user intends to work on exists in the database. If it DOES NOT exist, create it first. If it DOES exist, proceed normally, this should be done in the "connect" middleware:

var backend = new ShareDB();

backend.use('connect', function(context, next) {
  console.log('connect')

  var connection = backend.connect();
  doc = connection.get('collection_name', 'document_id');
  doc.fetch(function(err) {
    if (err) throw err;
    if (doc.type === null) {
      doc.create({content: ''});
      return;
    }
  });

  next()
})

But it triggers an infinite loop, because i trigger an connect action inside the connect middleware.

So i have no idea how i to access the database in the middleware... any idea?

Thanks!

0

There are 0 answers