[
  {
    "path": ".github/FUNDING.yml",
    "content": "custom: https://www.paypal.me/franciscopresencia/19\n"
  },
  {
    "path": ".github/workflows/tests.yml",
    "content": "name: tests\n\non: [push]\n\njobs:\n  build:\n\n    runs-on: ubuntu-latest\n\n    strategy:\n      matrix:\n        node-version: [8.x, 10.x, 12.x]\n\n    steps:\n    - uses: actions/checkout@v1\n    - name: Use Node.js ${{ matrix.node-version }}\n      uses: actions/setup-node@v1\n      with:\n        node-version: ${{ matrix.node-version }}\n    - name: install dependencies\n      run: npm install\n    - name: npm test\n      run: npm test\n      env:\n        CI: true\n"
  },
  {
    "path": ".gitignore",
    "content": "# Logs\nlogs\n*.log\nnpm-debug.log*\nyarn-debug.log*\nyarn-error.log*\n\n# Temporary folder\n/temp\n\n# Mac temporal file\n.DS_Store\n\n# SASS Cache\n.sass-cache\n\n# Runtime data\npids\n*.pid\n*.seed\n*.pid.lock\n\n# Directory for instrumented libs generated by jscoverage/JSCover\nlib-cov\n\n# Coverage directory used by tools like istanbul\ncoverage\n\n# nyc test coverage\n.nyc_output\n\n# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)\n.grunt\n\n# Bower dependency directory (https://bower.io/)\nbower_components\n\n# node-waf configuration\n.lock-wscript\n\n# Compiled binary addons (https://nodejs.org/api/addons.html)\nbuild/Release\n\n# Dependency directories\nnode_modules/\njspm_packages/\n\n# TypeScript v1 declaration files\ntypings/\n\n# Optional npm cache directory\n.npm\n\n# Optional eslint cache\n.eslintcache\n\n# Optional REPL history\n.node_repl_history\n\n# Output of 'npm pack'\n*.tgz\n\n# Yarn Integrity file\n.yarn-integrity\n\n# dotenv environment variables file\n.env\n\n# next.js build output\n.next\n\n# This is a library so don't include it\npackage-lock.json\n"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2015 Francisco Presencia\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n\n"
  },
  {
    "path": "README.md",
    "content": "# drive-db [![npm install drive-db](https://img.shields.io/badge/npm%20install-drive--db-blue.svg)](https://www.npmjs.com/package/drive-db) [![test badge](https://github.com/franciscop/drive-db/workflows/tests/badge.svg)](https://github.com/franciscop/drive-db/blob/master/index.test.js) [![demo](https://img.shields.io/badge/demo-blue.svg)](https://jsfiddle.net/franciscop/1w4t7mc5/)\n\n> 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.\n>\n> 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.\n>\n> 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`.\n\nUse Google Drive spreadsheets as a simple database for Node.js and the browser. Perfect for collaboration with multiple people editing the same spreadsheet:\n\n```js\nimport drive from 'drive-db';\n\n// Load the data from the Drive Spreadsheet\n(async () => {\n  const db = await drive(\"1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k\");\n  console.log(db);\n})();\n```\n\n| id | firstname | lastname | age | city          |\n|----|-----------|----------|-----|---------------|\n| 1  | John      | Smith    | 34  | San Francisco |\n| 2  | Merry     | Johnson  | 19  | Tokyo         |\n| 3  | Peter     | Williams | 45  | London        |\n\nBecomes an array of objects with the corresponding keys:\n\n```json\n[\n  {\n    \"id\": \"1\",\n    \"firstname\": \"John\",\n    \"lastname\": \"Smith\",\n    \"age\": \"34\",\n    \"city\": \"San Francisco\"\n  },\n  {\n    \"id\": \"2\",\n    \"firstname\": \"Merry\",\n    \"lastname\": \"Johnson\",\n    \"age\": \"19\",\n    \"city\": \"Tokyo\"\n  },\n  ...\n]\n```\n\n\n\n## Getting Started\n\nCreate the Google Drive spreadsheet and **publish it**:\n\n- Create [a Google Spreadsheet](https://www.google.com/sheets/about/)\n- File > Publish to the Web > Publish\n- Copy the id between `/spreadsheets/` and `/edit` in the url:\n\n    > [https://docs.google.com/spreadsheets/d/<strong>1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k</strong>/edit](https://docs.google.com/spreadsheets/d/1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k/edit)\n\nNow you can either add the CDN `<script>` or use a bundler. For the CDN:\n\n```html\n<script src=\"https://cdn.jsdelivr.net/npm/drive-db\"></script>\n```\n\nOtherwise install `drive-db` in your project:\n\n```bash\nnpm install drive-db\n```\n\nAnd then load the spreadsheet into your project:\n\n```js\n// Include the module and tell it which spreadsheet to use\nconst drive = require(\"drive-db\");\n\n// Create an async context to be able to call `await`\n(async () => {\n  // Load the data from the Drive Spreadsheet\n  const db = await drive(\"1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k\");\n\n  console.log(db);\n})();\n```\n\nThe table has to have a structure similar to this, where the first row are the alphanumeric field names:\n\n| id | firstname | lastname | age | city          |\n|----|-----------|----------|-----|---------------|\n| 1  | John      | Smith    | 34  | San Francisco |\n| 2  | Merry     | Johnson  | 19  | Tokyo         |\n| 3  | Peter     | Williams | 45  | London        |\n\nSee [this document](https://docs.google.com/spreadsheets/d/1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k/edit#gid=0) as an example. **Please do not request access to edit it**.\n\n\n\n## API\n\nYou import a single default export depending on your configuration:\n\n```js\n// For ES7 modules\nimport drive from \"drive-db\";\n\n// For common.js imports\nconst drive = require(\"drive-db\");\n```\n\nTo retrieve the data call it and await for the promise it returns:\n\n```js\n// With async/await:\nconst db = await drive(SHEET_ID);\nconst db = await drive(options);\nconsole.log(db);\n\n// Use the callback syntax:\ndrive(SHEET_ID).then(db => console.log(db));\ndrive(options).then(db => console.log(db));\n```\n\n**SHEET_ID**: alias of `options = { sheet: SHEET_ID }`:\n\n```js\nconst db = await drive(\"1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k\");\nconsole.log(db);\n```\n\n**options**: a simple object containing some options:\n\n```js\nconst db = await drive({\n  sheet: \"1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k\",\n  tab: \"1\",\n  cache: 3600\n});\n```\n\n- `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)\n- `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](https://jsfiddle.net/franciscop/oj0fg9n6/) as an example of how to load the second tab.\n- `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.\n\nIt 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.\n\nIf you are using [server for Node.js](https://serverjs.io/) with ES6+:\n\n```js\nconst drive = require(\"drive-db\");\nconst sheet = \"1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k\"; // Or from .env\n\nconst server = require(\"server\");\nconst { get } = server.router;\nconst { render } = server.reply;\n\nconst home = get(\"/\", async ctx => {\n  const records = await drive(sheet);\n  return render(\"index\", { records });\n});\n\nserver(home);\n```\n\n## Advanced\n\nThere 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.\n\n\n### Warm the cache\n\nTo warm the cache on project launch before the first request comes in, call the promise without awaiting or doing anything with it:\n\n```js\nconst drive = require(\"drive-db\");\nconst sheet = \"1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k\";\n// Warms the cache as soon as the Node.js project is launched\ndrive(sheet);\n\nconst server = require(\"server\");\nconst { get } = server.router;\nconst { render } = server.reply;\n\nconst home = get(\"/\", async ctx => {\n  // Cache is already warm when this request happens\n  const records = await drive(sheet);\n  return render(\"index\", { records });\n});\n\nserver(home);\n```\n\n\n### Refresh the cache\n\nTo force-refresh the cache at any point you can call `drive()` with a cache time of 0:\n\n```js\ndrive({ sheet, cache: 0 });\n```\n\n### Load locally\n\nThis is not available anymore. Since `drive-db` returns a plain array, you can just load the data locally:\n\n```js\nconst data = require(\"./data.json\");\n```\n\n\n\n## Thanks to\n\n- [Creating and publishing a node.js module](https://quickleft.com/blog/creating-and-publishing-a-node-js-module/)\n"
  },
  {
    "path": "demo.js",
    "content": "const drive = require(\".\");\n\n(async () => {\n  const db = await drive(\"1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k\");\n  console.log(db);\n})();\n"
  },
  {
    "path": "index.js",
    "content": "// Find the Google Drive data\nconst getKeys = row => Object.keys(row).filter(key => /^gsx\\$/.test(key));\nconst parseRow = row => {\n  return getKeys(row).reduce((obj, key) => {\n    obj[key.slice(4)] = row[key].$t;\n    return obj;\n  }, {});\n};\n\n// Make a GET HTTP response and parse the JSON response\nconst get = async url => {\n  // Try first with fetch() - browser, worker, polyfilled, etc\n  if (typeof fetch !== \"undefined\") {\n    const res = await fetch(url);\n    if (!res.ok) throw new Error(`Error ${res.status} retrieving ${url}`);\n    return res.json();\n  }\n\n  // Now try with Node.js, which needs to be promisified\n  if (typeof require !== \"undefined\") {\n    return new Promise((resolve, reject) => {\n      const handler = res => {\n        res.setEncoding(\"utf8\");\n        if (res.statusCode < 200 || res.statusCode >= 300) {\n          return reject(new Error(`Error ${res.statusCode} retrieving ${url}`));\n        }\n        let data = \"\";\n        res.on(\"data\", chunk => (data += chunk));\n        res.on(\"end\", () => resolve(JSON.parse(data)));\n      };\n      require(\"https\")\n        .get(url, handler)\n        .on(\"error\", reject);\n    });\n  }\n\n  // No supported method was found, display the warning\n  throw new Error(\"fetch() is not available, please polyfill it\");\n};\n\nconst retrieve = async ({ sheet, tab }) => {\n  const raw = await get(\n    `https://spreadsheets.google.com/feeds/list/${sheet}/${tab}/public/values?alt=json`\n  );\n  return raw.feed.entry.map(parseRow);\n};\n\n// Memoize a callback similar to React\nconst memo = (cb, map = {}) => async (options, timeout) => {\n  const key = JSON.stringify(options);\n  const time = new Date();\n  if (map[key] && time - map[key].time < timeout) {\n    return map[key].value;\n  }\n  map[key] = { value: await cb(options), time };\n  return map[key].value;\n};\n\n// It should be memoized here, since we memoize the whole *request* and *parse*\nconst getSheet = memo(retrieve);\n\n// The main drive() function\nexport default async options => {\n  const optObject = typeof options === \"object\" ? options : { sheet: options };\n  const { sheet = \"\", tab = \"default\", cache = 3600 } = optObject;\n\n  // To update the data we need to make sure we're working with an id\n  if (!sheet) throw new Error(\"Need a Google Drive sheet id to load\");\n\n  return getSheet({ sheet, tab }, cache);\n};\n"
  },
  {
    "path": "index.test.js",
    "content": "/* jshint expr:true */\nimport drive from \"./index.js\";\n\nconst https = jest.genMockFromModule(\"https\");\n\nconst delay = time => new Promise(done => setTimeout(done, time));\n\n// Testing that we are able to load the library\ndescribe(\"drive-db\", function() {\n  it(\"returns a table\", async () => {\n    const db = await drive(\"1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k\");\n    expect(Array.isArray(db)).toBe(true);\n    expect(db.length).toBe(6);\n    expect(db[0]).toEqual({\n      id: \"1\",\n      firstname: \"John\",\n      lastname: \"Smith\",\n      age: \"34\",\n      city: \"San Francisco\",\n      country: \"USA\",\n      timestamp: \"12/10/2010\"\n    });\n  });\n\n  it(\"requires a sheet\", async () => {\n    await expect(drive()).rejects.toEqual(\n      new Error(\"Need a Google Drive sheet id to load\")\n    );\n  });\n\n  it(\"throws with a wrong sheet id\", async () => {\n    await expect(drive(\"abc\")).rejects.toEqual(\n      new Error(\n        \"Error 400 retrieving https://spreadsheets.google.com/feeds/list/abc/default/public/values?alt=json\"\n      )\n    );\n  });\n\n  it(\"can work with old cache\", async () => {\n    const db = await drive(\"1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k\");\n    expect(Array.isArray(db)).toBe(true);\n    expect(db.length).toBe(6);\n    const db2 = await drive(\"1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k\");\n  });\n\n  it(\"can work with expired cache\", async () => {\n    const db = await drive(\"1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k\");\n    expect(Array.isArray(db)).toBe(true);\n    expect(db.length).toBe(6);\n    await delay(1000);\n    const db2 = await drive(\"1fvz34wY6phWDJsuIneqvOoZRPfo6CfJyPg1BYgHt59k\");\n  });\n});\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"drive-db\",\n  \"version\": \"6.0.1\",\n  \"description\": \"📊 Use Google Drive spreadsheets as a simple database\",\n  \"homepage\": \"https://github.com/franciscop/drive-db#readme\",\n  \"repository\": \"https://github.com/franciscop/drive-db.git\",\n  \"bugs\": \"https://github.com/franciscop/drive-db/issues\",\n  \"funding\": \"https://www.paypal.me/franciscopresencia/19\",\n  \"author\": \"Francisco Presencia <public@francisco.io> (https://francisco.io/)\",\n  \"license\": \"MIT\",\n  \"scripts\": {\n    \"build\": \"rollup index.js --name drive --output.format umd | uglifyjs -o index.min.js\",\n    \"demo\": \"node ./demo.js\",\n    \"start\": \"jest --watch\",\n    \"test\": \"jest --coverage\"\n  },\n  \"keywords\": [\n    \"google\",\n    \"drive\",\n    \"database\",\n    \"db\",\n    \"table\",\n    \"spreadsheet\",\n    \"mongodb\",\n    \"async\"\n  ],\n  \"main\": \"index.min.js\",\n  \"files\": [],\n  \"dependencies\": {},\n  \"devDependencies\": {\n    \"@babel/core\": \"^7.4.4\",\n    \"@babel/preset-env\": \"^7.4.4\",\n    \"babel-jest\": \"^24.8.0\",\n    \"jest\": \"^24.8.0\",\n    \"rollup\": \"^1.11.3\",\n    \"uglify-es\": \"^3.3.9\"\n  },\n  \"babel\": {\n    \"presets\": [\n      [\n        \"@babel/preset-env\",\n        {\n          \"targets\": {\n            \"node\": \"current\"\n          }\n        }\n      ]\n    ]\n  }\n}\n"
  }
]