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

Created .env file for mongodb connection string #4

Open
ramdeniakki opened this issue Dec 21, 2024 · 0 comments
Open

Created .env file for mongodb connection string #4

ramdeniakki opened this issue Dec 21, 2024 · 0 comments

Comments

@ramdeniakki
Copy link

import dotenv from "dotenv";
import mongoose, { model, Schema } from "mongoose";

dotenv.config();

if (!process.env.MONGODB_URI) {
throw new Error("MONGODB_URI is not defined in the .env file.");
}

// Connect to MongoDB
mongoose
.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
} as mongoose.ConnectOptions)
.then(() => {
console.log("Successfully connected to MongoDB.");
})
.catch((error) => {
console.error("Error connecting to MongoDB:", error);
process.exit(1);
});

//User schema
const UserSchema = new Schema({
username: { type: String, unique: true },
password: String,
});

export const UserModel = model("User", UserSchema);

// Content schema
const ContentSchema = new Schema({
title: String,
link: String,
tags: [{ type: mongoose.Types.ObjectId, ref: "Tag" }],
type: String,
userId: { type: mongoose.Types.ObjectId, ref: "User", required: true },
});

export const ContentModel = model("Content", ContentSchema);

// Link schema
const LinkSchema = new Schema({
hash: String,
userId: { type: mongoose.Types.ObjectId, ref: "User", required: true, unique: true },
});

export const LinkModel = model("Links", LinkSchema);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant