gajus / pg-formatter Goto Github PK
View Code? Open in Web Editor NEWA PostgreSQL SQL syntax beautifier.
License: Other
A PostgreSQL SQL syntax beautifier.
License: Other
Currently, pg_format 4.1 seems to be bundled. could you update it to current?
25:11
Formatting a CREATE TABLE
statement that specifies timestamp with time zone
as column type throws an exception.
This is directly related to darold/pgFormatter#45 which was fixed in 2018.
rm,
tar
and other commands can be not found.
Any workaround? Broke npm install totally. Also when this package inluded as dependency.
I'm using the Strawberry Perl distro on Windows, along with Node 20.10.0. When I invoke pg-formatter
out of the box, it does what it's supposed to, but then it also outputs:
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LC_ALL = "C.UTF-8",
(possibly more locale environment variables)
LANG = (unset)
are supported and installed on your system.
perl: warning: Falling back to the system default locale ("English_United States.1252").
I'm a newbie with Perl and haven't been able to find much info about locale handling on Windows. But after a lot of poking around, I tracked this down to:
Lines 121 to 123 in 6e1c8af
If I change the value of LC_ALL
to C
on line 122, then everything works fine with no warnings. (It also works if I remove lines 121-123 entirely.)
Is using C
an appropriate fix on Windows? Or is there a better approach? In either case, can this be built into pg-formatter
by checking process.platform
?
Create a CLI interface.
Hi @gajus,
Would it be possible to add an option for the --comma-break
option?
--comma-break
--comma-break
https://github.com/gajus/pg-formatter/blob/master/src/format.js#L112 runs perl with an executablePath
which is not properly quoted. If the working-directory name contains spaces or other special characters, they will be interpreted as shell syntax. In the normal case, someone made the mistake of checking out their project to a directory with spaces in a parent path, and this winds up crashing. (This happened to another developer on my team). In the worst case, admittedly an unlikely case given how this library would normally be used but definitely not impossible, this is running in a context where the parent path has an attacker-controlled directory name in it, and this injects arbitrary shell or perl code.
Greetings @gajus !
Thank you for a great package, I started using it in one of my projects, it is quite handy.
I am wondering if reading config from .pg_format
works, can't find the logic for it after briefly skimming the code.
So, what I was trying to do - adding a small config, like this:
no-extra-line=1
running pg_format
with this config works, but pg-formatter
seems to ignore it.
I also did not find -L | --no-extra-line
param in CLI here.
I am happy to provide a patch to add this one, if that'd be the way to go.
Best,
Rust
Hey @gajus ,
did you already consider putting this into a prettier plugin?
I really like your eslint plugin which provides formatting as an auto-fix (via this lib) for tagged template literals. However, for normal .sql a different solution is needed (e.g. calling the CLI manually).
Since there are pretty decent prettier integrations for most IDEs, it would be nice to have this as a prettier plugin.
I started using a very simple custom plugin like the following in my private projects:
const format = require('pg-formatter').format;
// see: https://github.com/gajus/pg-formatter#configuration
const FORMAT_OPTIONS = {
functionCase: 'uppercase',
keywordCase: 'uppercase',
spaces: 2,
};
const languages = [
{
name: 'SQL',
parsers: ['postgresql'],
extensions: ['.pgsql', '.sql'],
},
];
const parsers = {
postgresql: {
parse: sql => ({ sql }),
astFormat: 'postgresql',
},
};
const printers = {
postgresql: {
print(path) {
const sql = path.getValue().sql;
return format(sql, FORMAT_OPTIONS);
},
},
};
module.exports = {
languages,
parsers,
printers,
};
Maybe we can add a prettier plugin to format not only .sql files but tagged template literals as well?
The prettier API is somewhat limited (e.g. see prettier/prettier#12139 ) but I was thinking of overwriting the typescript/javascript parser and adding the formatter as post-processing plugin to the AST output. However, this is just a very rough idea and I have no PoC yet.
Hey. Handy wrapper 👍.
For me (v2.0.2) the CLI interface cannot be called directly:
❯ npm install pg-formatter -g
❯ pg-formatter --help
/opt/homebrew/bin/pg-formatter: line 1: use strict: command not found
/opt/homebrew/bin/pg-formatter: line 2: syntax error near unexpected token `('
/opt/homebrew/bin/pg-formatter: line 2: `var __importDefault = (this && this.__importDefault) || function (mod) {'
Same for installing this as devDependency and trying to call it via npx pg-formatter
Workaround for now is: ❯ node node_modules/.bin/pg-formatter
The dist file is missing: #!/usr/bin/env node
at the top
Can this project use in the browser, e.g. Chrome、Safari?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.