Skip to content
This repository has been archived by the owner on Jul 11, 2024. It is now read-only.

V1.9.9 #256

Merged
merged 5 commits into from
Dec 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"name": "wertik-js",
"version": "1.9.99-beta",
"version": "1.9.99",
"main": "lib/main.js",
"repository": "https://github.com/Uconnect-Technologies/wertik-js.git",
"welcomeResponse": "Welcome to wertik, You are successfully running Wertik.",
"keywords": [
"mysql",
"graphql",
Expand Down
60 changes: 60 additions & 0 deletions src/framework/database/helpers/paginate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import {get} from "lodash"
import convertFiltersIntoSequalizeObject from "./convertFiltersIntoSequalizeObject";
import { request } from "express";
import { removeColumnsFromAccordingToSelectIgnoreFields } from "../../helpers/index";
export default async function (model) {
return function (args, requestedFields: any = []) {
return new Promise(async (resolve, reject) => {
try {
if (!Array.isArray(requestedFields)) {
console.error("Requested fields must be an array");
process.exit();
}
let page = get(args, "pagination.page", 1);
let limit = get(args, "pagination.limit", 10);
let filters = get(args, "filters", {});
let sorting = get(args, "sorting", []);
let offset = limit * (page - 1);
let convertedFilters = await convertFiltersIntoSequalizeObject(filters);

requestedFields = removeColumnsFromAccordingToSelectIgnoreFields(
requestedFields,
model.selectIgnoreFields
);

let mainObject = {
order: sorting.map((c) => {
return [c.column, c.type];
}),
attributes: requestedFields,
};
if (mainObject.attributes.length === 0) {
delete mainObject["attributes"];
}
if (mainObject.order.length == 0) {
delete mainObject["order"];
}
const list = await model.findAndCountAll({
offset: offset,
limit: limit,
where: convertedFilters,
...mainObject,
});
resolve({
filters,
pagination: { page, limit },
list: list.rows,
paginationProperties: {
total: list.count,
nextPage: page + 1,
page: page,
previousPage: page == 1 ? 1 : page - 1,
pages: Math.ceil(list.count / limit),
},
});
} catch (error) {
reject(error);
}
});
}
}
144 changes: 144 additions & 0 deletions src/framework/database/helpers/stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import {
getQueryForLast7Days,
getQueryForLastYear,
getQueryForThisYear,
getQueryForThisMonth,
getQueryForLastMonth,
getQueryForThisWeek,
getQueryForToday,
getQueryForLast90Days,
} from "../../reporting/index";
import { get } from "lodash";

export default function (database, model) {
return function (requestedReports) {
return new Promise(async (resolve, reject) => {
try {
let statsInfo = {
total_count: null,
total_created_today: null,
total_created_this_week: null,
total_created_last_7_days: null,
total_created_this_month: null,
total_created_last_month: null,
total_created_last_90_days: null,
total_created_last_year: null,
total_created_this_year: null,
};
let count,
countLast7Days,
countToday,
countLastYear,
countThisYear,
countThisMonth,
countThisweek,
countLastMonth,
countLast90Days;
let selectOptions = {
type: database.QueryTypes.SELECT,
};
if (requestedReports.includes("total_count")) {
count = await database.query(
`select count(*) as total_count from ${model.getTableName()}`,
selectOptions
);
}
if (requestedReports.includes("total_created_last_7_days")) {
countLast7Days = await database.query(
getQueryForLast7Days(model.getTableName()),
selectOptions
);
}
if (requestedReports.includes("total_created_today")) {
countToday = await database.query(
getQueryForToday(model.getTableName()),
selectOptions
);
}
if (requestedReports.includes("total_created_last_year")) {
countLastYear = await database.query(
getQueryForLastYear(model.getTableName()),
selectOptions
);
}
if (requestedReports.includes("total_created_this_year")) {
countThisYear = await database.query(
getQueryForThisYear(model.getTableName()),
selectOptions
);
}
if (requestedReports.includes("total_created_this_month")) {
countThisMonth = await database.query(
getQueryForThisMonth(model.getTableName()),
selectOptions
);
}
if (requestedReports.includes("total_created_this_week")) {
countThisweek = await database.query(
getQueryForThisWeek(model.getTableName()),
selectOptions
);
}
if (requestedReports.includes("total_created_last_month")) {
countLastMonth = await database.query(
getQueryForLastMonth(model.getTableName()),
selectOptions
);
}
if (requestedReports.includes("total_created_last_90_days")) {
countLast90Days = await database.query(
getQueryForLast90Days(model.getTableName()),
selectOptions
);
}

statsInfo.total_count = get(count, "[0].total_count", 0);
statsInfo.total_created_this_month = get(
countThisMonth,
"[0].total_created_this_month",
0
);
statsInfo.total_created_this_week = get(
countThisweek,
"[0].total_created_this_week",
0
);
statsInfo.total_created_last_7_days = get(
countLast7Days,
"[0].total_created_last_7_days",
0
);
statsInfo.total_created_today = get(
countToday,
"[0].total_created_today",
0
);
statsInfo.total_created_last_month = get(
countLastMonth,
"[0].total_created_last_month",
0
);
statsInfo.total_created_last_90_days = get(
countLast90Days,
"[0].total_created_last_90_days",
0
);
statsInfo.total_created_last_year = get(
countLastYear,
"[0].total_created_last_year",
0
);
statsInfo.total_created_this_year = get(
countThisYear,
"[0].total_created_this_year",
0
);

resolve(statsInfo);
} catch (error) {
reject(error)
}
})

}
}
10 changes: 9 additions & 1 deletion src/framework/database/loadTables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { errorMessage } from "../logger/consoleMessages";
import { databaseDefaultOptions } from "../defaults/options/index";
import { IConfigurationCustomModule, IConfiguration } from "../types/configuration";
import { applyRelationshipSql } from "../moduleRelationships/database";
import stats from "./helpers/stats";
import paginate from "./helpers/paginate";

const checkDatabaseOptions = (moduleName, tableName) => {
if (moduleName && !tableName) {
Expand Down Expand Up @@ -48,6 +50,7 @@ export default function (connection, configuration: IConfiguration) {
},
}
);
tables[moduleName].selectIgnoreFields = get(module,'database.selectIgnoreFields', [])
}
}
};
Expand All @@ -70,7 +73,12 @@ export default function (connection, configuration: IConfiguration) {
} else if (element.constructor === Object) {
module = element;
}
applyRelationshipSql(module, tables);
applyRelationshipSql(module, tables);
});

Object.keys(tables).forEach( async table => {
tables[table].stats = await stats(connection, tables[table]);
tables[table].paginate = await paginate(tables[table]);
});

return tables;
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export default {
disable: false,
},
restApi: {
useCors: true,
useBodyParser: true,
useMorgan: true,
showWertik404Page: true,
onCustomApiFailure: function ({ path, res, err }) {
res.send("failed at " + path);
Expand Down
Loading