@hypequery/clickhouse 0.0.0-canary-20260306132943

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 (142) hide show
  1. package/LICENSE +201 -0
  2. package/README-CLI.md +123 -0
  3. package/README.md +316 -0
  4. package/dist/cli/bin.js +285 -0
  5. package/dist/cli/generate-types.d.ts +5 -0
  6. package/dist/cli/generate-types.js +218 -0
  7. package/dist/cli/index.d.ts +2 -0
  8. package/dist/cli/index.js +2 -0
  9. package/dist/core/cache/cache-manager.d.ts +4 -0
  10. package/dist/core/cache/cache-manager.d.ts.map +1 -0
  11. package/dist/core/cache/cache-manager.js +176 -0
  12. package/dist/core/cache/controller.d.ts +15 -0
  13. package/dist/core/cache/controller.d.ts.map +1 -0
  14. package/dist/core/cache/controller.js +58 -0
  15. package/dist/core/cache/key.d.ts +11 -0
  16. package/dist/core/cache/key.d.ts.map +1 -0
  17. package/dist/core/cache/key.js +26 -0
  18. package/dist/core/cache/providers/memory-lru.d.ts +31 -0
  19. package/dist/core/cache/providers/memory-lru.d.ts.map +1 -0
  20. package/dist/core/cache/providers/memory-lru.js +156 -0
  21. package/dist/core/cache/providers/noop.d.ts +7 -0
  22. package/dist/core/cache/providers/noop.d.ts.map +1 -0
  23. package/dist/core/cache/providers/noop.js +11 -0
  24. package/dist/core/cache/runtime-context.d.ts +30 -0
  25. package/dist/core/cache/runtime-context.d.ts.map +1 -0
  26. package/dist/core/cache/runtime-context.js +58 -0
  27. package/dist/core/cache/serialization.d.ts +6 -0
  28. package/dist/core/cache/serialization.d.ts.map +1 -0
  29. package/dist/core/cache/serialization.js +166 -0
  30. package/dist/core/cache/types.d.ts +52 -0
  31. package/dist/core/cache/types.d.ts.map +1 -0
  32. package/dist/core/cache/types.js +1 -0
  33. package/dist/core/cache/utils.d.ts +9 -0
  34. package/dist/core/cache/utils.d.ts.map +1 -0
  35. package/dist/core/cache/utils.js +30 -0
  36. package/dist/core/connection.d.ts +112 -0
  37. package/dist/core/connection.d.ts.map +1 -0
  38. package/dist/core/connection.js +150 -0
  39. package/dist/core/cross-filter.d.ts +73 -0
  40. package/dist/core/cross-filter.d.ts.map +1 -0
  41. package/dist/core/cross-filter.js +142 -0
  42. package/dist/core/env/auto-client.browser.d.ts +3 -0
  43. package/dist/core/env/auto-client.browser.d.ts.map +1 -0
  44. package/dist/core/env/auto-client.browser.js +3 -0
  45. package/dist/core/env/auto-client.d.ts +9 -0
  46. package/dist/core/env/auto-client.d.ts.map +1 -0
  47. package/dist/core/env/auto-client.js +21 -0
  48. package/dist/core/features/aggregations.d.ts +98 -0
  49. package/dist/core/features/aggregations.d.ts.map +1 -0
  50. package/dist/core/features/aggregations.js +36 -0
  51. package/dist/core/features/analytics.d.ts +81 -0
  52. package/dist/core/features/analytics.d.ts.map +1 -0
  53. package/dist/core/features/analytics.js +45 -0
  54. package/dist/core/features/cross-filtering.d.ts +11 -0
  55. package/dist/core/features/cross-filtering.d.ts.map +1 -0
  56. package/dist/core/features/cross-filtering.js +90 -0
  57. package/dist/core/features/executor.d.ts +21 -0
  58. package/dist/core/features/executor.d.ts.map +1 -0
  59. package/dist/core/features/executor.js +146 -0
  60. package/dist/core/features/filtering.d.ts +99 -0
  61. package/dist/core/features/filtering.d.ts.map +1 -0
  62. package/dist/core/features/filtering.js +118 -0
  63. package/dist/core/features/joins.d.ts +26 -0
  64. package/dist/core/features/joins.d.ts.map +1 -0
  65. package/dist/core/features/joins.js +17 -0
  66. package/dist/core/features/query-modifiers.d.ts +116 -0
  67. package/dist/core/features/query-modifiers.d.ts.map +1 -0
  68. package/dist/core/features/query-modifiers.js +51 -0
  69. package/dist/core/formatters/sql-formatter.d.ts +9 -0
  70. package/dist/core/formatters/sql-formatter.d.ts.map +1 -0
  71. package/dist/core/formatters/sql-formatter.js +131 -0
  72. package/dist/core/join-relationships.d.ts +51 -0
  73. package/dist/core/join-relationships.d.ts.map +1 -0
  74. package/dist/core/join-relationships.js +54 -0
  75. package/dist/core/query-builder.d.ts +236 -0
  76. package/dist/core/query-builder.d.ts.map +1 -0
  77. package/dist/core/query-builder.js +495 -0
  78. package/dist/core/tests/index.d.ts +2 -0
  79. package/dist/core/tests/index.d.ts.map +1 -0
  80. package/dist/core/tests/index.js +1 -0
  81. package/dist/core/tests/integration/setup.d.ts +48 -0
  82. package/dist/core/tests/integration/setup.d.ts.map +1 -0
  83. package/dist/core/tests/integration/setup.js +349 -0
  84. package/dist/core/tests/integration/test-config.d.ts +15 -0
  85. package/dist/core/tests/integration/test-config.d.ts.map +1 -0
  86. package/dist/core/tests/integration/test-config.js +14 -0
  87. package/dist/core/tests/integration/test-data.json +190 -0
  88. package/dist/core/tests/test-utils.d.ts +44 -0
  89. package/dist/core/tests/test-utils.d.ts.map +1 -0
  90. package/dist/core/tests/test-utils.js +65 -0
  91. package/dist/core/types/builder-state.d.ts +27 -0
  92. package/dist/core/types/builder-state.d.ts.map +1 -0
  93. package/dist/core/types/builder-state.js +1 -0
  94. package/dist/core/types/select-types.d.ts +33 -0
  95. package/dist/core/types/select-types.d.ts.map +1 -0
  96. package/dist/core/types/select-types.js +1 -0
  97. package/dist/core/types/type-helpers.d.ts +5 -0
  98. package/dist/core/types/type-helpers.d.ts.map +1 -0
  99. package/dist/core/types/type-helpers.js +1 -0
  100. package/dist/core/utils/logger.d.ts +43 -0
  101. package/dist/core/utils/logger.d.ts.map +1 -0
  102. package/dist/core/utils/logger.js +104 -0
  103. package/dist/core/utils/predicate-builder.d.ts +33 -0
  104. package/dist/core/utils/predicate-builder.d.ts.map +1 -0
  105. package/dist/core/utils/predicate-builder.js +95 -0
  106. package/dist/core/utils/sql-expressions.d.ts +77 -0
  107. package/dist/core/utils/sql-expressions.d.ts.map +1 -0
  108. package/dist/core/utils/sql-expressions.js +54 -0
  109. package/dist/core/utils/streaming-helpers.d.ts +2 -0
  110. package/dist/core/utils/streaming-helpers.d.ts.map +1 -0
  111. package/dist/core/utils/streaming-helpers.js +137 -0
  112. package/dist/core/utils.d.ts +3 -0
  113. package/dist/core/utils.d.ts.map +1 -0
  114. package/dist/core/utils.js +29 -0
  115. package/dist/core/validators/filter-validator.d.ts +9 -0
  116. package/dist/core/validators/filter-validator.d.ts.map +1 -0
  117. package/dist/core/validators/filter-validator.js +19 -0
  118. package/dist/core/validators/value-validator.d.ts +7 -0
  119. package/dist/core/validators/value-validator.d.ts.map +1 -0
  120. package/dist/core/validators/value-validator.js +47 -0
  121. package/dist/formatters/index.d.ts +2 -0
  122. package/dist/formatters/index.d.ts.map +1 -0
  123. package/dist/formatters/index.js +1 -0
  124. package/dist/index.d.ts +20 -0
  125. package/dist/index.d.ts.map +1 -0
  126. package/dist/index.js +21 -0
  127. package/dist/types/base.d.ts +50 -0
  128. package/dist/types/base.d.ts.map +1 -0
  129. package/dist/types/base.js +1 -0
  130. package/dist/types/clickhouse-types.d.ts +17 -0
  131. package/dist/types/clickhouse-types.d.ts.map +1 -0
  132. package/dist/types/clickhouse-types.js +1 -0
  133. package/dist/types/filters.d.ts +53 -0
  134. package/dist/types/filters.d.ts.map +1 -0
  135. package/dist/types/filters.js +1 -0
  136. package/dist/types/index.d.ts +5 -0
  137. package/dist/types/index.d.ts.map +1 -0
  138. package/dist/types/index.js +4 -0
  139. package/dist/types/schema.d.ts +19 -0
  140. package/dist/types/schema.d.ts.map +1 -0
  141. package/dist/types/schema.js +1 -0
  142. package/package.json +90 -0
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are advised to review the applicable
150
+ documentation for the specific language governing permissions and
151
+ limitations under each license.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2024 hypequery
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README-CLI.md ADDED
@@ -0,0 +1,123 @@
1
+ # hypequery TypeScript Generator
2
+
3
+ This tool automatically generates TypeScript type definitions from your ClickHouse database schema.
4
+
5
+ ## Installation
6
+
7
+ The TypeScript generator is included with the `@hypequery/clickhouse` package:
8
+
9
+ ```bash
10
+ npm install @hypequery/clickhouse
11
+ ```
12
+
13
+ ## Quick Start
14
+
15
+ Generate TypeScript types for your ClickHouse database:
16
+
17
+ ```bash
18
+ npx hypequery-generate-types
19
+ ```
20
+
21
+ This will:
22
+ 1. Connect to your ClickHouse database using environment variables
23
+ 2. Introspect all tables in your database
24
+ 3. Generate TypeScript definitions in `./generated-schema.ts`
25
+
26
+ ## Example Usage
27
+
28
+ **Generate types with a custom output path:**
29
+
30
+ ```bash
31
+ npx hypequery-generate-types ./src/types/db-schema.ts
32
+ ```
33
+
34
+ **Specify database connection directly:**
35
+
36
+ ```bash
37
+ CLICKHOUSE_HOST=http://clickhouse.example.com:8123 \
38
+ CLICKHOUSE_USER=myuser \
39
+ CLICKHOUSE_PASSWORD=mypassword \
40
+ CLICKHOUSE_DATABASE=analytics \
41
+ npx hypequery-generate-types
42
+ ```
43
+
44
+ ## Configuration
45
+
46
+ Configure the connection using environment variables:
47
+
48
+ | Variable | Description | Default |
49
+ |----------|-------------|---------|
50
+ | `CLICKHOUSE_HOST` | ClickHouse server URL | `http://localhost:8123` |
51
+ | `CLICKHOUSE_USER` | ClickHouse username | `default` |
52
+ | `CLICKHOUSE_PASSWORD` | ClickHouse password | _(empty)_ |
53
+ | `CLICKHOUSE_DATABASE` | ClickHouse database name | `default` |
54
+
55
+ You can set these in:
56
+ - A `.env` file in your project root
57
+ - Your system environment
58
+ - Directly when running the command
59
+
60
+ ## Using Generated Types
61
+
62
+ Import the generated types in your code:
63
+
64
+ ```typescript
65
+ import { createQueryBuilder } from '@hypequery/clickhouse';
66
+ import { IntrospectedSchema } from './generated-schema';
67
+
68
+ // Create a type-safe query builder
69
+ const db = createQueryBuilder<IntrospectedSchema>({
70
+ host: process.env.CLICKHOUSE_HOST,
71
+ username: process.env.CLICKHOUSE_USER,
72
+ password: process.env.CLICKHOUSE_PASSWORD,
73
+ database: process.env.CLICKHOUSE_DATABASE,
74
+ });
75
+
76
+ // Enjoy complete type safety!
77
+ const results = await db
78
+ .table('users') // TypeScript checks table exists
79
+ .select(['id', 'name']) // TypeScript checks columns exist
80
+ .where('id', 'gt', 10) // TypeScript validates types
81
+ .execute();
82
+
83
+ // Results are properly typed
84
+ results.forEach(user => {
85
+ console.log(user.name); // TypeScript knows this is a string
86
+ });
87
+ ```
88
+
89
+ ## Adding to Your Workflow
90
+
91
+ Add it to your npm scripts:
92
+
93
+ ```json
94
+ {
95
+ "scripts": {
96
+ "generate-types": "hypequery-generate-types ./src/types/db-schema.ts",
97
+ "prebuild": "npm run generate-types",
98
+ "build": "tsc"
99
+ }
100
+ }
101
+ ```
102
+
103
+ ## Troubleshooting
104
+
105
+ If you encounter issues:
106
+
107
+ 1. **Connection problems**
108
+ - Make sure ClickHouse is running and accessible
109
+ - Check your firewall settings
110
+
111
+ 2. **Authentication failures**
112
+ - Verify your username and password
113
+ - Ensure the user has sufficient permissions
114
+
115
+ 3. **Missing tables**
116
+ - Confirm you're connecting to the correct database
117
+ - Verify the tables exist in your ClickHouse instance
118
+
119
+ For more help, run:
120
+
121
+ ```bash
122
+ npx hypequery-generate-types --help
123
+ ```
package/README.md ADDED
@@ -0,0 +1,316 @@
1
+ <div align="center">
2
+ <h1>@hypequery/clickhouse</h1>
3
+ <p>A typescript-first library for building type-safe dashboards with ClickHouse</p>
4
+
5
+ [![GitHub license](https://img.shields.io/github/license/hypequery/hypequery)](https://github.com/hypequery/hypequery/blob/main/LICENSE)
6
+ [![npm version](https://badge.fury.io/js/@hypequery%2Fclickhouse.svg)](https://badge.fury.io/js/@hypequery%2Fclickhouse)
7
+ [![GitHub stars](https://img.shields.io/github/stars/hypequery/hypequery)](https://github.com/hypequery/hypequery/stargazers)
8
+ </div>
9
+
10
+
11
+ ## Overview
12
+
13
+ hypequery is a typescript-first query builder for ClickHouse designed specifically for building type-safe analytics dashboards. Unlike generic SQL query builders, hypequery understands your ClickHouse schema and provides full type checking, making it ideal for data-intensive applications.
14
+
15
+ ## Features
16
+
17
+ - 🎯 **Type-Safe**: Full TypeScript support with types from your ClickHouse schema
18
+ - 🚀 **Performant**: Built for real-time analytics with optimized query generation
19
+ - 🔍 **Cross Filtering**: Powerful cross-filtering capabilities for interactive dashboards
20
+ - 🛠️ **Developer Friendly**: Fluent API design for an intuitive development experience
21
+ - 📱 **Platform Agnostic**: Works in both Node.js and browser environments
22
+ - 🔄 **Schema Generation**: CLI tool to generate TypeScript types from your ClickHouse schema
23
+
24
+ ## Installation
25
+
26
+ This library requires one of the following ClickHouse clients as a peer dependency:
27
+
28
+ ### For Node.js environments
29
+ ```bash
30
+ npm install @hypequery/clickhouse
31
+ ```
32
+
33
+ ### For browser/universal environments
34
+ ```bash
35
+ npm install @hypequery/clickhouse @clickhouse/client-web
36
+ ```
37
+
38
+ **Note**: The library supports multiple client selection strategies:
39
+ - **Manual injection**: Explicitly provide a client instance (required for browser environments)
40
+ - **Auto-detection**: Automatically selects the client for Node.js environments
41
+
42
+ ## Quick Start
43
+
44
+ ### Node.js Environments
45
+
46
+ ```typescript
47
+ import { createQueryBuilder } from '@hypequery/clickhouse';
48
+ import type { IntrospectedSchema } from './generated-schema';
49
+
50
+ // Initialize the query builder
51
+ const db = createQueryBuilder<IntrospectedSchema>({
52
+ host: 'your-clickhouse-host',
53
+ username: 'default',
54
+ password: 'your-password',
55
+ database: 'default'
56
+ });
57
+
58
+ // Build and execute a query
59
+ const results = await db
60
+ .table('trips')
61
+ .select(['pickup_datetime', 'dropoff_datetime', 'total_amount'])
62
+ .where('total_amount', '>', 50)
63
+ .orderBy('pickup_datetime', 'DESC')
64
+ .limit(10)
65
+ .execute();
66
+ ```
67
+
68
+ ### Browser Environments
69
+
70
+ ```typescript
71
+ import { createQueryBuilder } from '@hypequery/clickhouse';
72
+ import { createClient } from '@clickhouse/client-web';
73
+ import type { IntrospectedSchema } from './generated-schema';
74
+
75
+ // Create the ClickHouse client explicitly
76
+ const client = createClient({
77
+ host: 'your-clickhouse-host',
78
+ username: 'default',
79
+ password: '',
80
+ database: 'default'
81
+ });
82
+
83
+ // Initialize the query builder with the client
84
+ const db = createQueryBuilder<IntrospectedSchema>({
85
+ client // Explicitly provide the client
86
+ });
87
+
88
+ // Build and execute a query
89
+ const results = await db
90
+ .table('trips')
91
+ .select(['pickup_datetime', 'dropoff_datetime', 'total_amount'])
92
+ .where('total_amount', '>', 50)
93
+ .orderBy('pickup_datetime', 'DESC')
94
+ .limit(10)
95
+ .execute();
96
+ ```
97
+
98
+ ## Caching (Experimental)
99
+
100
+ The query builder can cache results with deterministic keys, stale-while-revalidate behavior, and pluggable providers.
101
+
102
+ ```typescript
103
+ import { createQueryBuilder, MemoryCacheProvider } from '@hypequery/clickhouse';
104
+
105
+ const db = createQueryBuilder<IntrospectedSchema>({
106
+ host: 'your-clickhouse-host',
107
+ username: 'default',
108
+ password: '',
109
+ database: 'default',
110
+ cache: {
111
+ mode: 'stale-while-revalidate',
112
+ ttlMs: 2_000,
113
+ staleTtlMs: 30_000,
114
+ provider: new MemoryCacheProvider({ maxEntries: 5_000 })
115
+ }
116
+ });
117
+
118
+ const rows = await db
119
+ .table('users')
120
+ .select(['id', 'email'])
121
+ .where('active', '=', true)
122
+ .cache({ tags: ['users'] })
123
+ .execute();
124
+
125
+ // Programmatic invalidation
126
+ await db.cache.invalidateTags(['users']);
127
+ ```
128
+
129
+ Use `.cache()` to attach defaults to a fluent chain, pass `execute({ cache: { ... } })` for one-off overrides, or call `db.cache.*` for manual invalidation. For a deep dive on cache modes, invalidation, advanced serialization, and bring-your-own-provider recipes (Redis/Upstash, compression, etc.), see the [Caching guide](https://hypequery.com/docs/features/caching).
130
+
131
+ ## Schema Generation
132
+
133
+ hypequery provides a CLI tool to generate TypeScript types from your ClickHouse schema:
134
+
135
+ ```bash
136
+ # Install globally (optional)
137
+ npm install -g @hypequery/clickhouse
138
+
139
+ # Generate schema types
140
+ npx hypequery-generate-types --host your-clickhouse-host --database your-database
141
+ ```
142
+
143
+ This creates a `generated-schema.ts` file that you can import in your application:
144
+
145
+ ```typescript
146
+ import { createQueryBuilder } from '@hypequery/clickhouse';
147
+ import type { IntrospectedSchema } from './generated-schema';
148
+
149
+ const db = createQueryBuilder<IntrospectedSchema>({
150
+ // connection details
151
+ });
152
+ ```
153
+
154
+ ## Core Features
155
+
156
+ ### Type-Safe Queries
157
+
158
+ hypequery provides full TypeScript support, ensuring your queries are type-safe:
159
+
160
+ ```typescript
161
+ // Column names are type-checked
162
+ const query = db.table('trips')
163
+ .select(['pickup_datetime', 'total_amount'])
164
+ .where('total_amount', 'gt', 50)
165
+ .execute();
166
+
167
+ // Type error if column doesn't exist
168
+ db.table('trips').select(['non_existent_column']); // TypeScript error
169
+ ```
170
+
171
+ ### Cross Filtering
172
+
173
+ Implement interactive dashboards with cross-filtering support:
174
+
175
+ ```typescript
176
+ import { CrossFilter } from '@hypequery/clickhouse';
177
+
178
+ // Create a filter
179
+ const filter = new CrossFilter()
180
+ .add({
181
+ column: 'pickup_datetime',
182
+ operator: 'gte',
183
+ value: '2024-01-01'
184
+ })
185
+ .add({
186
+ column: 'total_amount',
187
+ operator: 'gt',
188
+ value: 20
189
+ });
190
+
191
+ // Apply to multiple queries
192
+ const query1 = db.table('trips')
193
+ .applyCrossFilters(filter)
194
+ .execute();
195
+
196
+ const query2 = db.table('drivers')
197
+ .applyCrossFilters(filter)
198
+ .execute();
199
+ ```
200
+
201
+ ### Advanced Queries
202
+
203
+ hypequery supports complex queries including joins, aggregations, and subqueries:
204
+
205
+ ```typescript
206
+ // Aggregations
207
+ const stats = await db.table('trips')
208
+ .avg('total_amount')
209
+ .max('trip_distance')
210
+ .count('trip_id')
211
+ .where('pickup_datetime', 'gte', '2024-01-01')
212
+ .execute();
213
+
214
+ // Joins
215
+ const tripsWithDrivers = await db.table('trips')
216
+ .select(['trips.trip_id', 'trips.total_amount', 'drivers.name'])
217
+ .join('drivers', 'trips.driver_id', 'drivers.id')
218
+ .execute();
219
+
220
+ // After joining, TypeScript understands the expanded scope
221
+ const tripsWithUsers = await db.table('trips')
222
+ .innerJoin('users', 'trips.user_id', 'users.id')
223
+ .select(['users.email', 'trips.trip_id'])
224
+ .where('users.email', 'like', '%@example.com')
225
+ .execute();
226
+
227
+ // Keep literal column inference with selectConst and reuse joined columns in ORDER BY / HAVING
228
+ const sortedTrips = await db.table('trips')
229
+ .innerJoin('users', 'trips.user_id', 'users.id')
230
+ .selectConst('users.email', 'trips.trip_id')
231
+ .groupBy(['users.email', 'trips.trip_id'])
232
+ .having('COUNT(*) > 1')
233
+ .orderBy('users.email', 'DESC')
234
+ .execute();
235
+
236
+ ```
237
+
238
+ `selectConst()` preserves literal column names (including aliases like `users.email`), which means TypeScript keeps those identifiers available for downstream `orderBy`, `groupBy`, and `having` calls.
239
+
240
+ **Benefits:**
241
+ - ✅ Works in all environments (Node.js, browser, bundlers)
242
+ - ✅ Explicit control over client configuration
243
+ - ✅ Required for browser environments (require() doesn't work in browsers)
244
+ - ✅ Synchronous API throughout
245
+
246
+ #### 2. Auto-Detection with Fallback (Node.js Environments Only)
247
+
248
+ ```typescript
249
+ const db = createQueryBuilder<IntrospectedSchema>({
250
+ host: 'your-clickhouse-host',
251
+ username: 'default',
252
+ password: '',
253
+ database: 'default'
254
+ });
255
+ ```
256
+
257
+
258
+ ## Testing
259
+
260
+ Run the fast feedback loop with:
261
+
262
+ ```bash
263
+ npm run test
264
+ ```
265
+
266
+ This command runs type checks + unit tests only. To exercise the ClickHouse-backed integration suite, copy `.env.test.example` to `.env.test`, point it at a ClickHouse instance, and run:
267
+
268
+ ```bash
269
+ npm run test:integration
270
+ ```
271
+
272
+ ## Versioning and Release Channels
273
+
274
+ hypequery follows semantic versioning and provides multiple release channels:
275
+
276
+ - **Latest**: Stable releases (`npm install @hypequery/clickhouse`)
277
+ - **Beta**: Pre-release versions (`npm install @hypequery/clickhouse@beta`)
278
+
279
+ ## Documentation
280
+
281
+ For detailed documentation and examples, visit our [documentation site](https://hypequery.com/docs).
282
+
283
+ - [Getting Started](https://hypequery.com/docs/installation)
284
+ - [Query Building](https://hypequery.com/docs/guides/query-building)
285
+ - [Filtering](https://hypequery.com/docs/guides/filtering)
286
+ - [API Reference](https://hypequery.com/docs/reference/api)
287
+
288
+
289
+ ## Troubleshooting
290
+
291
+ ### Common Issues
292
+
293
+ - **Connection Errors**: Ensure your ClickHouse server is running and accessible
294
+ - **CORS Issues**: Use a proxy server for browser environments
295
+ - **Type Errors**: Make sure to regenerate your schema types after schema changes
296
+ - **Client Not Found**: Make sure you have installed at least one of the required peer dependencies:
297
+ - `@clickhouse/client` (for Node.js environments)
298
+ - `@clickhouse/client-web` (for browser/universal environments)
299
+ - **Browser Auto-Detection**: Auto-detection doesn't work in browsers because `require()` calls don't work. Use manual injection instead.
300
+
301
+
302
+ ## License
303
+
304
+ This project is licensed under the Apache-2.0 License - see the [LICENSE](LICENSE) file for details.
305
+
306
+ ## Support
307
+
308
+ - 📚 [Documentation](https://hypequery.com/docs)
309
+ - 🐛 [Issue Tracker](https://github.com/hypequery/hypequery/issues)
310
+ - 💬 [Discussions](https://github.com/hypequery/hypequery/discussions)
311
+
312
+ ---
313
+
314
+ <div align="center">
315
+ <sub>Built with ❤️ by the hypequery team</sub>
316
+ </div>