Importing data with Tinybird.js

You can use Tinybird.js to import data from a client side application. There are mainly 3 ways to import your data:

  • Importing a remote file through its URL.

  • Importing a local file

  • Importing or appending data generated and/or gathered by your JavaScript application.

To be able to import data you will need an Auth token with SCOPE:IMPORT permissions. When appending data your Auth token will need SCOPE:APPEND permissions.

importUrl(url, name, options)

To create a Data Source from a remote file, use the importUrl() method. The file needs to be accessible via HTTP(S). Importing through a URL is by far the fastest way to import.

importUrl(url, datasource_name[,options])
//norun
// load Demographic Statistics By Zip Code
var res = await tinyb.importUrl('https://data.cityofnewyork.us/api/views/kku6-nxdu/rows.csv?accessType=DOWNLOAD', 'example')
if (res.error) {
  onError(res.error)
} else {
  console.log("imported " + res.datasource.name)
}
Function arguments

NAME

TYPE

DESCRIPTION

url

String

HTTP[s] url where the CSV file is located

datasource_name

String

Optional. Name of the Data Source that will be imported. If not specified, Tinybird Analytics will derive a name off of the CSV file name. Can only contain ASCII letters, numbers or underscores. Name must start with a letter

Options[schema]

String

Data Source schema in format ‘column1 Type, column2 Type2…’ See clickhouse documentation to learn more about the available column types

Options[headers]

Array(String)

An array of headers to be sent when fetching url

Options[mode]

String

Import mode, it can be: append, replace or create. If mode is append, Data Source schema must be compatible

import(rows, name, options)

The import() method creates a Data Source and then imports the data provided in the rows argument, which can be one of the following:

An array with multiple rows of comma-separated Strings

import(raw_csv, datasource_name[, options])
//norun
let tinyb = tinybird('<import_token>')
var rows = [
    ['a', 'b', 'c', 123],
    ['d', 'e', 'f', 456]
]
var res = await tinyb.import(rows, 'my_datasource_name');

A Blob coming, for example, from a form element

import(blob, datasource_name[, options])
//norun
let t = tinybird('<import_token>')
var fileInputElement = document.getElementById('file_input_form')
var file = fileInputElement.files[0]
var res = await tiny.import(file, 'datasource_name')
Function arguments

NAME

TYPE

DESCRIPTION

rows

Multi

Required.
Array of Strings with comma-separated values
Blob (e.g. coming from a file input element)

datasource_name

String

Optional. Name of the Data Source that will be imported. Can only contain ASCII letters, numbers or underscores. Name must start with a letter

Options[schema]

String

Data Source schema in format ‘column1 Type, column2 Type2…’ See clickhouse documentation to learn more about the available column types

Options[headers]

Array(String)

An aray of headers to be sent when fetching url

Options[mode]

String

Import mode, it can be: append, replace or create

append(rows, options)

Use the append() method to append data to an existing Data Source. Target Data Source schema should match the rows you are trying to append. The rows argument can be:

A String with raw comma-separated values

append(raw_csv [, options])
//norun
let tinyb = tinybird('<DATASOURCE:APPEND token>').datasource('<datasource_name>')
var res = await tinyb.append("1,8,Toyota Gazoo Racing,Sébastien BUEMI,Kazuki NAKAJIMA,Fernando ALONSO,,Toyota TS050 - Hybrid,M,LMP1,H,,Classified,388,24:00'52.247,,,5,3'17.658,248.2,,")

An array with multiple rows of comma-separated Strings

append(array[rows] [, options])
//norun
let tinyb = tinybird('<DATASOURCE:APPEND token>').datasource('<datasource_name>')
var res = await tinyb.append([
    ['a', 'b', 'c', 123],
    ['d', 'e', 'f', 456]
])

A Blob coming, for example, from a form element

append(blob [, options])
//norun
let tinyb = tinybird('<DATASOURCE:APPEND token>').datasource('<datasource_name>')
var fileInputElement = document.getElementById('file_input_form')
var file = fileInputElement.files[0]
var res = await tinyb.append(file)

A URL object (importURL could be used in this scenario instead)

append(url [, options])
//norun
let tinyb = tinybird('<DATASOURCE:APPEND token>').datasource('<datasource_name>')
var res = await tinyb.append(new URL('http://test.com/file.csv'))
Function arguments

NAME

TYPE

DESCRIPTION

rows

Multi

Required.
String of comma-separated values
Array of Strings with comma-separated values
Blob (e.g. coming from a file input element)
URL object

Options[headers]

Array(String)

An aray of headers to be sent when fetching url

replace(rows, options)

Use the replace() method to replace your Data Source data. The rows argument can be:

A String with raw comma-separated values

replace(raw_csv [, options])
//norun
let tinyb = tinybird('<import_token>').datasource('<datasource_name>')
var res = await tinyb.replace("1,8,Toyota Gazoo Racing,Sébastien BUEMI,Kazuki NAKAJIMA,Fernando ALONSO,,Toyota TS050 - Hybrid,M,LMP1,H,,Classified,388,24:00'52.247,,,5,3'17.658,248.2,,")

An array with multiple rows of comma-separated Strings

replace(array[rows] [, options])
//norun
let tinyb = tinybird('<import_token>').datasource('<datasource_name>')
var res = await tinyb.replace([
    ['a', 'b', 'c', 123],
    ['d', 'e', 'f', 456]
])

A Blob coming, for example, from a form element

replace(blob [, options])
//norun
let tinyb = tinybird('<import_token>').datasource('<datasource_name>')
var fileInputElement = document.getElementById('file_input_form')
var file = fileInputElement.files[0]
var res = await tinyb.replace(file)

A URL object (importURL could be used in this scenario instead)

replace(url [, options])
//norun
let tinyb = tinybird('<import_token>').datasource('<datasource_name>')
var res = await tinyb.replace(new URL('http://test.com/file.csv'))
Function arguments

NAME

TYPE

DESCRIPTION

rows

Multi

Required.
String of comma-separated values
Array of Strings with comma-separated values
Blob (e.g. coming from a file input element)
URL object

Options[headers]

Array(String)

An aray of headers to be sent when fetching url