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

gsheetsdb

v1.0.0

Published

Interact with Google Sheets as if it were a relational database.

Downloads

6

Readme

GSheetsDB 💾

Tests

A TypeScript/JavaScript library for interacting with a Google Sheets spreadsheet as if it were a relational database, supporting complex queries with aggregate functions and joins. Ideal for static websites that need to display dynamic data.

Installation

npm install gsheetsdb

Setup

Requirements

A database is a spreadsheet in Google Sheets with the following properties:

  • Each worksheet in the spreadsheet represents a table
  • The worksheet name corresponds to the table name
  • The first row of each worksheet contains the table's column names
  • Table data is in the left-most columns of the worksheet
  • The spreadsheet has public link sharing enabled

Here is an example spreadsheet meeting the requirements for GSheetsDB.

Connect

GSheetsDB connects to a database via its spreadsheet ID, which can be found in the Google Sheets URL:

https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/edit

With the spreadsheet ID, connecting to the database is as simple as the following:

const db = new SheetsDB('<SPREADSHEET_ID>');

Link a table

To interact with tables in the database, you must first link the tables. When linking, it is important to list columns in the order they appear in the spreadsheet.

db.linkTable({
    tableName: 'orders',
    cols: [
        // Order should match the spreadsheet
        { name: 'id' },
        { name: 'product' },
        { name: 'price'}
    ]
});

Querying

GSheetsDB provides a SQL-like query language. More specifically, it supports the Google Charts Query Language.

When writing queries, column names should be wrapped in pipes, like |column_name|. Below is a simple query:

const resultSet = await db.table('orders').query(
    "SELECT |ide|, |product|, |price|"
);

The language also supports queries with aggregate functions:

const resultSet = await db.table('orders').query(
    "SELECT MAX(|price|)"
);

Queries return a result set with a rows property containing the queried data:

{
    rows: [
        { id: 1, name: "Bob", age: 25 },
        { id: 2, name: "Alice", age: 24 }
    ]
}

Values in the result set are automatically converted to their proper JavaScript type. The supported types are strings, numbers, and datetimes.

Joins

Left joins are supported via the leftJoinWith method:

leftJoinWith(rightTable, leftTableColumn, rightTableColumn)

If the two joined tables contain duplicate column names, those columns are prefixed by the table name followed by an underscore.

The following is an example join between a users and orders table:

const ordersWithUser = orders.leftJoinWith(users, 'user_id', 'id');

Other

Result sets support various other utility methods.

The getColumns() method returns the names of the columns in a result set.

resultSet.getColumns()

The method withRenamedColumns(renamings) creates a new result set with renamed columns. It accepts an object whose keys are old column names, and whose values are new column names:

resultSet.withRenamedColumns({
    'oldColumn1': 'newColumn1',
    'oldColumn2': 'newColumn2'
});

Future Work

  • Support CREATE, UPDATE, and DELETE operations
  • Add support for other types of joins (eg, inner and outer joins)