@tinybirdco/sdk 0.0.68 → 0.0.70
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 +3 -0
- package/dist/cli/commands/migrate.test.js +47 -0
- package/dist/cli/commands/migrate.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/generator/pipe.d.ts.map +1 -1
- package/dist/generator/pipe.js +4 -1
- package/dist/generator/pipe.js.map +1 -1
- package/dist/generator/pipe.test.js +19 -0
- package/dist/generator/pipe.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/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/package.json +1 -1
- package/src/cli/commands/migrate.test.ts +75 -0
- 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/generator/pipe.test.ts +21 -0
- package/src/generator/pipe.ts +5 -0
- package/src/index.ts +2 -0
- 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
|
@@ -2,6 +2,7 @@ import * as fs from "node:fs";
|
|
|
2
2
|
import * as os from "node:os";
|
|
3
3
|
import * as path from "node:path";
|
|
4
4
|
import { afterEach, describe, expect, it } from "vitest";
|
|
5
|
+
import { buildFromInclude } from "../../generator/index.js";
|
|
5
6
|
import { runMigrate } from "./migrate.js";
|
|
6
7
|
|
|
7
8
|
function writeFile(dir: string, relativePath: string, content: string): void {
|
|
@@ -1284,6 +1285,35 @@ TYPE endpoint
|
|
|
1284
1285
|
expect(output).not.toContain(", engine,");
|
|
1285
1286
|
});
|
|
1286
1287
|
|
|
1288
|
+
it("migrates datasource with Null engine", async () => {
|
|
1289
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "tinybird-migrate-"));
|
|
1290
|
+
tempDirs.push(tempDir);
|
|
1291
|
+
|
|
1292
|
+
writeFile(
|
|
1293
|
+
tempDir,
|
|
1294
|
+
"null_source.datasource",
|
|
1295
|
+
`SCHEMA >
|
|
1296
|
+
id String,
|
|
1297
|
+
timestamp DateTime
|
|
1298
|
+
|
|
1299
|
+
ENGINE Null
|
|
1300
|
+
`
|
|
1301
|
+
);
|
|
1302
|
+
|
|
1303
|
+
const result = await runMigrate({
|
|
1304
|
+
cwd: tempDir,
|
|
1305
|
+
patterns: ["."],
|
|
1306
|
+
strict: true,
|
|
1307
|
+
});
|
|
1308
|
+
|
|
1309
|
+
expect(result.success).toBe(true);
|
|
1310
|
+
expect(result.errors).toHaveLength(0);
|
|
1311
|
+
|
|
1312
|
+
const output = fs.readFileSync(result.outputPath, "utf-8");
|
|
1313
|
+
expect(output).toContain('export const nullSource = defineDatasource("null_source", {');
|
|
1314
|
+
expect(output).toContain("engine: engine.null(),");
|
|
1315
|
+
});
|
|
1316
|
+
|
|
1287
1317
|
it("infers MergeTree when engine options exist without ENGINE directive", async () => {
|
|
1288
1318
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "tinybird-migrate-"));
|
|
1289
1319
|
tempDirs.push(tempDir);
|
|
@@ -1510,6 +1540,51 @@ TYPE endpoint
|
|
|
1510
1540
|
expect(output).toContain("{{ Int32(days) }}");
|
|
1511
1541
|
});
|
|
1512
1542
|
|
|
1543
|
+
it("escapes pipe SQL backslashes in migrated TypeScript template literals", async () => {
|
|
1544
|
+
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "tinybird-migrate-"));
|
|
1545
|
+
tempDirs.push(tempDir);
|
|
1546
|
+
|
|
1547
|
+
writeFile(
|
|
1548
|
+
tempDir,
|
|
1549
|
+
"escape_percent.pipe",
|
|
1550
|
+
String.raw`NODE endpoint
|
|
1551
|
+
SQL >
|
|
1552
|
+
%
|
|
1553
|
+
SELECT replaceAll({{ String(value) }}, '\\%', '%') AS value
|
|
1554
|
+
TYPE endpoint
|
|
1555
|
+
`
|
|
1556
|
+
);
|
|
1557
|
+
|
|
1558
|
+
const result = await runMigrate({
|
|
1559
|
+
cwd: tempDir,
|
|
1560
|
+
patterns: ["."],
|
|
1561
|
+
strict: true,
|
|
1562
|
+
});
|
|
1563
|
+
|
|
1564
|
+
expect(result.success).toBe(true);
|
|
1565
|
+
expect(result.errors).toHaveLength(0);
|
|
1566
|
+
|
|
1567
|
+
const output = fs.readFileSync(result.outputPath, "utf-8");
|
|
1568
|
+
expect(output).toContain(
|
|
1569
|
+
String.raw`SELECT replaceAll({{ String(value) }}, '\\\\%', '%') AS value`
|
|
1570
|
+
);
|
|
1571
|
+
|
|
1572
|
+
fs.writeFileSync(
|
|
1573
|
+
result.outputPath,
|
|
1574
|
+
output.replace("@tinybirdco/sdk", path.resolve(process.cwd(), "src/index.ts"))
|
|
1575
|
+
);
|
|
1576
|
+
|
|
1577
|
+
const build = await buildFromInclude({
|
|
1578
|
+
cwd: tempDir,
|
|
1579
|
+
includePaths: [result.outputPath],
|
|
1580
|
+
});
|
|
1581
|
+
|
|
1582
|
+
expect(build.resources.pipes).toHaveLength(1);
|
|
1583
|
+
expect(build.resources.pipes[0]?.content).toContain(
|
|
1584
|
+
String.raw`SELECT replaceAll({{ String(value) }}, '\\%', '%') AS value`
|
|
1585
|
+
);
|
|
1586
|
+
});
|
|
1587
|
+
|
|
1513
1588
|
it("migrates datasource with mixed explicit and default json paths", async () => {
|
|
1514
1589
|
const tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "tinybird-migrate-"));
|
|
1515
1590
|
tempDirs.push(tempDir);
|
|
@@ -187,6 +187,13 @@ describe("generateEngineCode", () => {
|
|
|
187
187
|
expect(code).toContain('version: "version"');
|
|
188
188
|
});
|
|
189
189
|
|
|
190
|
+
it("generates Null engine", () => {
|
|
191
|
+
const code = generateEngineCode({
|
|
192
|
+
type: "Null",
|
|
193
|
+
});
|
|
194
|
+
expect(code).toBe("engine.null()");
|
|
195
|
+
});
|
|
196
|
+
|
|
190
197
|
it("defaults to mergeTree for unknown engine types", () => {
|
|
191
198
|
const code = generateEngineCode({
|
|
192
199
|
type: "UnknownEngine",
|
package/src/codegen/utils.ts
CHANGED
|
@@ -81,6 +81,10 @@ export function generateEngineCode(engine: {
|
|
|
81
81
|
version?: string;
|
|
82
82
|
summing_columns?: string;
|
|
83
83
|
}): string {
|
|
84
|
+
if (engine.type === "Null") {
|
|
85
|
+
return "engine.null()";
|
|
86
|
+
}
|
|
87
|
+
|
|
84
88
|
const sortingKey = parseSortingKey(engine.sorting_key);
|
|
85
89
|
|
|
86
90
|
// Build options object
|
|
@@ -143,6 +147,7 @@ export function generateEngineCode(engine: {
|
|
|
143
147
|
AggregatingMergeTree: "engine.aggregatingMergeTree",
|
|
144
148
|
CollapsingMergeTree: "engine.collapsingMergeTree",
|
|
145
149
|
VersionedCollapsingMergeTree: "engine.versionedCollapsingMergeTree",
|
|
150
|
+
Null: "engine.null",
|
|
146
151
|
};
|
|
147
152
|
|
|
148
153
|
const engineFunc = engineFunctionMap[engine.type] ?? "engine.mergeTree";
|
|
@@ -51,6 +51,19 @@ describe('Datasource Generator', () => {
|
|
|
51
51
|
expect(result.content).toContain('ENGINE_SORTING_KEY "id"');
|
|
52
52
|
});
|
|
53
53
|
|
|
54
|
+
it('includes Null engine configuration without sorting key', () => {
|
|
55
|
+
const ds = defineDatasource('test_ds', {
|
|
56
|
+
schema: {
|
|
57
|
+
id: t.string(),
|
|
58
|
+
},
|
|
59
|
+
engine: engine.null(),
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const result = generateDatasource(ds);
|
|
63
|
+
expect(result.content).toContain('ENGINE Null');
|
|
64
|
+
expect(result.content).not.toContain('ENGINE_SORTING_KEY');
|
|
65
|
+
});
|
|
66
|
+
|
|
54
67
|
it('includes partition key in engine config', () => {
|
|
55
68
|
const ds = defineDatasource('test_ds', {
|
|
56
69
|
schema: {
|
|
@@ -306,6 +306,27 @@ WHERE workspaceId = {{ String(workspaceId) }}
|
|
|
306
306
|
);
|
|
307
307
|
});
|
|
308
308
|
|
|
309
|
+
it('does not emit unsupported keyword args for column params', () => {
|
|
310
|
+
const pipe = definePipe('visitor_filters', {
|
|
311
|
+
params: {
|
|
312
|
+
orderBy: p.column().optional('lastSeen').describe('Column used for sorting'),
|
|
313
|
+
},
|
|
314
|
+
nodes: [
|
|
315
|
+
node({
|
|
316
|
+
name: 'endpoint',
|
|
317
|
+
sql: 'SELECT * FROM visitors ORDER BY {{ column(orderBy) }}',
|
|
318
|
+
}),
|
|
319
|
+
],
|
|
320
|
+
output: simpleOutput,
|
|
321
|
+
endpoint: true,
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
const result = generatePipe(pipe);
|
|
325
|
+
expect(result.content).toContain("{{ column(orderBy, 'lastSeen') }}");
|
|
326
|
+
expect(result.content).not.toContain('column(orderBy, \'lastSeen\', required=False)');
|
|
327
|
+
expect(result.content).not.toContain('description="Column used for sorting"');
|
|
328
|
+
});
|
|
329
|
+
|
|
309
330
|
it('emits param metadata for defineEndpoint helper', () => {
|
|
310
331
|
const pipe = defineEndpoint('test_endpoint', {
|
|
311
332
|
params: {
|
package/src/generator/pipe.ts
CHANGED
|
@@ -22,6 +22,7 @@ import type { AnyParamValidator } from "../schema/params.js";
|
|
|
22
22
|
import {
|
|
23
23
|
getParamDefault,
|
|
24
24
|
getParamDescription,
|
|
25
|
+
getParamTinybirdType,
|
|
25
26
|
getParamRequiredModifier,
|
|
26
27
|
isParamRequired,
|
|
27
28
|
} from "../schema/params.js";
|
|
@@ -134,6 +135,10 @@ function buildParamTemplateArgs(
|
|
|
134
135
|
nextArgs.push(toTemplateDefaultLiteral(defaultValue as string | number | boolean));
|
|
135
136
|
}
|
|
136
137
|
|
|
138
|
+
if (getParamTinybirdType(validator) === "column") {
|
|
139
|
+
return nextArgs;
|
|
140
|
+
}
|
|
141
|
+
|
|
137
142
|
const requiredModifier = getParamRequiredModifier(validator);
|
|
138
143
|
if (requiredModifier && !hasKeywordArg(nextArgs, "required")) {
|
|
139
144
|
nextArgs.push(`required=${isParamRequired(validator) ? "True" : "False"}`);
|
package/src/index.ts
CHANGED
|
@@ -88,6 +88,7 @@ export {
|
|
|
88
88
|
export { engine, getEngineClause, getSortingKey, getPrimaryKey } from "./schema/engines.js";
|
|
89
89
|
export type {
|
|
90
90
|
EngineConfig,
|
|
91
|
+
MergeTreeEngineConfig,
|
|
91
92
|
BaseMergeTreeConfig,
|
|
92
93
|
MergeTreeConfig,
|
|
93
94
|
ReplacingMergeTreeConfig,
|
|
@@ -95,6 +96,7 @@ export type {
|
|
|
95
96
|
AggregatingMergeTreeConfig,
|
|
96
97
|
CollapsingMergeTreeConfig,
|
|
97
98
|
VersionedCollapsingMergeTreeConfig,
|
|
99
|
+
NullEngineConfig,
|
|
98
100
|
} from "./schema/engines.js";
|
|
99
101
|
|
|
100
102
|
// ============ Utilities ============
|
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) {
|