Google Sheets Database

1.0.4 · active · verified Wed Apr 22

sheets-database is a JavaScript/TypeScript library designed to enable developers to use Google Sheets as a lightweight, no-cost database or content management system. Currently at version 1.0.4, it provides a simple and intuitive API for common database operations like creating, reading, updating, and deleting (CRUD) tables and their entries. The library offers multiple authentication methods, including Service Account, OAuth, Access Token, and API Key, and includes features to optimize memory and network usage for suitable use cases. While it handles internal synchronization with Google Sheets, it is explicitly positioned for small applications and datasets, warning against its use for applications with many entries or complex joins where traditional databases are more appropriate. Release cadence appears to be minor patches within the 1.0.x range, indicating stability and ongoing maintenance.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes a SheetDatabase instance, authenticates using a Google Service Account (best practice for backend), synchronizes with the sheet, adds a new table, inserts and updates data, and demonstrates renaming a table. It includes robust environment variable handling for credentials.

import { SheetDatabase } from 'sheets-database';

// Initialize the Database with doc ID (long id in the sheets URL)
const db = new SheetDatabase(process.env.GOOGLE_SHEET_ID ?? '');

async function run() {
  // Initialize Auth using a Service Account for robust server-side access.
  // Ensure GOOGLE_SERVICE_ACCOUNT_EMAIL and GOOGLE_PRIVATE_KEY are set in your environment variables.
  // The private key may need newline characters replaced if loaded from a single-line environment variable.
  await db.useServiceAccount({
    client_email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL ?? '',
    private_key: (process.env.GOOGLE_PRIVATE_KEY ?? '').replace(/\\n/g, '\n'),
  });

  await db.sync(); // Connects with the sheet and fetches initial data

  // ADDING TABLES
  const table1 = await db.addTable('products', ['id', 'name', 'price']);
  console.log('Table "products" added.');

  // Insert some data
  await table1.insertOne({ 'id': 1, 'name': 'Laptop', 'price': 1200 });
  await table1.insert([
    { 'id': 2, 'name': 'Mouse', 'price': 25 },
    ['3', 'Keyboard', 75]
  ]);
  console.log('Data inserted into products table:', table1.getData());

  // RENAMING TABLES
  await table1.rename('inventory');
  console.log('Table renamed to "inventory".');

  // Update entries
  await db.inventory.updateRowsWhere(
    (currentData) => currentData.name === 'Laptop',
    (data) => ({ ...data, price: 1250 })
  );
  console.log('Updated inventory data:', db.inventory.getData());

  // DELETING TABLES (Commented out to prevent accidental deletion in quickstart)
  // await db.inventory.drop();
  // console.log('Table "inventory" dropped.');
}

run().catch(console.error);

view raw JSON →