Skip to content

Formatter

SurrealQL formatter

The surqlfmt tool reformats .surql files to follow a consistent style. It adjusts whitespace, indentation, and keyword casing so that SurrealQL code is easy to read and review.

surqlfmt is a separate command-line tool alongside the surreal CLI. It is shipped as an npm package rather than as part of the surreal binary, it never connects to a database, and it only reads and writes files.

The tool can be installed through a single npm install command:

npm install -g @surrealdb/surql-fmt
Usage
surqlfmt [OPTIONS] [FILES]...

Arguments

NameDescription
[FILES]...
Files to format, given as paths or glob patterns. Omit them when reading from stdin.

Options

NameDefaultDescription
--write, -w
NoneReformat each file in place instead of printing the result.
--check, -c
NoneReport each file that is not already formatted, without changing it, and exit with code 1 if any is not. Useful in CI.
--stdin, -s
NoneRead SurrealQL from standard input and write the formatted result to standard output.
--output, -o<FILE>
NoneWrite the result to a file instead of standard output.
--quiet, -q
NonePrint nothing to standard output.
--indent<WIDTH>
2Number of indent characters per level.
--indent-char<CHAR>
spaceCharacter used for indentation: space or tab.
--max-line-length<LENGTH>
80Line length the formatter tries to stay within, wrapping a statement that would go past it.
--version, -v
NonePrint the installed version.

The tool prints the reformatted file by default, and reformats a .surql file in place when --write is passed.

# Format a single file
surqlfmt ./query.surql

# Check if files are formatted
surqlfmt --check ./src/**/*.surql

# Format from stdin
cat query.surql | surqlfmt --stdin

# Configure indentation and line length
surqlfmt --indent 1 --indent-char tab --max-line-length 120 *.surql

# Write files in-place
surqlfmt --write ./src/**/*.surql

A long statement is split over several lines, one clause and one condition per line:

echo "SELECT name, age FROM person WHERE age > 18 AND email CONTAINS '@' ORDER BY age;" | surqlfmt --stdin
Output
SELECT
  name,
  age
FROM person
WHERE
  age > 18
  AND email CONTAINS '@'
ORDER BY age;

The formatter refuses a file that contains syntax it does not understand, rather than reformatting it, because reformatting a statement it cannot read could drop or reorder parts of it. The file is left unchanged, and surqlfmt reports the position of the first problem and exits with code 1:

Error output
error: query.surql: Unable to parse SurrealQL at position 28

The formatter can lag behind the newest SurrealQL syntax, so a query that runs on SurrealDB can still be refused here. A file with no statements, such as one holding only comments, is accepted and left as it is.

The same package exports the formatter as a library:

npm install @surrealdb/surql-fmt
import { format, formatRange, FormatSyntaxError } from "@surrealdb/surql-fmt";

// Format a whole query with four spaces per level
const formatted = format(query, { indent: 4, maxLineLength: 100 });

// Format only the statements that overlap characters 0 to 100
const partly = formatRange(query, 0, 100);

try {
    format("SELECT * FROM person WHERE (");
} catch (error) {
    if (error instanceof FormatSyntaxError) {
        console.log(`Syntax error at ${error.position}`);
    }
}

format and formatRange throw a FormatSyntaxError, carrying the position of the first syntax error, for the same input the command line refuses.

For the options of the version you have installed, run surqlfmt --help.

  • Before committing - run surqlfmt on any .surql migration or seed files to keep diffs clean.

  • In CI - add a surqlfmt --check step to catch inconsistencies early.

  • During development - pipe ad-hoc queries through the formatter for readability.

For the other command-line tools, see the CLI tools overview.

Was this page helpful?

Last updated