docs/api-reference/carto/data-sources.md
To ease interacting with the CARTO platform, the CARTO deck.gl module includes a number of functions, which simplify the use of fetching your data from CARTO. At a high level these can be thought of as wrappers around the browsers fetch function, except that rather than passing a URL, options that specify the data in the CARTO are used.
The data source functions are a compact way to describe the data that you want to fetch. For example, to fetch a table from a data warehouse from the connection carto_dw:
import {vectorTableSource} from '@carto/api-client';
const data = vectorTableSource({
accessToken: 'XXX',
connectionName: 'carto_dw',
tableName: 'carto-demo-data.demo_tables.chicago_crime_sample'
});
All data source functions return a Promise, which can be resolved to obtain the actual Tilejson. However, as the core deck.gl Layer prop supports Promises, it is often not necessary to resolve or await the Promise and the data source can be directly passed to the data prop:
import {H3TileLayer} from '@deck.gl/carto';
import {h3TilesetSource} from '@carto/api-client';
new H3TileLayer({
data: h3TilesetSource({
accessToken: 'XXX',
connectionName: 'carto_dw',
tableName: 'carto-demo-data.demo_tables.h3_data'
}),
getFillColor: d => d.properties.color
});
All the data source functions are fully typed, to aid in providing the correct parameters and working correctly with the return value.
The dataSource functions have an internal cache, which avoids fetching data from the server if the parameters have not changed. Thus they can be used, for example, in React render() functions without needing memoization.
All data source functions take the following global options:
type SourceOptions = {
accessToken: string;
connectionName: string;
apiBaseUrl?: string;
clientId?: string;
headers?: Record<string, string>;
maxLengthURL?: number;
};
In addition, the following options are supported on each source:
type VectorTableSourceOptions = {
columns?: string[];
spatialDataColumn?: string;
tableName: string;
aggregationExp?: string;
};
type VectorQuerySourceOptions = {
spatialDataColumn?: string;
sqlQuery: string;
queryParameters: QueryParameters;
aggregationExp?: string;
};
type VectorTilesetSourceOptions = {
tableName: string;
};
type H3TableSourceOptions = {
aggregationExp: string;
aggregationResLevel?: number;
columns?: string[];
spatialDataColumn?: string;
tableName: string;
};
type H3QuerySourceOptions = {
aggregationExp: string;
aggregationResLevel?: number;
spatialDataColumn?: string;
sqlQuery: string;
queryParameters: QueryParameters;
};
type H3TilesetSourceOptions = {
tableName: string;
};
type QuadbinTableSourceOptions = {
aggregationExp: string;
aggregationResLevel?: number;
columns?: string[];
spatialDataColumn?: string;
tableName: string;
};
type QuadbinQuerySourceOptions = {
aggregationExp: string;
aggregationResLevel?: number;
spatialDataColumn?: string;
sqlQuery: string;
queryParameters: QueryParameters;
};
type QuadbinTilesetSourceOptions = {
tableName: string;
};
type RasterSourceOptions = {
tableName: string;
};
Boundary sources are sources where both the tileset and the properties props need a specific schema to work. Read more about Boundaries in the CARTO documentation.
type BoundaryTableSourceOptions = {
tilesetTableName: string;
columns?: string[];
propertiesTableName: string;
};
type BoundaryQuerySourceOptions = {
tilesetTableName: string;
propertiesSqlQuery: string;
queryParameters?: QueryParameters;
};
QueryParameters are used to parametrize SQL queries. The format depends on the source's provider, some examples:
vectorQuerySource({
...,
sqlQuery: `select * from users where username=$1`,
queryParameters: ['my-name']
})
vectorQuerySource({
...,
sqlQuery: `select * from users where username=$1`,
queryParameters: ['my-name']
})
vectorQuerySource({
...,
sqlQuery: `select * from users where username=@username`,
queryParameters: { username: 'my-name' }
})
vectorQuerySource({
...,
sqlQuery: `select * from users where username=?`,
queryParameters: ['my-name']
});
or
vectorQuerySource({
data: `select * from users where username=:1`,
queryParameters: ['my-name']
});
vectorQuerySource({
...
data: `select * from users where username=?`,
queryParameters: ['my-name']
});