Git Product home page Git Product logo

Comments (5)

JamesMessinger avatar JamesMessinger commented on June 15, 2024

Good idea!

I had never thought of that use-case before, but it shouldn't be too hard to create a session-specific data store class. I'll add it to the to-do list.

In the meantime though, you can probably whip-up something that meets your needs pretty easily by inheriting from the DataStore abstract class and implementing something similar to the MemoryStore class, but with a bit of extra logic to separate the data by session.

from swagger-express-middleware.

alex-dow avatar alex-dow commented on June 15, 2024

well, browsing through the source, I spotted this:

      // Get the current DataStore (this can be changed at any time by third-party code)
      var db = util.isExpressApp(router) ? router.get('mock data store') || dataStore : dataStore;

I'm not sure this feature is documented anywhere, so I hope it's an intentional feature because it's allowed me to do this:

    app.use(function(req, res, next) {
        var cookie = req.cookies.__express_session_cookie;
        if (cookie == undefined) {
            console.log('CREATING NEW SESSION');
            var randomNumber = Math.random().toString();
            res.cookie('__express_session_cookie', randomNumber);
            console.log('creating new data store for SESSIONID:' + randomNumber);

            sessionDataStores[randomNumber] = prepareData();
            app.set('mock data store', sessionDataStores[randomNumber]);
        } else {
            console.log('USING EXISTING SESSION');
            if (!sessionDataStores[cookie]) {
                // eg: you restart the server but use the same browser which kept
                // the session cookie
                sessionDataStores[cookie] = prepareData();
            }   
            app.set('mock data store', sessionDataStores[cookie]);
        }   

        next();
    });

very very simple multi-session data storage. prepareData() is just returning a MemoryDataStore instance.

This is definitely better than messing around with key names.

from swagger-express-middleware.

alex-dow avatar alex-dow commented on June 15, 2024

here is something cleaner:

function createSessionStore(req, res, next) {
    var cookie = req.cookies.__express_session_cookie;

    if (cookie == undefined) {
        var randomNumber = Math.random().toString();
        res.cookie('__express_session_cookie', randomNumber);

        sessionDataStores[randomNumber] = prepareDataStore();
        app.set('mock data store', sessionDataStores[randomNumber]);
    } else {
        if (!sessionDataStores[randomNumber]) {
            sessionDataStores[cookie] = prepareDataStore();
        }   
    }   
    next();
}

Then you can use it like so:

app.use(cookieParser());
app.use(createSessionStore);

from swagger-express-middleware.

alex-dow avatar alex-dow commented on June 15, 2024

and my prepareDataStore() looks like:

function prepareDataStore() {
    var myDB = new swagger.MemoryDataStore();
    myDB.save(require('./dataEndpoint'));
    return myDB;
}

and dataEndpoint.js would look like:

var swagger = require('swagger-express-middleware');

module.exports = new swagger.Resource('/endpoint', {
    someKey: 'someValue'
});

from swagger-express-middleware.

alex-dow avatar alex-dow commented on June 15, 2024

Actually my little function had a bug.

function createSessionStore(req, res, next) {
    var cookie = req.cookies.__express_session_cookie;

    if (cookie == undefined) {
        var randomNumber = Math.random().toString();
        res.cookie('__express_session_cookie', randomNumber);

        sessionDataStores[randomNumber] = prepareDataStore();
        app.set('mock data store', sessionDataStores[randomNumber]);
    } else {
        if (!sessionDataStores[cookie]) {
            sessionDataStores[cookie] = prepareDataStore();
        }
        app.set('mock data store', sessionDataStores[cookie]);
    }
    next();
}

This should be much better. I'll see if I can turn this into its own npm installable module (and of course, have some tests).

But I would need confirmation that the approach I'm taking is leveraging an intentional feature that will continue to exist.

from swagger-express-middleware.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.