feat(datamart): get, add, update queries
This commit is contained in:
@@ -14,38 +14,83 @@
|
||||
* when a criteria is password over we will handle it by counting how many were passed up to 3 then deal with each one respectively
|
||||
*/
|
||||
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../db/db.controller.js";
|
||||
import { datamart } from "../db/schema/datamart.schema.js";
|
||||
import { prodQuery } from "../prodSql/prodSqlQuery.controller.js";
|
||||
import { returnFunc } from "../utils/returnHelper.utils.js";
|
||||
import { tryCatch } from "../utils/trycatch.utils.js";
|
||||
|
||||
type Data = {
|
||||
name: string;
|
||||
criteria: string;
|
||||
options: string;
|
||||
};
|
||||
|
||||
export const runDatamartQuery = async (data: Data) => {
|
||||
// search the query db for the query by name
|
||||
const dummyquery = {
|
||||
name: "something",
|
||||
query: "select * from tableA where start=[start] and end=[end]",
|
||||
};
|
||||
const { data: queryInfo, error: qIe } = await tryCatch(
|
||||
db.select().from(datamart).where(eq(datamart.name, data.name)),
|
||||
);
|
||||
|
||||
if (qIe) {
|
||||
return returnFunc({
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "datamart",
|
||||
subModule: "query",
|
||||
message: `Error getting ${data.name} info`,
|
||||
data: [qIe],
|
||||
notify: false,
|
||||
});
|
||||
}
|
||||
|
||||
// create the query with no changed just to have it here
|
||||
let datamartQuery = dummyquery.query;
|
||||
let datamartQuery = queryInfo[0]?.query || "";
|
||||
// split the criteria by "," then and then update the query
|
||||
if (data.criteria) {
|
||||
const params = new URLSearchParams(data.criteria);
|
||||
if (data.options) {
|
||||
const params = new URLSearchParams(data.options);
|
||||
|
||||
for (const [key, value] of params.entries()) {
|
||||
for (const [rawKey, rawValue] of params.entries()) {
|
||||
const key = rawKey.trim();
|
||||
const value = rawValue.trim();
|
||||
datamartQuery = datamartQuery.replaceAll(`[${key}]`, value);
|
||||
}
|
||||
}
|
||||
|
||||
const { data: queryRun, error } = await tryCatch(
|
||||
prodQuery(datamartQuery, `Running datamart query: ${data.name}`),
|
||||
);
|
||||
|
||||
if (error) {
|
||||
return returnFunc({
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "datamart",
|
||||
subModule: "query",
|
||||
message: `Data for: ${data.name} encountered an error while trying to get it`,
|
||||
data: [error],
|
||||
notify: false,
|
||||
});
|
||||
}
|
||||
|
||||
if (!queryRun.success) {
|
||||
return returnFunc({
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "datamart",
|
||||
subModule: "query",
|
||||
message: queryRun.message,
|
||||
data: queryRun.data,
|
||||
notify: false,
|
||||
});
|
||||
}
|
||||
return returnFunc({
|
||||
success: true,
|
||||
level: "info",
|
||||
module: "datamart",
|
||||
subModule: "query",
|
||||
message: `Data for: ${data.name}`,
|
||||
data: [{ data: datamartQuery }],
|
||||
data: queryRun.data,
|
||||
notify: false,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -4,6 +4,7 @@ import { db } from "../db/db.controller.js";
|
||||
import { datamart } from "../db/schema/datamart.schema.js";
|
||||
import { apiReturn } from "../utils/returnHelper.utils.js";
|
||||
import addQuery from "./datamartAdd.route.js";
|
||||
import updateQuery from "./datamartUpdate.route.js";
|
||||
import runQuery from "./getDatamart.route.js";
|
||||
|
||||
export const setupDatamartRoutes = (baseUrl: string, app: Express) => {
|
||||
@@ -11,6 +12,7 @@ export const setupDatamartRoutes = (baseUrl: string, app: Express) => {
|
||||
|
||||
app.use(`${baseUrl}/api/datamart`, runQuery);
|
||||
app.use(`${baseUrl}/api/datamart`, addQuery);
|
||||
app.use(`${baseUrl}/api/datamart`, updateQuery);
|
||||
|
||||
// just sending a get on datamart will return all the queries that we can call.
|
||||
app.get(`${baseUrl}/api/datamart`, async (_, res) => {
|
||||
|
||||
@@ -1,27 +1,96 @@
|
||||
import fs from "node:fs";
|
||||
import { Router } from "express";
|
||||
import multer from "multer";
|
||||
import z from "zod";
|
||||
import type { NewDatamart } from "../db/schema/datamart.schema.js";
|
||||
import { db } from "../db/db.controller.js";
|
||||
import { datamart, type NewDatamart } from "../db/schema/datamart.schema.js";
|
||||
import { apiReturn } from "../utils/returnHelper.utils.js";
|
||||
import { tryCatch } from "../utils/trycatch.utils.js";
|
||||
|
||||
const r = Router();
|
||||
const upload = multer({ dest: "uploads/" });
|
||||
|
||||
const newQuery = z.object({
|
||||
name: z.string().min(5),
|
||||
description: z.string().min(30),
|
||||
query: z.string().min(10),
|
||||
query: z.string().min(10).optional(),
|
||||
options: z
|
||||
.string()
|
||||
.describe("This should be a set of keys separated by a comma")
|
||||
.optional(),
|
||||
});
|
||||
|
||||
r.post("/", async (req, res) => {
|
||||
r.post("/", upload.single("queryFile"), async (req, res) => {
|
||||
try {
|
||||
const v = newQuery.parse(req.body);
|
||||
|
||||
const query: NewDatamart = { ...v };
|
||||
const query: NewDatamart = {
|
||||
...v,
|
||||
name: v.name?.trim().replaceAll(" ", "_"),
|
||||
};
|
||||
|
||||
console.log(query);
|
||||
//console.log(query);
|
||||
if (req.file) {
|
||||
const sqlContents = fs.readFileSync(req.file.path, "utf8");
|
||||
query.query = sqlContents;
|
||||
|
||||
// optional: delete temp file afterwards
|
||||
fs.unlink(req.file.path, () => {});
|
||||
}
|
||||
|
||||
// if we forget the file crash out
|
||||
if (!query.query) {
|
||||
// no query text anywhere
|
||||
return apiReturn(res, {
|
||||
success: true,
|
||||
level: "info", //connect.success ? "info" : "error",
|
||||
module: "routes",
|
||||
subModule: "datamart",
|
||||
message: `${query.name} missing sql file to parse`,
|
||||
data: [],
|
||||
status: 400, //connect.success ? 200 : 400,
|
||||
});
|
||||
}
|
||||
|
||||
// // if we didn't replace the test1 stuff crash out
|
||||
// if (!query.query.includes("test1")) {
|
||||
// return apiReturn(res, {
|
||||
// success: true,
|
||||
// level: "info", //connect.success ? "info" : "error",
|
||||
// module: "routes",
|
||||
// subModule: "datamart",
|
||||
// message:
|
||||
// "Query must include the 'test1' or everything switched to test1",
|
||||
// data: [],
|
||||
// status: 400, //connect.success ? 200 : 400,
|
||||
// });
|
||||
// }
|
||||
|
||||
const { data, error } = await tryCatch(db.insert(datamart).values(query));
|
||||
|
||||
if (error) {
|
||||
return apiReturn(res, {
|
||||
success: true,
|
||||
level: "error", //connect.success ? "info" : "error",
|
||||
module: "routes",
|
||||
subModule: "datamart",
|
||||
message: `${query.name} encountered an error while being added`,
|
||||
data: [error.cause],
|
||||
status: 200, //connect.success ? 200 : 400,
|
||||
});
|
||||
}
|
||||
|
||||
if (data) {
|
||||
return apiReturn(res, {
|
||||
success: true,
|
||||
level: "info", //connect.success ? "info" : "error",
|
||||
module: "routes",
|
||||
subModule: "datamart",
|
||||
message: `${query.name} was just added`,
|
||||
data: [query],
|
||||
status: 200, //connect.success ? 200 : 400,
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof z.ZodError) {
|
||||
const flattened = z.flattenError(err);
|
||||
|
||||
156
backend/src/datamart/datamartUpdate.route.ts
Normal file
156
backend/src/datamart/datamartUpdate.route.ts
Normal file
@@ -0,0 +1,156 @@
|
||||
import fs from "node:fs";
|
||||
import { eq, sql } from "drizzle-orm";
|
||||
import { Router } from "express";
|
||||
import multer from "multer";
|
||||
import z from "zod";
|
||||
import { db } from "../db/db.controller.js";
|
||||
import { datamart } from "../db/schema/datamart.schema.js";
|
||||
import { apiReturn } from "../utils/returnHelper.utils.js";
|
||||
import { tryCatch } from "../utils/trycatch.utils.js";
|
||||
|
||||
const r = Router();
|
||||
const upload = multer({ dest: "uploads/" });
|
||||
|
||||
const newQuery = z.object({
|
||||
name: z.string().min(5).optional(),
|
||||
description: z.string().min(30).optional(),
|
||||
query: z.string().min(10).optional(),
|
||||
options: z
|
||||
.string()
|
||||
.describe("This should be a set of keys separated by a comma")
|
||||
.optional(),
|
||||
setActive: z.string().optional(),
|
||||
active: z.boolean().optional(),
|
||||
});
|
||||
|
||||
r.patch("/:id", upload.single("queryFile"), async (req, res) => {
|
||||
const { id } = req.params;
|
||||
|
||||
try {
|
||||
const v = newQuery.parse(req.body);
|
||||
|
||||
const query = {
|
||||
...v,
|
||||
};
|
||||
|
||||
//console.log(query);
|
||||
if (req.file) {
|
||||
const sqlContents = fs.readFileSync(req.file.path, "utf8");
|
||||
query.query = sqlContents;
|
||||
|
||||
// optional: delete temp file afterwards
|
||||
fs.unlink(req.file.path, () => {});
|
||||
}
|
||||
|
||||
if (v.name) {
|
||||
query.name = v.name.trim().replaceAll(" ", "_");
|
||||
}
|
||||
|
||||
if (v.description) {
|
||||
query.options = v.description;
|
||||
}
|
||||
|
||||
if (v.options) {
|
||||
query.options = v.options;
|
||||
}
|
||||
|
||||
if (v.setActive) {
|
||||
query.active = v.setActive === "true";
|
||||
}
|
||||
|
||||
// if we forget the file crash out
|
||||
// if (!query.query) {
|
||||
// // no query text anywhere
|
||||
// return apiReturn(res, {
|
||||
// success: true,
|
||||
// level: "info", //connect.success ? "info" : "error",
|
||||
// module: "routes",
|
||||
// subModule: "datamart",
|
||||
// message: `${query.name} missing sql file to parse`,
|
||||
// data: [],
|
||||
// status: 400, //connect.success ? 200 : 400,
|
||||
// });
|
||||
// }
|
||||
|
||||
// // if we didn't replace the test1 stuff crash out
|
||||
|
||||
if (query.query && !query.query.includes("test1")) {
|
||||
return apiReturn(res, {
|
||||
success: true,
|
||||
level: "error", //connect.success ? "info" : "error",
|
||||
module: "routes",
|
||||
subModule: "datamart",
|
||||
message:
|
||||
"All queries must point to test1 this way we can keep it dynamic.",
|
||||
data: [],
|
||||
status: 400, //connect.success ? 200 : 400,
|
||||
});
|
||||
}
|
||||
|
||||
const { data, error } = await tryCatch(
|
||||
db
|
||||
.update(datamart)
|
||||
.set({
|
||||
...query,
|
||||
version: sql`${datamart.version} + 1`,
|
||||
upd_date: sql`NOW()`,
|
||||
upd_user: "lst_user",
|
||||
})
|
||||
.where(eq(datamart.id, id as string)),
|
||||
);
|
||||
|
||||
if (error) {
|
||||
return apiReturn(res, {
|
||||
success: true,
|
||||
level: "error", //connect.success ? "info" : "error",
|
||||
module: "routes",
|
||||
subModule: "datamart",
|
||||
message: `${query.name} encountered an error while being updated`,
|
||||
data: [error.cause],
|
||||
status: 200, //connect.success ? 200 : 400,
|
||||
});
|
||||
}
|
||||
|
||||
if (data) {
|
||||
return apiReturn(res, {
|
||||
success: true,
|
||||
level: "info", //connect.success ? "info" : "error",
|
||||
module: "routes",
|
||||
subModule: "datamart",
|
||||
message: `${query.name} was just updated`,
|
||||
data: [],
|
||||
status: 200, //connect.success ? 200 : 400,
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
if (err instanceof z.ZodError) {
|
||||
const flattened = z.flattenError(err);
|
||||
// return res.status(400).json({
|
||||
// error: "Validation failed",
|
||||
// details: flattened,
|
||||
// });
|
||||
|
||||
return apiReturn(res, {
|
||||
success: false,
|
||||
level: "error", //connect.success ? "info" : "error",
|
||||
module: "routes",
|
||||
subModule: "auth",
|
||||
message: "Validation failed",
|
||||
data: [flattened],
|
||||
status: 400, //connect.success ? 200 : 400,
|
||||
});
|
||||
}
|
||||
|
||||
return apiReturn(res, {
|
||||
success: false,
|
||||
level: "error",
|
||||
module: "routes",
|
||||
subModule: "datamart",
|
||||
message: "There was an error updating the query",
|
||||
data: [err],
|
||||
status: 200,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export default r;
|
||||
@@ -6,11 +6,11 @@ const r = Router();
|
||||
|
||||
r.get("/:name", async (req, res) => {
|
||||
const { name } = req.params;
|
||||
const criteria = new URLSearchParams(
|
||||
const options = new URLSearchParams(
|
||||
req.query as Record<string, string>,
|
||||
).toString();
|
||||
|
||||
const dataRan = await runDatamartQuery({ name, criteria });
|
||||
const dataRan = await runDatamartQuery({ name, options });
|
||||
return apiReturn(res, {
|
||||
success: dataRan.success,
|
||||
level: "info",
|
||||
|
||||
Reference in New Issue
Block a user