Skip to content

Commit

Permalink
Merge pull request #146 from WhyAsh5114/dependabot/npm_and_yarn/eslin…
Browse files Browse the repository at this point in the history
…t/plugin-kit-0.2.3

chore(deps-dev): bump @eslint/plugin-kit from 0.2.2 to 0.2.3
  • Loading branch information
WhyAsh5114 authored Nov 16, 2024
2 parents 53d3c97 + 4f3a2f2 commit 07ca196
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 3 additions & 6 deletions src/lib/mongo/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ import { MongoClient } from 'mongodb';
import 'dotenv/config';

const MONGODB_URI = process.env.MONGODB_URI;
if (!MONGODB_URI) {
throw new Error('Invalid/Missing environment variable: "MONGODB_URI"');
}

const uri = MONGODB_URI;
const options = {};

let client;
let clientPromise: Promise<MongoClient>;
let clientPromise: Promise<MongoClient> | undefined;

if (process.env.NODE_ENV === 'development') {
if (process.env.NODE_ENV === 'development' && uri) {
/*
* In development mode, use a global variable so that the value
* Is preserved across module reloads caused by HMR (Hot Module Replacement).
Expand All @@ -23,7 +20,7 @@ if (process.env.NODE_ENV === 'development') {
global._mongoClientPromise = client.connect();
}
clientPromise = global._mongoClientPromise;
} else {
} else if (uri) {
// In production mode, it's best to not use a global variable.
client = new MongoClient(uri, options);
clientPromise = client.connect();
Expand Down
10 changes: 10 additions & 0 deletions src/lib/trpc/routes/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ async function getPrismaAndMongoUser(userId: string) {
throw new TRPCError({ message: 'Migration has already been performed', code: 'CONFLICT' });
}

if (!clientPromise) {
throw new TRPCError({ message: "Couldn't connect V2's Mongo database", code: 'INTERNAL_SERVER_ERROR' });
}

const client = await clientPromise;
const mongoUser = await client.db().collection('users').findOne({ email: prismaUser?.email });
if (!mongoUser) {
Expand Down Expand Up @@ -91,6 +95,9 @@ export const users = t.router({
checkV2MigrationAvailability: t.procedure.query(async ({ ctx }) => {
try {
const { mongoUser } = await getPrismaAndMongoUser(ctx.userId);
if (!clientPromise) {
throw new TRPCError({ message: "Couldn't connect V2's Mongo database", code: 'INTERNAL_SERVER_ERROR' });
}
const client = await clientPromise;

const mesocycleTemplatesCount = await client
Expand Down Expand Up @@ -121,6 +128,9 @@ export const users = t.router({
.input(z.strictObject({ bodyweight: z.number(), duration: z.number() }))
.mutation(async ({ ctx, input }) => {
const { mongoUser } = await getPrismaAndMongoUser(ctx.userId);
if (!clientPromise) {
throw new TRPCError({ message: "Couldn't connect V2's Mongo database", code: 'INTERNAL_SERVER_ERROR' });
}

const client = await clientPromise;
const mesocycleTemplates = await client
Expand Down

0 comments on commit 07ca196

Please sign in to comment.