@tinybirdco/sdk 0.0.2 → 0.0.3

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 (67) hide show
  1. package/dist/api/build.d.ts +2 -0
  2. package/dist/api/build.d.ts.map +1 -1
  3. package/dist/api/build.js +13 -0
  4. package/dist/api/build.js.map +1 -1
  5. package/dist/api/build.test.js +1 -0
  6. package/dist/api/build.test.js.map +1 -1
  7. package/dist/api/deploy.d.ts.map +1 -1
  8. package/dist/api/deploy.js +3 -0
  9. package/dist/api/deploy.js.map +1 -1
  10. package/dist/api/deploy.test.js +1 -0
  11. package/dist/api/deploy.test.js.map +1 -1
  12. package/dist/generator/connection.d.ts +49 -0
  13. package/dist/generator/connection.d.ts.map +1 -0
  14. package/dist/generator/connection.js +78 -0
  15. package/dist/generator/connection.js.map +1 -0
  16. package/dist/generator/connection.test.d.ts +2 -0
  17. package/dist/generator/connection.test.d.ts.map +1 -0
  18. package/dist/generator/connection.test.js +106 -0
  19. package/dist/generator/connection.test.js.map +1 -0
  20. package/dist/generator/datasource.d.ts.map +1 -1
  21. package/dist/generator/datasource.js +20 -0
  22. package/dist/generator/datasource.js.map +1 -1
  23. package/dist/generator/datasource.test.js +92 -0
  24. package/dist/generator/datasource.test.js.map +1 -1
  25. package/dist/generator/index.d.ts +8 -2
  26. package/dist/generator/index.d.ts.map +1 -1
  27. package/dist/generator/index.js +10 -3
  28. package/dist/generator/index.js.map +1 -1
  29. package/dist/generator/loader.d.ts +8 -1
  30. package/dist/generator/loader.d.ts.map +1 -1
  31. package/dist/generator/loader.js +17 -2
  32. package/dist/generator/loader.js.map +1 -1
  33. package/dist/index.d.ts +4 -2
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +2 -0
  36. package/dist/index.js.map +1 -1
  37. package/dist/schema/connection.d.ts +83 -0
  38. package/dist/schema/connection.d.ts.map +1 -0
  39. package/dist/schema/connection.js +61 -0
  40. package/dist/schema/connection.js.map +1 -0
  41. package/dist/schema/connection.test.d.ts +2 -0
  42. package/dist/schema/connection.test.d.ts.map +1 -0
  43. package/dist/schema/connection.test.js +117 -0
  44. package/dist/schema/connection.test.js.map +1 -0
  45. package/dist/schema/datasource.d.ts +16 -0
  46. package/dist/schema/datasource.d.ts.map +1 -1
  47. package/dist/schema/datasource.js.map +1 -1
  48. package/dist/schema/project.d.ts +12 -3
  49. package/dist/schema/project.d.ts.map +1 -1
  50. package/dist/schema/project.js +2 -0
  51. package/dist/schema/project.js.map +1 -1
  52. package/package.json +1 -1
  53. package/src/api/build.test.ts +1 -0
  54. package/src/api/build.ts +20 -0
  55. package/src/api/deploy.test.ts +1 -0
  56. package/src/api/deploy.ts +3 -0
  57. package/src/generator/connection.test.ts +135 -0
  58. package/src/generator/connection.ts +104 -0
  59. package/src/generator/datasource.test.ts +108 -0
  60. package/src/generator/datasource.ts +27 -1
  61. package/src/generator/index.ts +16 -4
  62. package/src/generator/loader.ts +21 -3
  63. package/src/index.ts +12 -0
  64. package/src/schema/connection.test.ts +149 -0
  65. package/src/schema/connection.ts +123 -0
  66. package/src/schema/datasource.ts +17 -0
  67. package/src/schema/project.ts +20 -5
@@ -0,0 +1,123 @@
1
+ /**
2
+ * Connection definition for Tinybird
3
+ * Define external connections (Kafka, etc.) as TypeScript with full type safety
4
+ */
5
+
6
+ // Symbol for brand typing
7
+ const CONNECTION_BRAND = Symbol("tinybird.connection");
8
+
9
+ /**
10
+ * Kafka security protocol options
11
+ */
12
+ export type KafkaSecurityProtocol = "SASL_SSL" | "PLAINTEXT" | "SASL_PLAINTEXT";
13
+
14
+ /**
15
+ * Kafka SASL mechanism options
16
+ */
17
+ export type KafkaSaslMechanism = "PLAIN" | "SCRAM-SHA-256" | "SCRAM-SHA-512" | "OAUTHBEARER";
18
+
19
+ /**
20
+ * Options for creating a Kafka connection
21
+ */
22
+ export interface KafkaConnectionOptions {
23
+ /** Kafka bootstrap servers (host:port) */
24
+ bootstrapServers: string;
25
+ /** Security protocol (default: 'SASL_SSL') */
26
+ securityProtocol?: KafkaSecurityProtocol;
27
+ /** SASL mechanism for authentication */
28
+ saslMechanism?: KafkaSaslMechanism;
29
+ /** Kafka key/username - can use {{ tb_secret(...) }} */
30
+ key?: string;
31
+ /** Kafka secret/password - can use {{ tb_secret(...) }} */
32
+ secret?: string;
33
+ /** SSL CA certificate PEM - for private CA certs */
34
+ sslCaPem?: string;
35
+ }
36
+
37
+ /**
38
+ * Kafka-specific connection definition
39
+ */
40
+ export interface KafkaConnectionDefinition {
41
+ readonly [CONNECTION_BRAND]: true;
42
+ /** Connection name */
43
+ readonly _name: string;
44
+ /** Type marker for inference */
45
+ readonly _type: "connection";
46
+ /** Connection type */
47
+ readonly _connectionType: "kafka";
48
+ /** Kafka options */
49
+ readonly options: KafkaConnectionOptions;
50
+ }
51
+
52
+ /**
53
+ * A connection definition - union of all connection types
54
+ */
55
+ export type ConnectionDefinition = KafkaConnectionDefinition;
56
+
57
+ /**
58
+ * Create a Kafka connection
59
+ *
60
+ * @param name - The connection name (must be valid identifier)
61
+ * @param options - Kafka connection configuration
62
+ * @returns A connection definition that can be used in a project
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * import { createKafkaConnection } from '@tinybirdco/sdk';
67
+ *
68
+ * export const myKafka = createKafkaConnection('my_kafka', {
69
+ * bootstrapServers: 'kafka.example.com:9092',
70
+ * securityProtocol: 'SASL_SSL',
71
+ * saslMechanism: 'PLAIN',
72
+ * key: '{{ tb_secret("KAFKA_KEY") }}',
73
+ * secret: '{{ tb_secret("KAFKA_SECRET") }}',
74
+ * });
75
+ * ```
76
+ */
77
+ export function createKafkaConnection(
78
+ name: string,
79
+ options: KafkaConnectionOptions
80
+ ): KafkaConnectionDefinition {
81
+ // Validate name is a valid identifier
82
+ if (!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(name)) {
83
+ throw new Error(
84
+ `Invalid connection name: "${name}". Must start with a letter or underscore and contain only alphanumeric characters and underscores.`
85
+ );
86
+ }
87
+
88
+ return {
89
+ [CONNECTION_BRAND]: true,
90
+ _name: name,
91
+ _type: "connection",
92
+ _connectionType: "kafka",
93
+ options,
94
+ };
95
+ }
96
+
97
+ /**
98
+ * Check if a value is a connection definition
99
+ */
100
+ export function isConnectionDefinition(value: unknown): value is ConnectionDefinition {
101
+ return (
102
+ typeof value === "object" &&
103
+ value !== null &&
104
+ CONNECTION_BRAND in value &&
105
+ (value as Record<symbol, unknown>)[CONNECTION_BRAND] === true
106
+ );
107
+ }
108
+
109
+ /**
110
+ * Check if a value is a Kafka connection definition
111
+ */
112
+ export function isKafkaConnectionDefinition(value: unknown): value is KafkaConnectionDefinition {
113
+ return isConnectionDefinition(value) && value._connectionType === "kafka";
114
+ }
115
+
116
+ /**
117
+ * Get the connection type from a connection definition
118
+ */
119
+ export function getConnectionType<T extends ConnectionDefinition>(
120
+ connection: T
121
+ ): T["_connectionType"] {
122
+ return connection._connectionType;
123
+ }
@@ -5,6 +5,7 @@
5
5
 
6
6
  import type { AnyTypeValidator } from "./types.js";
7
7
  import type { EngineConfig } from "./engines.js";
8
+ import type { KafkaConnectionDefinition } from "./connection.js";
8
9
 
9
10
  // Symbol for brand typing
10
11
  const DATASOURCE_BRAND = Symbol("tinybird.datasource");
@@ -35,6 +36,20 @@ export interface TokenConfig {
35
36
  permissions: readonly ("READ" | "APPEND")[];
36
37
  }
37
38
 
39
+ /**
40
+ * Kafka ingestion configuration for a datasource
41
+ */
42
+ export interface KafkaConfig {
43
+ /** Kafka connection to use */
44
+ connection: KafkaConnectionDefinition;
45
+ /** Kafka topic to consume from */
46
+ topic: string;
47
+ /** Consumer group ID (optional) */
48
+ groupId?: string;
49
+ /** Where to start reading: 'earliest' or 'latest' (default: 'latest') */
50
+ autoOffsetReset?: "earliest" | "latest";
51
+ }
52
+
38
53
  /**
39
54
  * Options for defining a datasource
40
55
  */
@@ -55,6 +70,8 @@ export interface DatasourceOptions<TSchema extends SchemaDefinition> {
55
70
  * Defaults to true.
56
71
  */
57
72
  jsonPaths?: boolean;
73
+ /** Kafka ingestion configuration */
74
+ kafka?: KafkaConfig;
58
75
  }
59
76
 
60
77
  /**
@@ -5,6 +5,7 @@
5
5
 
6
6
  import type { DatasourceDefinition, SchemaDefinition } from "./datasource.js";
7
7
  import type { PipeDefinition, ParamsDefinition, OutputDefinition } from "./pipe.js";
8
+ import type { ConnectionDefinition } from "./connection.js";
8
9
  import { getEndpointConfig } from "./pipe.js";
9
10
  import type { TinybirdClient } from "../client/base.js";
10
11
  import type { QueryResult } from "../client/types.js";
@@ -23,6 +24,11 @@ export type DatasourcesDefinition = Record<string, DatasourceDefinition<SchemaDe
23
24
  */
24
25
  export type PipesDefinition = Record<string, PipeDefinition<ParamsDefinition, OutputDefinition>>;
25
26
 
27
+ /**
28
+ * Collection of connection definitions
29
+ */
30
+ export type ConnectionsDefinition = Record<string, ConnectionDefinition>;
31
+
26
32
  /**
27
33
  * Type for a single query method
28
34
  */
@@ -101,12 +107,15 @@ export interface TinybirdClientConfig<
101
107
  */
102
108
  export interface ProjectConfig<
103
109
  TDatasources extends DatasourcesDefinition = DatasourcesDefinition,
104
- TPipes extends PipesDefinition = PipesDefinition
110
+ TPipes extends PipesDefinition = PipesDefinition,
111
+ TConnections extends ConnectionsDefinition = ConnectionsDefinition
105
112
  > {
106
113
  /** All datasources in this project */
107
114
  datasources?: TDatasources;
108
115
  /** All pipes in this project */
109
116
  pipes?: TPipes;
117
+ /** All connections in this project */
118
+ connections?: TConnections;
110
119
  }
111
120
 
112
121
  /**
@@ -114,7 +123,8 @@ export interface ProjectConfig<
114
123
  */
115
124
  export interface ProjectDefinition<
116
125
  TDatasources extends DatasourcesDefinition = DatasourcesDefinition,
117
- TPipes extends PipesDefinition = PipesDefinition
126
+ TPipes extends PipesDefinition = PipesDefinition,
127
+ TConnections extends ConnectionsDefinition = ConnectionsDefinition
118
128
  > {
119
129
  readonly [PROJECT_BRAND]: true;
120
130
  /** Type marker for inference */
@@ -123,6 +133,8 @@ export interface ProjectDefinition<
123
133
  readonly datasources: TDatasources;
124
134
  /** All pipes */
125
135
  readonly pipes: TPipes;
136
+ /** All connections */
137
+ readonly connections: TConnections;
126
138
  /** Typed Tinybird client */
127
139
  readonly tinybird: ProjectClient<TDatasources, TPipes>;
128
140
  }
@@ -157,12 +169,14 @@ export interface ProjectDefinition<
157
169
  */
158
170
  export function defineProject<
159
171
  TDatasources extends DatasourcesDefinition,
160
- TPipes extends PipesDefinition
172
+ TPipes extends PipesDefinition,
173
+ TConnections extends ConnectionsDefinition
161
174
  >(
162
- config: ProjectConfig<TDatasources, TPipes>
163
- ): ProjectDefinition<TDatasources, TPipes> {
175
+ config: ProjectConfig<TDatasources, TPipes, TConnections>
176
+ ): ProjectDefinition<TDatasources, TPipes, TConnections> {
164
177
  const datasources = (config.datasources ?? {}) as TDatasources;
165
178
  const pipes = (config.pipes ?? {}) as TPipes;
179
+ const connections = (config.connections ?? {}) as TConnections;
166
180
 
167
181
  // Use the shared client builder
168
182
  const tinybird = buildProjectClient(datasources, pipes);
@@ -172,6 +186,7 @@ export function defineProject<
172
186
  _type: "project",
173
187
  datasources,
174
188
  pipes,
189
+ connections,
175
190
  tinybird,
176
191
  };
177
192
  }