Importing data

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

  • From a local file.

  • Importing a file located in an URL.

  • Import or append data generated and/or gathered by your JavaScript application.

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

Importing data from a URL

Create a table using a CSV file in a HTTP[s] server using the importUrl() function. We highly recommend this to gain speed.

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 datasource that will be imported

Options[schema]

String

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

Options[append]

Boolean

Default: False. If True, appends data to an existing datasource. Datasource schema must be compatible

Options[headers]

Array(String)

An aray of headers to be sent when fetching url

Appending data to an existing datasource

Append data to a datasource with the append() method. Target datasource schema should match with the rows to append. The rows argument can be:

A String with raw csv

append(raw_csv, datasource_name[, options])
//norun
let tinyb = tinybird('<token>').datasource('<table>')
var res = await tiny.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 many rows to import

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

A Blob coming, for example, from a form element

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

An URL object (importURL could be used in this scenario)

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

NAME

TYPE

DESCRIPTION

rows

Multi

As explained above

datasource_name

String

Name of the datasource to append data to

Options[headers]

Array(String)

An aray of headers to be sent when fetching url

More ways to import data

The import() method creates a datasource with given data and a given name. The rows argument can be:

A String with the raw csv

import(raw_csv, datasource_name[, options])
//norun
let tinyb = tinybird('<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('<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

As explained above

datasource_name

String

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

Options[schema]

String

Datasource 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