Explorar o código

refactor: Replaced mongoose with mongodb package

Owen Diffey %!s(int64=2) %!d(string=hai) anos
pai
achega
831200904c

A diferenza do arquivo foi suprimida porque é demasiado grande
+ 2090 - 734
backend/package-lock.json


+ 1 - 1
backend/package.json

@@ -27,7 +27,7 @@
 		"cors": "^2.8.5",
 		"express": "^4.18.2",
 		"moment": "^2.29.4",
-		"mongoose": "^6.6.5",
+		"mongodb": "4.11",
 		"nodemailer": "^6.8.0",
 		"oauth": "^0.10.0",
 		"object-hash": "^3.0.0",

+ 8 - 6
backend/src/modules/DataModule.spec.ts

@@ -3,7 +3,7 @@ import async from "async";
 import chai from "chai";
 import sinon from "sinon";
 import sinonChai from "sinon-chai";
-import mongoose from "mongoose";
+import { ObjectId } from "mongodb";
 import JobContext from "../JobContext";
 import JobQueue from "../JobQueue";
 import LogBook from "../LogBook";
@@ -25,13 +25,13 @@ describe("Data Module", function () {
 
 	before(async function () {
 		await dataModule.startup();
-		dataModule.redis = sinon.spy(dataModule.redis);
+		dataModule.redisClient = sinon.spy(dataModule.redisClient);
 	});
 
 	beforeEach(async function () {
 		testData.abc = await async.map(Array(10), async () =>
-			dataModule.collections?.abc.model.create({
-				_id: new mongoose.Types.ObjectId(),
+			dataModule.collections?.abc.collection.insertOne({
+				_id: new ObjectId(),
 				name: `Test${Math.round(Math.random() * 1000)}`,
 				autofill: {
 					enabled: !!Math.floor(Math.random())
@@ -69,7 +69,7 @@ describe("Data Module", function () {
 				find.createdAt.should.deep.equal(document.createdAt);
 
 				if (useCache) {
-					dataModule.redis?.GET.should.have.been.called;
+					dataModule.redisClient?.GET.should.have.been.called;
 				}
 			});
 		});
@@ -77,7 +77,9 @@ describe("Data Module", function () {
 
 	afterEach(async function () {
 		sinon.reset();
-		await dataModule.collections?.abc.model.deleteMany({ testData: true });
+		await dataModule.collections?.abc.collection.deleteMany({
+			testData: true
+		});
 	});
 
 	after(async function () {

+ 24 - 43
backend/src/modules/DataModule.ts

@@ -1,19 +1,23 @@
 // @ts-nocheck
 import async from "async";
 import config from "config";
-import mongoose, { Schema, Types as MongooseTypes } from "mongoose";
+import { Db, MongoClient } from "mongodb";
 import hash from "object-hash";
 import { createClient, RedisClientType } from "redis";
 import JobContext from "src/JobContext";
 import BaseModule from "../BaseModule";
 import ModuleManager from "../ModuleManager";
 import { UniqueMethods } from "../types/Modules";
-import { Collections, Types } from "../types/Collections";
+import { Collections } from "../types/Collections";
 
 export default class DataModule extends BaseModule {
-	collections?: Collections;
+	private collections?: Collections;
 
-	redis?: RedisClientType;
+	private mongoClient?: MongoClient;
+
+	private mongoDb?: Db;
+
+	private redisClient?: RedisClientType;
 
 	/**
 	 * Data Module
@@ -36,41 +40,32 @@ export default class DataModule extends BaseModule {
 					async () => {
 						const mongoUrl = config.get<string>("mongo.url");
 
-						return mongoose.connect(mongoUrl);
+						this.mongoClient = new MongoClient(mongoUrl);
+						await this.mongoClient.connect();
+						this.mongoDb = this.mongoClient.db();
 					},
 
 					async () => this.loadCollections(),
 
-					async () => {
-						if (this.collections) {
-							await async.each(
-								Object.values(this.collections),
-								async collection =>
-									collection.model.syncIndexes()
-							);
-						} else
-							throw new Error("Collections have not been loaded");
-					},
-
 					async () => {
 						const { url, password } = config.get<{
 							url: string;
 							password: string;
 						}>("redis");
 
-						this.redis = createClient({
+						this.redisClient = createClient({
 							url,
 							password
 						});
 
-						return this.redis.connect();
+						return this.redisClient.connect();
 					},
 
 					async () => {
-						if (!this.redis)
+						if (!this.redisClient)
 							throw new Error("Redis connection not established");
 
-						return this.redis.sendCommand([
+						return this.redisClient.sendCommand([
 							"CONFIG",
 							"GET",
 							"notify-keyspace-events"
@@ -112,8 +107,8 @@ export default class DataModule extends BaseModule {
 				.shutdown()
 				.then(async () => {
 					// TODO: Ensure the following shutdown correctly
-					if (this.redis) await this.redis.quit();
-					await mongoose.connection.close(false);
+					if (this.redisClient) await this.redisClient.quit();
+					if (this.mongoClient) await this.mongoClient.close(false);
 				})
 				.finally(() => resolve());
 		});
@@ -188,25 +183,11 @@ export default class DataModule extends BaseModule {
 		return new Promise(resolve => {
 			import(`../collections/${collectionName.toString()}`).then(
 				({ schema }: { schema: Collections[T]["schema"] }) => {
-					// Here we create a Mongoose schema and model based on our schema
-					const mongooseSchemaDocument =
-						this.convertSchemaToMongooseSchema(schema.document);
-
-					const mongoSchema = new Schema<
-						Collections[T]["schema"]["document"]
-					>(mongooseSchemaDocument, {
-						timestamps: schema.timestamps
-					});
-					const model = mongoose.model(
-						collectionName.toString(),
-						mongoSchema
-					);
-					// @ts-ignore
 					resolve({
-						// @ts-ignore
 						schema,
-						// @ts-ignore
-						model
+						collection: this.mongoDb?.collection(
+							collectionName.toString()
+						)
 					});
 				}
 			);
@@ -701,7 +682,7 @@ export default class DataModule extends BaseModule {
 							);
 
 							// Check if the query hash already exists in Redis, and get it if it is
-							const cachedQuery = await this.redis?.GET(
+							const cachedQuery = await this.redisClient?.GET(
 								`query.find.${queryHash}`
 							);
 
@@ -749,13 +730,13 @@ export default class DataModule extends BaseModule {
 
 						// TODO, add mongo projection. Make sure to keep in mind caching with queryHash.
 
-						return this.collections?.[collection].model
+						return this.collections?.[collection].collection
 							.find(mongoFilter, mongoProjection)
 							.limit(limit)
 							.skip((page - 1) * limit);
 					},
 
-					// Convert documents from Mongoose model to regular objects
+					// Convert documents from MongoDB model to regular objects
 					async (documents: any[]) =>
 						async.map(documents, async (document: any) =>
 							document._doc ? document._doc : document
@@ -765,7 +746,7 @@ export default class DataModule extends BaseModule {
 					async (documents: any[]) => {
 						// Adds query results to cache but doesnt await
 						if (cacheable && queryHash) {
-							this.redis!.SET(
+							this.redisClient!.SET(
 								`query.find.${queryHash}`,
 								JSON.stringify(documents),
 								{

+ 3 - 2
backend/src/types/Collections.ts

@@ -1,4 +1,5 @@
-import mongoose, { Model } from "mongoose";
+// @ts-nocheck
+import { Collection } from "mongodb";
 import { AbcCollection } from "../collections/abc";
 
 export enum Types {
@@ -47,6 +48,6 @@ export type DefaultSchema = {
 export type Collections = {
 	abc: {
 		schema: AbcCollection;
-		model: Model<AbcCollection["document"]>;
+		collection: Collection<AbcCollection["document"]>;
 	};
 };

Algúns arquivos non se mostraron porque demasiados arquivos cambiaron neste cambio