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

128 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-11-26 14:00:21 +00:00
export interface DynamicQueryStructure {
select: string[] | "*";
table: string;
2024-11-28 13:16:44 +00:00
where?: Array<ConditionStructure>;
2024-11-27 14:01:31 +00:00
join?: Array<DynamicQueryStructure & { foreignColumn: string }>;
2024-12-13 15:24:33 +00:00
orderBy?: Array<OrderByStructure>;
2024-11-26 14:00:21 +00:00
}
2024-11-28 16:53:22 +00:00
export type ConditionStructure = (
2024-11-28 13:16:44 +00:00
| {
2024-12-13 15:24:33 +00:00
structureType: "condition";
2024-11-28 13:16:44 +00:00
column: string;
operation: WhereOperation;
value: ConditionValue;
}
| {
2024-12-13 15:24:33 +00:00
structureType: "nested";
2024-11-28 16:53:22 +00:00
invert?: boolean;
2024-12-14 14:44:17 +00:00
conditions: Array<ConditionStructure>;
2024-11-28 16:53:22 +00:00
}
) & {
concat: WhereType;
structureType: "condition" | "nested";
};
2024-11-26 14:00:21 +00:00
2024-11-28 13:16:44 +00:00
export type ConditionValue = FieldType | Array<FieldType> | { start: FieldType; end: FieldType };
2024-11-26 14:00:21 +00:00
export type FieldType = number | string | Date | boolean;
2024-11-28 16:53:22 +00:00
export type WhereType = "OR" | "AND" | "_"; // _ represents initial where in (sub-)query
2024-11-26 14:00:21 +00: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 16:41:26 +00: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 14:00:21 +00:00
2024-12-13 15:24:33 +00:00
export type OrderByStructure = {
column: string;
order: OrderByType;
};
export type OrderByType = "ASC" | "DESC";
2024-12-14 14:44:17 +00:00
export const exampleQuery: DynamicQueryStructure = {
2024-11-26 14:00:21 +00:00
select: ["firstname", "lastname"],
table: "member",
2024-11-28 13:16:44 +00:00
where: [
{
2024-11-28 16:53:22 +00:00
structureType: "condition",
concat: "_",
2024-11-28 13:16:44 +00:00
column: "mail",
operation: "endsWith",
value: "@gmail.com",
2024-11-26 14:00:21 +00:00
},
2024-11-28 13:16:44 +00:00
{
2024-11-28 16:53:22 +00:00
structureType: "nested",
concat: "AND",
2024-12-14 14:44:17 +00:00
conditions: [
2024-11-28 13:16:44 +00:00
{
2024-11-28 16:53:22 +00:00
structureType: "condition",
concat: "_",
2024-11-28 13:16:44 +00:00
column: "firstname",
operation: "startsWith",
value: "J",
},
{
2024-11-28 16:53:22 +00:00
structureType: "condition",
concat: "OR",
2024-11-28 13:16:44 +00:00
column: "lastname",
operation: "startsWith",
value: "K",
},
],
},
],
2024-11-26 14:00:21 +00:00
join: [
{
select: "*",
2024-11-27 14:01:31 +00:00
table: "communication",
foreignColumn: "sendNewsletter",
2024-11-26 14:00:21 +00:00
},
2024-11-28 16:53:22 +00:00
{
select: "*",
table: "membership",
foreignColumn: "memberships",
join: [
{
select: "*",
table: "membership_status",
foreignColumn: "status",
where: [
{
structureType: "condition",
concat: "_",
column: "status",
operation: "eq",
value: "aktiv",
},
],
},
],
},
2024-11-26 14:00:21 +00:00
],
2024-12-13 15:24:33 +00:00
orderBy: [
{
column: "firstname",
order: "ASC",
},
{
column: "lastname",
order: "ASC",
},
],
2024-11-26 14:00:21 +00:00
};