/** * Project definition for Tinybird * Aggregates all datasources and pipes into a single schema */ import type { DatasourceDefinition, SchemaDefinition } from "./datasource.js"; import type { PipeDefinition, ParamsDefinition, OutputDefinition } from "./pipe.js"; import type { ConnectionDefinition } from "./connection.js"; import type { TinybirdClient } from "../client/base.js"; import type { AppendOptions, AppendResult, DatasourcesNamespace, DeleteOptions, DeleteResult, IngestOptions, IngestResult, QueryOptions, QueryResult, TruncateOptions, TruncateResult } from "../client/types.js"; import type { InferRow, InferParams, InferOutputRow } from "../infer/index.js"; import type { TokensNamespace } from "../client/tokens.js"; declare const PROJECT_BRAND: unique symbol; /** * Collection of datasource definitions */ export type DatasourcesDefinition = Record>; /** * Collection of pipe definitions */ export type PipesDefinition = Record>; /** * Collection of connection definitions */ export type ConnectionsDefinition = Record; /** * Type for a single query method */ type QueryMethod> = T extends PipeDefinition ? keyof P extends never ? () => Promise>> : (params: InferParams) => Promise>> : never; /** * Type for pipe entity accessors object * Note: At runtime, all declared pipes are included. Non-endpoint pipes throw * when queried with a clear error message. */ type PipeEntityAccessors = { [K in keyof T]: { query: QueryMethod; }; }; /** * Type for a datasource accessor with import/mutation methods */ type DatasourceAccessor> = { /** Ingest a single event row */ ingest(event: InferRow): Promise; /** Ingest multiple event rows in a batch */ ingestBatch(events: InferRow[], options?: IngestOptions): Promise; /** Append data from a URL or file */ append(options: AppendOptions): Promise; /** Replace datasource content from a URL or file */ replace(options: AppendOptions): Promise; /** Delete rows using a SQL condition */ delete(options: DeleteOptions): Promise; /** Truncate all rows */ truncate(options?: TruncateOptions): Promise; }; /** * Type for datasource accessors object * Maps each datasource to an accessor with import/mutation methods */ type DatasourceAccessors = { [K in keyof T]: DatasourceAccessor; }; /** * Base project client interface */ interface ProjectClientBase { /** Token operations (JWT creation, etc.) */ readonly tokens: TokensNamespace; /** Datasource operations (ingest/append/replace/delete/truncate) */ readonly datasources: DatasourcesNamespace; /** Execute raw SQL queries */ sql(sql: string, options?: QueryOptions): Promise>; /** Raw client for advanced usage */ readonly client: TinybirdClient; } /** * Typed client interface for a project * Includes datasource accessors as top-level properties */ export type ProjectClient = ProjectClientBase & DatasourceAccessors & PipeEntityAccessors; /** * Configuration for createTinybirdClient */ export interface TinybirdClientConfig { /** All datasources */ datasources: TDatasources; /** All pipes */ pipes: TPipes; /** Tinybird API base URL (defaults to TINYBIRD_URL env var or https://api.tinybird.co) */ baseUrl?: string; /** Tinybird API token (defaults to TINYBIRD_TOKEN env var) */ token?: string; /** Custom fetch implementation (optional, defaults to global fetch) */ fetch?: typeof fetch; /** * Directory to use as the starting point when searching for tinybird.json config. * In monorepo setups, this should be set to the directory containing tinybird.json * to ensure the config is found regardless of where the application runs from. */ configDir?: string; /** * Enable development mode for the client. * Defaults to `process.env.NODE_ENV === "development"` if not specified. */ devMode?: boolean; } /** * Project configuration */ export interface ProjectConfig { /** All datasources in this project */ datasources?: TDatasources; /** All pipes in this project */ pipes?: TPipes; /** All connections in this project */ connections?: TConnections; } /** * A project definition with full type information */ export interface ProjectDefinition { readonly [PROJECT_BRAND]: true; /** Type marker for inference */ readonly _type: "project"; /** All datasources */ readonly datasources: TDatasources; /** All pipes */ readonly pipes: TPipes; /** All connections */ readonly connections: TConnections; /** Typed Tinybird client */ readonly tinybird: ProjectClient; } /** * Define a Tinybird project * * This aggregates all datasources and pipes into a single schema definition * that can be used for code generation and type inference. * * @param config - Project configuration with datasources and pipes * @returns A project definition * * @example * ```ts * // tinybird/schema.ts * import { defineProject } from '@tinybirdco/sdk'; * import { events, users } from './datasources'; * import { topEvents, userActivity } from './pipes'; * * export default defineProject({ * datasources: { * events, * users, * }, * pipes: { * topEvents, * userActivity, * }, * }); * ``` */ export declare function defineProject(config: ProjectConfig): ProjectDefinition; /** * Check if a value is a project definition */ export declare function isProjectDefinition(value: unknown): value is ProjectDefinition; /** * Constructor interface for Tinybird class * This allows TypeScript to infer the correct return type with typed accessors */ interface TinybirdConstructor { new (config: TinybirdClientConfig): ProjectClient; } /** * Typed Tinybird client * * Creates a client with typed pipe query and datasource methods based on * the provided datasources and pipes. * * @example * ```ts * import { Tinybird } from '@tinybirdco/sdk'; * import { pageViews, events } from './datasources'; * import { topPages } from './pipes'; * * export const tinybird = new Tinybird({ * datasources: { pageViews, events }, * pipes: { topPages }, * }); * * // Query a pipe (fully typed) * const result = await tinybird.topPages.query({ * start_date: '2024-01-01 00:00:00', * end_date: '2024-01-31 23:59:59', * }); * * // Ingest an event (fully typed) * await tinybird.pageViews.ingest({ * timestamp: '2024-01-15 10:30:00', * pathname: '/home', * session_id: 'abc123', * }); * ``` */ export declare const Tinybird: TinybirdConstructor; /** * Create a typed Tinybird client * * @deprecated Use `new Tinybird(...)` instead. This function is kept for backward compatibility. * * @param config - Client configuration with datasources and pipes * @returns A typed client with pipe query and datasource methods */ export declare function createTinybirdClient(config: TinybirdClientConfig): ProjectClient; /** * Get all datasource names from a project */ export declare function getDatasourceNames(project: T): (keyof T["datasources"])[]; /** * Get all pipe names from a project */ export declare function getPipeNames(project: T): (keyof T["pipes"])[]; /** * Get a datasource by name from a project */ export declare function getDatasource(project: ProjectDefinition, name: K): TDatasources[K]; /** * Get a pipe by name from a project */ export declare function getPipe(project: ProjectDefinition, name: K): TPipes[K]; /** * Helper type to extract datasources from a project */ export type ExtractDatasources = T extends ProjectDefinition ? D : never; /** * Helper type to extract pipes from a project */ export type ExtractPipes = T extends ProjectDefinition ? P : never; /** * Data model type derived from a project * Useful for generating typed clients */ export type DataModel = { datasources: { [K in keyof T["datasources"]]: T["datasources"][K] extends DatasourceDefinition ? S : never; }; pipes: { [K in keyof T["pipes"]]: T["pipes"][K] extends PipeDefinition ? { params: P; output: O; } : never; }; }; export {}; //# sourceMappingURL=project.d.ts.map