@tinybirdco/sdk 0.0.69 → 0.0.71
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.
- package/README.md +7 -1
- package/dist/cli/commands/migrate.test.js +47 -0
- package/dist/cli/commands/migrate.test.js.map +1 -1
- package/dist/cli/utils/schema-validation.d.ts.map +1 -1
- package/dist/cli/utils/schema-validation.js +4 -2
- package/dist/cli/utils/schema-validation.js.map +1 -1
- package/dist/cli/utils/schema-validation.test.js +10 -0
- package/dist/cli/utils/schema-validation.test.js.map +1 -1
- package/dist/codegen/utils.d.ts.map +1 -1
- package/dist/codegen/utils.js +4 -0
- package/dist/codegen/utils.js.map +1 -1
- package/dist/codegen/utils.test.js +6 -0
- package/dist/codegen/utils.test.js.map +1 -1
- package/dist/generator/datasource.test.js +11 -0
- package/dist/generator/datasource.test.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/infer/index.d.ts +26 -6
- package/dist/infer/index.d.ts.map +1 -1
- package/dist/migrate/emit-ts.d.ts.map +1 -1
- package/dist/migrate/emit-ts.js +9 -2
- package/dist/migrate/emit-ts.js.map +1 -1
- package/dist/migrate/parse-datasource.js +1 -1
- package/dist/migrate/parse-datasource.js.map +1 -1
- package/dist/schema/engines.d.ts +19 -3
- package/dist/schema/engines.d.ts.map +1 -1
- package/dist/schema/engines.js +13 -0
- package/dist/schema/engines.js.map +1 -1
- package/dist/schema/engines.test.js +11 -0
- package/dist/schema/engines.test.js.map +1 -1
- package/dist/schema/pipe.test.js +20 -1
- package/dist/schema/pipe.test.js.map +1 -1
- package/dist/schema/types.d.ts +5 -0
- package/dist/schema/types.d.ts.map +1 -1
- package/dist/schema/types.js +6 -0
- package/dist/schema/types.js.map +1 -1
- package/dist/schema/types.test.js +13 -0
- package/dist/schema/types.test.js.map +1 -1
- package/package.json +1 -1
- package/src/cli/commands/migrate.test.ts +75 -0
- package/src/cli/utils/schema-validation.test.ts +14 -0
- package/src/cli/utils/schema-validation.ts +4 -2
- package/src/codegen/utils.test.ts +7 -0
- package/src/codegen/utils.ts +5 -0
- package/src/generator/datasource.test.ts +13 -0
- package/src/index.ts +2 -0
- package/src/infer/index.ts +33 -2
- package/src/migrate/emit-ts.ts +11 -2
- package/src/migrate/parse-datasource.ts +1 -1
- package/src/schema/engines.test.ts +13 -0
- package/src/schema/engines.ts +28 -3
- package/src/schema/pipe.test.ts +26 -1
- package/src/schema/types.test.ts +15 -0
- package/src/schema/types.ts +18 -0
package/src/infer/index.ts
CHANGED
|
@@ -82,6 +82,37 @@ type OptionalParamKeys<T extends ParamsDefinition> = {
|
|
|
82
82
|
[K in keyof T]: T[K] extends ParamValidator<unknown, string, false> ? K : never;
|
|
83
83
|
}[keyof T];
|
|
84
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Extract the required output keys from an output definition
|
|
87
|
+
*/
|
|
88
|
+
type RequiredOutputKeys<T extends OutputDefinition> = {
|
|
89
|
+
[K in keyof T]: T[K] extends TypeValidator<unknown, string, infer M>
|
|
90
|
+
? M extends { optional: true }
|
|
91
|
+
? never
|
|
92
|
+
: K
|
|
93
|
+
: K;
|
|
94
|
+
}[keyof T];
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Extract the optional output keys from an output definition
|
|
98
|
+
*/
|
|
99
|
+
type OptionalOutputKeys<T extends OutputDefinition> = {
|
|
100
|
+
[K in keyof T]: T[K] extends TypeValidator<unknown, string, infer M>
|
|
101
|
+
? M extends { optional: true }
|
|
102
|
+
? K
|
|
103
|
+
: never
|
|
104
|
+
: never;
|
|
105
|
+
}[keyof T];
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Extract a single output row type from an output definition
|
|
109
|
+
*/
|
|
110
|
+
type InferOutputObject<T extends OutputDefinition> = {
|
|
111
|
+
[K in RequiredOutputKeys<T>]: InferColumn<T[K]>;
|
|
112
|
+
} & {
|
|
113
|
+
[K in OptionalOutputKeys<T>]?: InferColumn<T[K]>;
|
|
114
|
+
};
|
|
115
|
+
|
|
85
116
|
/**
|
|
86
117
|
* Extract the params type from a pipe definition
|
|
87
118
|
*
|
|
@@ -131,14 +162,14 @@ export type InferParams<T> = T extends PipeDefinition<infer P, OutputDefinition>
|
|
|
131
162
|
* ```
|
|
132
163
|
*/
|
|
133
164
|
export type InferOutput<T> = T extends PipeDefinition<ParamsDefinition, infer O>
|
|
134
|
-
?
|
|
165
|
+
? InferOutputObject<O>[]
|
|
135
166
|
: never;
|
|
136
167
|
|
|
137
168
|
/**
|
|
138
169
|
* Extract a single output row type (without array wrapper)
|
|
139
170
|
*/
|
|
140
171
|
export type InferOutputRow<T> = T extends PipeDefinition<ParamsDefinition, infer O>
|
|
141
|
-
?
|
|
172
|
+
? InferOutputObject<O>
|
|
142
173
|
: never;
|
|
143
174
|
|
|
144
175
|
/**
|
package/src/migrate/emit-ts.ts
CHANGED
|
@@ -15,6 +15,10 @@ function escapeString(value: string): string {
|
|
|
15
15
|
return JSON.stringify(value);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
function escapeTemplateLiteral(value: string): string {
|
|
19
|
+
return value.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\${/g, "\\${");
|
|
20
|
+
}
|
|
21
|
+
|
|
18
22
|
function emitObjectKey(key: string): string {
|
|
19
23
|
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? key : escapeString(key);
|
|
20
24
|
}
|
|
@@ -196,6 +200,7 @@ function engineFunctionName(type: string): string {
|
|
|
196
200
|
AggregatingMergeTree: "aggregatingMergeTree",
|
|
197
201
|
CollapsingMergeTree: "collapsingMergeTree",
|
|
198
202
|
VersionedCollapsingMergeTree: "versionedCollapsingMergeTree",
|
|
203
|
+
Null: "null",
|
|
199
204
|
};
|
|
200
205
|
const functionName = map[type];
|
|
201
206
|
if (!functionName) {
|
|
@@ -205,6 +210,10 @@ function engineFunctionName(type: string): string {
|
|
|
205
210
|
}
|
|
206
211
|
|
|
207
212
|
function emitEngineOptions(engine: DatasourceEngineModel): string {
|
|
213
|
+
if (engine.type === "Null") {
|
|
214
|
+
return "engine.null()";
|
|
215
|
+
}
|
|
216
|
+
|
|
208
217
|
const options: string[] = [];
|
|
209
218
|
|
|
210
219
|
if (engine.sortingKey.length === 1) {
|
|
@@ -378,7 +387,7 @@ function emitDatasource(ds: DatasourceModel): string {
|
|
|
378
387
|
|
|
379
388
|
if (ds.forwardQuery) {
|
|
380
389
|
lines.push(" forwardQuery: `");
|
|
381
|
-
lines.push(ds.forwardQuery
|
|
390
|
+
lines.push(escapeTemplateLiteral(ds.forwardQuery));
|
|
382
391
|
lines.push(" `,");
|
|
383
392
|
}
|
|
384
393
|
|
|
@@ -561,7 +570,7 @@ function emitPipe(pipe: PipeModel): string {
|
|
|
561
570
|
lines.push(` description: ${escapeString(node.description)},`);
|
|
562
571
|
}
|
|
563
572
|
lines.push(" sql: `");
|
|
564
|
-
lines.push(node.sql
|
|
573
|
+
lines.push(escapeTemplateLiteral(node.sql));
|
|
565
574
|
lines.push(" `,");
|
|
566
575
|
lines.push(" }),");
|
|
567
576
|
}
|
|
@@ -540,7 +540,7 @@ export function parseDatasourceFile(resource: ResourceFile): DatasourceModel {
|
|
|
540
540
|
engineType = "MergeTree";
|
|
541
541
|
}
|
|
542
542
|
|
|
543
|
-
if (engineType && sortingKey.length === 0) {
|
|
543
|
+
if (engineType && engineType !== "Null" && sortingKey.length === 0) {
|
|
544
544
|
throw new MigrationParseError(
|
|
545
545
|
resource.filePath,
|
|
546
546
|
"datasource",
|
|
@@ -100,6 +100,13 @@ describe('Engine Configurations', () => {
|
|
|
100
100
|
});
|
|
101
101
|
});
|
|
102
102
|
|
|
103
|
+
describe('Null', () => {
|
|
104
|
+
it('creates Null config', () => {
|
|
105
|
+
const config = engine.null();
|
|
106
|
+
expect(config.type).toBe('Null');
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
103
110
|
describe('getEngineClause', () => {
|
|
104
111
|
it('generates basic MergeTree clause', () => {
|
|
105
112
|
const config = engine.mergeTree({ sortingKey: ['id'] });
|
|
@@ -187,6 +194,12 @@ describe('Engine Configurations', () => {
|
|
|
187
194
|
expect(clause).toContain('ENGINE_SIGN "sign_col"');
|
|
188
195
|
expect(clause).toContain('ENGINE_VERSION "version_col"');
|
|
189
196
|
});
|
|
197
|
+
|
|
198
|
+
it('generates Null engine without MergeTree directives', () => {
|
|
199
|
+
const clause = getEngineClause(engine.null());
|
|
200
|
+
expect(clause).toBe('ENGINE Null');
|
|
201
|
+
expect(clause).not.toContain('ENGINE_SORTING_KEY');
|
|
202
|
+
});
|
|
190
203
|
});
|
|
191
204
|
|
|
192
205
|
describe('Helper functions', () => {
|
package/src/schema/engines.ts
CHANGED
|
@@ -79,10 +79,18 @@ export interface VersionedCollapsingMergeTreeConfig extends BaseMergeTreeConfig
|
|
|
79
79
|
version: string;
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
+
/**
|
|
83
|
+
* Null engine configuration
|
|
84
|
+
* Discards inserted rows and returns an empty response when read
|
|
85
|
+
*/
|
|
86
|
+
export interface NullEngineConfig {
|
|
87
|
+
type: "Null";
|
|
88
|
+
}
|
|
89
|
+
|
|
82
90
|
/**
|
|
83
91
|
* Union type of all engine configurations
|
|
84
92
|
*/
|
|
85
|
-
export type
|
|
93
|
+
export type MergeTreeEngineConfig =
|
|
86
94
|
| MergeTreeConfig
|
|
87
95
|
| ReplacingMergeTreeConfig
|
|
88
96
|
| SummingMergeTreeConfig
|
|
@@ -90,6 +98,8 @@ export type EngineConfig =
|
|
|
90
98
|
| CollapsingMergeTreeConfig
|
|
91
99
|
| VersionedCollapsingMergeTreeConfig;
|
|
92
100
|
|
|
101
|
+
export type EngineConfig = MergeTreeEngineConfig | NullEngineConfig;
|
|
102
|
+
|
|
93
103
|
/**
|
|
94
104
|
* Helper to normalize sorting key to array format
|
|
95
105
|
*/
|
|
@@ -121,6 +131,9 @@ function normalizeSortingKey(key: string | readonly string[]): readonly string[]
|
|
|
121
131
|
* sortingKey: ['date', 'metric_name'],
|
|
122
132
|
* columns: ['value'],
|
|
123
133
|
* });
|
|
134
|
+
*
|
|
135
|
+
* // Null engine for materialized view source tables
|
|
136
|
+
* engine.null();
|
|
124
137
|
* ```
|
|
125
138
|
*/
|
|
126
139
|
export const engine = {
|
|
@@ -196,19 +209,27 @@ export const engine = {
|
|
|
196
209
|
type: "VersionedCollapsingMergeTree",
|
|
197
210
|
...config,
|
|
198
211
|
}),
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Null - Discards inserted rows and returns no rows when read
|
|
215
|
+
* Best for: Materialized view source tables that transform and discard raw input
|
|
216
|
+
*/
|
|
217
|
+
null: (): NullEngineConfig => ({
|
|
218
|
+
type: "Null",
|
|
219
|
+
}),
|
|
199
220
|
} as const;
|
|
200
221
|
|
|
201
222
|
/**
|
|
202
223
|
* Get the sorting key as an array
|
|
203
224
|
*/
|
|
204
|
-
export function getSortingKey(config:
|
|
225
|
+
export function getSortingKey(config: MergeTreeEngineConfig): readonly string[] {
|
|
205
226
|
return normalizeSortingKey(config.sortingKey);
|
|
206
227
|
}
|
|
207
228
|
|
|
208
229
|
/**
|
|
209
230
|
* Get the primary key as an array (defaults to sorting key)
|
|
210
231
|
*/
|
|
211
|
-
export function getPrimaryKey(config:
|
|
232
|
+
export function getPrimaryKey(config: MergeTreeEngineConfig): readonly string[] {
|
|
212
233
|
if (config.primaryKey) {
|
|
213
234
|
return normalizeSortingKey(config.primaryKey);
|
|
214
235
|
}
|
|
@@ -219,6 +240,10 @@ export function getPrimaryKey(config: EngineConfig): readonly string[] {
|
|
|
219
240
|
* Generate the engine clause for a datasource file
|
|
220
241
|
*/
|
|
221
242
|
export function getEngineClause(config: EngineConfig): string {
|
|
243
|
+
if (config.type === "Null") {
|
|
244
|
+
return "ENGINE Null";
|
|
245
|
+
}
|
|
246
|
+
|
|
222
247
|
const parts: string[] = [`ENGINE "${config.type}"`];
|
|
223
248
|
|
|
224
249
|
if (config.partitionKey) {
|
package/src/schema/pipe.test.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { describe, it, expect } from "vitest";
|
|
1
|
+
import { describe, it, expect, expectTypeOf } from "vitest";
|
|
2
2
|
import {
|
|
3
3
|
definePipe,
|
|
4
4
|
defineSinkPipe,
|
|
@@ -19,6 +19,7 @@ import { defineKafkaConnection, defineS3Connection } from "./connection.js";
|
|
|
19
19
|
import { t } from "./types.js";
|
|
20
20
|
import { p } from "./params.js";
|
|
21
21
|
import { engine } from "./engines.js";
|
|
22
|
+
import type { InferOutputRow } from "../infer/index.js";
|
|
22
23
|
|
|
23
24
|
describe("Pipe Schema", () => {
|
|
24
25
|
describe("node", () => {
|
|
@@ -82,6 +83,30 @@ describe("Pipe Schema", () => {
|
|
|
82
83
|
expect(pipe.options.description).toBe("A test pipe");
|
|
83
84
|
});
|
|
84
85
|
|
|
86
|
+
it("infers optional output fields as optional object properties", () => {
|
|
87
|
+
const pipe = definePipe("my_pipe", {
|
|
88
|
+
nodes: [node({ name: "endpoint", sql: "SELECT 1" })],
|
|
89
|
+
output: {
|
|
90
|
+
id: t.uint64(),
|
|
91
|
+
subtitle: t.string().optional(),
|
|
92
|
+
metadata: t.string().nullable().optional(),
|
|
93
|
+
},
|
|
94
|
+
endpoint: true,
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
type Row = InferOutputRow<typeof pipe>;
|
|
98
|
+
|
|
99
|
+
const requiredOnly: Row = { id: 1 };
|
|
100
|
+
const withOptionals: Row = { id: 1, subtitle: "hello", metadata: null };
|
|
101
|
+
|
|
102
|
+
expectTypeOf<Row["id"]>().toEqualTypeOf<number>();
|
|
103
|
+
expectTypeOf<Row["subtitle"]>().toEqualTypeOf<string | undefined>();
|
|
104
|
+
expectTypeOf<Row["metadata"]>().toEqualTypeOf<string | null | undefined>();
|
|
105
|
+
expect(requiredOnly).toEqual({ id: 1 });
|
|
106
|
+
expect(withOptionals).toEqual({ id: 1, subtitle: "hello", metadata: null });
|
|
107
|
+
expect(pipe._output?.subtitle?._modifiers.optional).toBe(true);
|
|
108
|
+
});
|
|
109
|
+
|
|
85
110
|
it("throws error for invalid pipe name", () => {
|
|
86
111
|
expect(() =>
|
|
87
112
|
definePipe("123invalid", {
|
package/src/schema/types.test.ts
CHANGED
|
@@ -64,6 +64,21 @@ describe("Type Validators (t.*)", () => {
|
|
|
64
64
|
});
|
|
65
65
|
});
|
|
66
66
|
|
|
67
|
+
describe("Optional modifier", () => {
|
|
68
|
+
it("marks field as optional without changing the Tinybird type", () => {
|
|
69
|
+
const type = t.string().optional();
|
|
70
|
+
expect(type._tinybirdType).toBe("String");
|
|
71
|
+
expect(type._modifiers.optional).toBe(true);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("can be combined with nullable fields", () => {
|
|
75
|
+
const type = t.string().nullable().optional();
|
|
76
|
+
expect(type._tinybirdType).toBe("Nullable(String)");
|
|
77
|
+
expect(type._modifiers.nullable).toBe(true);
|
|
78
|
+
expect(type._modifiers.optional).toBe(true);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
67
82
|
describe("LowCardinality modifier", () => {
|
|
68
83
|
it("wraps type in LowCardinality", () => {
|
|
69
84
|
const type = t.string().lowCardinality();
|
package/src/schema/types.ts
CHANGED
|
@@ -24,6 +24,12 @@ export interface TypeValidator<
|
|
|
24
24
|
/** Metadata about modifiers applied */
|
|
25
25
|
readonly _modifiers: TModifiers;
|
|
26
26
|
|
|
27
|
+
/** Mark this output field as optional (it may be absent from endpoint responses) */
|
|
28
|
+
optional(): TypeValidator<
|
|
29
|
+
TType,
|
|
30
|
+
TTinybirdType,
|
|
31
|
+
TModifiers & { optional: true }
|
|
32
|
+
>;
|
|
27
33
|
/** Make this column nullable */
|
|
28
34
|
nullable(): TypeValidator<
|
|
29
35
|
TType | null,
|
|
@@ -63,6 +69,7 @@ export interface TypeValidator<
|
|
|
63
69
|
}
|
|
64
70
|
|
|
65
71
|
export interface TypeModifiers {
|
|
72
|
+
optional?: boolean;
|
|
66
73
|
nullable?: boolean;
|
|
67
74
|
lowCardinality?: boolean;
|
|
68
75
|
hasDefault?: boolean;
|
|
@@ -94,6 +101,17 @@ function createValidator<TType, TTinybirdType extends string>(
|
|
|
94
101
|
tinybirdType,
|
|
95
102
|
modifiers,
|
|
96
103
|
|
|
104
|
+
optional() {
|
|
105
|
+
return createValidator<TType, TTinybirdType>(tinybirdType, {
|
|
106
|
+
...modifiers,
|
|
107
|
+
optional: true,
|
|
108
|
+
}) as TypeValidator<
|
|
109
|
+
TType,
|
|
110
|
+
TTinybirdType,
|
|
111
|
+
TypeModifiers & { optional: true }
|
|
112
|
+
>;
|
|
113
|
+
},
|
|
114
|
+
|
|
97
115
|
nullable() {
|
|
98
116
|
// If already has LowCardinality, we need to move Nullable inside
|
|
99
117
|
// ClickHouse requires: LowCardinality(Nullable(X)), not Nullable(LowCardinality(X))
|