This document provides a detailed reference for the TinyKit API.
Defines a schema for a Tinybird DataSource.
Signature:
defineSchema<T extends Record<string, ColumnDefinition>>(schema: T): TParameters:
schema: An object where the keys are the column names and the values are column definitions created with functions likestring(),int64(), etc.
Example:
import { defineSchema, string, int64 } from 'tinykit';
export const eventsSchema = defineSchema({
id: string('id'),
timestamp: int64('timestamp'),
event_type: string('event_type'),
});Defines a Tinybird DataSource.
Signature:
defineDataSource<TSchema extends SchemaDefinition>(
config: DataSourceConfig<TSchema>
): DataSourceConfig<TSchema>Parameters:
config: A configuration object for the DataSource.name: The name of the DataSource in Tinybird.schema: The schema definition for the DataSource, created withdefineSchema.engine: The table engine to use for the DataSource (e.g.,MergeTree).sortingKey: An array of column names to use as the sorting key.partitionBy: The partitioning scheme for the table.ttl: The Time-to-Live (TTL) expression for data in the table.version: The version of the DataSource, used for migrations.
Example:
import { defineDataSource } from 'tinykit';
import { eventsSchema } from './schema';
export const eventsDataSource = defineDataSource({
name: 'events__v1',
version: 1,
schema: eventsSchema,
engine: 'MergeTree',
sortingKey: ['timestamp', 'id'],
});Defines a Tinybird Pipe.
Signature:
definePipe<TSchema extends SchemaDefinition, TParams extends QueryParameters, const TName extends string>(
config: {
name: TName;
version?: number;
schema: TSchema;
parameters: TParams;
}
): PipeBuilder<TSchema, TParams, TName>Parameters:
config: A configuration object for the Pipe.name: The name of the Pipe in Tinybird.version: The version of the Pipe, used for migrations.schema: The schema definition for the primary DataSource used in the pipe.parameters: The parameters definition for the Pipe, created withdefineParameters.
Example:
import { definePipe, defineParameters, stringParam, query, count, param } from 'tinykit';
import { eventsSchema } from './schema';
const eventCountsParams = defineParameters({
startTime: int64Param('startTime', { required: true }),
endTime: int64Param('endTime', { required: true }),
});
export const getEventCountsPipe = definePipe({
name: 'get_event_counts__v1',
schema: eventsSchema,
parameters: eventCountsParams,
}).endpoint((q, params) =>
query(eventsSchema)
.selectRaw(`event, ${count()} as event_count`)
.from('events__v1')
.where(`timestamp >= ${param('startTime', 'Int64', true)}`)
.and(`timestamp <= ${param('endTime', 'Int64', true)}`)
.groupBy('event')
.orderBy('event_count DESC')
);Defines a set of parameters for a Tinybird Pipe.
Signature:
defineParameters<T extends QueryParameters>(params: T): TParameters:
params: An object where the keys are the parameter names and the values are parameter definitions created with functions likestringParam(),int64Param(), etc.
Example:
import { defineParameters, stringParam, int64Param } from 'tinykit';
export const eventCountsParams = defineParameters({
userId: stringParam('userId', { required: true }),
startTime: int64Param('startTime', { required: true }),
endTime: int64Param('endTime', { required: true }),
limit: int64Param('limit', { default: 100 }),
});Creates a new query builder instance.
Signature:
query<T extends SchemaDefinition>(schema: T): QueryBuilder<T>Parameters:
schema: The schema definition to use for the query.
Example:
import { query } from 'tinykit';
import { eventsSchema } from './schema';
const q = query(eventsSchema);These functions are used to define the columns in a DataSource schema.
string(name, options): Defines aStringcolumn.int32(name, options): Defines anInt32column.int64(name, options): Defines anInt64column.float64(name, options): Defines aFloat64column.boolean(name, options): Defines aBooleancolumn.dateTime(name, options): Defines aDateTime64column.date(name, options): Defines aDatecolumn.uuid(name, options): Defines aUUIDcolumn.array(name, schema, options): Defines anArraycolumn. TheinnerTypeoption is required to specify the underlying array type (e.g.,Array(String)).map(name, keySchema, valueSchema, options): Defines aMapcolumn.tuple(name, schema, options): Defines aTuplecolumn.nested(name, schema, options): Defines aNestedcolumn.lowCardinality(name, schema, options): Defines aLowCardinalitycolumn. This is useful for columns with a small number of unique values. TheinnerTypeoption is required.nullable(name, baseSchema, options): Defines aNullablecolumn. TheinnerTypeoption is required.json(name, options): Defines aJSONcolumn.ipv4(name, options): Defines anIPv4column.ipv6(name, options): Defines anIPv6column.
These functions are used to define the parameters for a Pipe.
stringParam(name, options): Defines aStringparameter.name: Parameter nameoptions:{ required?: boolean; default?: string }
int64Param(name, options): Defines anInt64parameter.name: Parameter nameoptions:{ required?: boolean; default?: number }
float64Param(name, options): Defines aFloat64parameter.name: Parameter nameoptions:{ required?: boolean; default?: number }
dateTimeParam(name, options): Defines aDateTimeparameter.name: Parameter nameoptions:{ required?: boolean; default?: string | Date | number }
dateParam(name, options): Defines aDateparameter.name: Parameter nameoptions:{ required?: boolean; default?: string | Date }
booleanParam(name, options): Defines aBooleanparameter.name: Parameter nameoptions:{ required?: boolean; default?: boolean }
enumParam(name, values, options): Defines anEnumparameter.name: Parameter namevalues: Array of string literals (e.g.,['1h', '1d', '1w'] as const)options:{ required?: boolean; default?: T[number] }
The query builder provides a fluent API for building SQL queries.
.select(...columns): Select specific columns..selectRaw(sql): Raw SQL select..from(table): FROM clause..where(column, operator): WHERE clause..and(column, operator): AND condition..or(column, operator): OR condition..groupBy(...columns): GROUP BY clause..orderBy(...columns): ORDER BY clause..limit(limit): LIMIT clause..offset(offset): OFFSET clause..join(table, condition, type): JOIN clause..union(query, type): UNION clause..with(alias, query): Adds a Common Table Expression (CTE) to the query..subquery(alias, query): Uses a subquery in the FROM clause..raw(sql): Replaces the entire query with a raw SQL string.
rowNumber(partitionBy, orderBy):ROW_NUMBER()window function.rank(partitionBy, orderBy):RANK()window function.denseRank(partitionBy, orderBy):DENSE_RANK()window function.lag(column, offset, defaultValue, partitionBy, orderBy):LAG()window function.lead(column, offset, defaultValue, partitionBy, orderBy):LEAD()window function.firstValue(column, partitionBy, orderBy):FIRST_VALUE()window function.lastValue(column, partitionBy, orderBy):LAST_VALUE()window function.
conditional(condition, whenTrue, whenFalse):if(condition, whenTrue, whenFalse)expression.
These functions are used to define and perform data ingestion.
defineIngest(config)streamingIngest(config)syncIngest(config)batchIngest(config)robustIngest(config)
The Tinybird class is the main entry point for interacting with the Tinybird API.
Creates a new Tinybird client.
Config:
token: Your Tinybird authentication token.datasources(optional): A record of DataSource configurations, keyed by a custom name.pipes(optional): A record of Pipe configurations, keyed by a custom name.
Example:
import { Tinybird } from 'tinykit';
import { eventsDataSource } from './datasources';
import { getEventsByUser, getEventCounts } from './pipes';
const tb = new Tinybird({
token: process.env.TINYBIRD_TOKEN!,
datasources: {
events: eventsDataSource,
},
pipes: {
getEventsByUser,
getEventCounts,
},
});Creates a query builder instance for a specific DataSource that has been defined in the client config.
Example:
const q = tb.from('events');Creates a function to execute a Tinybird Pipe query.
Example:
import { z } from 'zod';
const getUserEvents = tb.pipe({
pipe: 'get_events_by_user__v1',
data: z.object({
id: z.string(),
userId: z.string(),
event: z.string(),
timestamp: z.number(),
}),
});
const result = await getUserEvents({ userId: 'user-123', limit: 50 });Creates a function to ingest data into a Tinybird DataSource.
Example:
import { defineIngest } from 'tinykit';
const myIngest = defineIngest({
datasource: 'events__v2',
schema: eventsSchema,
});
const ingest = tb.ingest(myIngest);
await ingest([{ /* ... event data ... */ }]);