@tinybirdco/sdk 0.0.48 → 0.0.49

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 (52) hide show
  1. package/README.md +53 -3
  2. package/dist/cli/commands/migrate.d.ts.map +1 -1
  3. package/dist/cli/commands/migrate.js +32 -0
  4. package/dist/cli/commands/migrate.js.map +1 -1
  5. package/dist/cli/commands/migrate.test.js +398 -1
  6. package/dist/cli/commands/migrate.test.js.map +1 -1
  7. package/dist/generator/pipe.d.ts.map +1 -1
  8. package/dist/generator/pipe.js +31 -1
  9. package/dist/generator/pipe.js.map +1 -1
  10. package/dist/generator/pipe.test.js +50 -1
  11. package/dist/generator/pipe.test.js.map +1 -1
  12. package/dist/index.d.ts +2 -2
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/index.js +1 -1
  15. package/dist/index.js.map +1 -1
  16. package/dist/migrate/emit-ts.d.ts.map +1 -1
  17. package/dist/migrate/emit-ts.js +50 -9
  18. package/dist/migrate/emit-ts.js.map +1 -1
  19. package/dist/migrate/parse-datasource.d.ts.map +1 -1
  20. package/dist/migrate/parse-datasource.js +77 -49
  21. package/dist/migrate/parse-datasource.js.map +1 -1
  22. package/dist/migrate/parse-pipe.d.ts.map +1 -1
  23. package/dist/migrate/parse-pipe.js +254 -44
  24. package/dist/migrate/parse-pipe.js.map +1 -1
  25. package/dist/migrate/parser-utils.d.ts +5 -0
  26. package/dist/migrate/parser-utils.d.ts.map +1 -1
  27. package/dist/migrate/parser-utils.js +22 -0
  28. package/dist/migrate/parser-utils.js.map +1 -1
  29. package/dist/migrate/types.d.ts +22 -3
  30. package/dist/migrate/types.d.ts.map +1 -1
  31. package/dist/schema/datasource.test.js +1 -0
  32. package/dist/schema/datasource.test.js.map +1 -1
  33. package/dist/schema/pipe.d.ts +90 -3
  34. package/dist/schema/pipe.d.ts.map +1 -1
  35. package/dist/schema/pipe.js +84 -0
  36. package/dist/schema/pipe.js.map +1 -1
  37. package/dist/schema/pipe.test.js +70 -1
  38. package/dist/schema/pipe.test.js.map +1 -1
  39. package/package.json +1 -1
  40. package/src/cli/commands/migrate.test.ts +580 -1
  41. package/src/cli/commands/migrate.ts +35 -0
  42. package/src/generator/pipe.test.ts +56 -1
  43. package/src/generator/pipe.ts +41 -1
  44. package/src/index.ts +9 -0
  45. package/src/migrate/emit-ts.ts +52 -10
  46. package/src/migrate/parse-datasource.ts +82 -68
  47. package/src/migrate/parse-pipe.ts +359 -66
  48. package/src/migrate/parser-utils.ts +36 -1
  49. package/src/migrate/types.ts +25 -3
  50. package/src/schema/datasource.test.ts +1 -0
  51. package/src/schema/pipe.test.ts +89 -0
  52. package/src/schema/pipe.ts +188 -4
@@ -1,17 +1,21 @@
1
1
  import { describe, it, expect } from "vitest";
2
2
  import {
3
3
  definePipe,
4
+ defineSinkPipe,
4
5
  defineMaterializedView,
5
6
  node,
6
7
  isPipeDefinition,
7
8
  getEndpointConfig,
8
9
  getMaterializedConfig,
10
+ getSinkConfig,
9
11
  isMaterializedView,
12
+ isSinkPipe,
10
13
  getNodeNames,
11
14
  getNode,
12
15
  sql,
13
16
  } from "./pipe.js";
14
17
  import { defineDatasource } from "./datasource.js";
18
+ import { defineKafkaConnection, defineS3Connection } from "./connection.js";
15
19
  import { t } from "./types.js";
16
20
  import { p } from "./params.js";
17
21
  import { engine } from "./engines.js";
@@ -154,6 +158,91 @@ describe("Pipe Schema", () => {
154
158
  });
155
159
  });
156
160
 
161
+ describe("Sink pipes", () => {
162
+ it("creates a Kafka sink pipe", () => {
163
+ const kafka = defineKafkaConnection("events_kafka", {
164
+ bootstrapServers: "localhost:9092",
165
+ });
166
+
167
+ const pipe = defineSinkPipe("events_sink", {
168
+ nodes: [node({ name: "publish", sql: "SELECT * FROM events" })],
169
+ sink: {
170
+ connection: kafka,
171
+ topic: "events_out",
172
+ schedule: "@on-demand",
173
+ },
174
+ });
175
+
176
+ const sink = getSinkConfig(pipe);
177
+ expect(sink).toBeTruthy();
178
+ expect(sink && "topic" in sink ? sink.topic : undefined).toBe("events_out");
179
+ expect(isSinkPipe(pipe)).toBe(true);
180
+ });
181
+
182
+ it("creates an S3 sink pipe", () => {
183
+ const s3 = defineS3Connection("exports_s3", {
184
+ region: "us-east-1",
185
+ arn: "arn:aws:iam::123456789012:role/tinybird-s3-access",
186
+ });
187
+
188
+ const pipe = defineSinkPipe("exports_sink", {
189
+ nodes: [node({ name: "export", sql: "SELECT * FROM events" })],
190
+ sink: {
191
+ connection: s3,
192
+ bucketUri: "s3://exports/events/",
193
+ fileTemplate: "events_{date}",
194
+ schedule: "@once",
195
+ format: "csv",
196
+ strategy: "create_new",
197
+ compression: "gzip",
198
+ },
199
+ });
200
+
201
+ const sink = getSinkConfig(pipe);
202
+ expect(sink).toBeTruthy();
203
+ expect(sink && "bucketUri" in sink ? sink.bucketUri : undefined).toBe("s3://exports/events/");
204
+ expect(isSinkPipe(pipe)).toBe(true);
205
+ });
206
+
207
+ it("throws when Kafka sink connection type is invalid", () => {
208
+ const s3 = defineS3Connection("exports_s3", {
209
+ region: "us-east-1",
210
+ arn: "arn:aws:iam::123456789012:role/tinybird-s3-access",
211
+ });
212
+
213
+ expect(() =>
214
+ defineSinkPipe("bad_sink", {
215
+ nodes: [node({ name: "export", sql: "SELECT * FROM events" })],
216
+ sink: {
217
+ // Runtime validation rejects mismatched connection/type
218
+ connection: s3 as unknown as ReturnType<typeof defineKafkaConnection>,
219
+ topic: "events_out",
220
+ schedule: "@on-demand",
221
+ },
222
+ })
223
+ ).toThrow("requires a Kafka connection");
224
+ });
225
+
226
+ it("throws when sink configuration is passed to definePipe", () => {
227
+ const kafka = defineKafkaConnection("events_kafka", {
228
+ bootstrapServers: "localhost:9092",
229
+ });
230
+
231
+ expect(() =>
232
+ definePipe(
233
+ "bad_via_define_pipe",
234
+ {
235
+ nodes: [node({ name: "export", sql: "SELECT * FROM events" })],
236
+ sink: {
237
+ connection: kafka,
238
+ topic: "events_out",
239
+ },
240
+ } as unknown as Parameters<typeof definePipe>[1]
241
+ )
242
+ ).toThrow("must be created with defineSinkPipe");
243
+ });
244
+ });
245
+
157
246
  describe("getEndpointConfig", () => {
158
247
  it("returns null when endpoint is false", () => {
159
248
  const pipe = definePipe("my_pipe", {
@@ -9,6 +9,8 @@ import type { DatasourceDefinition, SchemaDefinition, ColumnDefinition } from ".
9
9
  import { getColumnType } from "./datasource.js";
10
10
  import { getTinybirdType } from "./types.js";
11
11
  import type { TokenDefinition, PipeTokenScope } from "./token.js";
12
+ import type { KafkaConnectionDefinition, S3ConnectionDefinition } from "./connection.js";
13
+ import { isKafkaConnectionDefinition, isS3ConnectionDefinition } from "./connection.js";
12
14
 
13
15
  /** Symbol for brand typing pipes - use Symbol.for() for global registry */
14
16
  export const PIPE_BRAND = Symbol.for("tinybird.pipe");
@@ -155,6 +157,55 @@ export interface CopyConfig<
155
157
  copy_schedule?: string;
156
158
  }
157
159
 
160
+ /**
161
+ * Sink export strategy.
162
+ * - 'create_new': write new files on each run
163
+ * - 'replace': replace destination data on each run
164
+ */
165
+ export type SinkStrategy = "create_new" | "replace";
166
+
167
+ /**
168
+ * S3 sink compression codec.
169
+ */
170
+ export type SinkCompression = "none" | "gzip" | "snappy";
171
+
172
+ /**
173
+ * Kafka sink configuration
174
+ */
175
+ export interface KafkaSinkConfig {
176
+ /** Kafka connection used to publish records */
177
+ connection: KafkaConnectionDefinition;
178
+ /** Destination Kafka topic */
179
+ topic: string;
180
+ /** Sink schedule (for example: @on-demand, @once, cron expression) */
181
+ schedule: string;
182
+ }
183
+
184
+ /**
185
+ * S3 sink configuration
186
+ */
187
+ export interface S3SinkConfig {
188
+ /** S3 connection used to write exported files */
189
+ connection: S3ConnectionDefinition;
190
+ /** Destination bucket URI (for example: s3://bucket/prefix/) */
191
+ bucketUri: string;
192
+ /** Output filename template (supports Tinybird placeholders) */
193
+ fileTemplate: string;
194
+ /** Output format (for example: csv, ndjson) */
195
+ format: string;
196
+ /** Sink schedule (for example: @on-demand, @once, cron expression) */
197
+ schedule: string;
198
+ /** Export strategy */
199
+ strategy?: SinkStrategy;
200
+ /** Compression codec */
201
+ compression?: SinkCompression;
202
+ }
203
+
204
+ /**
205
+ * Sink pipe configuration (Kafka or S3 only)
206
+ */
207
+ export type SinkConfig = KafkaSinkConfig | S3SinkConfig;
208
+
158
209
  /**
159
210
  * Inline token configuration for pipe access
160
211
  */
@@ -194,9 +245,9 @@ export interface PipeOptions<
194
245
  nodes: readonly NodeDefinition[];
195
246
  /** Output schema (optional for reusable pipes, required for endpoints) */
196
247
  output?: TOutput;
197
- /** Whether this pipe is an API endpoint (shorthand for { enabled: true }). Mutually exclusive with materialized and copy. */
248
+ /** Whether this pipe is an API endpoint (shorthand for { enabled: true }). Mutually exclusive with materialized, copy, and sink. */
198
249
  endpoint?: boolean | EndpointConfig;
199
- /** Materialized view configuration. Mutually exclusive with endpoint and copy. */
250
+ /** Materialized view configuration. Mutually exclusive with endpoint, copy, and sink. */
200
251
  materialized?: MaterializedConfig;
201
252
  /** Copy pipe configuration. Mutually exclusive with endpoint and materialized. */
202
253
  copy?: CopyConfig;
@@ -204,6 +255,33 @@ export interface PipeOptions<
204
255
  tokens?: readonly PipeTokenConfig[];
205
256
  }
206
257
 
258
+ /**
259
+ * Options for defining a sink pipe
260
+ */
261
+ export interface SinkPipeOptions<TParams extends ParamsDefinition> {
262
+ /** Human-readable description of the sink pipe */
263
+ description?: string;
264
+ /** Parameter definitions for query inputs */
265
+ params?: TParams;
266
+ /** Nodes in the transformation pipeline */
267
+ nodes: readonly NodeDefinition[];
268
+ /** Sink export configuration */
269
+ sink: SinkConfig;
270
+ /** Sink pipes are not endpoints */
271
+ endpoint?: never;
272
+ /** Sink pipes are not materialized views */
273
+ materialized?: never;
274
+ /** Sink pipes are not copy pipes */
275
+ copy?: never;
276
+ /** Access tokens for this sink pipe */
277
+ tokens?: readonly PipeTokenConfig[];
278
+ }
279
+
280
+ type PipeRuntimeOptions<
281
+ TParams extends ParamsDefinition,
282
+ TOutput extends OutputDefinition
283
+ > = (PipeOptions<TParams, TOutput> & { sink?: never }) | SinkPipeOptions<TParams>;
284
+
207
285
  /**
208
286
  * Options for defining an endpoint (API-exposed pipe)
209
287
  */
@@ -277,7 +355,7 @@ export interface PipeDefinition<
277
355
  /** Output schema (optional for reusable pipes) */
278
356
  readonly _output?: TOutput;
279
357
  /** Full options */
280
- readonly options: PipeOptions<TParams, TOutput>;
358
+ readonly options: PipeRuntimeOptions<TParams, TOutput>;
281
359
  }
282
360
 
283
361
  /**
@@ -428,6 +506,52 @@ function validateMaterializedSchema(
428
506
  }
429
507
  }
430
508
 
509
+ function validateSinkConfig(pipeName: string, sink: SinkConfig): void {
510
+ if ("topic" in sink) {
511
+ if (!isKafkaConnectionDefinition(sink.connection)) {
512
+ throw new Error(
513
+ `Pipe "${pipeName}" sink with topic requires a Kafka connection.`
514
+ );
515
+ }
516
+ if (typeof sink.topic !== "string" || !sink.topic.trim()) {
517
+ throw new Error(`Pipe "${pipeName}" sink topic cannot be empty.`);
518
+ }
519
+ if (typeof sink.schedule !== "string" || !sink.schedule.trim()) {
520
+ throw new Error(`Pipe "${pipeName}" sink schedule cannot be empty.`);
521
+ }
522
+ return;
523
+ }
524
+
525
+ if (!isS3ConnectionDefinition(sink.connection)) {
526
+ throw new Error(
527
+ `Pipe "${pipeName}" S3 sink requires an S3 connection.`
528
+ );
529
+ }
530
+ if (typeof sink.bucketUri !== "string" || !sink.bucketUri.trim()) {
531
+ throw new Error(`Pipe "${pipeName}" sink bucketUri cannot be empty.`);
532
+ }
533
+ if (typeof sink.fileTemplate !== "string" || !sink.fileTemplate.trim()) {
534
+ throw new Error(`Pipe "${pipeName}" sink fileTemplate cannot be empty.`);
535
+ }
536
+ if (typeof sink.format !== "string" || !sink.format.trim()) {
537
+ throw new Error(`Pipe "${pipeName}" sink format cannot be empty.`);
538
+ }
539
+ if (typeof sink.schedule !== "string" || !sink.schedule.trim()) {
540
+ throw new Error(`Pipe "${pipeName}" sink schedule cannot be empty.`);
541
+ }
542
+ if (sink.strategy && sink.strategy !== "create_new" && sink.strategy !== "replace") {
543
+ throw new Error(`Pipe "${pipeName}" sink strategy must be "create_new" or "replace".`);
544
+ }
545
+ if (
546
+ sink.compression &&
547
+ sink.compression !== "none" &&
548
+ sink.compression !== "gzip" &&
549
+ sink.compression !== "snappy"
550
+ ) {
551
+ throw new Error(`Pipe "${pipeName}" sink compression must be "none", "gzip", or "snappy".`);
552
+ }
553
+ }
554
+
431
555
  export function definePipe<
432
556
  TParams extends ParamsDefinition,
433
557
  TOutput extends OutputDefinition
@@ -447,6 +571,12 @@ export function definePipe<
447
571
  throw new Error(`Pipe "${name}" must have at least one node.`);
448
572
  }
449
573
 
574
+ if ("sink" in (options as unknown as object)) {
575
+ throw new Error(
576
+ `Pipe "${name}" sink configuration must be created with defineSinkPipe().`
577
+ );
578
+ }
579
+
450
580
  // Validate output is provided for endpoints and materialized views
451
581
  if ((options.endpoint || options.materialized) && (!options.output || Object.keys(options.output).length === 0)) {
452
582
  throw new Error(
@@ -480,7 +610,47 @@ export function definePipe<
480
610
  options: {
481
611
  ...options,
482
612
  params,
483
- },
613
+ } as PipeRuntimeOptions<TParams, TOutput>,
614
+ };
615
+ }
616
+
617
+ /**
618
+ * Define a Tinybird sink pipe
619
+ *
620
+ * Sink pipes export query results to external systems via Kafka or S3.
621
+ *
622
+ * @param name - The sink pipe name (must be valid identifier)
623
+ * @param options - Sink pipe configuration
624
+ * @returns A pipe definition configured as a sink pipe
625
+ */
626
+ export function defineSinkPipe<TParams extends ParamsDefinition>(
627
+ name: string,
628
+ options: SinkPipeOptions<TParams>
629
+ ): PipeDefinition<TParams, Record<string, never>> {
630
+ if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(name)) {
631
+ throw new Error(
632
+ `Invalid pipe name: "${name}". Must start with a letter or underscore and contain only alphanumeric characters and underscores.`
633
+ );
634
+ }
635
+
636
+ if (!options.nodes || options.nodes.length === 0) {
637
+ throw new Error(`Pipe "${name}" must have at least one node.`);
638
+ }
639
+
640
+ validateSinkConfig(name, options.sink);
641
+
642
+ const params = (options.params ?? {}) as TParams;
643
+
644
+ return {
645
+ [PIPE_BRAND]: true,
646
+ _name: name,
647
+ _type: "pipe",
648
+ _params: params,
649
+ _output: undefined,
650
+ options: {
651
+ ...options,
652
+ params,
653
+ } as PipeRuntimeOptions<TParams, Record<string, never>>,
484
654
  };
485
655
  }
486
656
 
@@ -790,6 +960,20 @@ export function isCopyPipe(pipe: PipeDefinition): boolean {
790
960
  return pipe.options.copy !== undefined;
791
961
  }
792
962
 
963
+ /**
964
+ * Get the sink configuration from a pipe
965
+ */
966
+ export function getSinkConfig(pipe: PipeDefinition): SinkConfig | null {
967
+ return "sink" in pipe.options ? (pipe.options.sink ?? null) : null;
968
+ }
969
+
970
+ /**
971
+ * Check if a pipe is a sink pipe
972
+ */
973
+ export function isSinkPipe(pipe: PipeDefinition): boolean {
974
+ return pipe.options.sink !== undefined;
975
+ }
976
+
793
977
  /**
794
978
  * Get all node names from a pipe
795
979
  */