@surrealdb/surql-fmt
v0.1.0-beta.3
Published
A SurrealQL Formatter implementation for JavaScript
Readme
SurrealQL Formatter
A formatter CLI and JavaScript library for formatting SurrealQL queries.
CLI Tool
The CLI can be used as a standalone tool to format SurrealQL queries. It is usually installed globally and accessed via the surqlfmt command.
Installation
The CLI tool can be installed using your preferred Node.js package manager.
npm install -g @surrealdb/surql-fmtUsage
# 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 4 --indent-char tab --max-line-length 120 *.surql
# Write files in-place
surqlfmt --write ./src/**/*.surqlYou can use surqlfmt --help to see all the available options.
Library
You can also install surql-fmt as a library to format SurrealQL queries programmatically.
Installation
You can install surql-fmt as a library using your preferred Node.js package manager.
npm install @surrealdb/surql-fmtUsage
You can use the format function to format a SurrealQL query string.
import { format } from "@surrealdb/surql-fmt";
// Format the query with 4 spaces indentation
const formatted = format(query, {
indent: 4
});You can also use the formatRange function to format a range of a SurrealQL query string.
// Format characters 0 to 100 of the query
const formatted = formatRange(query, 0, 100);Both functions refuse a query the parser does not fully understand, since formatting it could drop or reorder parts of it. They throw a FormatSyntaxError carrying the position of the first syntax error.
import { format, FormatSyntaxError } from "@surrealdb/surql-fmt";
try {
format(query);
} catch (error) {
if (error instanceof FormatSyntaxError) {
console.log(`Syntax error at ${error.position}`);
}
}