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-tables-to-jsonschema

v1.1.3

Published

A command-line utility and module to turn postgres tables into JSON Schemas

Downloads

495

Readme

pg-tables-to-jsonschema

NPM version

Npm Downloads

A command-line utility and module to turn postgresql tables into JSON Schemas. Uses pg-structure for the table to json conversion.

I wrote this module because I have a set of REST-like APIs using JSON Schema for their input and output validation. The tables provide the low level data interchange formats I use throughout my code. So pairing this with my other jsonschema-to-typings utility gives me both code completion and hinting alongside jsonschema based validation.

DISCLAIMER: I wrote this module to fit my specific project needs. I may have missed a few column types. Also complex types like arrays and geo-spatial data have not been added. Feel free to fork or add pull requests for anything you are missing

UPDATE: I've now released version 1.0.0 which doesn't break previous APIs and features but is a pretty big refactor. Switched to pg-structure and typescript and now supporting config based conversion.

Command-line usage

pgtables2jsonschema --pg-host localhost --pg-user admin --pg-password secret --pg-database my-db --pg-schema my_schema -b 'http://yourhost/schema/' -o test/

Calling with -h will provide you with all the possible options:

Usage: cli [options]

  Options:

    -V, --version                 output the version number
    -c, --config                  Path to configuration file. Additional parameters override config values
    --pg-host <value>             The postgresql host to connect to
    --pg-port <n>                 The postgresql host to connect to. Defaults to 5432
    --pg-database <value>         The postgresql database to connect to
    --pg-user <value>             The postgresql user to login with
    --pg-password <value>         The postgresql password to login with
    --pg-schema <value>           The postgresql schema to convert
    -i, --indent [size]           The indent size in spaces. Default: 2
    -o, --out [file]              Output folder. Default output is to STDOUT. A sub-folder will be created per schema
    -b, --base-url [url]          The optional base url for the schema id
    -p, --additional-properties   Allow additional properties on final schema. Set option to allow properties. Default: false
    -t, --include-tables <value>  Comma separated list of tables to process. Default is all tables found
    -e, --exclude-tables <value>  Comma separated list of tables to exclude. Default is to not exclude any
    -u, --unwrap                  Unwraps the schema if only 1 is returned
    -h, --help                    output usage information

You can find an example configuration in this repository.

Code usage

You can use the schema converter module as follows:

var converter = require( "pg-tables-to-jsonschema" );

// Schemas is an array of json-schema objects
//
const schemas = await converter( {
  pg: {
    host: 'localhost',
    port: 5432,
    user: 'admin',
    password: 'secret'
    database: 'db_name',
  },
  input: {
    schemas: ['public', 'stuff'],
    exclude: ['not_this_table'],
    include: []
  },
  output: {
    additionalProperties: false,
    baseUrl: 'http://api.localhost.com/schema/',
    defaultDescription: 'Missing description',
    indentSpaces: 2,
    outDir: 'dist/schema',
    unwrap: false
  }
} );