trying to add everything but models are messy with mongoose...just nonsense from higher perspective
This commit is contained in:
@@ -1,30 +1,42 @@
|
||||
// External Dependencies
|
||||
import * as mongoDB from "mongodb";
|
||||
|
||||
import { loadConfig } from "../config";
|
||||
import { IUser, UserStatus } from "@nirvana/core/models/user.model";
|
||||
|
||||
// Global Variables
|
||||
export const collections: {
|
||||
users?: mongoDB.Collection;
|
||||
} = {};
|
||||
import { ObjectId } from "mongodb";
|
||||
import { loadConfig } from "../config";
|
||||
import mongoose from "mongoose";
|
||||
|
||||
connectToDatabase().catch((err) => console.log(err));
|
||||
|
||||
// Initialize Connection
|
||||
export async function connectToDatabase() {
|
||||
const config = loadConfig();
|
||||
|
||||
const client: mongoDB.MongoClient = new mongoDB.MongoClient(
|
||||
config.MONGO_CONNECTION_STRING
|
||||
);
|
||||
await mongoose.connect(config.MONGO_CONNECTION_STRING);
|
||||
|
||||
await client.connect();
|
||||
|
||||
const db: mongoDB.Db = client.db(process.env.DB_NAME);
|
||||
|
||||
const usersCollection: mongoDB.Collection = db.collection("users");
|
||||
|
||||
collections.users = usersCollection;
|
||||
|
||||
console.log(
|
||||
`Successfully connected to database: ${db.databaseName} and collections`
|
||||
);
|
||||
console.log(`Successfully connected to mongoose/mongodb`);
|
||||
}
|
||||
|
||||
const userSchema = new mongoose.Schema<IUser>(
|
||||
{
|
||||
name: { type: String, required: true },
|
||||
email: { type: String, required: true },
|
||||
googleId: { type: String, required: true }, // our Google id that every google user has unique that we are going to use for now
|
||||
verifiedEmail: Boolean,
|
||||
givenName: { type: String, required: true },
|
||||
familyName: { type: String, required: true },
|
||||
picture: String,
|
||||
locale: String,
|
||||
|
||||
// additional properties specific to our users collection
|
||||
|
||||
createdDate: Date,
|
||||
status: String,
|
||||
lastUpdatedDate: Date,
|
||||
_id: ObjectId,
|
||||
},
|
||||
{ collection: "users" }
|
||||
);
|
||||
|
||||
export const UserModel = mongoose.model<IUser>("User", userSchema);
|
||||
|
||||
Reference in New Issue
Block a user