GithubHelp home page GithubHelp logo

doc22940 / drive-db Goto Github PK

View Code? Open in Web Editor NEW

This project forked from franciscop/drive-db

0.0 1.0 0.0 150 KB

:bar_chart: Use Google Drive spreadsheets as a simple database

License: MIT License

JavaScript 100.00%

drive-db's Introduction

drive-db npm install drive-db test badge demo

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

// Load the data from the Drive Spreadsheet
const db = await drive("1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k");
console.log(db);
id firstname lastname age city
1 John Smith 34 San Francisco
2 Merry Johnson 19 Tokyo
3 Peter Williams 45 London

Heads up! Since 5.0.0, the API has been simplified quite a lot! Also you can now use it in the front-end! ๐ŸŽ‰

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:

Install drive-db in your project:

npm install drive-db

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:

id firstname lastname age city
1 John Smith 34 San Francisco
2 Merry Johnson 19 Tokyo
3 Peter Williams 45 London

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,
  onload: data => data
});
  • sheet (required): when editing a google spreadsheet, it's the part between /spreadsheets/ and /edit in the url. Please make sure to also publish the spreadsheet before copying it (File > Publish to the Web > Publish)
  • tab ('1'): the tab to use in the spreadsheet, which defaults to the first tab. It's the number as a string of the tab. See this demo as an example of how to load the second tab.
  • cache (3600): set the maximum time (in seconds) that the current cache is valid. After this, the data will be loaded again when the function is called. This is really useful when combined with development env constant. Set to 0 to refresh in each request.
  • onload: a function that sets a transformation between the data of the spreadsheet and the local db. It accepts the whole array and must return the whole modified array and it's useful to avoid doing the operations on each request. You can return a promise here and it will be waited. It will be run ON EACH CALL, even if the underlying data was cached.

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

drive-db's People

Contributors

franciscop avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.