Skip to content

Commit fada2e1

Browse files
davemarcoMarcohoophalab
authored
refactor(webui): Migrate archive metadata SQL query route to new Fastify architecture. (#1028)
Co-authored-by: Marco <[email protected]> Co-authored-by: hoophalab <[email protected]>
1 parent d058631 commit fada2e1

File tree

6 files changed

+54
-29
lines changed

6 files changed

+54
-29
lines changed

components/webui/client/src/api/sql/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import axios from "axios";
88
* @return
99
*/
1010
const querySql = async <T>(queryString: string) => {
11-
return axios.post<T>("/query/sql", {queryString});
11+
return axios.post<T>("/api/archive-metadata/sql", {queryString});
1212
};
1313

1414
export {querySql};

components/webui/server/eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const EslintConfig = [
2222
{
2323
// TypeBox imports
2424
capIsNewExceptions: [
25+
"Type.Any",
2526
"Type.Enum",
2627
"Type.Integer",
2728
"Type.Literal",
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {
2+
FastifyPluginAsyncTypebox,
3+
Type,
4+
} from "@fastify/type-provider-typebox";
5+
import {StatusCodes} from "http-status-codes";
6+
7+
import {SqlSchema} from "../../../schemas/archive-metadata.js";
8+
9+
10+
/**
11+
* Archive metadata API routes.
12+
*
13+
* @param fastify
14+
*/
15+
const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
16+
const mysqlConnectionPool = fastify.mysql.pool;
17+
18+
fastify.post(
19+
"/sql",
20+
{
21+
schema: {
22+
body: SqlSchema,
23+
response: {
24+
[StatusCodes.OK]: Type.Any(),
25+
},
26+
tags: ["Archive Metadata"],
27+
},
28+
},
29+
async (req, reply) => {
30+
const {queryString} = req.body;
31+
const [result] = await mysqlConnectionPool.query(queryString);
32+
reply.code(StatusCodes.OK);
33+
34+
return result;
35+
},
36+
);
37+
};
38+
39+
export default plugin;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {Type} from "@sinclair/typebox";
2+
3+
import {StringSchema} from "./common.js";
4+
5+
6+
/**
7+
* Schema for SQL query request.
8+
*/
9+
const SqlSchema = Type.Object({
10+
queryString: StringSchema,
11+
});
12+
13+
export {SqlSchema};

components/webui/server/src/plugins/DbManager.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,6 @@ class DbManager {
8282
});
8383
}
8484

85-
/**
86-
* Submits a query to MySQL.
87-
*
88-
* @param queryString
89-
* @return The result from MySQL.
90-
*/
91-
async queryMySql (queryString: string) {
92-
const [result] = await this.#mysqlConnectionPool.query(queryString);
93-
return result;
94-
}
95-
96-
9785
/**
9886
* Submits a stream extraction job to the scheduler and waits for it to finish.
9987
*

components/webui/server/src/routes/query.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,9 @@ import {
1616
* @param app
1717
* @return
1818
*/
19-
// eslint-disable-next-line max-lines-per-function
2019
const routes: FastifyPluginAsync = async (app) => {
2120
const fastify = app.withTypeProvider<TypeBoxTypeProvider>();
2221

23-
fastify.post(
24-
"/query/sql",
25-
{
26-
schema: {
27-
body: Type.Object({
28-
queryString: Type.String({minLength: 1}),
29-
}),
30-
},
31-
},
32-
async (req) => {
33-
const {queryString} = req.body;
34-
return await fastify.dbManager.queryMySql(queryString);
35-
},
36-
);
37-
3822
fastify.post("/query/extract-stream", {
3923
schema: {
4024
body: Type.Object({

0 commit comments

Comments
 (0)