@tinybirdco/sdk 0.0.54 → 0.0.56

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.
@@ -14,7 +14,7 @@ const VALIDATOR_BRAND = Symbol.for("tinybird.validator");
14
14
  export interface TypeValidator<
15
15
  TType,
16
16
  TTinybirdType extends string = string,
17
- TModifiers extends TypeModifiers = TypeModifiers
17
+ TModifiers extends TypeModifiers = TypeModifiers,
18
18
  > {
19
19
  readonly [VALIDATOR_BRAND]: true;
20
20
  /** The inferred TypeScript type */
@@ -25,15 +25,33 @@ export interface TypeValidator<
25
25
  readonly _modifiers: TModifiers;
26
26
 
27
27
  /** Make this column nullable */
28
- nullable(): TypeValidator<TType | null, `Nullable(${TTinybirdType})`, TModifiers & { nullable: true }>;
28
+ nullable(): TypeValidator<
29
+ TType | null,
30
+ `Nullable(${TTinybirdType})`,
31
+ TModifiers & { nullable: true }
32
+ >;
29
33
  /** Apply LowCardinality optimization (for strings with few unique values) */
30
- lowCardinality(): TypeValidator<TType, `LowCardinality(${TTinybirdType})`, TModifiers & { lowCardinality: true }>;
34
+ lowCardinality(): TypeValidator<
35
+ TType,
36
+ `LowCardinality(${TTinybirdType})`,
37
+ TModifiers & { lowCardinality: true }
38
+ >;
31
39
  /** Set a default value for the column */
32
- default(value: TType): TypeValidator<TType, TTinybirdType, TModifiers & { hasDefault: true; defaultValue: TType }>;
40
+ default(
41
+ value: TType,
42
+ ): TypeValidator<
43
+ TType,
44
+ TTinybirdType,
45
+ TModifiers & { hasDefault: true; defaultValue: TType }
46
+ >;
33
47
  /** Set a codec for compression */
34
- codec(codec: string): TypeValidator<TType, TTinybirdType, TModifiers & { codec: string }>;
48
+ codec(
49
+ codec: string,
50
+ ): TypeValidator<TType, TTinybirdType, TModifiers & { codec: string }>;
35
51
  /** Set an explicit JSON path for extraction (overrides autogenerated path) */
36
- jsonPath(path: string): TypeValidator<TType, TTinybirdType, TModifiers & { jsonPath: string }>;
52
+ jsonPath(
53
+ path: string,
54
+ ): TypeValidator<TType, TTinybirdType, TModifiers & { jsonPath: string }>;
37
55
  }
38
56
 
39
57
  export interface TypeModifiers {
@@ -46,15 +64,18 @@ export interface TypeModifiers {
46
64
  }
47
65
 
48
66
  // Internal implementation
49
- interface ValidatorImpl<TType, TTinybirdType extends string, TModifiers extends TypeModifiers>
50
- extends TypeValidator<TType, TTinybirdType, TModifiers> {
67
+ interface ValidatorImpl<
68
+ TType,
69
+ TTinybirdType extends string,
70
+ TModifiers extends TypeModifiers,
71
+ > extends TypeValidator<TType, TTinybirdType, TModifiers> {
51
72
  readonly tinybirdType: TTinybirdType;
52
73
  readonly modifiers: TModifiers;
53
74
  }
54
75
 
55
76
  function createValidator<TType, TTinybirdType extends string>(
56
77
  tinybirdType: TTinybirdType,
57
- modifiers: TypeModifiers = {}
78
+ modifiers: TypeModifiers = {},
58
79
  ): TypeValidator<TType, TTinybirdType, TypeModifiers> {
59
80
  const validator: ValidatorImpl<TType, TTinybirdType, TypeModifiers> = {
60
81
  [VALIDATOR_BRAND]: true,
@@ -69,34 +90,53 @@ function createValidator<TType, TTinybirdType extends string>(
69
90
  // ClickHouse requires: LowCardinality(Nullable(X)), not Nullable(LowCardinality(X))
70
91
  if (modifiers.lowCardinality) {
71
92
  // Extract base type from LowCardinality(X) and wrap as LowCardinality(Nullable(X))
72
- const baseType = tinybirdType.replace(/^LowCardinality\((.+)\)$/, '$1');
93
+ const baseType = tinybirdType.replace(/^LowCardinality\((.+)\)$/, "$1");
73
94
  const newType = `LowCardinality(Nullable(${baseType}))`;
74
- return createValidator<TType | null, `LowCardinality(Nullable(${string}))`>(
75
- newType as `LowCardinality(Nullable(${string}))`,
76
- { ...modifiers, nullable: true }
77
- ) as unknown as TypeValidator<TType | null, `Nullable(${TTinybirdType})`, TypeModifiers & { nullable: true }>;
95
+ return createValidator<
96
+ TType | null,
97
+ `LowCardinality(Nullable(${string}))`
98
+ >(newType as `LowCardinality(Nullable(${string}))`, {
99
+ ...modifiers,
100
+ nullable: true,
101
+ }) as unknown as TypeValidator<
102
+ TType | null,
103
+ `Nullable(${TTinybirdType})`,
104
+ TypeModifiers & { nullable: true }
105
+ >;
78
106
  }
79
107
  return createValidator<TType | null, `Nullable(${TTinybirdType})`>(
80
108
  `Nullable(${tinybirdType})` as `Nullable(${TTinybirdType})`,
81
- { ...modifiers, nullable: true }
82
- ) as TypeValidator<TType | null, `Nullable(${TTinybirdType})`, TypeModifiers & { nullable: true }>;
109
+ { ...modifiers, nullable: true },
110
+ ) as TypeValidator<
111
+ TType | null,
112
+ `Nullable(${TTinybirdType})`,
113
+ TypeModifiers & { nullable: true }
114
+ >;
83
115
  },
84
116
 
85
117
  lowCardinality() {
86
118
  // If already nullable, wrap as LowCardinality(Nullable(X))
87
119
  if (modifiers.nullable) {
88
120
  // Extract base type from Nullable(X) and wrap as LowCardinality(Nullable(X))
89
- const baseType = tinybirdType.replace(/^Nullable\((.+)\)$/, '$1');
121
+ const baseType = tinybirdType.replace(/^Nullable\((.+)\)$/, "$1");
90
122
  const newType = `LowCardinality(Nullable(${baseType}))`;
91
123
  return createValidator<TType, `LowCardinality(Nullable(${string}))`>(
92
124
  newType as `LowCardinality(Nullable(${string}))`,
93
- { ...modifiers, lowCardinality: true }
94
- ) as unknown as TypeValidator<TType, `LowCardinality(${TTinybirdType})`, TypeModifiers & { lowCardinality: true }>;
125
+ { ...modifiers, lowCardinality: true },
126
+ ) as unknown as TypeValidator<
127
+ TType,
128
+ `LowCardinality(${TTinybirdType})`,
129
+ TypeModifiers & { lowCardinality: true }
130
+ >;
95
131
  }
96
132
  return createValidator<TType, `LowCardinality(${TTinybirdType})`>(
97
133
  `LowCardinality(${tinybirdType})` as `LowCardinality(${TTinybirdType})`,
98
- { ...modifiers, lowCardinality: true }
99
- ) as TypeValidator<TType, `LowCardinality(${TTinybirdType})`, TypeModifiers & { lowCardinality: true }>;
134
+ { ...modifiers, lowCardinality: true },
135
+ ) as TypeValidator<
136
+ TType,
137
+ `LowCardinality(${TTinybirdType})`,
138
+ TypeModifiers & { lowCardinality: true }
139
+ >;
100
140
  },
101
141
 
102
142
  default(value: TType) {
@@ -104,21 +144,33 @@ function createValidator<TType, TTinybirdType extends string>(
104
144
  ...modifiers,
105
145
  hasDefault: true,
106
146
  defaultValue: value,
107
- }) as TypeValidator<TType, TTinybirdType, TypeModifiers & { hasDefault: true; defaultValue: TType }>;
147
+ }) as TypeValidator<
148
+ TType,
149
+ TTinybirdType,
150
+ TypeModifiers & { hasDefault: true; defaultValue: TType }
151
+ >;
108
152
  },
109
153
 
110
154
  codec(codec: string) {
111
155
  return createValidator<TType, TTinybirdType>(tinybirdType, {
112
156
  ...modifiers,
113
157
  codec,
114
- }) as TypeValidator<TType, TTinybirdType, TypeModifiers & { codec: string }>;
158
+ }) as TypeValidator<
159
+ TType,
160
+ TTinybirdType,
161
+ TypeModifiers & { codec: string }
162
+ >;
115
163
  },
116
164
 
117
165
  jsonPath(path: string) {
118
166
  return createValidator<TType, TTinybirdType>(tinybirdType, {
119
167
  ...modifiers,
120
168
  jsonPath: path,
121
- }) as TypeValidator<TType, TTinybirdType, TypeModifiers & { jsonPath: string }>;
169
+ }) as TypeValidator<
170
+ TType,
171
+ TTinybirdType,
172
+ TypeModifiers & { jsonPath: string }
173
+ >;
122
174
  },
123
175
  };
124
176
 
@@ -139,116 +191,141 @@ function createValidator<TType, TTinybirdType extends string>(
139
191
  * tags: t.array(t.string()),
140
192
  * metadata: t.json(),
141
193
  * };
194
+ *
195
+ * // With custom types for narrower type inference:
196
+ * type UserId = string & { readonly __brand: 'UserId' };
197
+ * type Timestamp = string & { readonly __brand: 'Timestamp' };
198
+ *
199
+ * const typedSchema = {
200
+ * user_id: t.string<UserId>(),
201
+ * created_at: t.dateTime<Timestamp>(),
202
+ * };
142
203
  * ```
143
204
  */
144
205
  export const t = {
145
206
  // ============ String Types ============
146
207
 
147
208
  /** String type - variable length UTF-8 string */
148
- string: () => createValidator<string, "String">("String"),
209
+ string: <T extends string = string>() =>
210
+ createValidator<T, "String">("String"),
149
211
 
150
212
  /** FixedString(N) - fixed length string, padded with null bytes */
151
- fixedString: (length: number) =>
152
- createValidator<string, `FixedString(${number})`>(`FixedString(${length})`),
213
+ fixedString: <T extends string = string>(length: number) =>
214
+ createValidator<T, `FixedString(${number})`>(`FixedString(${length})`),
153
215
 
154
216
  /** UUID - 16-byte universally unique identifier */
155
- uuid: () => createValidator<string, "UUID">("UUID"),
217
+ uuid: <T extends string = string>() => createValidator<T, "UUID">("UUID"),
156
218
 
157
219
  // ============ Integer Types ============
158
220
 
159
221
  /** Int8 - signed 8-bit integer (-128 to 127) */
160
- int8: () => createValidator<number, "Int8">("Int8"),
222
+ int8: <T extends number = number>() => createValidator<T, "Int8">("Int8"),
161
223
 
162
224
  /** Int16 - signed 16-bit integer */
163
- int16: () => createValidator<number, "Int16">("Int16"),
225
+ int16: <T extends number = number>() => createValidator<T, "Int16">("Int16"),
164
226
 
165
227
  /** Int32 - signed 32-bit integer */
166
- int32: () => createValidator<number, "Int32">("Int32"),
228
+ int32: <T extends number = number>() => createValidator<T, "Int32">("Int32"),
167
229
 
168
230
  /** Int64 - signed 64-bit integer (represented as number, may lose precision) */
169
- int64: () => createValidator<number, "Int64">("Int64"),
231
+ int64: <T extends number = number>() => createValidator<T, "Int64">("Int64"),
170
232
 
171
233
  /** Int128 - signed 128-bit integer (represented as bigint) */
172
- int128: () => createValidator<bigint, "Int128">("Int128"),
234
+ int128: <T extends bigint = bigint>() =>
235
+ createValidator<T, "Int128">("Int128"),
173
236
 
174
237
  /** Int256 - signed 256-bit integer (represented as bigint) */
175
- int256: () => createValidator<bigint, "Int256">("Int256"),
238
+ int256: <T extends bigint = bigint>() =>
239
+ createValidator<T, "Int256">("Int256"),
176
240
 
177
241
  /** UInt8 - unsigned 8-bit integer (0 to 255) */
178
- uint8: () => createValidator<number, "UInt8">("UInt8"),
242
+ uint8: <T extends number = number>() => createValidator<T, "UInt8">("UInt8"),
179
243
 
180
244
  /** UInt16 - unsigned 16-bit integer */
181
- uint16: () => createValidator<number, "UInt16">("UInt16"),
245
+ uint16: <T extends number = number>() =>
246
+ createValidator<T, "UInt16">("UInt16"),
182
247
 
183
248
  /** UInt32 - unsigned 32-bit integer */
184
- uint32: () => createValidator<number, "UInt32">("UInt32"),
249
+ uint32: <T extends number = number>() =>
250
+ createValidator<T, "UInt32">("UInt32"),
185
251
 
186
252
  /** UInt64 - unsigned 64-bit integer (represented as number, may lose precision) */
187
- uint64: () => createValidator<number, "UInt64">("UInt64"),
253
+ uint64: <T extends number = number>() =>
254
+ createValidator<T, "UInt64">("UInt64"),
188
255
 
189
256
  /** UInt128 - unsigned 128-bit integer (represented as bigint) */
190
- uint128: () => createValidator<bigint, "UInt128">("UInt128"),
257
+ uint128: <T extends bigint = bigint>() =>
258
+ createValidator<T, "UInt128">("UInt128"),
191
259
 
192
260
  /** UInt256 - unsigned 256-bit integer (represented as bigint) */
193
- uint256: () => createValidator<bigint, "UInt256">("UInt256"),
261
+ uint256: <T extends bigint = bigint>() =>
262
+ createValidator<T, "UInt256">("UInt256"),
194
263
 
195
264
  // ============ Float Types ============
196
265
 
197
266
  /** Float32 - 32-bit floating point */
198
- float32: () => createValidator<number, "Float32">("Float32"),
267
+ float32: <T extends number = number>() =>
268
+ createValidator<T, "Float32">("Float32"),
199
269
 
200
270
  /** Float64 - 64-bit floating point (double precision) */
201
- float64: () => createValidator<number, "Float64">("Float64"),
271
+ float64: <T extends number = number>() =>
272
+ createValidator<T, "Float64">("Float64"),
202
273
 
203
274
  /** Decimal(precision, scale) - fixed-point decimal number */
204
- decimal: (precision: number, scale: number) =>
205
- createValidator<number, `Decimal(${number}, ${number})`>(
206
- `Decimal(${precision}, ${scale})`
275
+ decimal: <T extends number = number>(precision: number, scale: number) =>
276
+ createValidator<T, `Decimal(${number}, ${number})`>(
277
+ `Decimal(${precision}, ${scale})`,
207
278
  ),
208
279
 
209
280
  // ============ Boolean ============
210
281
 
211
282
  /** Bool - boolean value (true/false) */
212
- bool: () => createValidator<boolean, "Bool">("Bool"),
283
+ bool: <T extends boolean = boolean>() => createValidator<T, "Bool">("Bool"),
213
284
 
214
285
  // ============ Date/Time Types ============
215
286
 
216
287
  /** Date - string in YYYY-MM-DD format (e.g. 2024-01-15) */
217
- date: () => createValidator<string, "Date">("Date"),
288
+ date: <T extends string = string>() => createValidator<T, "Date">("Date"),
218
289
 
219
290
  /** Date32 - string in YYYY-MM-DD format (e.g. 2024-01-15, extended date range) */
220
- date32: () => createValidator<string, "Date32">("Date32"),
291
+ date32: <T extends string = string>() =>
292
+ createValidator<T, "Date32">("Date32"),
221
293
 
222
294
  /** DateTime - string in YYYY-MM-DD HH:MM:SS format (e.g. 2024-01-15 10:30:00) */
223
- dateTime: (timezone?: string) =>
295
+ dateTime: <T extends string = string>(timezone?: string) =>
224
296
  timezone
225
- ? createValidator<string, `DateTime('${string}')`>(`DateTime('${timezone}')`)
226
- : createValidator<string, "DateTime">("DateTime"),
297
+ ? createValidator<T, `DateTime('${string}')`>(`DateTime('${timezone}')`)
298
+ : createValidator<T, "DateTime">("DateTime"),
227
299
 
228
300
  /** DateTime64 - string in YYYY-MM-DD HH:MM:SS[.fraction] format (e.g. 2024-01-15 10:30:00.123) */
229
- dateTime64: (precision: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 = 3, timezone?: string) =>
301
+ dateTime64: <T extends string = string>(
302
+ precision: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 = 3,
303
+ timezone?: string,
304
+ ) =>
230
305
  timezone
231
- ? createValidator<string, `DateTime64(${number}, '${string}')`>(
232
- `DateTime64(${precision}, '${timezone}')`
306
+ ? createValidator<T, `DateTime64(${number}, '${string}')`>(
307
+ `DateTime64(${precision}, '${timezone}')`,
233
308
  )
234
- : createValidator<string, `DateTime64(${number})`>(`DateTime64(${precision})`),
309
+ : createValidator<T, `DateTime64(${number})`>(`DateTime64(${precision})`),
235
310
 
236
311
  // ============ Complex Types ============
237
312
 
238
313
  /** Array(T) - array of elements of type T */
239
314
  array: <TElement extends TypeValidator<unknown, string, TypeModifiers>>(
240
- element: TElement
315
+ element: TElement,
241
316
  ): TypeValidator<
242
317
  TElement["_type"][],
243
318
  `Array(${TElement["_tinybirdType"]})`,
244
319
  TypeModifiers
245
320
  > =>
246
321
  createValidator<TElement["_type"][], `Array(${TElement["_tinybirdType"]})`>(
247
- `Array(${element._tinybirdType})` as `Array(${TElement["_tinybirdType"]})`
322
+ `Array(${element._tinybirdType})` as `Array(${TElement["_tinybirdType"]})`,
248
323
  ),
249
324
 
250
325
  /** Tuple(T1, T2, ...) - tuple of heterogeneous types */
251
- tuple: <TElements extends readonly TypeValidator<unknown, string, TypeModifiers>[]>(
326
+ tuple: <
327
+ TElements extends readonly TypeValidator<unknown, string, TypeModifiers>[],
328
+ >(
252
329
  ...elements: TElements
253
330
  ): TypeValidator<
254
331
  { [K in keyof TElements]: TElements[K]["_type"] },
@@ -263,10 +340,10 @@ export const t = {
263
340
  /** Map(K, V) - dictionary/map type */
264
341
  map: <
265
342
  TKey extends TypeValidator<string | number, string, TypeModifiers>,
266
- TValue extends TypeValidator<unknown, string, TypeModifiers>
343
+ TValue extends TypeValidator<unknown, string, TypeModifiers>,
267
344
  >(
268
345
  keyType: TKey,
269
- valueType: TValue
346
+ valueType: TValue,
270
347
  ): TypeValidator<
271
348
  Map<TKey["_type"], TValue["_type"]>,
272
349
  `Map(${TKey["_tinybirdType"]}, ${TValue["_tinybirdType"]})`,
@@ -288,7 +365,7 @@ export const t = {
288
365
  .map((v, i) => `'${v.replace(/'/g, "\\'")}' = ${i + 1}`)
289
366
  .join(", ");
290
367
  return createValidator<TValues[number], `Enum8(${string})`>(
291
- `Enum8(${enumMapping})` as `Enum8(${string})`
368
+ `Enum8(${enumMapping})` as `Enum8(${string})`,
292
369
  );
293
370
  },
294
371
 
@@ -298,27 +375,27 @@ export const t = {
298
375
  .map((v, i) => `'${v.replace(/'/g, "\\'")}' = ${i + 1}`)
299
376
  .join(", ");
300
377
  return createValidator<TValues[number], `Enum16(${string})`>(
301
- `Enum16(${enumMapping})` as `Enum16(${string})`
378
+ `Enum16(${enumMapping})` as `Enum16(${string})`,
302
379
  );
303
380
  },
304
381
 
305
382
  // ============ Special Types ============
306
383
 
307
384
  /** IPv4 - IPv4 address */
308
- ipv4: () => createValidator<string, "IPv4">("IPv4"),
385
+ ipv4: <T extends string = string>() => createValidator<T, "IPv4">("IPv4"),
309
386
 
310
387
  /** IPv6 - IPv6 address */
311
- ipv6: () => createValidator<string, "IPv6">("IPv6"),
388
+ ipv6: <T extends string = string>() => createValidator<T, "IPv6">("IPv6"),
312
389
 
313
390
  // ============ Aggregate Function States ============
314
391
 
315
392
  /** SimpleAggregateFunction - for materialized views with simple aggregates */
316
393
  simpleAggregateFunction: <
317
394
  TFunc extends string,
318
- TType extends TypeValidator<unknown, string, TypeModifiers>
395
+ TType extends TypeValidator<unknown, string, TypeModifiers>,
319
396
  >(
320
397
  func: TFunc,
321
- type: TType
398
+ type: TType,
322
399
  ): TypeValidator<
323
400
  TType["_type"],
324
401
  `SimpleAggregateFunction(${TFunc}, ${TType["_tinybirdType"]})`,
@@ -332,10 +409,10 @@ export const t = {
332
409
  /** AggregateFunction - for materialized views with complex aggregates */
333
410
  aggregateFunction: <
334
411
  TFunc extends string,
335
- TType extends TypeValidator<unknown, string, TypeModifiers>
412
+ TType extends TypeValidator<unknown, string, TypeModifiers>,
336
413
  >(
337
414
  func: TFunc,
338
- type: TType
415
+ type: TType,
339
416
  ): TypeValidator<
340
417
  TType["_type"],
341
418
  `AggregateFunction(${TFunc}, ${TType["_tinybirdType"]})`,