Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
ShareDB - Run Database queries in middleware

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!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!