Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not create a new mongo client on every context execution #2817

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions site/gatsby-site/server/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { MongoClient } from "mongodb";
import config from "./config";
import * as reporter from "./reporter";

const client = new MongoClient(config.MONGODB_CONNECTION_STRING);

function extractToken(header: string) {

if (header && header!.startsWith('Bearer ')) {
Expand Down Expand Up @@ -56,8 +58,6 @@ async function verifyToken(token: string) {

async function getUser(userId: string) {

const client = new MongoClient(config.MONGODB_CONNECTION_STRING);

const db = client.db('customData');

const collection = db.collection('users');
Expand Down Expand Up @@ -95,8 +95,6 @@ export const context = async ({ req }: { req: IncomingMessage }) => {

const user = await getUserFromHeader(req.headers.authorization!);

const client = new MongoClient(config.MONGODB_CONNECTION_STRING!);

return { user, req, client };
}
catch (e) {
Expand Down