Skip to content

Commit

Permalink
MongoDB Connection (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaid-maker authored Jul 2, 2024
2 parents 4213ee3 + f7bf882 commit fc46388
Show file tree
Hide file tree
Showing 5 changed files with 307 additions and 4 deletions.
22 changes: 22 additions & 0 deletions lib/database/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import mongoose from "mongoose";

const MONGODB_URI = process.env.MONGODB_URI;

let cached = (global as any).mongoose || { conn: null, promise: null };

export const connectToDatabase = async () => {
if (cached.conn) return cached.conn;

if (!MONGODB_URI) throw new Error("MONGODB_URI is missing!");

cached.promise =
cached.promise ||
mongoose.connect(MONGODB_URI, {
dbName: "evently-nextjs",
bufferCommands: false,
});

cached.conn = await cached.promise;

return cached.conn;
};
36 changes: 36 additions & 0 deletions lib/database/models/event.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Document, Schema, model, models } from "mongoose";

export interface IEvent extends Document {
_id: string;
title: string;
description?: string;
location?: string;
createdAt: string;
imageUrl: string;
startDateTime: string;
endDateTime: string;
price: string;
isFree: boolean;
url?: string;
category: { _id: string; name: string };
organizer: { _id: string; firstName: string; lastName: string };
}

const EventSchema = new Schema({
title: { type: String, required: true },
description: { type: String },
location: { type: String },
createdAt: { type: Date, default: Date.now },
imageUrl: { type: String, required: true },
startDateTime: { type: Date, default: Date.now },
endDateTime: { type: Date, default: Date.now },
price: { type: String },
isFree: { type: Boolean, default: false },
url: { type: String },
category: { type: Schema.Types.ObjectId, ref: "Category" },
organizer: { type: Schema.Types.ObjectId, ref: "User" },
});

const Event = models.Event || model("Event", EventSchema);

export default Event;
14 changes: 14 additions & 0 deletions lib/database/models/user.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Schema, model, models } from "mongoose";

const UserSchema = new Schema({
clerkId: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
username: { type: String, required: true, unique: true },
firstName: { type: String, required: true },
lastName: { type: String, required: true },
photo: { type: String, required: true },
});

const User = models.User || model("User", UserSchema);

export default User;
Loading

0 comments on commit fc46388

Please sign in to comment.