From 6b7b5a99d6d9bd9bd2b3289b6773b2f1a2b6c805 Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Wed, 18 Dec 2024 22:27:33 +0100 Subject: [PATCH] query store --- src/command/queryStoreCommand.ts | 7 +++++-- src/command/queryStoreCommandHandler.ts | 7 +++++-- src/controller/admin/queryStoreController.ts | 6 ++++-- src/entity/query.ts | 3 +++ src/factory/admin/queryStore.ts | 3 ++- src/migrations/1734187754677-queryStore.ts | 1 + src/migrations/1734520998539-memberDataViews.ts | 6 ++++++ src/viewmodel/admin/queryStore.models.ts | 5 ++++- src/views/memberView.ts | 6 ++++++ 9 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/command/queryStoreCommand.ts b/src/command/queryStoreCommand.ts index 4404fbe..7043159 100644 --- a/src/command/queryStoreCommand.ts +++ b/src/command/queryStoreCommand.ts @@ -1,10 +1,13 @@ +import { DynamicQueryStructure } from "../type/dynamicQueries"; + export interface CreateQueryStoreCommand { - query: string; + title: string; + query: string | DynamicQueryStructure; } export interface UpdateQueryStoreCommand { id: number; - query: string; + query: string | DynamicQueryStructure; } export interface DeleteQueryStoreCommand { diff --git a/src/command/queryStoreCommandHandler.ts b/src/command/queryStoreCommandHandler.ts index 7981204..7eeb80c 100644 --- a/src/command/queryStoreCommandHandler.ts +++ b/src/command/queryStoreCommandHandler.ts @@ -15,7 +15,9 @@ export default abstract class QueryStoreCommandHandler { .insert() .into(query) .values({ - query: createQueryStore.query, + title: createQueryStore.title, + query: + typeof createQueryStore.query == "string" ? createQueryStore.query : JSON.stringify(createQueryStore.query), }) .execute() .then((result) => { @@ -36,7 +38,8 @@ export default abstract class QueryStoreCommandHandler { .createQueryBuilder() .update(query) .set({ - queryStore: updateQueryStore.query, + query: + typeof updateQueryStore.query == "string" ? updateQueryStore.query : JSON.stringify(updateQueryStore.query), }) .where("id = :id", { id: updateQueryStore.id }) .execute() diff --git a/src/controller/admin/queryStoreController.ts b/src/controller/admin/queryStoreController.ts index 6207e97..6c6e9c6 100644 --- a/src/controller/admin/queryStoreController.ts +++ b/src/controller/admin/queryStoreController.ts @@ -42,14 +42,16 @@ export async function getQueryStoreById(req: Request, res: Response): Promise { const query = req.body.query; + const title = req.body.title; let createQueryStore: CreateQueryStoreCommand = { + title: title, query: query, }; - await QueryStoreCommandHandler.create(createQueryStore); + let id = await QueryStoreCommandHandler.create(createQueryStore); - res.sendStatus(204); + res.status(200).send(id); } /** diff --git a/src/entity/query.ts b/src/entity/query.ts index b03d4e2..eb1bfe7 100644 --- a/src/entity/query.ts +++ b/src/entity/query.ts @@ -5,6 +5,9 @@ export class query { @PrimaryColumn({ generated: "increment", type: "int" }) id: number; + @Column({ type: "varchar", length: 255, unique: true }) + title: string; + @Column({ type: "text", default: "" }) query: string; } diff --git a/src/factory/admin/queryStore.ts b/src/factory/admin/queryStore.ts index 478e48b..7574ab8 100644 --- a/src/factory/admin/queryStore.ts +++ b/src/factory/admin/queryStore.ts @@ -10,7 +10,8 @@ export default abstract class QueryStoreFactory { public static mapToSingle(record: query): QueryStoreViewModel { return { id: record.id, - query: record.query, + title: record.title, + query: record.query.startsWith("{") ? JSON.parse(record.query) : record.query, }; } diff --git a/src/migrations/1734187754677-queryStore.ts b/src/migrations/1734187754677-queryStore.ts index b86fa23..6912110 100644 --- a/src/migrations/1734187754677-queryStore.ts +++ b/src/migrations/1734187754677-queryStore.ts @@ -12,6 +12,7 @@ export class QueryStore1734187754677 implements MigrationInterface { name: "query", columns: [ { name: "id", type: variableType_int, isPrimary: true, isGenerated: true, generationStrategy: "increment" }, + { name: "title", type: "varchar", length: "255", isNullable: false, isUnique: true }, { name: "query", type: "text", isNullable: false, default: "''" }, ], }), diff --git a/src/migrations/1734520998539-memberDataViews.ts b/src/migrations/1734520998539-memberDataViews.ts index 11b9656..d03fd94 100644 --- a/src/migrations/1734520998539-memberDataViews.ts +++ b/src/migrations/1734520998539-memberDataViews.ts @@ -15,6 +15,12 @@ export class MemberDataViews1734520998539 implements MigrationInterface { datasource .getRepository(member) .createQueryBuilder("member") + .select("member.id", "id") + .addSelect("member.salutation", "salutation") + .addSelect("member.firstname", "firstname") + .addSelect("member.lastname", "lastname") + .addSelect("member.nameaffix", "nameaffix") + .addSelect("member.birthdate", "birthdate") .addSelect("TIMESTAMPDIFF(YEAR, member.birthdate, CURDATE())", "todayAge") .addSelect("YEAR(CURDATE()) - YEAR(member.birthdate)", "ageThisYear"), }), diff --git a/src/viewmodel/admin/queryStore.models.ts b/src/viewmodel/admin/queryStore.models.ts index 10d547b..ff697c6 100644 --- a/src/viewmodel/admin/queryStore.models.ts +++ b/src/viewmodel/admin/queryStore.models.ts @@ -1,4 +1,7 @@ +import { DynamicQueryStructure } from "../../type/dynamicQueries"; + export interface QueryStoreViewModel { id: number; - query: string; + title: string; + query: string | DynamicQueryStructure; } diff --git a/src/views/memberView.ts b/src/views/memberView.ts index d28c944..08f147c 100644 --- a/src/views/memberView.ts +++ b/src/views/memberView.ts @@ -7,6 +7,12 @@ import { Salutation } from "../enums/salutation"; datasource .getRepository(member) .createQueryBuilder("member") + .select("member.id", "id") + .addSelect("member.salutation", "salutation") + .addSelect("member.firstname", "firstname") + .addSelect("member.lastname", "lastname") + .addSelect("member.nameaffix", "nameaffix") + .addSelect("member.birthdate", "birthdate") .addSelect("TIMESTAMPDIFF(YEAR, member.birthdate, CURDATE())", "todayAge") .addSelect("YEAR(CURDATE()) - YEAR(member.birthdate)", "ageThisYear"), })