New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@ovotech/potygen-cli

Package Overview
Dependencies
Maintainers
0
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ovotech/potygen-cli

Command line interface for potygen (Postgres Typescript Generator)

0.10.11
latest
npm
Version published
Weekly downloads
3.3K
-27.96%
Maintainers
0
Weekly downloads
 
Created
Source

Potygen CLI (Postgres typescript generator)

Command line tool for generating types for potygen

Installation

yarn add --dev @ovotech/potygen-cli

Usage

If we had sql string templates like this:

examples/simple.ts

import { sql } from '@ovotech/potygen';
import { Client } from 'pg';

const db = new Client(process.env.POSTGRES_CONNECTION);

async function main() {
  await db.connect();
  const productsSql = sql`SELECT product FROM orders WHERE region = $region`;
  const data = await productsSql(db, { region: 'Sofia' });

  console.log(data);

  await db.end();
}

main();

We could then run the potygen cli tool to generate types for them

yarn potygen -n postgres://localhost:5432/postgres examples/simple.ts

After that you can import the types and use them, which will type both parameters of the query as well as the results

examples/simple-typed.ts

import { sql } from '@ovotech/potygen';
import { Client } from 'pg';
import { ProductsSqlQuery } from './simple-typed.queries';

const db = new Client(process.env.POSTGRES_CONNECTION);

async function main() {
  await db.connect();
  const productsSql = sql<ProductsSqlQuery>`SELECT product FROM orders WHERE region = $region`;
  const data = await productsSql(db, { region: 'Sofia' });

  console.log(data);

  await db.end();
}

main();

Config

You can define potygen.config.json or provide a json file to the --config property. The full list of parameters are:

Usage: potygen [options]

Convert postgres query files into typescript types

Options:
  -V, --version                  output the version number
  -c, --config <config>          A configuration file to load (default: "potygen.config.json")
  -f, --files <files>            A glob pattern to search files by (default: "**/*.sql")
  -w, --watch                    Watch for file changes and update live
  -v, --verbose                  Show verbose logs
  -a, --cache-file <cacheFile>   Cache file to be used by --cache (default: ".cache/potygen.cache")
  -r, --cache-clear              Clear the cache
  -e, --cache                    Cache which files have been processed, defaults .cache/potygen.cache
  -s, --silent                   Only show error logs
  -p, --typePrefix <typePrefix>  Prefix generated types
  -l, --preload                  Load all data at once. Slower start but faster for a lot of files
  -r, --root <root>              Set the root directory (default: ~/Projects/potygen/packages/cli)
  -n, --connection <connection>  Connection to the postgres database. URI (default: "postgres://localhost:5432/db")
  -t, --template <template>      A template of the path, where to generate the typescript type files. The parameters are the response from node's path.parse function (default:
                                 "{{dir}}/{{name}}.queries.ts")
  -h, --help                     display help for command

FAQs

Package last updated on 05 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts