@tinybirdco/sdk 0.0.35 → 0.0.37

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/api.d.ts +17 -1
  2. package/dist/api/api.d.ts.map +1 -1
  3. package/dist/api/api.js +91 -0
  4. package/dist/api/api.js.map +1 -1
  5. package/dist/api/api.test.js +160 -0
  6. package/dist/api/api.test.js.map +1 -1
  7. package/dist/api/regions.d.ts +33 -0
  8. package/dist/api/regions.d.ts.map +1 -0
  9. package/dist/api/regions.js +52 -0
  10. package/dist/api/regions.js.map +1 -0
  11. package/dist/api/regions.test.d.ts +2 -0
  12. package/dist/api/regions.test.d.ts.map +1 -0
  13. package/dist/api/regions.test.js +69 -0
  14. package/dist/api/regions.test.js.map +1 -0
  15. package/dist/cli/commands/init.d.ts +2 -0
  16. package/dist/cli/commands/init.d.ts.map +1 -1
  17. package/dist/cli/commands/init.js +25 -11
  18. package/dist/cli/commands/init.js.map +1 -1
  19. package/dist/cli/commands/init.test.js +7 -0
  20. package/dist/cli/commands/init.test.js.map +1 -1
  21. package/dist/cli/commands/login.d.ts.map +1 -1
  22. package/dist/cli/commands/login.js +13 -3
  23. package/dist/cli/commands/login.js.map +1 -1
  24. package/dist/cli/commands/login.test.js +12 -1
  25. package/dist/cli/commands/login.test.js.map +1 -1
  26. package/dist/cli/region-selector.d.ts +39 -0
  27. package/dist/cli/region-selector.d.ts.map +1 -0
  28. package/dist/cli/region-selector.js +118 -0
  29. package/dist/cli/region-selector.js.map +1 -0
  30. package/dist/cli/region-selector.test.d.ts +2 -0
  31. package/dist/cli/region-selector.test.d.ts.map +1 -0
  32. package/dist/cli/region-selector.test.js +176 -0
  33. package/dist/cli/region-selector.test.js.map +1 -0
  34. package/dist/client/base.d.ts +26 -1
  35. package/dist/client/base.d.ts.map +1 -1
  36. package/dist/client/base.js +39 -0
  37. package/dist/client/base.js.map +1 -1
  38. package/dist/client/base.test.js +25 -0
  39. package/dist/client/base.test.js.map +1 -1
  40. package/dist/client/types.d.ts +49 -0
  41. package/dist/client/types.d.ts.map +1 -1
  42. package/dist/index.d.ts +2 -2
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js.map +1 -1
  45. package/dist/schema/project.d.ts +22 -3
  46. package/dist/schema/project.d.ts.map +1 -1
  47. package/dist/schema/project.js +12 -0
  48. package/dist/schema/project.js.map +1 -1
  49. package/dist/schema/project.test.js +52 -0
  50. package/dist/schema/project.test.js.map +1 -1
  51. package/package.json +1 -1
  52. package/src/api/api.test.ts +222 -0
  53. package/src/api/api.ts +117 -0
  54. package/src/api/regions.test.ts +89 -0
  55. package/src/api/regions.ts +75 -0
  56. package/src/cli/commands/init.test.ts +8 -0
  57. package/src/cli/commands/init.ts +30 -11
  58. package/src/cli/commands/login.test.ts +13 -1
  59. package/src/cli/commands/login.ts +14 -3
  60. package/src/cli/region-selector.test.ts +227 -0
  61. package/src/cli/region-selector.ts +151 -0
  62. package/src/client/base.test.ts +32 -0
  63. package/src/client/base.ts +48 -0
  64. package/src/client/types.ts +54 -0
  65. package/src/index.ts +5 -0
  66. package/src/schema/project.test.ts +64 -0
  67. package/src/schema/project.ts +42 -3
@@ -189,3 +189,57 @@ export interface TypedDatasourceIngest<TRow> {
189
189
  /** Send multiple events in a batch */
190
190
  sendBatch(events: TRow[], options?: IngestOptions): Promise<IngestResult>;
191
191
  }
192
+
193
+ /**
194
+ * Data format for append operations
195
+ */
196
+ export type AppendFormat = "csv" | "ndjson" | "parquet";
197
+
198
+ /**
199
+ * CSV dialect options for customizing CSV parsing
200
+ */
201
+ export interface CsvDialectOptions {
202
+ /** Field delimiter character (default: ',') */
203
+ delimiter?: string;
204
+ /** New line character(s) (default: '\n') */
205
+ newLine?: string;
206
+ /** Escape character for special characters */
207
+ escapeChar?: string;
208
+ }
209
+
210
+ /**
211
+ * Options for append operation
212
+ * Exactly one of `url` or `file` must be provided
213
+ */
214
+ export interface AppendOptions {
215
+ /** Remote URL to import data from */
216
+ url?: string;
217
+ /** Local file path to import data from */
218
+ file?: string;
219
+ /** CSV dialect options (only applicable for csv format) */
220
+ csvDialect?: CsvDialectOptions;
221
+ /** Request timeout in milliseconds */
222
+ timeout?: number;
223
+ /** AbortController signal for cancellation */
224
+ signal?: AbortSignal;
225
+ }
226
+
227
+ /**
228
+ * Result of an append operation
229
+ */
230
+ export interface AppendResult {
231
+ /** Number of rows successfully appended */
232
+ successful_rows: number;
233
+ /** Number of rows that failed to append (quarantined) */
234
+ quarantined_rows: number;
235
+ /** Import ID for tracking */
236
+ import_id?: string;
237
+ }
238
+
239
+ /**
240
+ * Datasources namespace interface for raw client
241
+ */
242
+ export interface DatasourcesNamespace {
243
+ /** Append data to a datasource from a URL or file */
244
+ append(datasourceName: string, options: AppendOptions): Promise<AppendResult>;
245
+ }
package/src/index.ts CHANGED
@@ -202,8 +202,12 @@ export type {
202
202
  export { TinybirdClient, createClient } from "./client/base.js";
203
203
  export { TinybirdError } from "./client/types.js";
204
204
  export type {
205
+ AppendOptions,
206
+ AppendResult,
205
207
  ClientConfig,
206
208
  ClientContext,
209
+ CsvDialectOptions,
210
+ DatasourcesNamespace,
207
211
  QueryResult,
208
212
  IngestResult,
209
213
  QueryOptions,
@@ -226,6 +230,7 @@ export type {
226
230
  TinybirdApiConfig,
227
231
  TinybirdApiQueryOptions,
228
232
  TinybirdApiIngestOptions,
233
+ TinybirdApiAppendOptions,
229
234
  TinybirdApiRequestInit,
230
235
  } from "./api/api.js";
231
236
 
@@ -93,6 +93,37 @@ describe("Project Schema", () => {
93
93
  expect(typeof project.tinybird.ingest.eventsBatch).toBe("function");
94
94
  });
95
95
 
96
+ it("creates datasource accessors with append method", () => {
97
+ const events = defineDatasource("events", {
98
+ schema: { timestamp: t.dateTime() },
99
+ });
100
+
101
+ const project = defineProject({
102
+ datasources: { events },
103
+ });
104
+
105
+ expect(project.tinybird.events).toBeDefined();
106
+ expect(typeof project.tinybird.events.append).toBe("function");
107
+ });
108
+
109
+ it("creates multiple datasource accessors", () => {
110
+ const events = defineDatasource("events", {
111
+ schema: { timestamp: t.dateTime() },
112
+ });
113
+ const pageViews = defineDatasource("page_views", {
114
+ schema: { pathname: t.string() },
115
+ });
116
+
117
+ const project = defineProject({
118
+ datasources: { events, pageViews },
119
+ });
120
+
121
+ expect(project.tinybird.events).toBeDefined();
122
+ expect(project.tinybird.pageViews).toBeDefined();
123
+ expect(typeof project.tinybird.events.append).toBe("function");
124
+ expect(typeof project.tinybird.pageViews.append).toBe("function");
125
+ });
126
+
96
127
  it("throws error when accessing client before initialization", () => {
97
128
  const project = defineProject({});
98
129
 
@@ -269,6 +300,39 @@ describe("Project Schema", () => {
269
300
  expect(typeof client.ingest.eventsBatch).toBe("function");
270
301
  });
271
302
 
303
+ it("creates datasource accessors with append method", () => {
304
+ const events = defineDatasource("events", {
305
+ schema: { id: t.string() },
306
+ });
307
+
308
+ const client = createTinybirdClient({
309
+ datasources: { events },
310
+ pipes: {},
311
+ });
312
+
313
+ expect(client.events).toBeDefined();
314
+ expect(typeof client.events.append).toBe("function");
315
+ });
316
+
317
+ it("creates multiple datasource accessors", () => {
318
+ const events = defineDatasource("events", {
319
+ schema: { id: t.string() },
320
+ });
321
+ const pageViews = defineDatasource("page_views", {
322
+ schema: { pathname: t.string() },
323
+ });
324
+
325
+ const client = createTinybirdClient({
326
+ datasources: { events, pageViews },
327
+ pipes: {},
328
+ });
329
+
330
+ expect(client.events).toBeDefined();
331
+ expect(client.pageViews).toBeDefined();
332
+ expect(typeof client.events.append).toBe("function");
333
+ expect(typeof client.pageViews.append).toBe("function");
334
+ });
335
+
272
336
  it("accepts devMode option", () => {
273
337
  const events = defineDatasource("events", {
274
338
  schema: { id: t.string() },
@@ -8,7 +8,7 @@ import type { PipeDefinition, ParamsDefinition, OutputDefinition } from "./pipe.
8
8
  import type { ConnectionDefinition } from "./connection.js";
9
9
  import { getEndpointConfig } from "./pipe.js";
10
10
  import type { TinybirdClient } from "../client/base.js";
11
- import type { QueryResult } from "../client/types.js";
11
+ import type { AppendOptions, AppendResult, QueryResult } from "../client/types.js";
12
12
  import type { InferRow, InferParams, InferOutputRow } from "../infer/index.js";
13
13
 
14
14
  // Symbol for brand typing - use Symbol.for() for global registry
@@ -72,9 +72,25 @@ type IngestMethods<T extends DatasourcesDefinition> = {
72
72
  };
73
73
 
74
74
  /**
75
- * Typed client interface for a project
75
+ * Type for a datasource accessor with append method
76
+ */
77
+ type DatasourceAccessor = {
78
+ /** Append data from a URL or file */
79
+ append(options: AppendOptions): Promise<AppendResult>;
80
+ };
81
+
82
+ /**
83
+ * Type for datasource accessors object
84
+ * Maps each datasource to an accessor with append method
76
85
  */
77
- export interface ProjectClient<
86
+ type DatasourceAccessors<T extends DatasourcesDefinition> = {
87
+ [K in keyof T]: DatasourceAccessor;
88
+ };
89
+
90
+ /**
91
+ * Base project client interface
92
+ */
93
+ interface ProjectClientBase<
78
94
  TDatasources extends DatasourcesDefinition,
79
95
  TPipes extends PipesDefinition
80
96
  > {
@@ -86,6 +102,15 @@ export interface ProjectClient<
86
102
  readonly client: TinybirdClient;
87
103
  }
88
104
 
105
+ /**
106
+ * Typed client interface for a project
107
+ * Includes datasource accessors as top-level properties
108
+ */
109
+ export type ProjectClient<
110
+ TDatasources extends DatasourcesDefinition,
111
+ TPipes extends PipesDefinition
112
+ > = ProjectClientBase<TDatasources, TPipes> & DatasourceAccessors<TDatasources>;
113
+
89
114
  /**
90
115
  * Configuration for createTinybirdClient
91
116
  */
@@ -294,8 +319,22 @@ function buildProjectClient<
294
319
  };
295
320
  }
296
321
 
322
+ // Build datasource accessors for top-level access
323
+ const datasourceAccessors: Record<string, DatasourceAccessor> = {};
324
+ for (const [name, datasource] of Object.entries(datasources)) {
325
+ const tinybirdName = datasource._name;
326
+
327
+ datasourceAccessors[name] = {
328
+ append: async (options: AppendOptions) => {
329
+ const client = await getClient();
330
+ return client.datasources.append(tinybirdName, options);
331
+ },
332
+ };
333
+ }
334
+
297
335
  // Create the typed client object
298
336
  return {
337
+ ...datasourceAccessors,
299
338
  query: queryMethods,
300
339
  ingest: ingestMethods,
301
340
  get client(): TinybirdClient {