@housekit/orm 0.1.1

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 (50) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +224 -0
  3. package/dist/builders/delete.d.ts +21 -0
  4. package/dist/builders/insert.d.ts +128 -0
  5. package/dist/builders/prepared.d.ts +11 -0
  6. package/dist/builders/select.d.ts +352 -0
  7. package/dist/builders/select.types.d.ts +76 -0
  8. package/dist/builders/update.d.ts +23 -0
  9. package/dist/client.d.ts +52 -0
  10. package/dist/codegen/zod.d.ts +4 -0
  11. package/dist/column.d.ts +76 -0
  12. package/dist/compiler.d.ts +27 -0
  13. package/dist/core.d.ts +6 -0
  14. package/dist/data-types.d.ts +150 -0
  15. package/dist/dictionary.d.ts +263 -0
  16. package/dist/engines.d.ts +558 -0
  17. package/dist/expressions.d.ts +72 -0
  18. package/dist/external.d.ts +177 -0
  19. package/dist/index.d.ts +187 -0
  20. package/dist/index.js +222 -0
  21. package/dist/logger.d.ts +8 -0
  22. package/dist/materialized-views.d.ts +271 -0
  23. package/dist/metadata.d.ts +33 -0
  24. package/dist/modules/aggregates.d.ts +205 -0
  25. package/dist/modules/array.d.ts +122 -0
  26. package/dist/modules/conditional.d.ts +110 -0
  27. package/dist/modules/conversion.d.ts +189 -0
  28. package/dist/modules/geo.d.ts +202 -0
  29. package/dist/modules/hash.d.ts +7 -0
  30. package/dist/modules/index.d.ts +12 -0
  31. package/dist/modules/json.d.ts +130 -0
  32. package/dist/modules/math.d.ts +28 -0
  33. package/dist/modules/string.d.ts +167 -0
  34. package/dist/modules/time.d.ts +154 -0
  35. package/dist/modules/types.d.ts +177 -0
  36. package/dist/modules/window.d.ts +27 -0
  37. package/dist/relational.d.ts +33 -0
  38. package/dist/relations.d.ts +15 -0
  39. package/dist/schema-builder.d.ts +172 -0
  40. package/dist/table.d.ts +172 -0
  41. package/dist/utils/background-batcher.d.ts +20 -0
  42. package/dist/utils/batch-transform.d.ts +20 -0
  43. package/dist/utils/binary-reader.d.ts +48 -0
  44. package/dist/utils/binary-serializer.d.ts +160 -0
  45. package/dist/utils/binary-worker-code.d.ts +1 -0
  46. package/dist/utils/binary-worker-pool.d.ts +76 -0
  47. package/dist/utils/binary-worker.d.ts +12 -0
  48. package/dist/utils/insert-processing.d.ts +23 -0
  49. package/dist/utils/lru-cache.d.ts +10 -0
  50. package/package.json +68 -0
@@ -0,0 +1,150 @@
1
+ import { ClickHouseColumn } from './column';
2
+ export type JsonValue = Record<string, any> | any[];
3
+ export declare const int8: (name: string) => ClickHouseColumn<number, true, false>;
4
+ export declare const int16: (name: string) => ClickHouseColumn<number, true, false>;
5
+ export declare const integer: (name: string) => ClickHouseColumn<number, true, false>;
6
+ export declare const int32: (name: string) => ClickHouseColumn<number, true, false>;
7
+ export declare const int64: (name: string) => ClickHouseColumn<number, true, false>;
8
+ /**
9
+ * ⚠️ Advanced: Very wide integer type (128-bit).
10
+ * Use only if you need to store numbers larger than 2^63-1.
11
+ * For most cases, `int64` is sufficient.
12
+ */
13
+ export declare const int128: (name: string) => ClickHouseColumn<number, true, false>;
14
+ /**
15
+ * ⚠️ Advanced: Extremely wide integer type (256-bit).
16
+ * Use only if you need to store numbers larger than 2^127-1.
17
+ * For most cases, `int64` is sufficient.
18
+ */
19
+ export declare const int256: (name: string) => ClickHouseColumn<number, true, false>;
20
+ export declare const uint8: (name: string) => ClickHouseColumn<number, true, false>;
21
+ export declare const uint16: (name: string) => ClickHouseColumn<number, true, false>;
22
+ export declare const uint32: (name: string) => ClickHouseColumn<number, true, false>;
23
+ export declare const uint64: (name: string) => ClickHouseColumn<number, true, false>;
24
+ /**
25
+ * ⚠️ Advanced: Very wide unsigned integer type (128-bit).
26
+ * Use only if you need to store numbers larger than 2^64-1.
27
+ * For most cases, `uint64` is sufficient.
28
+ */
29
+ export declare const uint128: (name: string) => ClickHouseColumn<number, true, false>;
30
+ /**
31
+ * ⚠️ Advanced: Extremely wide unsigned integer type (256-bit).
32
+ * Use only if you need to store numbers larger than 2^128-1.
33
+ * For most cases, `uint64` is sufficient.
34
+ */
35
+ export declare const uint256: (name: string) => ClickHouseColumn<number, true, false>;
36
+ export declare const float32: (name: string) => ClickHouseColumn<number, true, false>;
37
+ export declare const float: (name: string) => ClickHouseColumn<number, true, false>;
38
+ export declare const float64: (name: string) => ClickHouseColumn<number, true, false>;
39
+ /**
40
+ * ⚠️ Advanced: Brain Floating Point (16-bit).
41
+ * Primarily used for machine learning applications.
42
+ * Lower precision than `float32`. Use only if you know exactly why.
43
+ */
44
+ export declare const bfloat16: (name: string) => ClickHouseColumn<number, true, false>;
45
+ export declare const decimal: (name: string, precision?: number, scale?: number) => ClickHouseColumn<number, true, false>;
46
+ export declare const decimal32: (name: string, scale?: number) => ClickHouseColumn<number, true, false>;
47
+ export declare const decimal64: (name: string, scale?: number) => ClickHouseColumn<number, true, false>;
48
+ /**
49
+ * ⚠️ Advanced: High precision decimal.
50
+ * Use only if you need more precision than `decimal64`.
51
+ */
52
+ export declare const decimal128: (name: string, scale?: number) => ClickHouseColumn<number, true, false>;
53
+ /**
54
+ * ⚠️ Advanced: Extreme precision decimal.
55
+ * Use only if you need more precision than `decimal128`.
56
+ */
57
+ export declare const decimal256: (name: string, scale?: number) => ClickHouseColumn<number, true, false>;
58
+ export declare const text: (name: string) => ClickHouseColumn<string, true, false>;
59
+ export declare const string: (name: string) => ClickHouseColumn<string, true, false>;
60
+ export declare const fixedString: (name: string, length: number) => ClickHouseColumn<string, true, false>;
61
+ export declare const date: (name: string) => ClickHouseColumn<string | Date, true, false>;
62
+ export declare const date32: (name: string) => ClickHouseColumn<string | Date, true, false>;
63
+ export declare const timestamp: (name: string, timezone?: string) => ClickHouseColumn<string | Date, true, false>;
64
+ export declare const datetime: (name: string, timezone?: string) => ClickHouseColumn<string | Date, true, false>;
65
+ export declare const datetime64: (name: string, precision?: number, timezone?: string) => ClickHouseColumn<string | Date, true, false>;
66
+ export declare const boolean: (name: string) => ClickHouseColumn<boolean, true, false>;
67
+ export declare const bool: (name: string) => ClickHouseColumn<boolean, true, false>;
68
+ export declare const uuid: (name: string) => ClickHouseColumn<string, true, false>;
69
+ export declare const ipv4: (name: string) => ClickHouseColumn<string, true, false>;
70
+ export declare const ipv6: (name: string) => ClickHouseColumn<string, true, false>;
71
+ export declare const array: <T>(col: ClickHouseColumn<T>) => ClickHouseColumn<T[], true, false>;
72
+ export declare const tuple: (name: string, types: string[]) => ClickHouseColumn<any, true, false>;
73
+ export declare const map: (name: string, keyType?: string, valueType?: string) => ClickHouseColumn<Record<string, any>, true, false>;
74
+ export declare const nested: (name: string, fields: Record<string, string>) => ClickHouseColumn<any, true, false>;
75
+ export declare const nullable: <T, TNotNull extends boolean, TAutoGenerated extends boolean>(col: ClickHouseColumn<T, TNotNull, TAutoGenerated>) => ClickHouseColumn<T, false, TAutoGenerated>;
76
+ export declare const lowCardinality: <T, TNotNull extends boolean, TAutoGenerated extends boolean>(col: ClickHouseColumn<T, TNotNull, TAutoGenerated>) => ClickHouseColumn<T, TNotNull, TAutoGenerated>;
77
+ export declare const json: <TSchema = JsonValue>(name: string) => ClickHouseColumn<TSchema, false, false>;
78
+ /**
79
+ * ⚠️ Advanced: Dynamic typing (experimental/niche).
80
+ * Allows storing values of different types in the same column.
81
+ * This is still evolving in ClickHouse. Use with caution.
82
+ */
83
+ export declare const dynamic: (name: string, maxTypes?: number) => ClickHouseColumn<any, true, false>;
84
+ export declare const aggregateFunction: (name: string, funcName: string, ...argTypes: string[]) => ClickHouseColumn<any, true, false>;
85
+ export declare const simpleAggregateFunction: (name: string, funcName: string, argType: string) => ClickHouseColumn<any, true, false>;
86
+ export declare const point: (name: string) => ClickHouseColumn<[number, number], true, false>;
87
+ export declare const ring: (name: string) => ClickHouseColumn<[number, number][], true, false>;
88
+ export declare const polygon: (name: string) => ClickHouseColumn<[number, number][][], true, false>;
89
+ export declare const multiPolygon: (name: string) => ClickHouseColumn<[number, number][][][], true, false>;
90
+ export declare const varchar: (name: string, opts?: {
91
+ length?: number;
92
+ }) => ClickHouseColumn<string, true, false>;
93
+ export declare const enumType: (name: string, values: readonly string[]) => ClickHouseColumn<string, false, false>;
94
+ export interface TTLRule {
95
+ expression: string;
96
+ action?: 'DELETE' | 'TO DISK' | 'TO VOLUME';
97
+ target?: string;
98
+ }
99
+ export declare const ttl: {
100
+ /**
101
+ * Delete rows after a time interval
102
+ * @example ttl.delete(events.timestamp, { days: 30 })
103
+ */
104
+ delete: (column: ClickHouseColumn, interval: {
105
+ days?: number;
106
+ hours?: number;
107
+ months?: number;
108
+ years?: number;
109
+ }) => TTLRule;
110
+ /**
111
+ * Move rows to cold storage after a time interval
112
+ * @example ttl.toDisk(events.timestamp, { days: 7 }, 'cold_storage')
113
+ */
114
+ toDisk: (column: ClickHouseColumn, interval: {
115
+ days?: number;
116
+ hours?: number;
117
+ months?: number;
118
+ years?: number;
119
+ }, disk: string) => TTLRule;
120
+ /**
121
+ * Move rows to a volume after a time interval
122
+ * @example ttl.toVolume(events.timestamp, { days: 30 }, 'archive')
123
+ */
124
+ toVolume: (column: ClickHouseColumn, interval: {
125
+ days?: number;
126
+ hours?: number;
127
+ months?: number;
128
+ years?: number;
129
+ }, volume: string) => TTLRule;
130
+ /**
131
+ * Custom TTL expression
132
+ * @example ttl.custom('created_at + INTERVAL 1 YEAR')
133
+ */
134
+ custom: (expression: string) => TTLRule;
135
+ /**
136
+ * Combine multiple TTL rules (tiered storage)
137
+ * @example ttl.tiered(events.timestamp, [
138
+ * { days: 7, action: 'toDisk', target: 'cold' },
139
+ * { days: 30, action: 'delete' }
140
+ * ])
141
+ */
142
+ tiered: (column: ClickHouseColumn, tiers: Array<{
143
+ days?: number;
144
+ hours?: number;
145
+ months?: number;
146
+ years?: number;
147
+ action: "delete" | "toDisk" | "toVolume";
148
+ target?: string;
149
+ }>) => TTLRule[];
150
+ };
@@ -0,0 +1,263 @@
1
+ /**
2
+ * HouseKit Dictionary DSL - Type-Safe ClickHouse Dictionaries
3
+ *
4
+ * Dictionaries are ClickHouse's way of doing ultra-fast key-value lookups.
5
+ * They're loaded into memory and are much faster than JOINs for lookup tables.
6
+ *
7
+ * Standard ORMs often lack concept of dictionaries - this is a significant advantage for HouseKit.
8
+ */
9
+ import { type TableColumns } from './table';
10
+ import { type SQLExpression } from './expressions';
11
+ /**
12
+ * Dictionary source from ClickHouse table
13
+ */
14
+ export interface ClickHouseSource {
15
+ type: 'clickhouse';
16
+ /** Database name (optional, defaults to current) */
17
+ database?: string;
18
+ /** Table name */
19
+ table: string;
20
+ /** Custom query (alternative to table) */
21
+ query?: string;
22
+ /** Invalidate query - returns value that changes when dictionary should be reloaded */
23
+ invalidateQuery?: string;
24
+ /** Update field - only reload rows where this field changed */
25
+ updateField?: string;
26
+ }
27
+ /**
28
+ * Dictionary source from MySQL
29
+ */
30
+ export interface MySQLSource {
31
+ type: 'mysql';
32
+ host: string;
33
+ port: number;
34
+ database: string;
35
+ table: string;
36
+ user: string;
37
+ password: string;
38
+ query?: string;
39
+ invalidateQuery?: string;
40
+ }
41
+ /**
42
+ * Dictionary source from PostgreSQL
43
+ */
44
+ export interface PostgreSQLSource {
45
+ type: 'postgresql';
46
+ host: string;
47
+ port: number;
48
+ database: string;
49
+ table: string;
50
+ user: string;
51
+ password: string;
52
+ query?: string;
53
+ invalidateQuery?: string;
54
+ }
55
+ /**
56
+ * Dictionary source from HTTP endpoint
57
+ */
58
+ export interface HTTPSource {
59
+ type: 'http';
60
+ url: string;
61
+ format: 'TabSeparated' | 'CSV' | 'JSONEachRow' | string;
62
+ credentials?: {
63
+ user: string;
64
+ password: string;
65
+ };
66
+ headers?: Record<string, string>;
67
+ }
68
+ /**
69
+ * Dictionary source from executable
70
+ */
71
+ export interface ExecutableSource {
72
+ type: 'executable';
73
+ command: string;
74
+ format: 'TabSeparated' | 'CSV' | 'JSONEachRow' | string;
75
+ /** Send dictionary content to stdin for pool updates */
76
+ sendInitalTables?: boolean;
77
+ }
78
+ /**
79
+ * Dictionary source from file
80
+ */
81
+ export interface FileSource {
82
+ type: 'file';
83
+ path: string;
84
+ format: 'TabSeparated' | 'CSV' | 'JSONEachRow' | string;
85
+ }
86
+ /**
87
+ * Union of all dictionary source types
88
+ */
89
+ export type DictionarySource = ClickHouseSource | MySQLSource | PostgreSQLSource | HTTPSource | ExecutableSource | FileSource;
90
+ /**
91
+ * Flat layout - for small dictionaries with numeric keys (0 to N)
92
+ * Extremely fast but memory intensive for sparse keys
93
+ */
94
+ export interface FlatLayout {
95
+ type: 'flat';
96
+ /** Initial array size */
97
+ initialArraySize?: number;
98
+ /** Max array size - will fail if key exceeds this */
99
+ maxArraySize?: number;
100
+ }
101
+ /**
102
+ * Hashed layout - for dictionaries with any key type
103
+ * Good balance of speed and memory for most use cases
104
+ */
105
+ export interface HashedLayout {
106
+ type: 'hashed';
107
+ /** Pre-allocate memory for this many elements */
108
+ preallocate?: number;
109
+ }
110
+ /**
111
+ * Sparse hashed layout - uses less memory than hashed for sparse data
112
+ */
113
+ export interface SparseHashedLayout {
114
+ type: 'sparse_hashed';
115
+ preallocate?: number;
116
+ }
117
+ /**
118
+ * Complex key hashed - for dictionaries with composite keys
119
+ */
120
+ export interface ComplexKeyHashedLayout {
121
+ type: 'complex_key_hashed';
122
+ preallocate?: number;
123
+ }
124
+ /**
125
+ * Range hashed - for time-range lookups
126
+ * Key + validity range (start/end dates)
127
+ */
128
+ export interface RangeHashedLayout {
129
+ type: 'range_hashed';
130
+ /** Column containing range start */
131
+ rangeMin: string;
132
+ /** Column containing range end */
133
+ rangeMax: string;
134
+ }
135
+ /**
136
+ * Cache layout - LRU cache for very large dictionaries
137
+ * Only stores recently accessed values
138
+ */
139
+ export interface CacheLayout {
140
+ type: 'cache';
141
+ /** Cache size in cells */
142
+ sizeInCells: number;
143
+ }
144
+ /**
145
+ * IP Trie layout - optimized for IP address lookups
146
+ * Uses CIDR notation
147
+ */
148
+ export interface IPTrieLayout {
149
+ type: 'ip_trie';
150
+ }
151
+ /**
152
+ * Direct layout - stores data in a linear array indexed by key
153
+ */
154
+ export interface DirectLayout {
155
+ type: 'direct';
156
+ }
157
+ /**
158
+ * Union of all layout types
159
+ */
160
+ export type DictionaryLayout = FlatLayout | HashedLayout | SparseHashedLayout | ComplexKeyHashedLayout | RangeHashedLayout | CacheLayout | IPTrieLayout | DirectLayout;
161
+ /**
162
+ * Lifetime configuration for dictionary reload
163
+ */
164
+ export interface DictionaryLifetime {
165
+ /** Minimum seconds before checking for updates */
166
+ min: number;
167
+ /** Maximum seconds before forcing reload */
168
+ max: number;
169
+ }
170
+ /**
171
+ * Primary/ID key configuration
172
+ */
173
+ export interface DictionaryKey {
174
+ /** Key columns */
175
+ columns: string[];
176
+ /** Whether key is hierarchical */
177
+ isHierarchical?: boolean;
178
+ }
179
+ /**
180
+ * Full dictionary configuration
181
+ */
182
+ export interface DictionaryOptions<TCols extends TableColumns> {
183
+ /** Data source configuration */
184
+ source: DictionarySource;
185
+ /** Memory layout */
186
+ layout: DictionaryLayout;
187
+ /** Reload lifetime */
188
+ lifetime: number | DictionaryLifetime;
189
+ /** Primary key column(s) */
190
+ primaryKey: keyof TCols | (keyof TCols)[];
191
+ /** Cluster to create dictionary on */
192
+ onCluster?: string;
193
+ }
194
+ /**
195
+ * Dictionary definition with helper methods
196
+ */
197
+ export interface DictionaryDefinition<TCols extends TableColumns> {
198
+ $dict: string;
199
+ $columns: TCols;
200
+ $options: DictionaryOptions<TCols>;
201
+ /**
202
+ * Get a value from the dictionary using dictGet
203
+ *
204
+ * @example
205
+ * ```typescript
206
+ * // Get user's country by ID
207
+ * userDict.get('country', userId)
208
+ * // Generates: dictGet('user_dict', 'country', user_id)
209
+ * ```
210
+ */
211
+ get<TCol extends keyof TCols & string>(column: TCol, ...keyValues: (SQLExpression | string | number)[]): SQLExpression;
212
+ /**
213
+ * Get a value with a default if key not found
214
+ */
215
+ getOrDefault<TCol extends keyof TCols & string>(column: TCol, defaultValue: any, ...keyValues: (SQLExpression | string | number)[]): SQLExpression;
216
+ /**
217
+ * Check if a key exists in the dictionary
218
+ */
219
+ has(...keyValues: (SQLExpression | string | number)[]): SQLExpression;
220
+ /**
221
+ * Get child keys for hierarchical dictionaries
222
+ */
223
+ getChildren(...keyValues: (SQLExpression | string | number)[]): SQLExpression;
224
+ /**
225
+ * Get ancestor at level N for hierarchical dictionaries
226
+ */
227
+ getAncestor(keyValue: SQLExpression | string | number, level: number): SQLExpression;
228
+ /**
229
+ * Generate CREATE DICTIONARY statement
230
+ */
231
+ toSQL(): string;
232
+ }
233
+ /**
234
+ * Create a type-safe ClickHouse dictionary definition.
235
+ *
236
+ * @example
237
+ * ```typescript
238
+ * import { chDictionary, uint32, text, Engine } from '@housekit/orm';
239
+ *
240
+ * // Define a user lookup dictionary
241
+ * const userDict = chDictionary('user_dict', {
242
+ * id: uint32('id'),
243
+ * name: text('name'),
244
+ * country: text('country'),
245
+ * }, {
246
+ * source: {
247
+ * type: 'clickhouse',
248
+ * table: 'users',
249
+ * },
250
+ * layout: { type: 'hashed' },
251
+ * lifetime: 3600, // Reload every hour
252
+ * primaryKey: 'id',
253
+ * });
254
+ *
255
+ * // Use in queries - much faster than JOIN!
256
+ * const query = db.select({
257
+ * eventType: events.event_type,
258
+ * userName: userDict.get('name', events.user_id),
259
+ * userCountry: userDict.get('country', events.user_id),
260
+ * }).from(events);
261
+ * ```
262
+ */
263
+ export declare function chDictionary<TCols extends TableColumns>(name: string, columns: TCols, options: DictionaryOptions<TCols>): DictionaryDefinition<TCols>;