outers
Version:
outers - a all in one package for your day to day use
264 lines • 16 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _Mongo_MongoURL, _Mongo_Database_Name, _Mongo_ConnectionState, _Mongo_NeverDisconnect, _Mongo_Schema, _Mongo_models, _Mongo_CollectionName, _Mongo_connection, _Mongo_InstantConnect, _Mongo_isTimeStamps;
Object.defineProperty(exports, "__esModule", { value: true });
/* eslint-disable @typescript-eslint/no-explicit-any */
const mongoose_1 = require("mongoose"); // import the mongoose module
const Provider_1 = __importStar(require("../Config/Provider")); // Import All Function from Provider
// class to run on start/* The above class is a TypeScript implementation of a MongoDB connection
// handler that can connect to a local or cloud server and listen for
// connection events. */
class Mongo {
/**
* This is a constructor function that initializes properties for a MongoDB connection, including
* the URL and logging options.
* @param {str} [MongoURL=mongodb://localhost:27017/test] - A string representing the URL of the
* MongoDB database to connect to. The default value is 'mongodb://localhost:27017/test', which
* connects to a local MongoDB instance on the default port and uses a database named 'test'.
* @param {bool} [Log=true] - The "Log" parameter is a boolean value that determines whether or not
* to enable logging. If set to true, the code will log information about the database connection.
* If set to false, no logging will occur.
*/
constructor(Details) {
/* These are private properties of the `Mongo` class in TypeScript. */
_Mongo_MongoURL.set(this, void 0); // string value to store the URL of the MongoDB database to connect to
_Mongo_Database_Name.set(this, void 0); // string value to check if the connection is to cloud or local
_Mongo_ConnectionState.set(this, void 0); // string value to check if the connection is to cloud or local
_Mongo_NeverDisconnect.set(this, void 0); // boolean value to check if the connection is to cloud or local
_Mongo_Schema.set(this, void 0); // mongoose schema type
_Mongo_models.set(this, void 0); // mongoose model type
_Mongo_CollectionName.set(this, void 0); // string value to store the name of the collection
_Mongo_connection.set(this, void 0); // mongoose connection type
_Mongo_InstantConnect.set(this, void 0); // function to connect to the database
_Mongo_isTimeStamps.set(this, false); // boolean value to check if the connection is to cloud or local
__classPrivateFieldSet(this, _Mongo_Database_Name, Details === undefined || Details.Database_Name === undefined
? "test"
: Details.Database_Name, "f"); // assign the Database_Name property
__classPrivateFieldSet(this, _Mongo_MongoURL, Details === undefined || Details.MongoURL === undefined
? `mongodb://localhost:27017/${__classPrivateFieldGet(this, _Mongo_Database_Name, "f")}`
: `${Details.MongoURL}${__classPrivateFieldGet(this, _Mongo_Database_Name, "f")}`, "f"); // assign the MongoURL property
__classPrivateFieldSet(this, _Mongo_NeverDisconnect, Details === undefined || Details.NeverDisconnect === undefined
? false
: Details.NeverDisconnect, "f"); // assign the NeverDisconnect property
__classPrivateFieldSet(this, _Mongo_Schema, Details === undefined || Details.Schema === undefined
? {}
: Details.Schema, "f"); // assign the Schema property
__classPrivateFieldSet(this, _Mongo_isTimeStamps, Details === undefined || Details.isTimeStamps === undefined
? false
: Details.isTimeStamps, "f"); // assign the isTimeStamps property
__classPrivateFieldSet(this, _Mongo_CollectionName, Details === undefined || Details.CollectionName === undefined
? "test"
: Details.CollectionName, "f"); // assign the Collection property
__classPrivateFieldSet(this, _Mongo_ConnectionState, "Local", "f"); // assign the ConnectionState property
__classPrivateFieldSet(this, _Mongo_connection, mongoose_1.connection, "f"); // assign the connection property
__classPrivateFieldSet(this, _Mongo_InstantConnect, Provider_1.ConnectDB, "f"); // assign the Connect property
__classPrivateFieldSet(this, _Mongo_models, Provider_1.default.CreateModel(Provider_1.default.CreateSchema(__classPrivateFieldGet(this, _Mongo_Schema, "f"), __classPrivateFieldGet(this, _Mongo_isTimeStamps, "f")), __classPrivateFieldGet(this, _Mongo_CollectionName, "f")), "f"); // assign the models property
} // end of constructor
/* The above code is a TypeScript method called `LogGen` which is marked as `private` and `async`. It
returns a `Promise` of type `globe`. */
LogGen() {
return __awaiter(this, void 0, void 0, function* () {
if (__classPrivateFieldGet(this, _Mongo_MongoURL, "f").includes("mongodb+srv")) {
__classPrivateFieldSet(this, _Mongo_ConnectionState, "Cloud", "f");
}
else {
__classPrivateFieldSet(this, _Mongo_ConnectionState, "Local", "f");
} // check if the connection is to cloud or local
});
}
/* The above code is defining a method called `listen` which listens for events related to the MongoDB
connection. It listens for the `connected`, `error`, and `disconnected` events. */
listen() {
__classPrivateFieldGet(this, _Mongo_connection, "f").on("connected", () => __awaiter(this, void 0, void 0, function* () {
console.log(`MongoDB connected successfully with ${__classPrivateFieldGet(this, _Mongo_ConnectionState, "f")} Server`);
})); // listen for connected event
__classPrivateFieldGet(this, _Mongo_connection, "f").on("error", () => __awaiter(this, void 0, void 0, function* () {
console.log(" Error: MongoDB connection failed");
yield (0, mongoose_1.connect)(__classPrivateFieldGet(this, _Mongo_MongoURL, "f")); // connect to the database
console.log(`MongoDB reconnected successfully with ${__classPrivateFieldGet(this, _Mongo_ConnectionState, "f")} Server`);
}));
__classPrivateFieldGet(this, _Mongo_connection, "f").on("disconnected", () => __awaiter(this, void 0, void 0, function* () {
// check if the connection is to cloud or local
console.log(`MongoDB disconnected with ${__classPrivateFieldGet(this, _Mongo_ConnectionState, "f")} Server and trying to reconnect`);
yield (0, mongoose_1.connect)(__classPrivateFieldGet(this, _Mongo_MongoURL, "f")); // connect to the database
console.log(`MongoDB reconnected successfully with ${__classPrivateFieldGet(this, _Mongo_ConnectionState, "f")} Server`);
}));
} // end of listen method
Connect() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield __classPrivateFieldGet(this, _Mongo_InstantConnect, "f").call(this, __classPrivateFieldGet(this, _Mongo_MongoURL, "f")); // connect to the database
yield this.LogGen(); // Checking if The Connection String is Local or Server in private function
if (__classPrivateFieldGet(this, _Mongo_NeverDisconnect, "f") === true) {
this.listen(); // listen for events related to the database connection
} // check if this is a never disconnect connection
return {
status: true,
message: `MongoDB connected successfully with ${__classPrivateFieldGet(this, _Mongo_ConnectionState, "f")} Server`,
};
}
catch (_a) {
return {
status: false,
message: "Error: MongoDB connection failed",
};
}
});
} // end of SingleConnect method
disconnect() {
return __awaiter(this, void 0, void 0, function* () {
try {
if (__classPrivateFieldGet(this, _Mongo_NeverDisconnect, "f") === false) {
console.log("This is not a never disconnect connection, to disconnect use set NeverDisconnect to false");
return {
status: false,
message: "This is not a never disconnect connection, to disconnect use set NeverDisconnect to false",
};
} // check if this is a never disconnect connection
else {
__classPrivateFieldGet(this, _Mongo_connection, "f").close(); // disconnect from the database
return {
status: true,
message: "MongoDB disconnected successfully",
};
}
}
catch (_a) {
return {
status: false,
message: "Error: MongoDB disconnection failed",
};
}
});
} // end of disconnect method
// method to find a document in the database
find(type = "AND", Filter = [], limit = 0, skip = 0) {
return __awaiter(this, void 0, void 0, function* () {
try {
return {
skipped: skip,
limit,
count: Array.from(yield (0, Provider_1.ReadData)(type, Filter, __classPrivateFieldGet(this, _Mongo_models, "f"), limit, skip)).length, // find the document in the database
Data: yield (0, Provider_1.ReadData)(type, Filter, __classPrivateFieldGet(this, _Mongo_models, "f"), limit, skip), // find the document in the database
};
}
catch (_a) {
console.log("Error while finding the document");
return [];
}
});
} // end of find method
// method to findAndCount a document in the database
findAndCount(type = "AND", Filter = [], limit = 0, skip = 0) {
return __awaiter(this, void 0, void 0, function* () {
try {
return {
skipped: skip,
limit,
count: Array.from(yield (0, Provider_1.ReadData)(type, Filter, __classPrivateFieldGet(this, _Mongo_models, "f"), limit, skip)).length, // find the document in the database
Data: yield (0, Provider_1.ReadData)(type, Filter, __classPrivateFieldGet(this, _Mongo_models, "f"), limit, skip), // find the document in the database
};
}
catch (_a) {
console.log("Error while finding the document");
return {
count: 0,
Data: [],
};
}
});
} // FindAndCount Function End
// method to create a document in the database
create(Data) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield (0, Provider_1.CreateData)(Data, __classPrivateFieldGet(this, _Mongo_models, "f")); // create the document in the database
}
catch (_a) {
console.log("Error while creating the document");
return {
NewCount: 0,
NewData: [],
};
}
});
} // end of create method
// method to update a document in the database
update(Filter = [], Data, MultiUpdate = false) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield (0, Provider_1.UpdateData)(Filter, Data, __classPrivateFieldGet(this, _Mongo_models, "f"), MultiUpdate);
}
catch (_a) {
console.log("Error while updating the document");
return {
status: false,
message: "Failed to Update Data",
UpdatedCount: 0,
UpdatedData: [],
};
}
});
} // end of Update function
delete(Filter = [], MultiDelete = false) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield (0, Provider_1.DeleteData)(Filter, __classPrivateFieldGet(this, _Mongo_models, "f"), MultiDelete);
}
catch (_a) {
console.log("Error while deleting the document");
return {
status: false,
message: "Failed to Delete Data",
DeletedCount: 0,
DeletedData: [],
};
}
});
} // end of delete method
} // end of alwaysRun class
_Mongo_MongoURL = new WeakMap(), _Mongo_Database_Name = new WeakMap(), _Mongo_ConnectionState = new WeakMap(), _Mongo_NeverDisconnect = new WeakMap(), _Mongo_Schema = new WeakMap(), _Mongo_models = new WeakMap(), _Mongo_CollectionName = new WeakMap(), _Mongo_connection = new WeakMap(), _Mongo_InstantConnect = new WeakMap(), _Mongo_isTimeStamps = new WeakMap();
exports.default = Mongo;
//# sourceMappingURL=Connection.js.map