@housekit/orm 0.1.47 → 0.1.48

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 (79) hide show
  1. package/README.md +34 -0
  2. package/dist/builders/delete.js +112 -0
  3. package/dist/builders/insert.d.ts +0 -91
  4. package/dist/builders/insert.js +393 -0
  5. package/dist/builders/prepared.d.ts +1 -2
  6. package/dist/builders/prepared.js +30 -0
  7. package/dist/builders/select.d.ts +0 -161
  8. package/dist/builders/select.js +562 -0
  9. package/dist/builders/select.types.js +1 -0
  10. package/dist/builders/update.js +136 -0
  11. package/dist/client.d.ts +0 -6
  12. package/dist/client.js +140 -0
  13. package/dist/codegen/zod.js +107 -0
  14. package/dist/column.d.ts +1 -25
  15. package/dist/column.js +133 -0
  16. package/dist/compiler.d.ts +0 -7
  17. package/dist/compiler.js +513 -0
  18. package/dist/core.js +6 -0
  19. package/dist/data-types.d.ts +0 -61
  20. package/dist/data-types.js +127 -0
  21. package/dist/dictionary.d.ts +0 -149
  22. package/dist/dictionary.js +158 -0
  23. package/dist/engines.d.ts +0 -385
  24. package/dist/engines.js +292 -0
  25. package/dist/expressions.d.ts +0 -10
  26. package/dist/expressions.js +268 -0
  27. package/dist/external.d.ts +0 -112
  28. package/dist/external.js +224 -0
  29. package/dist/index.d.ts +0 -51
  30. package/dist/index.js +139 -6853
  31. package/dist/logger.js +36 -0
  32. package/dist/materialized-views.d.ts +0 -188
  33. package/dist/materialized-views.js +380 -0
  34. package/dist/metadata.js +59 -0
  35. package/dist/modules/aggregates.d.ts +0 -164
  36. package/dist/modules/aggregates.js +121 -0
  37. package/dist/modules/array.d.ts +0 -98
  38. package/dist/modules/array.js +71 -0
  39. package/dist/modules/conditional.d.ts +0 -84
  40. package/dist/modules/conditional.js +138 -0
  41. package/dist/modules/conversion.d.ts +0 -147
  42. package/dist/modules/conversion.js +109 -0
  43. package/dist/modules/geo.d.ts +0 -164
  44. package/dist/modules/geo.js +112 -0
  45. package/dist/modules/hash.js +4 -0
  46. package/dist/modules/index.js +12 -0
  47. package/dist/modules/json.d.ts +0 -106
  48. package/dist/modules/json.js +76 -0
  49. package/dist/modules/math.d.ts +0 -16
  50. package/dist/modules/math.js +16 -0
  51. package/dist/modules/string.d.ts +0 -136
  52. package/dist/modules/string.js +89 -0
  53. package/dist/modules/time.d.ts +0 -123
  54. package/dist/modules/time.js +91 -0
  55. package/dist/modules/types.d.ts +0 -133
  56. package/dist/modules/types.js +114 -0
  57. package/dist/modules/window.js +140 -0
  58. package/dist/relational.d.ts +0 -82
  59. package/dist/relational.js +290 -0
  60. package/dist/relations.js +21 -0
  61. package/dist/schema-builder.d.ts +0 -90
  62. package/dist/schema-builder.js +140 -0
  63. package/dist/table.d.ts +0 -42
  64. package/dist/table.js +406 -0
  65. package/dist/utils/background-batcher.js +75 -0
  66. package/dist/utils/batch-transform.js +51 -0
  67. package/dist/utils/binary-reader.d.ts +0 -6
  68. package/dist/utils/binary-reader.js +334 -0
  69. package/dist/utils/binary-serializer.d.ts +0 -125
  70. package/dist/utils/binary-serializer.js +637 -0
  71. package/dist/utils/binary-worker-code.js +1 -0
  72. package/dist/utils/binary-worker-pool.d.ts +0 -34
  73. package/dist/utils/binary-worker-pool.js +206 -0
  74. package/dist/utils/binary-worker.d.ts +0 -11
  75. package/dist/utils/binary-worker.js +63 -0
  76. package/dist/utils/insert-processing.d.ts +0 -2
  77. package/dist/utils/insert-processing.js +163 -0
  78. package/dist/utils/lru-cache.js +30 -0
  79. package/package.json +68 -3
@@ -1,154 +1,31 @@
1
1
  import { ClickHouseColumn } from '../core';
2
2
  import { SQLExpression } from '../expressions';
3
- /**
4
- * Get current timestamp
5
- */
6
3
  export declare function timeNow(): SQLExpression;
7
- /**
8
- * Get current date (without time)
9
- */
10
4
  export declare function timeToday(): SQLExpression;
11
- /**
12
- * Get yesterday's date (without time)
13
- */
14
5
  export declare function timeYesterday(): SQLExpression;
15
- /**
16
- * Convert to Date type (without time)
17
- * @param col - Date/datetime column or expression
18
- */
19
6
  export declare function timeToDate(col: ClickHouseColumn | SQLExpression): SQLExpression;
20
- /**
21
- * Convert to DateTime type
22
- * @param col - Date/datetime column or expression
23
- */
24
7
  export declare function timeToDateTime(col: ClickHouseColumn | SQLExpression): SQLExpression;
25
- /**
26
- * Convert to Unix timestamp (super common in time-series analytics)
27
- * @param col - DateTime column or expression
28
- */
29
8
  export declare function toUnixTimestamp(col: ClickHouseColumn | SQLExpression): SQLExpression;
30
- /**
31
- * Format datetime to string
32
- * @param col - DateTime column or expression
33
- * @param format - Format string (e.g., '%Y-%m-%d %H:%M:%S')
34
- */
35
9
  export declare function timeFormatDateTime(col: ClickHouseColumn | SQLExpression, format: string): SQLExpression;
36
- /**
37
- * Truncate to start of hour
38
- * @param col - DateTime column or expression
39
- */
40
10
  export declare function timeToStartOfHour(col: ClickHouseColumn | SQLExpression): SQLExpression;
41
- /**
42
- * Truncate to start of day
43
- * @param col - DateTime column or expression
44
- */
45
11
  export declare function timeToStartOfDay(col: ClickHouseColumn | SQLExpression): SQLExpression;
46
- /**
47
- * Truncate to start of week
48
- * @param col - DateTime column or expression
49
- */
50
12
  export declare function timeToStartOfWeek(col: ClickHouseColumn | SQLExpression): SQLExpression;
51
- /**
52
- * Truncate to start of month
53
- * @param col - DateTime column or expression
54
- */
55
13
  export declare function timeToStartOfMonth(col: ClickHouseColumn | SQLExpression): SQLExpression;
56
- /**
57
- * Truncate to start of quarter
58
- * @param col - DateTime column or expression
59
- */
60
14
  export declare function toStartOfQuarter(col: ClickHouseColumn | SQLExpression): SQLExpression;
61
- /**
62
- * Truncate to start of year
63
- * @param col - DateTime column or expression
64
- */
65
15
  export declare function toStartOfYear(col: ClickHouseColumn | SQLExpression): SQLExpression;
66
- /**
67
- * Truncate to start of interval (very flexible for custom time buckets)
68
- * @param col - DateTime column or expression
69
- * @param interval - Interval string (e.g., 'INTERVAL 1 HOUR', 'INTERVAL 15 MINUTE')
70
- * @param timezone - Optional timezone
71
- */
72
16
  export declare function toStartOfInterval(col: ClickHouseColumn | SQLExpression, interval: string, timezone?: string): SQLExpression;
73
- /**
74
- * Calculate difference between two dates
75
- * @param unit - Unit of difference ('year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second')
76
- * @param start - Start date
77
- * @param end - End date
78
- */
79
17
  export declare function dateDiff(unit: 'year' | 'quarter' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second', start: ClickHouseColumn | SQLExpression, end: ClickHouseColumn | SQLExpression): SQLExpression;
80
- /**
81
- * Add interval to date
82
- * @param unit - Unit to add ('year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second')
83
- * @param amount - Amount to add
84
- * @param date - Date to add to
85
- */
86
18
  export declare function dateAdd(unit: 'year' | 'quarter' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second', amount: number, date: ClickHouseColumn | SQLExpression): SQLExpression;
87
- /**
88
- * Subtract interval from date
89
- * @param unit - Unit to subtract ('year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second')
90
- * @param amount - Amount to subtract
91
- * @param date - Date to subtract from
92
- */
93
19
  export declare function dateSub(unit: 'year' | 'quarter' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second', amount: number, date: ClickHouseColumn | SQLExpression): SQLExpression;
94
- /**
95
- * Convert datetime to different timezone
96
- * @param col - DateTime column or expression
97
- * @param timezone - Target timezone
98
- */
99
20
  export declare function toTimezone(col: ClickHouseColumn | SQLExpression, timezone: string): SQLExpression;
100
- /**
101
- * Get timezone offset for datetime
102
- * @param col - DateTime column or expression
103
- */
104
21
  export declare function timeOffset(col: ClickHouseColumn | SQLExpression): SQLExpression;
105
- /**
106
- * Extract year from date
107
- * @param col - Date/datetime column or expression
108
- */
109
22
  export declare function toYear(col: ClickHouseColumn | SQLExpression): SQLExpression;
110
- /**
111
- * Extract quarter from date
112
- * @param col - Date/datetime column or expression
113
- */
114
23
  export declare function toQuarter(col: ClickHouseColumn | SQLExpression): SQLExpression;
115
- /**
116
- * Extract month from date
117
- * @param col - Date/datetime column or expression
118
- */
119
24
  export declare function toMonth(col: ClickHouseColumn | SQLExpression): SQLExpression;
120
- /**
121
- * Extract day of month from date
122
- * @param col - Date/datetime column or expression
123
- */
124
25
  export declare function toDayOfMonth(col: ClickHouseColumn | SQLExpression): SQLExpression;
125
- /**
126
- * Extract day of week from date
127
- * @param col - Date/datetime column or expression
128
- */
129
26
  export declare function toDayOfWeek(col: ClickHouseColumn | SQLExpression): SQLExpression;
130
- /**
131
- * Extract hour from datetime
132
- * @param col - DateTime column or expression
133
- */
134
27
  export declare function toHour(col: ClickHouseColumn | SQLExpression): SQLExpression;
135
- /**
136
- * Extract minute from datetime
137
- * @param col - DateTime column or expression
138
- */
139
28
  export declare function toMinute(col: ClickHouseColumn | SQLExpression): SQLExpression;
140
- /**
141
- * Extract second from datetime
142
- * @param col - DateTime column or expression
143
- */
144
29
  export declare function toSecond(col: ClickHouseColumn | SQLExpression): SQLExpression;
145
- /**
146
- * Get ISO week number from date
147
- * @param col - Date/datetime column or expression
148
- */
149
30
  export declare function toISOWeek(col: ClickHouseColumn | SQLExpression): SQLExpression;
150
- /**
151
- * Get week number from date (ClickHouse-specific)
152
- * @param col - Date/datetime column or expression
153
- */
154
31
  export declare function toWeek(col: ClickHouseColumn | SQLExpression): SQLExpression;
@@ -0,0 +1,91 @@
1
+ import { sql } from '../expressions';
2
+ export function timeNow() {
3
+ return sql `now()`;
4
+ }
5
+ export function timeToday() {
6
+ return sql `today()`;
7
+ }
8
+ export function timeYesterday() {
9
+ return sql `yesterday()`;
10
+ }
11
+ export function timeToDate(col) {
12
+ return sql `toDate(${col})`;
13
+ }
14
+ export function timeToDateTime(col) {
15
+ return sql `toDateTime(${col})`;
16
+ }
17
+ export function toUnixTimestamp(col) {
18
+ return sql `toUnixTimestamp(${col})`;
19
+ }
20
+ export function timeFormatDateTime(col, format) {
21
+ return sql `formatDateTime(${col}, '${format}')`;
22
+ }
23
+ export function timeToStartOfHour(col) {
24
+ return sql `toStartOfHour(${col})`;
25
+ }
26
+ export function timeToStartOfDay(col) {
27
+ return sql `toStartOfDay(${col})`;
28
+ }
29
+ export function timeToStartOfWeek(col) {
30
+ return sql `toStartOfWeek(${col})`;
31
+ }
32
+ export function timeToStartOfMonth(col) {
33
+ return sql `toStartOfMonth(${col})`;
34
+ }
35
+ export function toStartOfQuarter(col) {
36
+ return sql `toStartOfQuarter(${col})`;
37
+ }
38
+ export function toStartOfYear(col) {
39
+ return sql `toStartOfYear(${col})`;
40
+ }
41
+ export function toStartOfInterval(col, interval, timezone) {
42
+ if (timezone) {
43
+ return sql `toStartOfInterval(${col}, ${interval}, '${timezone}')`;
44
+ }
45
+ return sql `toStartOfInterval(${col}, ${interval})`;
46
+ }
47
+ export function dateDiff(unit, start, end) {
48
+ return sql `dateDiff('${unit}', ${start}, ${end})`;
49
+ }
50
+ export function dateAdd(unit, amount, date) {
51
+ return sql `dateAdd(${unit}, ${amount}, ${date})`;
52
+ }
53
+ export function dateSub(unit, amount, date) {
54
+ return sql `dateSub(${unit}, ${amount}, ${date})`;
55
+ }
56
+ export function toTimezone(col, timezone) {
57
+ return sql `toTimezone(${col}, '${timezone}')`;
58
+ }
59
+ export function timeOffset(col) {
60
+ return sql `timeOffset(${col})`;
61
+ }
62
+ export function toYear(col) {
63
+ return sql `toYear(${col})`;
64
+ }
65
+ export function toQuarter(col) {
66
+ return sql `toQuarter(${col})`;
67
+ }
68
+ export function toMonth(col) {
69
+ return sql `toMonth(${col})`;
70
+ }
71
+ export function toDayOfMonth(col) {
72
+ return sql `toDayOfMonth(${col})`;
73
+ }
74
+ export function toDayOfWeek(col) {
75
+ return sql `toDayOfWeek(${col})`;
76
+ }
77
+ export function toHour(col) {
78
+ return sql `toHour(${col})`;
79
+ }
80
+ export function toMinute(col) {
81
+ return sql `toMinute(${col})`;
82
+ }
83
+ export function toSecond(col) {
84
+ return sql `toSecond(${col})`;
85
+ }
86
+ export function toISOWeek(col) {
87
+ return sql `toISOWeek(${col})`;
88
+ }
89
+ export function toWeek(col) {
90
+ return sql `toWeek(${col})`;
91
+ }
@@ -1,118 +1,26 @@
1
1
  export type JsonValue = Record<string, any> | any[];
2
2
  import { ClickHouseColumn } from '../column';
3
- /**
4
- * Low cardinality string column (very common for dimensions)
5
- * @param name - Column name
6
- */
7
3
  export declare function lowCardinalityString(name: string): ClickHouseColumn<string>;
8
- /**
9
- * Low cardinality nullable string column
10
- * @param name - Column name
11
- */
12
4
  export declare function lowCardinalityNullableString(name: string): ClickHouseColumn<string | null>;
13
- /**
14
- * Nullable JSON column
15
- * @param name - Column name
16
- */
17
5
  export declare function nullableJson(name: string): ClickHouseColumn<JsonValue | null>;
18
- /**
19
- * Low cardinality JSON column
20
- * @param name - Column name
21
- */
22
6
  export declare function lowCardinalityJson(name: string): ClickHouseColumn<JsonValue>;
23
- /**
24
- * Low cardinality nullable JSON column
25
- * @param name - Column name
26
- */
27
7
  export declare function lowCardinalityNullableJson(name: string): ClickHouseColumn<JsonValue | null>;
28
- /**
29
- * Create an Enum8 column with TypeScript union type support
30
- * @param name - Column name
31
- * @param values - Array of possible values
32
- */
33
8
  export declare function enum8<T extends string>(name: string, values: readonly T[]): ClickHouseColumn<T>;
34
- /**
35
- * Create a nullable Enum8 column with TypeScript union type support
36
- * @param name - Column name
37
- * @param values - Array of possible values
38
- */
39
9
  export declare function nullableEnum8<T extends string>(name: string, values: readonly T[]): ClickHouseColumn<T | null>;
40
- /**
41
- * Create an Enum16 column with TypeScript union type support
42
- * @param name - Column name
43
- * @param values - Array of possible values
44
- */
45
10
  export declare function enum16<T extends string>(name: string, values: readonly T[]): ClickHouseColumn<T>;
46
- /**
47
- * Create a nullable Enum16 column with TypeScript union type support
48
- * @param name - Column name
49
- * @param values - Array of possible values
50
- */
51
11
  export declare function nullableEnum16<T extends string>(name: string, values: readonly T[]): ClickHouseColumn<T | null>;
52
- /**
53
- * Array of strings with low cardinality
54
- * @param name - Column name
55
- */
56
12
  export declare function lowCardinalityStringArray(name: string): ClickHouseColumn<string[]>;
57
- /**
58
- * Nullable array of strings
59
- * @param name - Column name
60
- */
61
13
  export declare function nullableStringArray(name: string): ClickHouseColumn<string[] | null>;
62
- /**
63
- * Array of nullable strings
64
- * @param name - Column name
65
- */
66
14
  export declare function stringNullableArray(name: string): ClickHouseColumn<(string | null)[]>;
67
- /**
68
- * Array of integers with low cardinality
69
- * @param name - Column name
70
- */
71
15
  export declare function lowCardinalityInt32Array(name: string): ClickHouseColumn<number[]>;
72
- /**
73
- * Email column (low cardinality string)
74
- * @param name - Column name
75
- */
76
16
  export declare function email(name: string): ClickHouseColumn<string>;
77
- /**
78
- * Nullable email column
79
- * @param name - Column name
80
- */
81
17
  export declare function nullableEmail(name: string): ClickHouseColumn<string | null>;
82
- /**
83
- * Phone number column (low cardinality string)
84
- * @param name - Column name
85
- */
86
18
  export declare function phone(name: string): ClickHouseColumn<string>;
87
- /**
88
- * Nullable phone number column
89
- * @param name - Column name
90
- */
91
19
  export declare function nullablePhone(name: string): ClickHouseColumn<string | null>;
92
- /**
93
- * Country code column (Enum8 with common country codes)
94
- * @param name - Column name
95
- */
96
20
  export declare function countryCode(name: string): ClickHouseColumn<string>;
97
- /**
98
- * Nullable country code column
99
- * @param name - Column name
100
- */
101
21
  export declare function nullableCountryCode(name: string): ClickHouseColumn<string | null>;
102
- /**
103
- * Status column (Enum8 with common statuses)
104
- * @param name - Column name
105
- */
106
22
  export declare function status<T extends string>(name: string, values: readonly T[]): ClickHouseColumn<T>;
107
- /**
108
- * Nullable status column
109
- * @param name - Column name
110
- * @param values - Array of possible status values
111
- */
112
23
  export declare function nullableStatus<T extends string>(name: string, values: readonly T[]): ClickHouseColumn<T | null>;
113
- /**
114
- * Common status values for reference
115
- */
116
24
  export declare const COMMON_STATUSES: {
117
25
  readonly ACTIVE: "active";
118
26
  readonly INACTIVE: "inactive";
@@ -125,53 +33,12 @@ export declare const COMMON_STATUSES: {
125
33
  readonly ARCHIVED: "archived";
126
34
  readonly DELETED: "deleted";
127
35
  };
128
- /**
129
- * Create a status column with common predefined values
130
- * @param name - Column name
131
- * @param customValues - Optional custom status values
132
- */
133
36
  export declare function commonStatus(name: string, customValues?: readonly string[]): ClickHouseColumn<string>;
134
- /**
135
- * Create a nullable status column with common predefined values
136
- * @param name - Column name
137
- * @param customValues - Optional custom status values
138
- */
139
37
  export declare function nullableCommonStatus(name: string, customValues?: readonly string[]): ClickHouseColumn<string | null>;
140
- /**
141
- * Created at timestamp column (DateTime with default now())
142
- * @param name - Column name
143
- * @param timezone - Optional timezone
144
- */
145
38
  export declare function createdAt(name?: string, timezone?: string): ClickHouseColumn<string>;
146
- /**
147
- * Updated at timestamp column (DateTime with default now() and on update)
148
- * @param name - Column name
149
- * @param timezone - Optional timezone
150
- */
151
39
  export declare function updatedAt(name?: string, timezone?: string): ClickHouseColumn<string>;
152
- /**
153
- * Deleted at timestamp column (nullable DateTime for soft deletes)
154
- * @param name - Column name
155
- * @param timezone - Optional timezone
156
- */
157
40
  export declare function deletedAt(name?: string, timezone?: string): ClickHouseColumn<string | null>;
158
- /**
159
- * Auto-generated UUID primary key
160
- * @param name - Column name
161
- */
162
41
  export declare function uuidPrimaryKey(name?: string): ClickHouseColumn<string>;
163
- /**
164
- * Auto-generated UUID primary key with low cardinality (for distributed systems)
165
- * @param name - Column name
166
- */
167
42
  export declare function lowCardinalityUuidPrimaryKey(name?: string): ClickHouseColumn<string>;
168
- /**
169
- * Sequential integer primary key
170
- * @param name - Column name
171
- */
172
43
  export declare function int32PrimaryKey(name?: string): ClickHouseColumn<number>;
173
- /**
174
- * Sequential big integer primary key
175
- * @param name - Column name
176
- */
177
44
  export declare function int64PrimaryKey(name?: string): ClickHouseColumn<number>;
@@ -0,0 +1,114 @@
1
+ import { ClickHouseColumn } from '../column';
2
+ import { sql } from '../expressions';
3
+ export function lowCardinalityString(name) {
4
+ return new ClickHouseColumn(name, 'LowCardinality(String)');
5
+ }
6
+ export function lowCardinalityNullableString(name) {
7
+ return new ClickHouseColumn(name, 'LowCardinality(Nullable(String))', true);
8
+ }
9
+ export function nullableJson(name) {
10
+ return new ClickHouseColumn(name, 'Nullable(JSON)', true, { isJson: true });
11
+ }
12
+ export function lowCardinalityJson(name) {
13
+ return new ClickHouseColumn(name, 'LowCardinality(JSON)', false, { isJson: true });
14
+ }
15
+ export function lowCardinalityNullableJson(name) {
16
+ return new ClickHouseColumn(name, 'LowCardinality(Nullable(JSON))', true, { isJson: true });
17
+ }
18
+ export function enum8(name, values) {
19
+ const enumDef = values.map((v, i) => `'${v}'=${i}`).join(', ');
20
+ return new ClickHouseColumn(name, `Enum8(${enumDef})`, false, { enumValues: values });
21
+ }
22
+ export function nullableEnum8(name, values) {
23
+ const enumDef = values.map((v, i) => `'${v}'=${i}`).join(', ');
24
+ return new ClickHouseColumn(name, `Nullable(Enum8(${enumDef}))`, true, { enumValues: values });
25
+ }
26
+ export function enum16(name, values) {
27
+ const enumDef = values.map((v, i) => `'${v}'=${i}`).join(', ');
28
+ return new ClickHouseColumn(name, `Enum16(${enumDef})`, false, { enumValues: values });
29
+ }
30
+ export function nullableEnum16(name, values) {
31
+ const enumDef = values.map((v, i) => `'${v}'=${i}`).join(', ');
32
+ return new ClickHouseColumn(name, `Nullable(Enum16(${enumDef}))`, true, { enumValues: values });
33
+ }
34
+ export function lowCardinalityStringArray(name) {
35
+ return new ClickHouseColumn(name, 'Array(LowCardinality(String))');
36
+ }
37
+ export function nullableStringArray(name) {
38
+ return new ClickHouseColumn(name, 'Nullable(Array(String))', true);
39
+ }
40
+ export function stringNullableArray(name) {
41
+ return new ClickHouseColumn(name, 'Array(Nullable(String))');
42
+ }
43
+ export function lowCardinalityInt32Array(name) {
44
+ return new ClickHouseColumn(name, 'Array(LowCardinality(Int32))');
45
+ }
46
+ export function email(name) {
47
+ return lowCardinalityString(name);
48
+ }
49
+ export function nullableEmail(name) {
50
+ return lowCardinalityNullableString(name);
51
+ }
52
+ export function phone(name) {
53
+ return lowCardinalityString(name);
54
+ }
55
+ export function nullablePhone(name) {
56
+ return lowCardinalityNullableString(name);
57
+ }
58
+ export function countryCode(name) {
59
+ return enum8(name, ['US', 'CA', 'GB', 'DE', 'FR', 'IT', 'ES', 'JP', 'CN', 'IN', 'BR', 'MX', 'AU', 'RU', 'KR', 'OTHER']);
60
+ }
61
+ export function nullableCountryCode(name) {
62
+ return nullableEnum8(name, ['US', 'CA', 'GB', 'DE', 'FR', 'IT', 'ES', 'JP', 'CN', 'IN', 'BR', 'MX', 'AU', 'RU', 'KR', 'OTHER']);
63
+ }
64
+ export function status(name, values) {
65
+ return enum8(name, values);
66
+ }
67
+ export function nullableStatus(name, values) {
68
+ return nullableEnum8(name, values);
69
+ }
70
+ export const COMMON_STATUSES = {
71
+ ACTIVE: 'active',
72
+ INACTIVE: 'inactive',
73
+ PENDING: 'pending',
74
+ COMPLETED: 'completed',
75
+ FAILED: 'failed',
76
+ CANCELLED: 'cancelled',
77
+ DRAFT: 'draft',
78
+ PUBLISHED: 'published',
79
+ ARCHIVED: 'archived',
80
+ DELETED: 'deleted',
81
+ };
82
+ export function commonStatus(name, customValues) {
83
+ const values = customValues || Object.values(COMMON_STATUSES);
84
+ return enum8(name, values);
85
+ }
86
+ export function nullableCommonStatus(name, customValues) {
87
+ const values = customValues || Object.values(COMMON_STATUSES);
88
+ return nullableEnum8(name, values);
89
+ }
90
+ export function createdAt(name = 'created_at', timezone) {
91
+ const col = new ClickHouseColumn(name, timezone ? `DateTime('${timezone}')` : 'DateTime');
92
+ return col.default(sql `now()`);
93
+ }
94
+ export function updatedAt(name = 'updated_at', timezone) {
95
+ const col = new ClickHouseColumn(name, timezone ? `DateTime('${timezone}')` : 'DateTime');
96
+ return col.default(sql `now()`);
97
+ }
98
+ export function deletedAt(name = 'deleted_at', timezone) {
99
+ const col = new ClickHouseColumn(name, timezone ? `DateTime('${timezone}')` : 'DateTime', true);
100
+ return col.nullable();
101
+ }
102
+ export function uuidPrimaryKey(name = 'id') {
103
+ return new ClickHouseColumn(name, 'UUID').autoGenerate().primaryKey();
104
+ }
105
+ export function lowCardinalityUuidPrimaryKey(name = 'id') {
106
+ const col = new ClickHouseColumn(name, 'LowCardinality(UUID)');
107
+ return col.autoGenerate().primaryKey();
108
+ }
109
+ export function int32PrimaryKey(name = 'id') {
110
+ return new ClickHouseColumn(name, 'Int32').primaryKey();
111
+ }
112
+ export function int64PrimaryKey(name = 'id') {
113
+ return new ClickHouseColumn(name, 'Int64').primaryKey();
114
+ }
@@ -0,0 +1,140 @@
1
+ import { ClickHouseColumn } from '../core';
2
+ import { sql } from '../expressions';
3
+ class WindowExpression {
4
+ base;
5
+ spec;
6
+ _type;
7
+ constructor(base, spec) {
8
+ this.base = base;
9
+ this.spec = spec;
10
+ }
11
+ as(alias) {
12
+ const clone = new WindowExpression(this.base, this.spec);
13
+ clone._alias = alias;
14
+ return clone;
15
+ }
16
+ over(spec) {
17
+ return new WindowExpression(this.base, mergeSpecs(this.spec, spec));
18
+ }
19
+ toSQL(options) {
20
+ const baseResult = this.base.toSQL(options);
21
+ const params = { ...baseResult.params };
22
+ const clause = buildWindowClause(this.spec, options, params);
23
+ return {
24
+ sql: `${baseResult.sql} OVER ${clause}`,
25
+ params
26
+ };
27
+ }
28
+ walk(visitor) {
29
+ const baseWalk = this.base.walk(visitor);
30
+ const walkExpr = (expr) => {
31
+ if (expr instanceof ClickHouseColumn) {
32
+ return expr.tableName ? `${expr.tableName}.${expr.name}` : expr.name;
33
+ }
34
+ return expr.walk(visitor);
35
+ };
36
+ const parts = [];
37
+ const spec = this.spec;
38
+ const partitions = normalizeArray(spec?.partitionBy);
39
+ if (partitions.length > 0) {
40
+ const partitionSql = partitions.map(part => walkExpr(part)).join(', ');
41
+ parts.push(`PARTITION BY ${partitionSql}`);
42
+ }
43
+ const orderings = normalizeArray(spec?.orderBy).map(order => {
44
+ if (order && typeof order === 'object' && 'col' in order) {
45
+ return { col: order.col, dir: order.dir };
46
+ }
47
+ return { col: order, dir: undefined };
48
+ });
49
+ if (orderings.length > 0) {
50
+ const orderSql = orderings
51
+ .map(order => {
52
+ const rendered = walkExpr(order.col);
53
+ return order.dir ? `${rendered} ${order.dir}` : rendered;
54
+ })
55
+ .join(', ');
56
+ parts.push(`ORDER BY ${orderSql}`);
57
+ }
58
+ const clause = parts.length === 0 ? '()' : `(${parts.join(' ')})`;
59
+ return `${baseWalk} OVER ${clause}`;
60
+ }
61
+ }
62
+ function mergeSpecs(current, next) {
63
+ if (!current && !next)
64
+ return undefined;
65
+ return {
66
+ partitionBy: next?.partitionBy ?? current?.partitionBy,
67
+ orderBy: next?.orderBy ?? current?.orderBy
68
+ };
69
+ }
70
+ function normalizeArray(value) {
71
+ if (value === undefined)
72
+ return [];
73
+ return Array.isArray(value) ? value : [value];
74
+ }
75
+ function formatColumn(col, options) {
76
+ if (options?.ignoreTablePrefix) {
77
+ return `\`${col.name}\``;
78
+ }
79
+ return col.tableName ? `\`${col.tableName}\`.\`${col.name}\`` : `\`${col.name}\``;
80
+ }
81
+ function renderExpr(expr, options, params) {
82
+ if (expr instanceof ClickHouseColumn) {
83
+ return formatColumn(expr, options);
84
+ }
85
+ const res = expr.toSQL(options);
86
+ Object.assign(params, res.params);
87
+ return res.sql;
88
+ }
89
+ function buildWindowClause(spec, options, params) {
90
+ const parts = [];
91
+ const partitions = normalizeArray(spec?.partitionBy);
92
+ if (partitions.length > 0) {
93
+ const partitionSql = partitions.map(part => renderExpr(part, options, params)).join(', ');
94
+ parts.push(`PARTITION BY ${partitionSql}`);
95
+ }
96
+ const orderings = normalizeArray(spec?.orderBy).map(order => {
97
+ if (order && typeof order === 'object' && 'col' in order) {
98
+ return { col: order.col, dir: order.dir };
99
+ }
100
+ return { col: order, dir: undefined };
101
+ });
102
+ if (orderings.length > 0) {
103
+ const orderSql = orderings
104
+ .map(order => {
105
+ const rendered = renderExpr(order.col, options, params);
106
+ return order.dir ? `${rendered} ${order.dir}` : rendered;
107
+ })
108
+ .join(', ');
109
+ parts.push(`ORDER BY ${orderSql}`);
110
+ }
111
+ if (parts.length === 0) {
112
+ return '()';
113
+ }
114
+ return `(${parts.join(' ')})`;
115
+ }
116
+ export function over(expr, spec) {
117
+ return new WindowExpression(expr, spec);
118
+ }
119
+ export function rowNumber(spec) {
120
+ return new WindowExpression(sql `row_number()`, spec);
121
+ }
122
+ export function rank(spec) {
123
+ return new WindowExpression(sql `rank()`, spec);
124
+ }
125
+ export function denseRank(spec) {
126
+ return new WindowExpression(sql `dense_rank()`, spec);
127
+ }
128
+ export function lag(col, offset = 1, defaultValue, spec) {
129
+ const base = defaultValue !== undefined
130
+ ? sql `lag(${col}, ${offset}, ${defaultValue})`
131
+ : sql `lag(${col}, ${offset})`;
132
+ return new WindowExpression(base, spec);
133
+ }
134
+ export function lead(col, offset = 1, defaultValue, spec) {
135
+ const base = defaultValue !== undefined
136
+ ? sql `lead(${col}, ${offset}, ${defaultValue})`
137
+ : sql `lead(${col}, ${offset})`;
138
+ return new WindowExpression(base, spec);
139
+ }
140
+ export const windowFns = { over, rowNumber, rank, denseRank, lag, lead };