Proposal: add a CLI that dumps the schemas in JSON format to the console
Closed this issue · 1 comments
Motivation
When writing schemalint rules, it is often useful to see the output of the extractSchemas function for the given database. This is currently possible by writing a simple script available in the README. However, it would be more convenient to have a single command to dump the database schemas in JSON format to the console. Thanks to the npx feature, a user can run the command without installing the extract-pg-schema package.
Additionally, if the command is provided, it would be easier to integrate the extract-pg-schema with other tools, such as jq.
Proposed interface
The command would be called extract-pg-schema
and would have the following interface:
npx extract-pg-schema [-h <host>] [-p <port>] [-U <username>] [<database>]
Options:
-h, --host: host
-p, --port: port
-U, --username: username
-d, --dbname: database name
-n, --schema: schema name pattern
-N, --exclude-schema: exclude schema name pattern
This interface is intended to be a simple subset of the pg_dump command, which are already familiar to many users.
A password is expected to be prompted when the command is run just as in pg_dump, unless provided via the PGPASSWORD environment variable.
Implementation notes
I understand that this command is not an essential feature of the library, so it should not depend on any additional packages.
- To parse the command line options, I would use the util.parseArgs function in the Node.js standard library. This function is available since Node.js v18.3.0 and v16.17.0.
- In pg_dump, -n and -N options accept complex patterns, such as glob patterns and regular expressions, for filtering schemas. For simplicity, I would accept only Node.js standard regular expression strings, not glob patterns.
If this proposal is accepted, I would be happy to implement the feature and submit a pull request.
I like this. I can see the CLI being used in other setups and this will make that easier.