Giter VIP home page Giter VIP logo

pjs's Introduction

pjs

Yes as in pijamas; pjs started as an experiment with jackc/pgx to see if I could read data without knowing what it was ahead of time. In the end it became a somewhat useful CLI postgres client that auto-outputs to json.

When combined with jq I've found it to be very useful.

Install

go install github.com/ingcr3at1on/pjs/cmd/pjs@latest

Use

See test-local.env for a list of recognized postgres env variables. With appropriate variables set simple queries can be run as follows:

echo "SELECT * FROM pg_proc LIMIT 1;" | pjs | jq .

For more complex queries consider writing to a file and reading with cat.

cat <file> | pjs | jq .

pjs also supports passing a dsn string directly instead of using env variables by use of the --dsn (or -d) flag.

Custom Queries

Experimental support for custom queries is baked into the cobra functionality; because of this the location for storing custom queries cannot be controlled from flags. The env value PJS_CUSTOM_QUERIES can be set to use a specific directory otherwise the default is $HOME/.pjs/queries.

Custom queries are read via HCL. The template parameter is a Go template, the template values correspond to the args which are processed in order against the input of the command.

A simple query is included in custom_queries.

# The label is the name and usage message used for the cobra command.
query "indexes-for-table <table>" {
    // Description is the cobra.Command.Long and prints as part of the help text.
    description = "Returns a list of indexes for the provided table name."
    template = "SELECT * FROM pg_indexes WHERE tablename = '{{ .Table }}';"
    args = [ "Table" ]
}

A more complex query would probably use heredoc to write the template parameter across multiple lines.

query "indexes-for-table <table>" {
    description = "Returns a list of indexes for the provided table name."
    template = <<EOF
SELECT * FROM pg_indexes
WHERE tablename = '{{ .Table }}';
EOF
    args = [ "Table" ]
}

Tests

docker-compose up -d
sleep 3
godotenv -f test-local.env go test -race ./...
godotenv -f test-local-alt.env go test -race ./...
docker-compose down

pjs's People

Contributors

ingcr3at1on avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.