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

@sogelink-research/pgrest-client

v1.4.0

Published

Simple JS client for PGRest

Downloads

9

Readme

@sogelink-research/pgrest-client

@sogelink-research/pgrest-client is a JavaScript client library for interacting with PGRest servers.

Installation

Using npm

To install the package via npm:

npm install @sogelink-research/pgrest-client

Using jsDelivr

To use the package directly in the browser via jsDelivr, include the following script tag in your HTML file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PGRestClient Example</title>
</head>
<body>
    <script type="module">
        import { PGRestClient } from 'https://cdn.jsdelivr.net/npm/@sogelink-research/pgrest-client@latest/dist/pgrest-client.esm.js';

        const client = new PGRestClient('https://example.com', 'your-client-id', 'your-client-secret');

        async function fetchData() {
            try {
                const response = await client.query('SELECT * FROM table');
                console.log(response);
            } catch (error) {
                console.error('Error:', error);
            }
        }

        fetchData();
    </script>
</body>
</html>

Usage

Constructor

Create an instance of PGRestClient with the following constructor:

const client = new PGRestClient(url, clientID, clientSecret, [connection]);
  • url: The URL to the PGRest server.
  • clientID: The client ID for authentication.
  • clientSecret: The client secret for authentication.
  • connection (optional): The name of the connection to use. Defaults to "default".

query Method

The query method executes a query against the server:

client.query(query, [options])

Parameters

  • query: A string representing the query to execute.
  • options (optional): An object with the following properties:
    • connection: A string specifying the connection to use. Defaults to the client's connection.
    • format: The response format. Defaults to "json". Options include:
      • "json"
      • "jsonDataArray"
      • "csv"
      • "arrow"
      • "parquet"
    • encoding: The response encoding. Defaults to "gzip, br".
    • executionTimeFormatter: A function to format the execution time. Defaults to the client's default formatter.

Returns

A promise that resolves to the server's response in the specified format. Throws an error object when result is not ok.

Example usage

import { PGRestClient } from '@sogelink-research/pgrest-client';

const client = new PGRestClient('https://example.com', 'your-client-id', 'your-client-secret');

function logError(error) {
    console.log("--------------------------------");
    console.log(`${error.status} - ${error.statusText}`);
    console.log("--------------------------------");
    console.log(`Message: ${error.error}`);
    if(error.details) {
        console.log(`Details: ${error.details}`);
    }
}

// Example 1: Execute a query in JSON format
async function fetchData() {
  try {
    const response = await client.query('SELECT * FROM table');
    console.table(result.data);
  } catch (error) {
    logError(error);
  }
}

fetchData();

// Example 2: Execute a query in CSV format
async function fetchCSVData() {
  try {
    const response = await client.query('SELECT * FROM table', { format: 'csv' });
    console.log(response);
  } catch (error) {
    logError(error);
  }
}

fetchCSVData();