-
Notifications
You must be signed in to change notification settings - Fork 83
refactor(webui): Migrate archive metadata SQL query route to new Fastify architecture. #1028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7a5449b
5e88f4d
ecc6199
1fde043
5378604
644cc26
65197de
88fdee7
3a2944e
b1d09c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { | ||
| FastifyPluginAsyncTypebox, | ||
| Type, | ||
| } from "@fastify/type-provider-typebox"; | ||
| import {StatusCodes} from "http-status-codes"; | ||
|
|
||
| import {SqlSchema} from "../../../schemas/archive-metadata.js"; | ||
|
|
||
hoophalab marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Archive metadata API routes. | ||
| * | ||
| * @param fastify | ||
| */ | ||
| const plugin: FastifyPluginAsyncTypebox = async (fastify) => { | ||
| const mysqlConnectionPool = fastify.mysql.pool; | ||
|
|
||
| fastify.post( | ||
| "/sql", | ||
| { | ||
| schema: { | ||
| body: SqlSchema, | ||
| response: { | ||
| [StatusCodes.OK]: Type.Any(), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 Nitpick (assertive) Consider improving response type safety. Using -[StatusCodes.OK]: Type.Any(),
+[StatusCodes.OK]: Type.Object({
+ // Define expected response structure based on your use case
+ rows: Type.Array(Type.Any()),
+ fields: Type.Optional(Type.Array(Type.Any())),
+}),🤖 Prompt for AI Agents
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. quick question: why do we want to specify
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @junhaoliao You raise an excellent point about the trade-offs between flexibility and type safety/documentation. The current Why
Benefits of defining specific response fields:
Suggested middle-ground approach: [StatusCodes.OK]: Type.Object({
data: Type.Any(), // The actual query results
queryType: Type.Optional(Type.String()), // e.g., "SELECT", "INSERT", etc.
rowCount: Type.Optional(Type.Number()),
fields: Type.Optional(Type.Array(Type.Object({
name: Type.String(),
type: Type.String(),
}))),
}),This provides:
The challenge is that MySQL query results have different shapes (result sets vs execution metadata), so some level of What's your preference for this API design? 🧠 Learnings used
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similiarly to what the AI responded, the response will be different for types of queries see resultheader or a custom row. Typing of response is not exactly clear with arbitrary query
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, looking at https://sidorares.github.io/node-mysql2/docs/examples/queries/simple-queries/select#queryoptions , i was thinking that the return type can be written as Anyways as you suggested, it probably won't make a difference given we can't specify the element's types. This is fine to leave as-is until we come up with better API endpoints testing / documentation infra like Swagger
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes i think since its generic there is not much we can do for now, but the client can specify typing |
||
| }, | ||
| tags: ["Archive Metadata"], | ||
| }, | ||
| }, | ||
| async (req, reply) => { | ||
| const {queryString} = req.body; | ||
| const [result] = await mysqlConnectionPool.query(queryString); | ||
| reply.code(StatusCodes.OK); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel we don't actually need explicit statuscode, but feel free to keep it here.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in the search routes i am explicity using fancier positive status codes(not always OK) like NO CONTENT, and CREATE. I like the explicit status code since it tells dev that something must be returned to client. I would prefer to leave it here
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds fairly reasonable. Let's keep it here. |
||
|
|
||
| return result; | ||
| }, | ||
hoophalab marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
| }; | ||
|
|
||
| export default plugin; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import {Type} from "@sinclair/typebox"; | ||
|
|
||
| import {StringSchema} from "./common.js"; | ||
|
|
||
|
|
||
| /** | ||
| * Schema for SQL query request. | ||
| */ | ||
| const SqlSchema = Type.Object({ | ||
| queryString: StringSchema, | ||
| }); | ||
|
|
||
| export {SqlSchema}; |
Uh oh!
There was an error while loading. Please reload this page.