A simple and efficient DQL (Dynatrace Query Language) formatter.
- Formats DQL queries with proper indentation and line breaks.
- Handles quoted strings correctly.
- Enforces spacing rules for brackets
[],{}and colons:. - Splits commands starting with
|into new lines. - Aligns arguments for better readability.
- Aligns closing curly brackets with the beginning of the word preceding the opening bracket for multiline blocks.
- Keeps top-level root commands (like
fetch) single-line if possible. - Ensures commas are followed by a space if not followed by a newline.
- Formats multiple arguments inside brackets
[]and{}on new lines with indentation. - Formats subqueries inside brackets
[]recursively according to the block depth. - Preserves template string variables
${...}without formatting. - Aligns subsequent arguments of a command with the first argument, which is kept on the same line as the command.
- Indents "semantic" arguments (arguments starting with
key:) by 2 spaces from the command start, instead of aligning with the first argument. - Ensures every math operator like
=is surrounded by exactly one space (handles=,==,!=,<=,>=). - Collapses multiple successive newlines into a single newline.
import { formatDql } from './src/format-dql';
const dql = '| fields entity, queryCount = toLong(queryCount), errorCount = toLong(errorCount)';
const formatted = formatDql(dql);
console.log(formatted);A CLI tool to parse files and search for DQL commands to pass them to the DQL formatter.
Build the project first so the CLI entrypoint is available in dist:
pnpm buildThen you can link it globally to use the pretty-dql command:
pnpm linkAfter linking, you can use the pretty-dql command directly:
pretty-dql <path...> [--ext=.ts,.tsx] [--fix]Alternatively, you can execute the module locally using npx without linking:
npx . <path...> [--ext=.ts,.tsx] [--fix]If you add the library to another module, you can add it as a script to your package.json:
{
"scripts": {
"pretty-dql": "pretty-dql"
}
}Then run it via:
npm/pnpm run pretty-dql -- <path...> [--ext=.ts,.tsx] [--fix]Where <path...> can be one or more:
- Individual files
- Directories (they will be scanned recursively)
By default, dql-format looks for DQL strings inside files with the following extensions:
.txt.dql.js.jsx.ts.tsx
You can override the extensions to scan using the optional --ext flag. Pass a comma-separated
list of extensions (with or without the leading dot). Examples:
--ext=.ts– only.tsfiles--ext=.ts,.tsx–.tsand.tsxfiles--ext=ts,tsx– same as above; dots are added automatically
You can also use the --fix flag to automatically replace the DQL strings in the files with the formatted versions.
For every matching file, the tool:
- Reads the file contents.
- Extracts strings that contain DQL queries.
- Formats each DQL command and prints it to
stdout, one per line.
You can also format raw DQL strings directly, without reading from files, using the --raw flag:
pretty-dql --raw "data from logs" "| filter status == 200"In this mode, each argument after --raw is treated as a DQL command string and passed directly to
formatDqlCommand, and the formatted result is printed to stdout.
Parse a single file:
pretty-dql examples/sample.tsParse multiple files:
pretty-dql src/file1.ts src/file2.ts tests/sample.txtParse an entire directory (recursively):
pretty-dql srcMix files and directories:
pretty-dql src tests some-other-file.dqlRestrict to specific extensions:
pretty-dql src --ext=.ts,.tsxFix DQL strings in files:
pretty-dql src --fixFormat raw DQL strings:
pretty-dql --raw "data from logs" "| filter status == 200"The CLI uses the following exit codes:
0– Success- At least one path was provided and at least one file was processed, or raw strings were formatted.
1– Incorrect usage or no matching files- No positional paths were provided in file mode, or no strings were provided in
--rawmode. - If no matching files are found under the given paths, it prints
No matching files foundand exits with1.
- No positional paths were provided in file mode, or no strings were provided in
2– File or path not found- At least one of the provided paths does not exist.
3– Error reading a file- An unexpected I/O error occurred while reading a file.
pnpm installpnpm run buildpnpm testpnpm run lintpnpm run format