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

pgzod

v3.3.0

Published

Convert a database schema into a Zod schema

Downloads

868

Readme

Contributors Forks Stargazers Issues MIT License

1. About The Project

Keeping Typescript types in sync with your PostgreSQL database schemas is very useful but challenging. With PGZod that you can maintain all your tables in sync with Typescript through "Zod". Zod is, in their own words:

a TypeScript-first schema declaration and validation library. You can use a Zod schema for validation or as a normal type.

It works great when you couple it with a PostgreSQL client like slonik.

1.1. Built With

2. Getting Started

The best way to run PGZod is by installing it globally or using it through npx.

  • npm
    npm install pgzod -g
  • yarn
    yarn global add pgzod

If you want to use npx, you don't have to install PGZod run:

# Shows the command help.
npx pgzod --help

3. Usage

You can use PGZod from the command line. It needs the credentials and the address of the live database from where to read the schema. You can provide this information through command options or environment variables. All the variables from PostgreSQL are supported.

Your database credentials could be stored on a local .env file, or even better, on a secret manager. You then load the credentials to your current session and run the PGZod command.

For example, you could build the following .env file:

PGHOST=some.postgresql.host
PGPORT=12345
PGDATABASE=app
PGSSLMODE=require
PGUSER=postgres
PGPASSWORD=yourpassword!

Load the .env variables, and override others through command options.

env $(xargs < .env) pgzod --pghost 127.0.0.1 --pgport 5432 --schema public

PGZod will not look for tables on your whole database just a single schema. You can indicate your database schema through the--schema option. If you don't provide it, PGZod runs against the public schemas.

For more examples, please refer to the Documentation

Strategies

PGZod offers the concept of strategies to define how it should create the Zod validator files. Here is the current list of strategies exposed:

| Value | Description | | --- | --- | | write | Creates Zod validators considering only writes to the table. Columns that have default values will be marked as optional. | | readwrite | Creates Zod validators for both read and write actions. PGZod will create two validators for each table with the Read and Write suffix to differentiate both actions. This is probably the strategy you want to work with. |

4. Roadmap

  • Find a better solution to deal with dates, and currently, we treat them as strings.
  • Add support for more types.
  • Allow the user to override the current type mappings.
  • Add tests.

See the open issues for a complete list of proposed features (and known issues).

5. Contributing

Your contributions make the open-source community a fantastic place to learn, inspire, and create.

To suggest how to improve this project, please fork the repo and create a pull request.

You can also create an issue with the tag "enhancement."

Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

6. License

Distributed under the MIT License. See LICENSE.txt for more information.

7. Contact

Your Name - @owncoral - [email protected]

Project Link: https://github.com/owncoral/pgzod