query store

This commit is contained in:
Julian Krauser 2024-12-18 22:27:33 +01:00
parent 7ca51c3670
commit 6b7b5a99d6
9 changed files with 36 additions and 8 deletions

View file

@ -1,10 +1,13 @@
import { DynamicQueryStructure } from "../type/dynamicQueries";
export interface CreateQueryStoreCommand { export interface CreateQueryStoreCommand {
query: string; title: string;
query: string | DynamicQueryStructure;
} }
export interface UpdateQueryStoreCommand { export interface UpdateQueryStoreCommand {
id: number; id: number;
query: string; query: string | DynamicQueryStructure;
} }
export interface DeleteQueryStoreCommand { export interface DeleteQueryStoreCommand {

View file

@ -15,7 +15,9 @@ export default abstract class QueryStoreCommandHandler {
.insert() .insert()
.into(query) .into(query)
.values({ .values({
query: createQueryStore.query, title: createQueryStore.title,
query:
typeof createQueryStore.query == "string" ? createQueryStore.query : JSON.stringify(createQueryStore.query),
}) })
.execute() .execute()
.then((result) => { .then((result) => {
@ -36,7 +38,8 @@ export default abstract class QueryStoreCommandHandler {
.createQueryBuilder() .createQueryBuilder()
.update(query) .update(query)
.set({ .set({
queryStore: updateQueryStore.query, query:
typeof updateQueryStore.query == "string" ? updateQueryStore.query : JSON.stringify(updateQueryStore.query),
}) })
.where("id = :id", { id: updateQueryStore.id }) .where("id = :id", { id: updateQueryStore.id })
.execute() .execute()

View file

@ -42,14 +42,16 @@ export async function getQueryStoreById(req: Request, res: Response): Promise<an
*/ */
export async function createQueryStore(req: Request, res: Response): Promise<any> { export async function createQueryStore(req: Request, res: Response): Promise<any> {
const query = req.body.query; const query = req.body.query;
const title = req.body.title;
let createQueryStore: CreateQueryStoreCommand = { let createQueryStore: CreateQueryStoreCommand = {
title: title,
query: query, query: query,
}; };
await QueryStoreCommandHandler.create(createQueryStore); let id = await QueryStoreCommandHandler.create(createQueryStore);
res.sendStatus(204); res.status(200).send(id);
} }
/** /**

View file

@ -5,6 +5,9 @@ export class query {
@PrimaryColumn({ generated: "increment", type: "int" }) @PrimaryColumn({ generated: "increment", type: "int" })
id: number; id: number;
@Column({ type: "varchar", length: 255, unique: true })
title: string;
@Column({ type: "text", default: "" }) @Column({ type: "text", default: "" })
query: string; query: string;
} }

View file

@ -10,7 +10,8 @@ export default abstract class QueryStoreFactory {
public static mapToSingle(record: query): QueryStoreViewModel { public static mapToSingle(record: query): QueryStoreViewModel {
return { return {
id: record.id, id: record.id,
query: record.query, title: record.title,
query: record.query.startsWith("{") ? JSON.parse(record.query) : record.query,
}; };
} }

View file

@ -12,6 +12,7 @@ export class QueryStore1734187754677 implements MigrationInterface {
name: "query", name: "query",
columns: [ columns: [
{ name: "id", type: variableType_int, isPrimary: true, isGenerated: true, generationStrategy: "increment" }, { 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: "''" }, { name: "query", type: "text", isNullable: false, default: "''" },
], ],
}), }),

View file

@ -15,6 +15,12 @@ export class MemberDataViews1734520998539 implements MigrationInterface {
datasource datasource
.getRepository(member) .getRepository(member)
.createQueryBuilder("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("TIMESTAMPDIFF(YEAR, member.birthdate, CURDATE())", "todayAge")
.addSelect("YEAR(CURDATE()) - YEAR(member.birthdate)", "ageThisYear"), .addSelect("YEAR(CURDATE()) - YEAR(member.birthdate)", "ageThisYear"),
}), }),

View file

@ -1,4 +1,7 @@
import { DynamicQueryStructure } from "../../type/dynamicQueries";
export interface QueryStoreViewModel { export interface QueryStoreViewModel {
id: number; id: number;
query: string; title: string;
query: string | DynamicQueryStructure;
} }

View file

@ -7,6 +7,12 @@ import { Salutation } from "../enums/salutation";
datasource datasource
.getRepository(member) .getRepository(member)
.createQueryBuilder("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("TIMESTAMPDIFF(YEAR, member.birthdate, CURDATE())", "todayAge")
.addSelect("YEAR(CURDATE()) - YEAR(member.birthdate)", "ageThisYear"), .addSelect("YEAR(CURDATE()) - YEAR(member.birthdate)", "ageThisYear"),
}) })