title: What to write set/get/destroy methods on fastify-session to store the session into MongoDB?
body: <p>I'm using TypeScript.</p>
  <p>I want to store the session into mongoDB. Not on memory.
  I read the document, but I could not figure it out.
  (<a href="https://github.com/SerayaEryn/fastify-session" rel="nofollow noreferrer">https://github.com/SerayaEryn/fastify-session</a>)</p>
  <p>What should I do to store the session into mongoDB?</p>
  <p>Write set method that access mongoDB and insert record with sessionId?
  get and destroy as well</p>
  <p>I wrote that. It seems to work well to me. Is there anything wrong with it?</p>
  <pre><code>import fastifySession from 'fastify-session';
  import mongoose from 'mongoose';
  import Session from './schemas/sessionSchema';
  
  mongoose.set('useNewUrlParser', true);
  mongoose.set('useUnifiedTopology', true);
  mongoose.set('useFindAndModify', false);
  mongoose.set('useCreateIndex', true);
  
  class Database {
      constructor() {
          this.connect();
          this.sessionStore = new SessionStore();
      }
  
      sessionStore: fastifySession.SessionStore;
  
      connect() {
          mongoose.connect('mongodb://127.0.0.1:27017/mydb')
              .then(() =&gt; {
                  console.log('Database conneciton successful');
              })
              .catch((err) =&gt; {
                  console.log(`Database connection error ${err}`)
              })
      }
  }
  
  class SessionStore implements fastifySession.SessionStore {
  
      set(sessionId: string, session: any, callback: (err?: Error) =&gt; void) {
          console.log('set');
          Session.findOneAndUpdate({ _id: sessionId }, { _id: sessionId, session: JSON.stringify(session) }, { upsert: true })
              .then(() =&gt; {
                  callback();
              })
              .catch(callback);
      }
  
      get(sessionId: string, callback: (err?: Error, session?: any) =&gt; void) {
          console.log('get');
          Session.findOne({ _id: sessionId })
              .then((result) =&gt; {
                  if (result) {
                      callback(undefined, JSON.parse(result.session));
  
                  } else {
                      callback();
                  }
              })
              .catch(callback);
      }
  
      destroy(sessionId: string, callback: (err?: Error) =&gt; void) {
          console.log('destroy');
          Session.findOneAndDelete({ _id: sessionId })
              .then(() =&gt; { callback(); })
              .catch(callback);
      }
  }
  
  export default new Database();
  
  </code></pre>
  <pre><code>import mongoose from 'mongoose';
  const Schema = mongoose.Schema;
  
  const sessionSchema = new Schema(
      {
          _id: { type: String },
          expires: { type: Date },
          session: { type: String }
      }
  );
  
  const session = mongoose.model&lt;ISessionDocument&gt;('Session', sessionSchema);
  
  interface ISessionDocument extends mongoose.Document {
      _id: string,
      expires: Date,
      session: string
  }
  
  export default session;
  </code></pre>
  <pre><code>import fastify from 'fastify';
  import fastifyCookie from 'fastify-cookie';
  import fastifySession from 'fastify-session';
  
  import database from './database';
  
  const PORT = 8080;
  const app = fastify();
  
  app.register(fastifyCookie);
  app.register(fastifySession, {
      secret: 'mysecretmysecretmysecretmysecret',
      cookie: { secure: false },
      // false for develop on HTTP.
  
      store: database.sessionStore
  });
  
  app.get('/' async (req) =&gt; {
   return `hello`;
  });
  
  
  app.listen(PORT, (err, address) =&gt; {
      if (err) {
          console.error(err);
          process.exit(1);
      }
  
      console.log(`Server listening at ${address}`);
  });
  </code></pre>