Querying data

Using Tinybird.js you just need your Pipe name and an Auth token to query your data.

Running queries

There are two ways to receive data from a resulting Pipe.

  • .json(sql): it will provide the result in JSON format, included meta information.

  • csv(sql): only provides the data in CSV with headers.

You can add any specific sql in order to filter your Pipe for both methods.

json(sql) · showing response data
// tinybird.js constructor function requires an Auth token.
const tinyb = tinybird('<token>')
const pipe = tinyb.pipe('<pipe>')
const res = await pipe.json('select * from _ limit 3')
console.log(res.data)

Apart from the resulting data itself, the response object contains also metadata about the resulting columns of your pipe in the .metadata object.

json(sql) · showing response metadata
const tinyb = tinybird('<token>')
const pipe = tinyb.pipe('<pipe>')
const res = await pipe.json('select * from _ limit 5')
console.log(res.meta)

And some query statistics as well in the .statistics object.

json(sql) · showing query statistics
const tinyb = tinybird('<token>')
const pipe = tinyb.pipe('<pipe>')
const res = await pipe.json('select * from _ limit 2')
console.log(res.statistics, res.statistics.elapsed)

And finally, get the data as CSV format.

json(sql) · showing query statistics
const tinyb = tinybird('<token>')
const pipe = tinyb.pipe('<pipe>')
const res = await pipe.csv('select * from _ limit 10')
console.log(res)

And that’s it. As you can see, receive your Pipe data directly from your javascript application is really easy and fast. You just need a valid Auth token and a valid Pipe name.

Shortcuts

There are a bunch of handy methods to help you query your data when developing your applications. Most of them can be replaced by a SQL query but, aren’t them cool?

Setting a defalt Pipe

This will save you some keystrokes, the pipe() method helps you setting the default pipe for your queries, so you can refer to it by typing _:

pipe(pipe_name)
const pipe = tinybird('<token>').pipe('<pipe>')
const res = await pipe.json('select count() from _')
console.log(res.data)

Adding where filters

Another handy tool to avoid hand-made SQL is where(). It allows to set filters over your Pipe. This will become especially useful when creating dashboards containing custom filters.

where(filter)
//norun
const pipe = tinybird(your_auth_token).pipe(pipe_name)
pipe.where('attr > 10')
const res = await pipe.data('select count() from _^')
console.log(res.data)

This prints the number of records with attr > 10. Note the _^ shortcut, this specifies Tinybird.js library to use the default Pipe with all the filters.

Counting rows

The count() method is just a shortcut for select count() from pipe.

count()
const pipe = tinybird('<token>').pipe('<pipe>')
console.log(await pipe.count())

Retrieving first row

first() provides the first row of the results.

first()
const pipe = tinybird('<token>').pipe('<pipe>');
const res = await pipe.first('select uniq(<column>) as differents from _')

console.log(`Different rows -> ${res.differents}`)

Getting single values

single() returns the first element of the first row, useful when you just want one value, like when counting rows.

single()
const pipe = tinybird('<token>').pipe('<pipe>')
console.log(await pipe.single('select uniq(<column>) from _'))

More advanced methods

Calculating facets fast

With facets() you can get all the facets for the Pipe:

facets()
const pipe = tinybird('<token>').pipe('<pipe>')
console.log(await pipe.facets())

Counting occurrences

By using countPerValue(), you can directly get the number of occurrences of each different value (for a particular column).

countPerValue()
const pipe = tinybird('<token>').pipe('<pipe>')
console.log(await pipe.countPerValue('<column>'))