Skip to content

Commit

Permalink
Merge pull request #61 from ExWeiv/dev
Browse files Browse the repository at this point in the history
Validators, Security Updates, TS Updates, Docs Updates, Error Handling Updates
  • Loading branch information
loeiks authored May 15, 2024
2 parents 6994c3b + 34767a2 commit 3d2cefe
Show file tree
Hide file tree
Showing 233 changed files with 6,158 additions and 4,812 deletions.
28 changes: 14 additions & 14 deletions app/lib/Aggregate/data_aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.WeivDataAggregate = void 0;
const pipeline_helpers_1 = require("../Helpers/pipeline_helpers");
const data_aggregate_result_1 = require("./data_aggregate_result");
class WeivDataAggregate extends data_aggregate_result_1.InternalWeivDataAggregateResult {
class WeivDataAggregate extends data_aggregate_result_1.AggregateResult {
constructor(collectionId) {
if (!collectionId) {
throw Error(`WeivData - Database and Collection name required`);
throw new Error(`WeivData - Database and Collection name required`);
}
super(collectionId);
}
ascending(propertyName) {
if (!propertyName) {
throw Error(`WeivData - Property name required!`);
throw new Error(`WeivData - Property name required!`);
}
this.sorting = {
propertyName,
Expand All @@ -22,7 +22,7 @@ class WeivDataAggregate extends data_aggregate_result_1.InternalWeivDataAggregat
}
avg(propertyName, projectedName = `${propertyName}Avg`) {
if (!propertyName) {
throw Error(`WeivData - Property name is required!`);
throw new Error(`WeivData - Property name is required!`);
}
this.addGroup({
_id: "0",
Expand All @@ -38,7 +38,7 @@ class WeivDataAggregate extends data_aggregate_result_1.InternalWeivDataAggregat
}
descending(propertyName) {
if (!propertyName) {
throw Error(`WeivData - Property name is required!`);
throw new Error(`WeivData - Property name is required!`);
}
this.sorting = {
propertyName,
Expand All @@ -48,7 +48,7 @@ class WeivDataAggregate extends data_aggregate_result_1.InternalWeivDataAggregat
}
filter(filter) {
if (!filter) {
throw Error(`WeivData - Filter is empty, please add a filter using weivData.filter method!`);
throw new Error(`WeivData - Filter is empty, please add a filter using weivData.filter method!`);
}
this.pipeline = (0, pipeline_helpers_1.checkPipelineArray)(this.pipeline);
this.pipeline.push({
Expand All @@ -60,10 +60,10 @@ class WeivDataAggregate extends data_aggregate_result_1.InternalWeivDataAggregat
}
group(...propertyName) {
if (!propertyName) {
throw Error(`WeivData - Property or properties are required!`);
throw new Error(`WeivData - Property or properties are required!`);
}
if (this.groupCreated === true) {
throw Error(`WeivData - Group is already set!`);
throw new Error(`WeivData - Group is already set!`);
}
let propertyNames = {};
if (typeof propertyName === "string") {
Expand All @@ -82,7 +82,7 @@ class WeivDataAggregate extends data_aggregate_result_1.InternalWeivDataAggregat
}
having(filter) {
if (!filter) {
throw Error(`WeivData - Filter is empty, please add a filter using weivData.filter method!`);
throw new Error(`WeivData - Filter is empty, please add a filter using weivData.filter method!`);
}
this.havingFilter = {
$match: {
Expand All @@ -93,7 +93,7 @@ class WeivDataAggregate extends data_aggregate_result_1.InternalWeivDataAggregat
}
limit(limit) {
if (!limit && limit != 0) {
throw Error(`WeivData - Limit number is required please specify a limit amount`);
throw new Error(`WeivData - Limit number is required please specify a limit amount`);
}
if (limit != 0) {
this.limitNumber = limit;
Expand All @@ -102,7 +102,7 @@ class WeivDataAggregate extends data_aggregate_result_1.InternalWeivDataAggregat
}
max(propertyName, projectedName = `${propertyName}Max`) {
if (!propertyName) {
throw Error(`WeivData - Property name is required!`);
throw new Error(`WeivData - Property name is required!`);
}
this.addGroup({
_id: "0",
Expand All @@ -114,7 +114,7 @@ class WeivDataAggregate extends data_aggregate_result_1.InternalWeivDataAggregat
}
min(propertyName, projectedName = `${propertyName}Min`) {
if (!propertyName) {
throw Error(`WeivData - Property name is required!`);
throw new Error(`WeivData - Property name is required!`);
}
this.addGroup({
_id: "0",
Expand Down Expand Up @@ -193,14 +193,14 @@ class WeivDataAggregate extends data_aggregate_result_1.InternalWeivDataAggregat
}
skip(skip) {
if (!skip && skip != 0) {
throw Error(`WeivData - Skip number is required please specify a skip number`);
throw new Error(`WeivData - Skip number is required please specify a skip number`);
}
this.skipNumber = skip;
return this;
}
sum(propertyName, projectedName = `${propertyName}Sum`) {
if (!propertyName) {
throw Error(`WeivData - Property name is required!`);
throw new Error(`WeivData - Property name is required!`);
}
this.addGroup({
_id: "0",
Expand Down
8 changes: 4 additions & 4 deletions app/lib/Aggregate/data_aggregate_result.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InternalWeivDataAggregateResult = void 0;
exports.AggregateResult = void 0;
const pipeline_helpers_1 = require("../Helpers/pipeline_helpers");
const automatic_connection_provider_1 = require("../Connection/automatic_connection_provider");
const name_helpers_1 = require("../Helpers/name_helpers");
class InternalWeivDataAggregateResult {
class AggregateResult {
constructor(collectionId) {
this.pageSize = 50;
this.currentPage = 1;
Expand Down Expand Up @@ -62,8 +62,8 @@ class InternalWeivDataAggregateResult {
return { collection, memberId };
}
catch (err) {
throw Error(`WeivData - Error when connecting to MongoDB Client via aggregate function class: ${err}`);
throw new Error(`WeivData - Error when connecting to MongoDB Client via aggregate function class: ${err}`);
}
}
}
exports.InternalWeivDataAggregateResult = InternalWeivDataAggregateResult;
exports.AggregateResult = AggregateResult;
11 changes: 5 additions & 6 deletions app/lib/Collections/createCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.createCollection = void 0;
const connection_helpers_1 = require("../Helpers/connection_helpers");
const name_helpers_1 = require("../Helpers/name_helpers");
const validator_1 = require("../Helpers/validator");
async function createCollection(collectionId, options, createOptions) {
try {
if (!collectionId) {
throw Error(`WeivData - One or more required param is undefined - Required Params: collectionId`);
}
const { suppressAuth } = options || {};
const { safeCollectionOptions, safeOptions } = await (0, validator_1.validateParams)({ collectionId, collectionOptions: createOptions, options }, ["collectionId"], "createCollection");
const { suppressAuth } = safeOptions || {};
const { database } = await (0, connection_helpers_1.connectionHandler)(collectionId, suppressAuth, true);
const { collectionName } = (0, name_helpers_1.splitCollectionId)(collectionId);
await database.createCollection(collectionName, createOptions);
await database.createCollection(collectionName, safeCollectionOptions);
}
catch (err) {
throw Error(`WeivData - Error when creating a new collection in a database, details: ${err}`);
throw new Error(`WeivData - Error when creating a new collection in a database, details: ${err}`);
}
}
exports.createCollection = createCollection;
11 changes: 5 additions & 6 deletions app/lib/Collections/deleteCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteCollection = void 0;
const connection_helpers_1 = require("../Helpers/connection_helpers");
const name_helpers_1 = require("../Helpers/name_helpers");
const validator_1 = require("../Helpers/validator");
async function deleteCollection(collectionId, options, deleteOptions) {
try {
if (!collectionId) {
throw Error(`WeivData - One or more required param is undefined - Required Params: collectionId`);
}
const { suppressAuth } = options || {};
const { safeCollectionOptions, safeOptions } = await (0, validator_1.validateParams)({ collectionId, collectionOptions: deleteOptions, options }, ["collectionId"], "deleteCollection");
const { suppressAuth } = safeOptions || {};
const { database } = await (0, connection_helpers_1.connectionHandler)(collectionId, suppressAuth, true);
const { collectionName } = (0, name_helpers_1.splitCollectionId)(collectionId);
return await database.dropCollection(collectionName, deleteOptions);
return await database.dropCollection(collectionName, safeCollectionOptions);
}
catch (err) {
throw Error(`WeivData - Error when deleting a collection in a database, details: ${err}`);
throw new Error(`WeivData - Error when deleting a collection in a database, details: ${err}`);
}
}
exports.deleteCollection = deleteCollection;
11 changes: 5 additions & 6 deletions app/lib/Collections/listCollections.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
Object.defineProperty(exports, "__esModule", { value: true });
exports.listCollections = void 0;
const connection_helpers_1 = require("../Helpers/connection_helpers");
const validator_1 = require("../Helpers/validator");
async function listCollections(databaseName, options, filter, listOptions) {
try {
if (!databaseName) {
throw Error(`WeivData - One or more required param is undefined - Required Params: databaseName`);
}
const { suppressAuth } = options || {};
const { safeCollectionFilter, safeCollectionOptions, safeOptions } = await (0, validator_1.validateParams)({ databaseName, options, collectionFilter: filter, collectionOptions: listOptions }, ["databaseName"], "listCollections");
const { suppressAuth } = safeOptions || {};
const { database } = await (0, connection_helpers_1.connectionHandler)(`${databaseName}/`, suppressAuth, true);
return await database.listCollections(filter, listOptions).toArray();
return await database.listCollections(safeCollectionFilter, safeCollectionOptions).toArray();
}
catch (err) {
throw Error(`WeivData - Error when listing all collections in a database, details: ${err}`);
throw new Error(`WeivData - Error when listing all collections in a database, details: ${err}`);
}
}
exports.listCollections = listCollections;
11 changes: 5 additions & 6 deletions app/lib/Collections/renameCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.renameCollection = void 0;
const connection_helpers_1 = require("../Helpers/connection_helpers");
const name_helpers_1 = require("../Helpers/name_helpers");
const validator_1 = require("../Helpers/validator");
async function renameCollection(collectionId, newCollectionName, options, renameOptions) {
try {
if (!collectionId || !newCollectionName) {
throw Error(`WeivData - One or more required param is undefined - Required Params: collectionId, newCollectionName`);
}
const { suppressAuth } = options || {};
const { safeCollectionOptions, safeOptions } = await (0, validator_1.validateParams)({ collectionId, newCollectionName, options, collectionOptions: renameOptions }, ["collectionId", "newCollectionName"], "renameCollection");
const { suppressAuth } = safeOptions || {};
const { database } = await (0, connection_helpers_1.connectionHandler)(collectionId, suppressAuth, true);
const { collectionName } = (0, name_helpers_1.splitCollectionId)(collectionId);
await database.renameCollection(collectionName, newCollectionName, renameOptions);
await database.renameCollection(collectionName, newCollectionName, safeCollectionOptions);
}
catch (err) {
throw Error(`WeivData - Error when renaming a collection, details: ${err}`);
throw new Error(`WeivData - Error when renaming a collection, details: ${err}`);
}
}
exports.renameCollection = renameCollection;
14 changes: 7 additions & 7 deletions app/lib/Connection/automatic_connection_provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ async function setupClient(uri, role) {
return connection;
}
else {
throw Error(`There is a problem with client caching and it's a important problem please report it! This will directly impact to all operations`);
throw new Error(`there is a problem with client caching and it's a important problem please report it! This will directly impact to all operations`);
}
}
else {
return createNewClient(uri, role);
}
}
catch (err) {
throw Error(`WeivData - Error when connecting to MongoDB Client via setupClient: ${err}`);
throw new Error(`Error when connecting to MongoDB Client via setupClient: ${err}`);
}
}
const createNewClient = async (uri, role) => {
Expand Down Expand Up @@ -64,11 +64,11 @@ const createNewClient = async (uri, role) => {
return connection;
}
else {
throw Error(`WeivData - Failed to connect to a MongoClient: connection: ${newMongoClient}`);
throw new Error(`Failed to connect to a MongoClient: connection: ${newMongoClient}`);
}
}
catch (err) {
throw Error(`WeivData - Error when creating a new MongoDB client: ${err}`);
throw new Error(`Error when creating a new MongoDB client: ${err}`);
}
};
const listenersMap = new Map();
Expand All @@ -90,7 +90,7 @@ const connectClient = async (client, uri) => {
const handleError = async () => {
clientCache.del(uri.slice(0, 20));
statusCache.set(uri.slice(0, 20), false);
throw Error(`WeivData - Error when trying to connect client (connection error): ${uri}`);
throw new Error(`when trying to connect client (connection error): ${uri}`);
};
client.on('close', handleClose);
client.on('error', handleError);
Expand All @@ -102,7 +102,7 @@ const connectClient = async (client, uri) => {
return connectedClient;
}
catch (err) {
throw Error(`WeivData - Unexpected error: ${err}`);
throw new Error(`Unexpected error: ${err}`);
}
};
async function useClient(suppressAuth = false) {
Expand All @@ -112,7 +112,7 @@ async function useClient(suppressAuth = false) {
return { pool, memberId };
}
catch (err) {
throw Error(`WeivData - Error when connecting to cached MongoClient via useClient: ${err}`);
throw new Error(`when connecting to cached MongoClient via useClient: ${err}`);
}
}
exports.useClient = useClient;
Expand Down
14 changes: 7 additions & 7 deletions app/lib/Connection/permission_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ async function getMongoURI(suppressAuth = false) {
}
}
catch (err) {
throw Error(`Error on getting URI for MongoDB based on permission of current user: ${err}`);
throw new Error(`Error on getting URI for MongoDB based on permission of current user: ${err}`);
}
}
exports.getMongoURI = getMongoURI;
Expand All @@ -43,11 +43,11 @@ const getVisitorURI = async () => {
return { uri: secret, role: "visitorClientOptions" };
}
else {
throw Error(`WeivData - WeivDataURIs Secret Not Found or Not Configured Correctly`);
throw new Error(`WeivDataURIs Secret Not Found or Not Configured Correctly`);
}
}
catch (err) {
throw Error(`Error when getting VisitorURI: ${err}`);
throw new Error(`Error when getting VisitorURI: ${err}`);
}
};
const getAdminURI = async () => {
Expand All @@ -72,11 +72,11 @@ const getAdminURI = async () => {
};
}
else {
throw Error(`WeivData - WeivDataURIs Secret Not Found or Not Configured Correctly`);
throw new Error(`WeivDataURIs Secret Not Found or Not Configured Correctly`);
}
}
catch (err) {
throw Error(`WeivData - Error when getting AdminURI: ${err}`);
throw new Error(`Error when getting AdminURI: ${err}`);
}
};
const getMemberURI = async () => {
Expand Down Expand Up @@ -117,11 +117,11 @@ const getMemberURI = async () => {
};
}
else {
throw Error(`WeivDataURIs Secret Not Found or Not Configured Correctly`);
throw new Error(`WeivDataURIs Secret Not Found or Not Configured Correctly`);
}
}
catch (err) {
throw Error(`Error when getting MemberURI: ${err}`);
throw new Error(`Error when getting MemberURI: ${err}`);
}
};
function getPermissionsCache() {
Expand Down
17 changes: 8 additions & 9 deletions app/lib/Functions/Helpers/findOne.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ const connection_helpers_1 = require("../../Helpers/connection_helpers");
const hook_helpers_1 = require("../../Helpers/hook_helpers");
const hook_manager_1 = require("../../Hooks/hook_manager");
const node_cache_1 = __importDefault(require("node-cache"));
const validator_1 = require("../../Helpers/validator");
const cache = new node_cache_1.default({
checkperiod: 5,
useClones: false,
deleteOnExpire: true
});
async function findOne(collectionId, propertyName, value, options) {
try {
if (!collectionId || !propertyName || !value) {
throw Error(`WeivData - One or more required param is undefined - Required Params: collectionId, propertyName, value`);
}
const { safeValue, safeOptions } = await (0, validator_1.validateParams)({ collectionId, propertyName, value, options }, ["collectionId", "propertyName", "value"], "findOne");
const context = (0, hook_helpers_1.prepareHookContext)(collectionId);
const { suppressAuth, suppressHooks, readConcern, enableCache, cacheTimeout } = options || {};
let editedFilter = { propertyName, value };
const { suppressAuth, suppressHooks, readConcern, enableCache, cacheTimeout } = safeOptions || {};
let editedFilter = { propertyName, value: safeValue };
if (suppressHooks != true) {
const modifiedFilter = await (0, hook_manager_1.runDataHook)(collectionId, "beforeFindOne", [{ propertyName, value }, context]).catch((err) => {
throw Error(`WeivData - beforeFindOne Hook Failure ${err}`);
const modifiedFilter = await (0, hook_manager_1.runDataHook)(collectionId, "beforeFindOne", [{ propertyName, value: safeValue }, context]).catch((err) => {
throw new Error(`beforeFindOne Hook Failure ${err}`);
});
if (modifiedFilter) {
editedFilter = modifiedFilter;
Expand All @@ -41,7 +40,7 @@ async function findOne(collectionId, propertyName, value, options) {
if (item) {
if (suppressHooks != true) {
const modifiedResult = await (0, hook_manager_1.runDataHook)(collectionId, "afterFindOne", [item, context]).catch((err) => {
throw Error(`WeivData - afterFindOne Hook Failure ${err}`);
throw new Error(`afterFindOne Hook Failure ${err}`);
});
if (modifiedResult) {
return modifiedResult;
Expand All @@ -57,7 +56,7 @@ async function findOne(collectionId, propertyName, value, options) {
}
}
catch (err) {
throw Error(`WeivData - Error when finding an item from collection (findOne): ${err}`);
throw new Error(`WeivData - Error when finding an item from collection (findOne): ${err}`);
}
}
exports.findOne = findOne;
Loading

0 comments on commit 3d2cefe

Please sign in to comment.