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

83 lines
1.8 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-11-26 14:00:21 +00:00
orderBy?: { [key: string]: "ASC" | "DESC" };
}
2024-11-28 13:16:44 +00:00
export type ConditionStructure =
| {
type: WhereType;
column: string;
operation: WhereOperation;
value: ConditionValue;
}
| {
type: WhereType;
condition: Array<ConditionStructure>;
};
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 13:16:44 +00:00
export type WhereType = "OR" | "AND" | "_";
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
| "endsWith"; // Ends with
const exampleQuery: DynamicQueryStructure = {
select: ["firstname", "lastname"],
table: "member",
2024-11-28 13:16:44 +00:00
where: [
{
type: "_",
column: "mail",
operation: "endsWith",
value: "@gmail.com",
2024-11-26 14:00:21 +00:00
},
2024-11-28 13:16:44 +00:00
{
type: "AND",
condition: [
{
type: "_",
column: "firstname",
operation: "startsWith",
value: "J",
},
{
type: "OR",
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
},
],
orderBy: {
firstname: "ASC",
lastname: "ASC",
},
};