feat(datamart): intial setup to datamart migrations
This commit is contained in:
18
backend/src/datamart/datamartAdd.route.ts
Normal file
18
backend/src/datamart/datamartAdd.route.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Router } from "express";
|
||||
import { apiReturn } from "../utils/returnHelper.utils.js";
|
||||
|
||||
const r = Router();
|
||||
|
||||
r.post("/add", async (_, res) => {
|
||||
apiReturn(res, {
|
||||
success: true,
|
||||
level: "info",
|
||||
module: "routes",
|
||||
subModule: "prodSql",
|
||||
message: "connect.message",
|
||||
data: [{ connect: "" }],
|
||||
status: 200,
|
||||
});
|
||||
});
|
||||
|
||||
export default r;
|
||||
16
backend/src/db/db.controller.ts
Normal file
16
backend/src/db/db.controller.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { drizzle } from "drizzle-orm/postgres-js";
|
||||
import postgres from "postgres";
|
||||
|
||||
const dbURL = `postgres://${process.env.DATABASE_USER}:${process.env.DATABASE_PASSWORD}@${process.env.DATABASE_HOST}:${process.env.DATABASE_PORT}/${process.env.DATABASE_DB}`;
|
||||
|
||||
const queryClient = postgres(dbURL, {
|
||||
max: 10,
|
||||
idle_timeout: 60,
|
||||
connect_timeout: 30,
|
||||
max_lifetime: 1000 * 6 * 5,
|
||||
onnotice: (n) => {
|
||||
console.info("PG notice: ", n.message);
|
||||
},
|
||||
});
|
||||
|
||||
export const db = drizzle({ client: queryClient });
|
||||
30
backend/src/db/schema/datamart.schema.ts
Normal file
30
backend/src/db/schema/datamart.schema.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import {
|
||||
boolean,
|
||||
integer,
|
||||
pgTable,
|
||||
text,
|
||||
timestamp,
|
||||
uuid,
|
||||
} from "drizzle-orm/pg-core";
|
||||
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
|
||||
import type { z } from "zod";
|
||||
|
||||
export const datamart = pgTable("datamart", {
|
||||
id: uuid("id").defaultRandom().primaryKey(),
|
||||
name: text("name"),
|
||||
description: text("description").notNull(),
|
||||
query: text("query"),
|
||||
version: integer("version").notNull(),
|
||||
active: boolean("active").default(true),
|
||||
options: text("checked").default(""),
|
||||
add_date: timestamp("add_date").defaultNow(),
|
||||
add_user: text("add_user").default("lst-system"),
|
||||
upd_date: timestamp("upd_date").defaultNow(),
|
||||
upd_user: text("upd_date").default("lst-system"),
|
||||
});
|
||||
|
||||
export const datamartSchema = createSelectSchema(datamart);
|
||||
export const newDataMartSchema = createInsertSchema(datamart);
|
||||
|
||||
export type Datamart = z.infer<typeof datamartSchema>;
|
||||
export type NewDatamart = z.infer<typeof newDataMartSchema>;
|
||||
@@ -1,7 +1,7 @@
|
||||
import build from "pino-abstract-transport";
|
||||
import { db } from "../db/db.controller.js";
|
||||
import { logs } from "../db/schema/logs.schema.js";
|
||||
import { tryCatch } from "../utils/trycatch.utlis.js";
|
||||
import { tryCatch } from "../utils/trycatch.utils.js";
|
||||
|
||||
const pinoLogLevels: Record<number, string> = {
|
||||
10: "trace",
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
import type { Express } from "express";
|
||||
|
||||
// import the routes and route setups
|
||||
import { setupApiDocsRoutes } from "./configs/scaler.config.js";
|
||||
import { setupDatamartRoutes } from "./datamart/datamart.routes.js";
|
||||
import { setupProdSqlRoutes } from "./prodSql/prodSql.routes.js";
|
||||
// import { setupApiDocsRoutes } from "./configs/scaler.config.js";
|
||||
// import { setupDatamartRoutes } from "./datamart/datamart.routes.js";
|
||||
// import { setupProdSqlRoutes } from "./prodSql/prodSql.routes.js";
|
||||
import stats from "./system/stats.route.js";
|
||||
|
||||
export const setupRoutes = (baseUrl: string, app: Express) => {
|
||||
//setup all the routes
|
||||
setupApiDocsRoutes(baseUrl, app);
|
||||
setupProdSqlRoutes(baseUrl, app);
|
||||
setupDatamartRoutes(baseUrl, app);
|
||||
|
||||
app.use(`${baseUrl}/api/stats`, stats);
|
||||
//setup all the routes
|
||||
// setupApiDocsRoutes(baseUrl, app);
|
||||
// setupProdSqlRoutes(baseUrl, app);
|
||||
// setupDatamartRoutes(baseUrl, app);
|
||||
};
|
||||
|
||||
28
backend/src/utils/trycatch.utils.ts
Normal file
28
backend/src/utils/trycatch.utils.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
type Success<T> = { data: T; error: null };
|
||||
type Failure<E> = { data: null; error: E };
|
||||
|
||||
export type Result<T, E = Error> = Success<T> | Failure<E>;
|
||||
|
||||
/**
|
||||
* A universal tryCatch wrapper that:
|
||||
* - Never throws
|
||||
* - Always resolves to Result<T,E>
|
||||
* - Allows optional error mapping function for strong typing
|
||||
*/
|
||||
|
||||
export async function tryCatch<T, E = Error>(
|
||||
promise: Promise<T>,
|
||||
onError?: (error: unknown) => E,
|
||||
): Promise<Result<T, E>> {
|
||||
try {
|
||||
const data = await promise;
|
||||
return { data, error: null };
|
||||
} catch (err: unknown) {
|
||||
const error = onError
|
||||
? onError(err)
|
||||
: err instanceof Error
|
||||
? (err as E)
|
||||
: (new Error(String(err)) as E);
|
||||
return { data: null, error };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user