npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@team_seki/bigquery-plugin

v0.5.0

Published

This library was generated with [Nx](https://nx.dev).

Downloads

29

Readme

bigquery-plugin

This library was generated with Nx.

Building

Run nx build bigquery-plugin to build the library.

Running unit tests

Run nx test bigquery-plugin to execute the unit tests via Jest.

How to use it

Use this docker-compose.yml file to emulate BigQuery in your local environment.

services:

  bigquery:
    container_name: bigquery
    image: ghcr.io/goccy/bigquery-emulator:0.2.10
    command: --project=local --dataset=dev
    ports:
      - 9050:9050
      - 9060:9060

Create your settings file for the cloud component(for example: bigquery_default_settings.json):

{
  "projectId": "local",
  "dataset": "dev",
  "apiEndpoint": "http://localhost:9050"
}

Use the plugin in your code:

import { BigQuery, BigQueryPlugin } from '@team_seki/bigquery-plugin'

export class Service {
  private plugin = new BigQueryPlugin()
  private bigQuery: BigQuery = this.plugin.getClient()
  private datasetId = this.plugin.getDataset()
  private tableId = "employees"
  private tableSchema = [
    {name: 'Name', type: 'STRING', mode: 'REQUIRED'},
    {name: 'Age', type: 'INTEGER'},
    {name: 'Weight', type: 'FLOAT'}
  ]

  async bigquery_plugin_example(): Promise<{ [key: string]: string }> {
    await this.listDatasets()
    await this.listTables()
    await this.createTable()
    await this.insertRows()
    console.log(`------ run query on ${this.datasetId}.${this.tableId} table ------`)
    await this.runQuery(`${this.datasetId}.${this.tableId}`)

    return {
      status: `ok`
    }
  }

  private async listDatasets() {
    // Lists all datasets in the specified project
    const [datasets] = await this.bigQuery.getDatasets()
    console.log('--- Datasets ---')
    datasets.forEach(dataset => console.log(dataset.id))
    console.log('--- -------- ---')
  }

  private async listTables() {
    // List all tables in the dataset
    const [tables] = await this.bigQuery.dataset(this.datasetId).getTables();

    console.log('--- Tables ---');
    tables.forEach(table => console.log(table.id));
    console.log('--- ------ ---');
  }

  private async createTable(): Promise<void> {
    // For all options, see https://cloud.google.com/bigquery/docs/reference/v2/tables#resource
    const options = {
      schema: this.tableSchema,
      location: 'US',
    }

    const [tables] = await this.bigQuery.dataset(this.datasetId).getTables();
    if (!tables.some(t => t.id === this.tableId)) {
      // Create a new table in the dataset
      const [table] = await this.bigQuery
        .dataset(this.datasetId)
        .createTable(this.tableId, options)
      console.log(`Table ${table.id} created.`)
    } else {
      console.log(`Table(${this.tableId}) already exists`)
    }
  }

  private async insertRows() {
    const rows = [
      {Name: 'Tom', Age: 30},
      {Name: 'Jane', Age: 32},
    ]

    // Insert data into a table
    await this.bigQuery
      .dataset(this.datasetId)
      .table(this.tableId)
      .insert(rows)
    console.log(`Inserted ${rows.length} rows`)
  }

  private async runQuery(tableName: string) {
    // Queries the U.S. given names dataset for the state of Texas.
    const query = `SELECT * FROM \`${tableName}\` LIMIT 100`

    // For all options, see https://cloud.google.com/bigquery/docs/reference/rest/v2/jobs/query
    const options = {
      query: query,
      // Location must match that of the dataset(s) referenced in the query.
      location: this.bigQuery.location//'US',
    }

    // Run the query as a job
    const [job] = await this.bigQuery.createQueryJob(options)
    console.log(`Job ${job.id} started.`)

    // Wait for the query to finish
    const [rows] = await job.getQueryResults()

    // Print the results
    console.log('--- Query result ---')
    console.log('Rows:')
    rows.forEach(row => console.log(row))
  }