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

pg-scanner

v1.0.0

Published

Reports PostgreSQL database statistics

Downloads

30

Readme

PG-Scanner

PG-Scanner is a library which reports statistics about PostgreSQL databases.

TL;DR

const { Scanner } = require("pg-scanner");

const config = {
  host: "localhost",
  port: 5432,
  user: "postgres",
  password: "password",
  database: "mydb",
};

const scanner = new Scanner({ config });

(async () => {
  await scanner.init();
  scheduleScan(60 * 60 * 1000);
})();

function scheduleScan(delay) {
  setTimeout(async () => {
    const stats = await scanner.scan();
    dump(stats);
    scheduleScan(delay);
  }, delay).unref();
}

function dump(stats) {
  const serializer = (_, value) => typeof value === "bigint" ? value.toString() : value;
  const text = JSON.stringify(stats, serializer);
  console.log(text);
}

In this example, we create a new instance of the Scanner class with the database configuration. We intialise the scanner to establish a baseline, then re-scan once per hour, logging the statistics each time.

The custom serializer is required because JavaScripts maximum safe integer is less than the maximum value of a PostgreSQL integer, and the numerical stats have a type of BigInt.

We call unref to ensure the scheduled scans to not prevent your application from shutting down if the event loop is otherwise inactive, but if you are running the above script in a standalone process you may wish to remove this call.

Index

Installation

To use the Scanner module in your project, follow these steps:

Install the Scanner module using npm or yarn:

npm install pg-scanner
# or
yarn add pg-scanner

Import the Scanner module into your JavaScript or TypeScript file:

const { Scanner } = require('pg-scanner');
// or
import { Scanner } from 'pg-scanner';

API

Scanner(options? : ScannerOptions)

| Name | Required | Notes | | ------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | config | No | A configuration object which is passed directly to node-pg. Alternatively, you can use environment variables if you prefer. | | filter | No | A function for filtering out unwanted tables. It will be called with an object with a table and schema property and should return truthy if the table is to be included in the statistics. See the filter example for more details. |

init() : Promise<void>

await scanner.init();

The init method is responsible for initialising the scanner wise baseline statistics. It will error if called repeatedly.

scan() : Promise<Stats>

await scanner.scan();

The scan method is responsible for retrieving and augmenting database statistics.

Stats

The stats returned by the scan is an array of objects with the following properties

| Name | Notes | | -------------------- | --------------------------------------------------------------------- | | schema | The schema to which the stats relate | | table | The table to which the stats relate | | sequentialScans | The total number of sequential scans performed on the table | | rowsScanned | The total number of rows returned by the sequential scans | | sequentialScansDelta | The change in sequential scans since the last check | | rowsScannedDelta | The change in rows scanned since the last check |

Contributing

Contributions to the Scanner module are welcome. If you find a bug, have a feature request, or want to improve the code, please open an issue or submit a pull request on GitHub.

License

This project is licensed under the MIT License. Feel free to use and modify the code as needed.