-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add client for Google's BigQuery database (#183)
- Loading branch information
1 parent
ffbdc8b
commit 3f47e24
Showing
31 changed files
with
1,811 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
--- | ||
id: bigquery-client | ||
title: BigQuery Node.js Client | ||
sidebar_label: Client | ||
--- | ||
|
||
To access BigQuery, you will need to create a BigQueryClient. | ||
|
||
### `BigQueryClient.query(SQLQuery): Promise<any[]>` | ||
|
||
Run an SQL Query and get a promise for an array of results. | ||
|
||
### `BigQueryClient.queryStream(SQLQuery): AsyncIterable<any>` | ||
|
||
Run an SQL Query and get an async iterable of the results. e.g. | ||
|
||
```js | ||
for await (const record of db.queryStream(sql`SELECT * FROM massive_table`)) { | ||
console.log(result); | ||
} | ||
``` | ||
|
||
### `BigQueryClient.queryNodeStream(SQLQuery): ReadableStream` | ||
|
||
Run an SQL Query and get a node.js readable stream of the results. e.g. | ||
|
||
```js | ||
const Stringifier = require('newline-json').Stringifier; | ||
|
||
db.queryNodeStream(sql`SELECT * FROM massive_table`) | ||
.pipe(new Stringifier()) | ||
.pipe(process.stdout); | ||
``` | ||
|
||
### `BigQueryClient.dataset(name): BigQueryDataSet` | ||
|
||
Get a BigQuery API that's scoped to a dataset. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
id: bigquery-dataset | ||
title: BigQuery Node.js DataSet | ||
sidebar_label: DataSet | ||
--- | ||
|
||
A BigQueryDataSet is a client for BigQuery that's scoped to a single data set. | ||
|
||
### `BigQueryDataSet.query(SQLQuery): Promise<any[]>` | ||
|
||
This is equivalent to `.query` on the BigQueryClient, except the query is automatically scoped to the dataset. See [`BigQueryClient.queryStream`](bigquery-client.md) for details. | ||
|
||
### `BigQueryDataSet.queryStream(SQLQuery): AsyncIterable<any>` | ||
|
||
This is equivalent to `.queryStream` on the BigQueryClient, except the query is automatically scoped to the dataset. See [`BigQueryClient.queryStream`](bigquery-client.md) for details. | ||
|
||
### `BigQueryDataSet.queryNodeStream(SQLQuery): ReadableStream` | ||
|
||
This is equivalent to `.queryNodeStream` on the BigQueryClient, except the query is automatically scoped to the dataset. See [`BigQueryClient.queryStream`](bigquery-client.md) for details. | ||
|
||
### `BigQueryDataSet.createTable(name, options): Promise<BigQueryTable>` | ||
|
||
Create a table and get a BigQuery Table API for inserting records into the table. | ||
|
||
### `BigQueryDataSet.table(name): BigQueryTable` | ||
|
||
Get a BigQuery Table API for inserting records into the table. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
--- | ||
id: bigquery | ||
title: BigQuery | ||
sidebar_label: API | ||
--- | ||
|
||
The `@databases/bigquery` library provides a safe and convenient API for querying Google's BigQuery in node.js. | ||
|
||
## `connect(options)` | ||
|
||
Creates a client for BigQuery. You can specify a `projectId` and `keyFilename` to authenticate, or you can authenticate using the gcloud CLI before initializing this library. | ||
|
||
For all methods, see [BigQueryClient](bigquery-client.md) | ||
|
||
## Creating BigQuery tables in Node.js | ||
|
||
You can create BigQuery tables from the cloud console. If you need to create tables dynamically, you can use @databases: | ||
|
||
```typescript | ||
import connect, { | ||
sql, | ||
BigQueryTableType, | ||
BigQueryPartitionType, | ||
} from '@databases/bigquery'; | ||
|
||
const db = connect(); | ||
|
||
db.dataset(`my_dataset`) | ||
.createTable(`my_table`, { | ||
type: BigQueryTableType.Table, | ||
fields: [ | ||
{name: `id`, type: `INT64`}, | ||
{name: `value`, type: `STRING`}, | ||
], | ||
partition: { | ||
type: BigQueryPartitionType.Time, | ||
granularity: 'HOUR', | ||
}, | ||
}) | ||
.catch((err) => console.error(err)); | ||
``` | ||
|
||
```javascript | ||
const connect = require('@databases/bigquery'); | ||
const { | ||
sql, | ||
BigQueryTableType, | ||
BigQueryPartitionType, | ||
} = require('@databases/bigquery'); | ||
|
||
const db = connect(); | ||
|
||
db.dataset(`my_dataset`) | ||
.createTable(`my_table`, { | ||
type: BigQueryTableType.Table, | ||
fields: [ | ||
{name: `id`, type: `INT64`}, | ||
{name: `value`, type: `STRING`}, | ||
], | ||
partition: { | ||
type: BigQueryPartitionType.Time, | ||
granularity: 'HOUR', | ||
}, | ||
}) | ||
.catch((err) => console.error(err)); | ||
``` | ||
|
||
## Inserting Records into BigQuery tables in Node.js | ||
|
||
You can use `INSERT` statements to insert data into BigQuery tables, but it's often more efficient & convenient to use the `.insert` API | ||
|
||
```typescript | ||
import connect, { | ||
sql, | ||
BigQueryTableType, | ||
BigQueryPartitionType, | ||
} from '@databases/bigquery'; | ||
|
||
const db = connect(); | ||
|
||
db.dataset(`my_dataset`) | ||
.table(`my_table`) | ||
.insert([ | ||
{id: 1, value: 'hello'}, | ||
{id: 2, value: 'world'}, | ||
]) | ||
.catch((err) => console.error(err)); | ||
``` | ||
|
||
```javascript | ||
const connect = require('@databases/bigquery'); | ||
const { | ||
sql, | ||
BigQueryTableType, | ||
BigQueryPartitionType, | ||
} = require('@databases/bigquery'); | ||
|
||
const db = connect(); | ||
|
||
db.dataset(`my_dataset`) | ||
.table(`my_table`) | ||
.insert([ | ||
{id: 1, value: 'hello'}, | ||
{id: 2, value: 'world'}, | ||
]) | ||
.catch((err) => console.error(err)); | ||
``` | ||
|
||
## Querying BigQuery in Node.js | ||
|
||
```typescript | ||
import connect, {sql} from '@databases/bigquery'; | ||
|
||
const db = connect(); | ||
|
||
db.query(sql`SELECT * FROM my_dataset.my_table;`).then( | ||
(results) => console.log(results), | ||
(err) => console.error(err), | ||
); | ||
``` | ||
|
||
```javascript | ||
const connect = require('@databases/bigquery'); | ||
const {sql} = require('@databases/bigquery'); | ||
|
||
const db = connect(); | ||
|
||
db.query(sql`SELECT * FROM my_dataset.my_table;`).then( | ||
(results) => console.log(results), | ||
(err) => console.error(err), | ||
); | ||
``` | ||
|
||
For details on how to build queries, see [Building SQL Queries](sql.md) | ||
|
||
> N.B. BigQuery is billed based on the bytes scanned by your query. The lack of indexes can make seemingly simple queries very expensive if your tables are sufficiently large. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# @databases/bigquery | ||
|
||
For documentation, see https://www.atdatabases.org/docs/bigquery |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "@databases/bigquery", | ||
"version": "0.0.0", | ||
"description": "", | ||
"main": "./lib/index.js", | ||
"types": "./lib/index.d.ts", | ||
"dependencies": { | ||
"@babel/code-frame": "^7.0.0", | ||
"@databases/escape-identifier": "^0.0.0", | ||
"@databases/split-sql-query": "^0.0.0", | ||
"@databases/sql": "^0.0.0", | ||
"@google-cloud/bigquery": "^5.7.1", | ||
"@types/big.js": "^6.1.1", | ||
"assert-never": "^1.2.1", | ||
"big.js": "^6.0.0" | ||
}, | ||
"scripts": {}, | ||
"repository": "https://github.com/ForbesLindesay/atdatabases/tree/master/packages/bigquery", | ||
"bugs": "https://github.com/ForbesLindesay/atdatabases/issues", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"homepage": "https://www.atdatabases.org/docs/bigquery" | ||
} |
Oops, something went wrong.