Skip to content

Repository files navigation

pg-tables-to-zod

🌲 Purpose

A command-line utility and module to turn postgres tables into Zod (v4) schemas

🌱 Getting started

You can use this project as a dependency for your project:

# Bun
bun add pg-tables-to-zod

# Node.js
npm install pg-tables-to-zod

You can use the converter as a module:

import { convert, type TConfiguration } from 'pg-tables-to-zod';

const configuration: TConfiguration = {
	...
}

const outputSchemas = await convert(configuration);

outputSchemas.map((outputSchema) => {
	console.log(`Schema file: ${outputSchema.path}`);
	console.log(`Schema contents: ${outputSchema.schema}`);
})

Or as a CLI command:

pgtables2zod --pg-host localhost --pg-user admin --pg-password secret --pg-database my-db --pg-schema my_schema -o test/

The above example will connect to the Postgresql instance on localhost and output Zod schemas for all tables and views in my_schema to the test/ directory. Calling with -h will provide you with all the possible parameters and options. Using with the -c my-config.json option most convenient. If there is no password in the configuration file you will be prompted to enter it.

🔨 Development

Run:

# Install dependencies
bun install

Please look at example-config.json for how to setup a configuration file. You can then use bun run to generate output schemas and validate any code changes:

bun run src/cli.ts -c my-config.json

🆒 Technology stack

  • bun
  • typescript
  • zod
  • pg-structure

🔀 Branch strategy and protection

The main branch has protection enabled against direct pushes. All changes need to be done through a reviewed pull request.

🆙 Deploying

Package is published to NPM using trusted publishing from the GitHub action called release. The version bump can be done by running bun run release

Contributing

Please see CONTRIBUTING.md for contribution guidelines.

License

MIT

About

A command-line utility and module to turn postgres tables into Zod (v4) schemas

Resources

Contributing

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages