Home

Awesome

drive-db npm install drive-db test badge demo

NOTICE: Google has killed Google Sheets API v3 that allowed this library to exist. So unfortunately this library no longer works. Their new API does not provide the same functionality and it doesn't allow you to publish a spreadshit, so we cannot do anything on our end.

If you depended on this library for anything important, we are very sorry. The new Sheets v4 does work with you creating a whole backend-frontend OAuth workflow (which is what this library was designed to avoid), so please use other libraries out there that allow for this.

Thank you so much everyone for using this library for as long as you have and the feedback and love sent. It is one of the first and favorite libraries I've ever made, and it has powered all sort of things AFAIK, from blog posts, public charts, Covid19 panels, etc. So it is with a heavy heart that I have to write this notice, deprecating drive-db.

Use Google Drive spreadsheets as a simple database for Node.js and the browser. Perfect for collaboration with multiple people editing the same spreadsheet:

import drive from 'drive-db';

// Load the data from the Drive Spreadsheet
(async () => {
  const db = await drive("1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k");
  console.log(db);
})();
idfirstnamelastnameagecity
1JohnSmith34San Francisco
2MerryJohnson19Tokyo
3PeterWilliams45London

Becomes an array of objects with the corresponding keys:

[
  {
    "id": "1",
    "firstname": "John",
    "lastname": "Smith",
    "age": "34",
    "city": "San Francisco"
  },
  {
    "id": "2",
    "firstname": "Merry",
    "lastname": "Johnson",
    "age": "19",
    "city": "Tokyo"
  },
  ...
]

Getting Started

Create the Google Drive spreadsheet and publish it:

Now you can either add the CDN <script> or use a bundler. For the CDN:

<script src="https://cdn.jsdelivr.net/npm/drive-db"></script>

Otherwise install drive-db in your project:

npm install drive-db

And then load the spreadsheet into your project:

// Include the module and tell it which spreadsheet to use
const drive = require("drive-db");

// Create an async context to be able to call `await`
(async () => {
  // Load the data from the Drive Spreadsheet
  const db = await drive("1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k");

  console.log(db);
})();

The table has to have a structure similar to this, where the first row are the alphanumeric field names:

idfirstnamelastnameagecity
1JohnSmith34San Francisco
2MerryJohnson19Tokyo
3PeterWilliams45London

See this document as an example. Please do not request access to edit it.

API

You import a single default export depending on your configuration:

// For ES7 modules
import drive from "drive-db";

// For common.js imports
const drive = require("drive-db");

To retrieve the data call it and await for the promise it returns:

// With async/await:
const db = await drive(SHEET_ID);
const db = await drive(options);
console.log(db);

// Use the callback syntax:
drive(SHEET_ID).then(db => console.log(db));
drive(options).then(db => console.log(db));

SHEET_ID: alias of options = { sheet: SHEET_ID }:

const db = await drive("1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k");
console.log(db);

options: a simple object containing some options:

const db = await drive({
  sheet: "1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k",
  tab: "1",
  cache: 3600
});

It returns a plain Javascript array. With ES6+, operations on arrays are great, but feel free to use Lodash or similar if you want some more advanced queries.

If you are using server for Node.js with ES6+:

const drive = require("drive-db");
const sheet = "1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k"; // Or from .env

const server = require("server");
const { get } = server.router;
const { render } = server.reply;

const home = get("/", async ctx => {
  const records = await drive(sheet);
  return render("index", { records });
});

server(home);

Advanced

There are some more advanced things that you might consider. While I recommend you to read the code for this, here are a couple of examples.

Warm the cache

To warm the cache on project launch before the first request comes in, call the promise without awaiting or doing anything with it:

const drive = require("drive-db");
const sheet = "1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k";
// Warms the cache as soon as the Node.js project is launched
drive(sheet);

const server = require("server");
const { get } = server.router;
const { render } = server.reply;

const home = get("/", async ctx => {
  // Cache is already warm when this request happens
  const records = await drive(sheet);
  return render("index", { records });
});

server(home);

Refresh the cache

To force-refresh the cache at any point you can call drive() with a cache time of 0:

drive({ sheet, cache: 0 });

Load locally

This is not available anymore. Since drive-db returns a plain array, you can just load the data locally:

const data = require("./data.json");

Thanks to