fixing things regarding bson error with node and such...now working?

This commit is contained in:
talksik
2022-04-03 09:04:25 -04:00
parent 7205fac3f5
commit 19b90b2196
11 changed files with 248 additions and 41 deletions
+75 -9
View File
@@ -1,11 +1,77 @@
import {
Conversation,
ConversationMember,
} from "../../core/models/conversation.model";
import { client, collections } from "./database.service";
import { ObjectId } from "mongodb";
export class ConversationService {
// static async getUserById(userId: string) {
// const query = { googleId: userId };
// const res = await collections.users?.findOne(query);
// // exists
// if (res?._id) {
// return res as User;
// }
// return null;
// }
static async getConversationByOtherUserId(otherUserId: ObjectId) {
// get all of the conversations for this user that have exactly two conversation members
// get all of the conversationMembers for this user
// get all of the conversations for this user
// get all of the conversations
// const query = { googleId: userId };
// const res = await collections.users?.findOne(query);
// // exists
// if (res?._id) {
// return res as User;
// }
// return null;
}
static async createConversation(
convo: Conversation,
convoMembers: ConversationMember[]
) {
const session = client.startSession();
try {
const transactionResults = await session.withTransaction(async () => {
// todo: check if convoMembers userId's actually exist
const insertConvoRes = await collections.conversations?.insertOne(
convo
);
if (!insertConvoRes?.insertedId) {
await session.abortTransaction();
console.error("failed to create convo");
return;
}
const insertConvoMembersRes =
await collections.conversationMembers?.insertMany(convoMembers);
if (!insertConvoMembersRes?.insertedCount) {
await session.abortTransaction();
console.error("failed to create conversation members");
return;
}
console.log("success");
return insertConvoMembersRes;
});
console.log(transactionResults);
return "success";
// if (transactionResults) {
// console.log("The convo was successfully created.");
// return transactionResults;
// } else {
// console.log("The convo was intentionally aborted.");
// return null;
// }
} catch (e) {
console.log(
"The transaction was aborted due to an unexpected error: " + e
);
} finally {
await session.endSession();
}
return null;
}
}
+22 -13
View File
@@ -6,25 +6,34 @@ import { loadConfig } from "../config";
// Global Variables
export const collections: {
users?: mongoDB.Collection;
conversations?: mongoDB.Collection;
conversationMembers?: mongoDB.Collection;
audioClips?: mongoDB.Collection;
} = {};
// Initialize Connection
export async function connectToDatabase() {
const config = loadConfig();
const config = loadConfig();
const client: mongoDB.MongoClient = new mongoDB.MongoClient(
config.MONGO_CONNECTION_STRING
);
export const client: mongoDB.MongoClient = new mongoDB.MongoClient(
config.MONGO_CONNECTION_STRING
);
await client.connect();
client.connect();
const db: mongoDB.Db = client.db(process.env.DB_NAME);
const db: mongoDB.Db = client.db(process.env.DB_NAME);
const usersCollection: mongoDB.Collection = db.collection("users");
const usersCollection: mongoDB.Collection = db.collection("users");
const convoCollection: mongoDB.Collection = db.collection("conversations");
const convoMembersCollection: mongoDB.Collection = db.collection(
"conversationMembers"
);
const audioClipsCollection: mongoDB.Collection = db.collection("audioClips");
collections.users = usersCollection;
collections.users = usersCollection;
collections.conversations = convoCollection;
collections.conversationMembers = convoMembersCollection;
collections.audioClips = audioClipsCollection;
console.log(
`Successfully connected to database: ${db.databaseName} and collections`
);
}
console.log(
`Successfully connected to database: ${db.databaseName} and collections`
);