@lakeql/query-builder 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.d.mts +99 -15
  2. package/package.json +10 -1
package/dist/index.d.mts CHANGED
@@ -2,10 +2,20 @@ import { GraphQLResolveInfo } from "graphql";
2
2
  import { CompiledQuery, ExpressionBuilder, ExpressionWrapper, SelectExpression, SqlBool } from "kysely";
3
3
 
4
4
  //#region src/index.d.ts
5
+ /**
6
+ * Logical operators for combining filter conditions.
7
+ */
5
8
  declare enum WhereOperator {
6
9
  AND = "and",
7
10
  OR = "or"
8
11
  }
12
+ /**
13
+ * Comparison operators available for filtering a single field.
14
+ * Only one operator should be set per FieldOptions object.
15
+ *
16
+ * String operators compare the field value against the provided string.
17
+ * Boolean operators (`is`/`isNot`) check for equality with `true`/`false`.
18
+ */
9
19
  interface FieldOptions {
10
20
  eq?: string;
11
21
  neq?: string;
@@ -20,15 +30,32 @@ interface FieldOptions {
20
30
  is?: boolean;
21
31
  isNot?: boolean;
22
32
  }
33
+ /**
34
+ * Defines the sort order for a query result.
35
+ *
36
+ * @template TableDefinition - The table type used to constrain the `field` to valid column names.
37
+ */
23
38
  interface SortInput<TableDefinition> {
24
39
  field: SelectExpression<KyselyDatabase<TableDefinition>, keyof KyselyDatabase<TableDefinition>>;
25
40
  direction: string;
26
41
  }
42
+ /**
43
+ * Controls offset-based pagination.
44
+ * When `limit` is not specified, defaults to 100 rows.
45
+ */
27
46
  interface PagingInput {
28
47
  limit?: number;
29
48
  offset?: number;
30
49
  }
50
+ /**
51
+ * A single field condition — a record where the key is the field name
52
+ * and the value is a [FieldOptions](#field-options) object describing the comparison.
53
+ */
31
54
  type Field = Record<string, FieldOptions>;
55
+ /**
56
+ * A recursive filter structure that can contain AND/OR groups,
57
+ * each holding field conditions or further nested groups.
58
+ */
32
59
  type Where = Partial<Record<WhereOperator, (Where | Field)[]>>;
33
60
  /**
34
61
  * KyselyDatabase is our generic interface which simulates
@@ -38,37 +65,74 @@ interface KyselyDatabase<TableDefinition> {
38
65
  tablename: TableDefinition;
39
66
  }
40
67
  /**
41
- * We have to pass `TableDefinition` since we use some
42
- * kysely types e.g. to get the select fields
43
- * i don't think this is required, but it seems to be the right solution,
44
- * because we don't have to use `@ts-ignore` or `@ts-expect-error`
68
+ * Parameters for generating a Trino-compatible SQL query from GraphQL resolve info.
69
+ *
70
+ * @template TableDefinition - The table type definition used by Kysely for type-safe column references.
45
71
  */
46
72
  interface GenerateQueryProps<TableDefinition> {
73
+ /** The Trino catalog name (e.g. "hive"). */
47
74
  catalog: string;
75
+ /** The Trino schema name (e.g. "sales"). */
48
76
  schema: string;
77
+ /** The table name to query. */
49
78
  table: string;
79
+ /** The columns to select, derived from GraphQL field selections. */
50
80
  selectFields: SelectExpression<KyselyDatabase<TableDefinition>, keyof KyselyDatabase<TableDefinition>>[];
81
+ /** The user-provided filter query (WHERE clause). */
51
82
  userQuery: Where;
83
+ /** Pagination parameters (limit and offset). */
52
84
  paging?: PagingInput;
85
+ /** Sort order definitions. */
53
86
  sorting: SortInput<TableDefinition>[];
87
+ /** Maps GraphQL field names to database column names. */
54
88
  transformFields?: Record<string, string>;
89
+ /** Fields that should be wrapped in `to_unixtime()` for date conversion. */
55
90
  dateFields?: string[];
56
91
  }
92
+ /**
93
+ * Parameters for building a single field comparison expression.
94
+ */
95
+ interface GetFieldQueryProps<TableDefinition> {
96
+ /** The Kysely expression builder for constructing SQL expressions. */
97
+ eb: ExpressionBuilder<KyselyDatabase<TableDefinition>, "tablename">;
98
+ /** The column name to filter on. */
99
+ fieldName: string;
100
+ /** The comparison operator (eq, neq, lt, like, etc.). */
101
+ operator: string;
102
+ /** The value to compare against. */
103
+ value: unknown;
104
+ }
105
+ /**
106
+ * Builds a single field comparison expression for use in WHERE clauses.
107
+ *
108
+ * @param props - The field query parameters.
109
+ * @returns A Kysely expression wrapper representing the comparison.
110
+ */
57
111
  declare function getFieldQuery<TableDefinition>({
58
112
  eb,
59
113
  fieldName,
60
114
  operator,
61
115
  value
62
- }: {
63
- eb: ExpressionBuilder<KyselyDatabase<TableDefinition>, "tablename">;
64
- fieldName: string;
65
- operator: string;
66
- value: unknown;
67
- }): ExpressionWrapper<KyselyDatabase<TableDefinition>, "tablename", SqlBool>;
116
+ }: GetFieldQueryProps<TableDefinition>): ExpressionWrapper<KyselyDatabase<TableDefinition>, "tablename", SqlBool>;
68
117
  /**
69
- * Normalizes filter objects by ensuring each field is in its own object within and/or arrays
118
+ * Normalizes filter objects by ensuring each field is in its own object within and/or arrays.
119
+ *
120
+ * Splits multi-field filter objects into separate entries so the query builder
121
+ * can process each field condition independently.
122
+ *
123
+ * @param filter - The filter object to normalize.
124
+ * @returns A normalized filter with individual field entries.
70
125
  */
71
126
  declare function normalizeFilter(filter: Where): Where;
127
+ /**
128
+ * Wraps bare field-level filters into an AND-rooted structure.
129
+ *
130
+ * If the query already has a single `and` or `or` key at the root, it is returned as-is.
131
+ * Otherwise, all entries are combined under an `and` key.
132
+ *
133
+ * @param userQuery - The user-provided filter query.
134
+ * @returns A normalized query with a logical operator at the root.
135
+ */
72
136
  declare function normalizeUserQuery(userQuery: Where): Where;
73
137
  declare function generateQuery<TableDefinition>({
74
138
  catalog,
@@ -83,11 +147,31 @@ declare function generateQuery<TableDefinition>({
83
147
  }: GenerateQueryProps<TableDefinition>): CompiledQuery<{
84
148
  total_records: number;
85
149
  }>;
150
+ /**
151
+ * Parameters for formatting a compiled SQL query.
152
+ */
153
+ interface FormatQueryProps<T> {
154
+ /** The compiled Kysely query to format. */
155
+ query: CompiledQuery<T>;
156
+ }
157
+ /**
158
+ * Formats a compiled SQL query for readability.
159
+ *
160
+ * Applies uppercase keywords, PostgreSQL dialect, and interpolates parameters.
161
+ *
162
+ * @param props - The format query parameters.
163
+ * @returns The formatted SQL string with interpolated parameters.
164
+ */
86
165
  declare function formatQuery<T>({
87
166
  query
88
- }: {
89
- query: CompiledQuery<T>;
90
- }): string;
167
+ }: FormatQueryProps<T>): string;
168
+ /**
169
+ * Extracts selected field names from a GraphQL resolve info object.
170
+ *
171
+ * @param graphqlInfo - The GraphQL resolve info from the resolver.
172
+ * @param withNodes - When true, looks inside the `nodes` selection (for Connection types).
173
+ * @returns An array of field names to use as SELECT columns.
174
+ */
91
175
  declare function getSelectFields<T>(graphqlInfo: GraphQLResolveInfo, withNodes?: boolean): GenerateQueryProps<T>["selectFields"];
92
176
  //#endregion
93
- export { Field, FieldOptions, PagingInput, SortInput, Where, WhereOperator, formatQuery, generateQuery, getFieldQuery, getSelectFields, normalizeFilter, normalizeUserQuery };
177
+ export { Field, FieldOptions, FormatQueryProps, GenerateQueryProps, GetFieldQueryProps, PagingInput, SortInput, Where, WhereOperator, formatQuery, generateQuery, getFieldQuery, getSelectFields, normalizeFilter, normalizeUserQuery };
package/package.json CHANGED
@@ -1,6 +1,15 @@
1
1
  {
2
2
  "name": "@lakeql/query-builder",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
+ "description": "Kysely-based SQL query builder that translates GraphQL queries into Trino SQL",
5
+ "keywords": [
6
+ "graphql",
7
+ "kysely",
8
+ "lakeql",
9
+ "query-builder",
10
+ "sql",
11
+ "trino"
12
+ ],
4
13
  "homepage": "https://lakeql.dev",
5
14
  "license": "Apache-2.0",
6
15
  "author": {