Skip to content

adding mongo db

Ray edited this page Dec 31, 2024 · 5 revisions

First, modify the class constructors to accept the MongoDB client:

import { MongoClient } from 'mongodb'

export class StateStore implements NodeSavedStateStore {
  constructor(private db: Database, private dbm: MongoClient) {}
  // ... rest of the class
}

export class SessionStore implements NodeSavedSessionStore {
  constructor(private db: Database, private dbm: MongoClient) {}
  // ... rest of the class
}

Copy

Apply

src\auth\storage.ts Then when creating instances of these stores in your application, pass both the Database and MongoClient:

const stateStore = new StateStore(db, ctx.dbm)
const sessionStore = new SessionStore(db, ctx.dbm)

Copy

Apply

This way you'll have access to both database clients in your storage classes. The MongoDB client (dbm) comes from the application context as shown in src/index.ts where it's initialized during server creation.

Clone this wiki locally