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

grist-api

v0.1.7

Published

NodeJS client for interacting with Grist

Downloads

75

Readme

grist-api

npm version Build Status

NodeJS client for interacting with Grist.

The grist-api package simplifies using the Grist API in Javascript/TypeScript. There is also an analogous Python package.

Installation

npm install grist-api

Usage Example

const {GristDocAPI} = require('grist-api');

// Put here the URL of your document.
const DOC_URL = "https://docs.getgrist.com/123456789abc/My-Document";

async function main() {
  const api = new GristDocAPI(DOC_URL);
  // Add some rows to a table
  await api.addRecords('Food', [
    {Name: 'eggs', AmountToBuy: 12},
    {Name: 'beets', AmountToBuy: 1},
  ]);

  // Fetch all rows.
  const data = await api.fetchTable('Food');
  console.log(data);

  // Sync data by a key column.
  await api.syncTable('Food', [{Name: 'eggs', AmountToBuy: 0}], ['Name']);
}

main();

To run this, first prepare a Grist doc to play with:

  1. Create a Grist doc
  2. Add a table named Food with columns Name and AmountToBuy
  3. Set DOC_URL in the code above to that of your document (the part after doc ID doesn't matter).

To use the API, you need to get your API key in Grist from Profile Settings. This API key may be provided to GristDocAPI in several ways, and is looked for in this order:

  • As a constructor argument: new GristDocAPI(DOC_URL, {apiKey: 'XXX'}).
  • In an environment variable: GRIST_API_KEY=<key>.
  • In the ~/.grist-api-key file.

Public documents may be accessed without an API key, or with an empty string for the API key (to stop searching the locations above).

Classes and methods

new GristDocAPI(docUrlOrId, options)

Create an API instance. You may specify either a doc URL, or just the doc ID (the part of the URL after "/doc/"). If you specify a URL, then options.server is unneeded and ignored.

The options are:

  • apiKey (string) The API key, available in Grist from Profile Settings. If omitted, will be taken from GRIST_API_KEY env var, or ~/.grist-api-key file.
  • server (string) The server URL, i.e. the part of the document URL before "/doc/". Ignored if you specify a full URL for the first argument.
  • dryrun (boolean) If set, will not make any changes to the doc. You may run with DEBUG=grist-api to see what calls it would make.
  • chunkSize (number, default: 500) Split large requests into smaller one, each limited to chunkSize rows. If your requests are very large and hit size limits, try using a smaller value.

fetchTable(tableName, filters?)

Fetch all data in the table by the given name, returning a list of records with attributes corresponding to the columns in that table.

If filters is given, it should be an object mapping column names to array values, to fetch only records that match. For example {Name: ['eggs']}.

addRecords(tableName, records)

Adds new records to the given table. The data is a list of objects, with attributes corresponding to the columns in the table. Returns a list of added rowIds.

deleteRecords(tableName, recordIds)

Deletes records from the given table. The data is a list of record IDs.

updateRecords(tableName, records)

Update existing records in the given table. The data is a list of objects, with attributes corresponding to the columns in the table. Each object must contain the key "id" with the rowId of the row to update.

If records aren't all for the same set of columns, then a single-call update is impossible, so we'll make multiple calls.

syncTable(tableName, records, keyColIds, {filters?})

Updates Grist table with new data, updating existing rows or adding new ones, matching rows on the given key columns. (This method does not remove rows from Grist.)

The records parameter is a list of objects with column IDs as attributes.

The keyColIds parameter lists primary-key columns, which must be present in the given records.

If options.filters is given, it should be an object mapping colIds to arrays of values. Only records matching these filters will be matched as candidates for existing rows to update. New records whose columns don't match filters will be ignored.