visql
visql is a React component for building SQL queries without writing SQL.
People lay a query out on a canvas (the tables, how they join, the
conditions, the groups, the columns they want) and your app gets back a
JSON description of it. You can store that, open it again later, check
it, and turn it into SQL for MySQL, PostgreSQL or SQL Server.
I built it for report screens, where the people asking the questions know
the data but not SQL. Because the query travels as data rather than as SQL,
nothing a browser sends is ever run as SQL: your backend checks the query
and writes the SQL itself.
visql is published as is, under the MIT license. I'm not taking issues or
pull requests at the moment.
What's in it
The canvas follows the order a database works in: FROM, then WHERE, then
grouping, then SELECT, then sorting and paging.
- Tables and joins, with the join condition suggested from your foreign
keys. Pick a table that isn't directly related and the tables in between
come with it.
- Conditions in nested AND / OR / NOT groups, reordered by dragging. That
includes IN, BETWEEN, IS NULL, and EXISTS for "customers with at least
one order over 1,000".
- GROUP BY and HAVING. Add a SUM next to a column and the column becomes a
group key by itself.
- Any expression as a value or a column: functions, IF, CASE, arithmetic.
When one gets big, it opens on a canvas of its own.
- Sub queries, UNION ALL and WITH, each edited on its own canvas. These are
off until you turn them on.
- A Run button that hands the query to your backend and shows the rows it
returns, and a Check performance button that shows what the database's
query plan says: a table read whole, an index it would need, a big sort.
The plan readers for PostgreSQL, SQL Server and MySQL are in the server
package.
- Undo and redo, read-only mode, per-user table and column visibility,
translations, themes, right-to-left and vertical layouts, and a
swappable UI kit (MUI is the default).
Install
pnpm add @nongt/visql react react-dom @xyflow/react iconoir-react
# the MUI kit, which is what most apps use
pnpm add @mui/material @emotion/react @emotion/styled @emotion/cache
On a server you only need the query logic, which comes as its own package
with no dependencies:
pnpm add @nongt/visql-core
The editor works with React 18.2 or later, and 19. Both packages are always
released together with the same version. Keep the editor and the server on
the same version so the server can read every query the editor writes.
Quick start
import { useState } from 'react';
import { QueryBuilder } from '@nongt/visql/mui';
import { astToSql, hasBaseTable, type QueryValue } from '@nongt/visql';
import '@nongt/visql/style.css';
import { schema } from './schema';
export function ReportEditor() {
const [query, setQuery] = useState<QueryValue | null>(null);
const sql = query && hasBaseTable(query) ? astToSql(query, 'postgres') : '';
return (
<>
<div style={{ height: 600 }}>
<QueryBuilder schema={schema} value={query} onChange={setQuery} />
</div>
<pre>{sql}</pre>
</>
);
}
The editor fills its container, so give the container a height. astToSql
here writes the values into the SQL, which is fine for showing it. To run a
query, send it to your server, check it with validateQuery and write it
with astToParameterizedSql.
Documentation
Everything else is on the website, which
runs the editor live in its examples:
To run the playground from a clone of the repository, pnpm install and
then pnpm dev.
License
MIT