Skip to content

Implement proper sessions for file db #1186

@jescalada

Description

@jescalada

Is your feature request related to a problem? Please describe.
It seems that user sessions are currently used only when MongoDB is active. We'd like to extend this so that using GitProxy with the file database allows this too.

Describe the solution you'd like
Implement the getSessionStore function for the file DB, potentially something along these lines (disclaimer: AI-generated):

import session from 'express-session';
import Datastore from 'nedb';

class NeDBStore extends session.Store {
  private db: Datastore;

  constructor(filename: string) {
    super();
    this.db = new Datastore({ filename, autoload: true });
    this.db.persistence.setAutocompactionInterval(60 * 1000);
  }

  get(sid: string, callback: (err?: any, session?: any) => void) {
    this.db.findOne({ sid }, (err, doc) => {
      if (err) return callback(err);
      if (!doc) return callback();
      try {
        return callback(null, doc.session);
      } catch (e) {
        return callback(e);
      }
    });
  }

  set(sid: string, sessionData: any, callback?: (err?: any) => void) {
    const doc = { sid, session: sessionData, updatedAt: Date.now() };
    this.db.update({ sid }, doc, { upsert: true }, (err) => {
      if (callback) callback(err);
    });
  }

  destroy(sid: string, callback?: (err?: any) => void) {
    this.db.remove({ sid }, { multi: false }, (err) => {
      if (callback) callback(err);
    });
  }
}

export function getSessionStore() {
  return new NeDBStore('./sessions.db');
}

Additional context
#1166 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions