mydb-expose

mydb express middleware

mydb-expose

Connect middleware to expose documents through MyDB.

// dependencies 
var mydb = require('mydb-expose');
var express = require('express');
var app = express();
var db = require('monk')():
 
// middleware 
app.use(express.cookieParser());
app.use(mydb({ mongo: '127.0.0.1:27017', url: '127.0.0.1:3000' }));
 
// example session route 
app.post('/login', function(reqres){
  req.session.set('user_id', '123');
  res.send(200);
});
 
// example document expose 
app.get('/some/doc', function(reqres){
  res.send(db.get('collection').findOne({ some: 'query' }));
});

Options:

  • url
    • a string like cloudup.mydb.io or a reference to the mydb server object in case it's in the same process
  • redis
    • a redis client instance or
    • a string like localhost:6379
  • mongo
    • a monk instance
    • a string like localhost:27017/test
  • sessionExpose
    • fields to expose from the session document (defaults to '-sid')

The express method is overridden to detect monk-style Promise objects.

If the request contains the header X-Requested-With: MyDB.

Points to the

Operations executed on the session object defer the response sent to the client automatically, unless a callback is passed to one of the functions below.

Errors are nextd automatically.

The object is populated with the latest snapshot from the database.

Reloads the session from database.

Calls $set with the given key and val.

Calls $unset with the given key.

Calls $pop with the given key.

Calls $pop with the given key and -1, producing a shift.

Calls $push to the given key with val.

Calls $push to the given key with vals.

Calls $pull with the given key and val.

Calls $pullAll with the given key and vals.

Calls $rename with the given key and newKey.

Gets the given key asynchronously. Normally you just want to access req.session[key].