ff-admin-server/src/type/dynamicQueries.ts

151 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-11-26 15:00:21 +01:00
export interface DynamicQueryStructure {
2025-04-15 10:02:15 +02:00
id: string;
2024-11-26 15:00:21 +01:00
select: string[] | "*";
table: string;
2024-11-28 14:16:44 +01:00
where?: Array<ConditionStructure>;
2025-04-16 16:11:33 +02:00
join?: Array<DynamicQueryStructure & JoinStructure>;
2025-04-15 10:02:15 +02:00
orderBy?: Array<OrderByStructure>; // only at top level
2024-11-26 15:00:21 +01:00
}
2024-11-28 17:53:22 +01:00
export type ConditionStructure = (
2024-11-28 14:16:44 +01:00
| {
2024-12-13 16:24:33 +01:00
structureType: "condition";
2024-11-28 14:16:44 +01:00
column: string;
operation: WhereOperation;
value: ConditionValue;
}
| {
2024-12-13 16:24:33 +01:00
structureType: "nested";
2024-11-28 17:53:22 +01:00
invert?: boolean;
2024-12-14 15:44:17 +01:00
conditions: Array<ConditionStructure>;
2024-11-28 17:53:22 +01:00
}
) & {
concat: WhereType;
structureType: "condition" | "nested";
};
2024-11-26 15:00:21 +01:00
2024-11-28 14:16:44 +01:00
export type ConditionValue = FieldType | Array<FieldType> | { start: FieldType; end: FieldType };
2024-11-26 15:00:21 +01:00
export type FieldType = number | string | Date | boolean;
2024-11-28 17:53:22 +01:00
export type WhereType = "OR" | "AND" | "_"; // _ represents initial where in (sub-)query
2024-11-26 15:00:21 +01:00
export type WhereOperation =
| "eq" // Equal
| "neq" // Not equal
| "lt" // Less than
| "lte" // Less than or equal to
| "gt" // Greater than
| "gte" // Greater than or equal to
| "in" // Included in an array
| "notIn" // Not included in an array
| "contains" // Contains
| "notContains" // Does not contain
| "null" // Is null
| "notNull" // Is not null
| "between" // Is between
| "startsWith" // Starts with
2024-12-16 17:41:26 +01:00
| "endsWith" // Ends with
| "timespanEq"; // Date before x years (YYYY-01-01 <bis> YYYY-12-31)
// TODO: age between | age equals | age greater | age smaller
2024-11-26 15:00:21 +01:00
2025-04-16 16:11:33 +02:00
export type JoinStructure = { foreignColumn: string; type: "defined" } | { condition: string; type: "custom" };
2024-12-13 16:24:33 +01:00
export type OrderByStructure = {
2025-04-15 10:02:15 +02:00
id: string;
depth: number;
table: string;
2024-12-13 16:24:33 +01:00
column: string;
order: OrderByType;
};
export type OrderByType = "ASC" | "DESC";
2024-12-27 13:17:23 +01:00
export type QueryResult = {
[key: string]: FieldType | QueryResult | Array<QueryResult>;
};
2024-12-14 15:44:17 +01:00
export const exampleQuery: DynamicQueryStructure = {
2025-04-15 10:02:15 +02:00
id: "1234",
2024-11-26 15:00:21 +01:00
select: ["firstname", "lastname"],
table: "member",
2024-11-28 14:16:44 +01:00
where: [
{
2024-11-28 17:53:22 +01:00
structureType: "condition",
concat: "_",
2024-11-28 14:16:44 +01:00
column: "mail",
operation: "endsWith",
value: "@gmail.com",
2024-11-26 15:00:21 +01:00
},
2024-11-28 14:16:44 +01:00
{
2024-11-28 17:53:22 +01:00
structureType: "nested",
concat: "AND",
2024-12-14 15:44:17 +01:00
conditions: [
2024-11-28 14:16:44 +01:00
{
2024-11-28 17:53:22 +01:00
structureType: "condition",
concat: "_",
2024-11-28 14:16:44 +01:00
column: "firstname",
operation: "startsWith",
value: "J",
},
{
2024-11-28 17:53:22 +01:00
structureType: "condition",
concat: "OR",
2024-11-28 14:16:44 +01:00
column: "lastname",
operation: "startsWith",
value: "K",
},
],
},
],
2024-11-26 15:00:21 +01:00
join: [
{
2025-04-15 10:02:15 +02:00
id: "5678",
2024-11-26 15:00:21 +01:00
select: "*",
2024-11-27 15:01:31 +01:00
table: "communication",
foreignColumn: "sendNewsletter",
2025-04-16 16:11:33 +02:00
type: "defined",
2024-11-26 15:00:21 +01:00
},
2024-11-28 17:53:22 +01:00
{
2025-04-15 10:02:15 +02:00
id: "91011",
2024-11-28 17:53:22 +01:00
select: "*",
table: "membership",
foreignColumn: "memberships",
2025-04-16 16:11:33 +02:00
type: "defined",
2024-11-28 17:53:22 +01:00
join: [
{
2025-04-15 10:02:15 +02:00
id: "121314",
2024-11-28 17:53:22 +01:00
select: "*",
table: "membership_status",
foreignColumn: "status",
2025-04-16 16:11:33 +02:00
type: "defined",
2024-11-28 17:53:22 +01:00
where: [
{
structureType: "condition",
concat: "_",
column: "status",
operation: "eq",
value: "aktiv",
},
],
},
],
},
2024-11-26 15:00:21 +01:00
],
2024-12-13 16:24:33 +01:00
orderBy: [
{
2025-04-15 10:02:15 +02:00
id: "1234",
depth: 0,
table: "member",
2024-12-13 16:24:33 +01:00
column: "firstname",
order: "ASC",
},
{
2025-04-15 10:02:15 +02:00
id: "1234",
depth: 0,
table: "member",
2024-12-13 16:24:33 +01:00
column: "lastname",
order: "ASC",
},
],
2024-11-26 15:00:21 +01:00
};