{"id":42613,"library":"dbgate-sqltree","title":"dbgate-sqltree","description":"dbgate-sqltree is a JavaScript/TypeScript SQL query-builder library that represents SQL queries as raw JSON objects. The current stable version is 7.1.13. It supports multiple SQL dialects including MySQL, PostgreSQL, MSSQL, and SQLite through driver plugins. Unlike ORM-based solutions, it provides a low-level, flexible JSON structure for constructing queries programmatically. The library is part of the dbgate ecosystem and relies on external database plugins for SQL dialect specifics. It is primarily used for generating SQL strings from query trees.","status":"active","version":"7.1.13","language":"javascript","source_language":"en","source_url":"https://github.com/dbgate/dbgate","tags":["javascript","sql","mssql","postgresql","query","dbgate","typescript"],"install":[{"cmd":"npm install dbgate-sqltree","lang":"bash","label":"npm"},{"cmd":"yarn add dbgate-sqltree","lang":"bash","label":"yarn"},{"cmd":"pnpm add dbgate-sqltree","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required to generate dialect-specific SQL for MySQL; other plugins for PostgreSQL, MSSQL, etc.","package":"dbgate-plugin-mysql","optional":true}],"imports":[{"note":"ESM import is preferred; CommonJS require works as shown, but ensure you access the named export correctly.","wrong":"const treeToSql = require('dbgate-sqltree').treeToSql","symbol":"treeToSql","correct":"import { treeToSql } from 'dbgate-sqltree'"},{"note":"Similar to treeToSql, this is a named export used as the format function in treeToSql.","wrong":"const dumpSqlSelect = require('dbgate-sqltree').dumpSqlSelect","symbol":"dumpSqlSelect","correct":"import { dumpSqlSelect } from 'dbgate-sqltree'"},{"note":"Default import is not available; use namespace import to access all exports.","wrong":"const sqltree = require('dbgate-sqltree')","symbol":"default","correct":"import * as sqltree from 'dbgate-sqltree'"}],"quickstart":{"code":"import { treeToSql, dumpSqlSelect } from 'dbgate-sqltree';\nimport mysqlDriver from 'dbgate-plugin-mysql';\n\nconst select = {\n  commandType: 'select',\n  from: { name: { pureName: 'Album' } },\n  columns: [{ exprType: 'column', columnName: 'name' }],\n  orderBy: [{ exprType: 'column', columnName: 'id', direction: 'ASC' }],\n};\n\nconst sql = treeToSql(mysqlDriver, select, dumpSqlSelect);\nconsole.log(sql); // SELECT \"Album\".\"name\" FROM \"Album\" ORDER BY \"Album\".\"id\" ASC","lang":"typescript","description":"Shows how to create a SELECT query tree and convert it to SQL using MySQL dialect."},"warnings":[{"fix":"Update query trees to include exprType for columns and use { pureName: 'tableName' } for table names.","message":"Version 7.x changed the structure of query tree nodes: 'exprType' is required for column expressions, and table references use { pureName: string } instead of a simple string.","severity":"breaking","affected_versions":">=7.0.0"},{"fix":"Ensure your query tree matches the expected TypeScript interfaces (see types.ts in repo).","message":"The library does not validate query objects; invalid JSON structures produce runtime errors or malformed SQL.","severity":"gotcha","affected_versions":"all"},{"fix":"Switch to ESM imports with named exports.","message":"Older versions used a different import path: require('dbgate-sqltree') provides treeToSql and other functions, but the preferred method is ESM import.","severity":"deprecated","affected_versions":"<7.0.0"},{"fix":"Use ESM imports or destructure from the require result.","message":"When using CommonJS require, you must use require('dbgate-sqltree').treeToSql. const { treeToSql } = require('dbgate-sqltree') may not work in all bundlers.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"search_vec":"'7.1.13':28 'base':46 'builder':13 'construct':58 'current':24 'databas':73 'dbgate':2,5,67,95 'dbgate-sqltre':1,4 'dialect':33,77 'driver':41 'ecosystem':68 'extern':72 'flexibl':54 'generat':84 'includ':34 'javascript':90 'javascript/typescript':9 'json':21,55 'level':53 'librari':14,62 'low':52 'low-level':51 'mssql':37,92 'multipl':31 'mysql':35 'object':22 'orm':45 'orm-bas':44 'part':64 'plugin':42,74 'postgresql':36,93 'primarili':81 'programmat':60 'provid':49 'queri':12,18,59,88,94 'query-build':11 'raw':20 'reli':70 'repres':16 'solut':47 'specif':78 'sql':10,17,32,76,85,91 'sqlite':39 'sqltree':3,6 'stabl':25 'string':86 'structur':56 'support':30 'tree':89 'typescript':96 'unlik':43 'use':82 'version':26","created_at":"2026-06-05T16:55:49.297701+00:00","updated_at":"2026-06-05T16:55:49.297701+00:00","problems":[{"fix":"Run npm install dbgate-plugin-mysql","cause":"The mysql driver plugin is not installed.","error":"Error: Cannot find module 'dbgate-plugin-mysql'"},{"fix":"Use import { treeToSql } from 'dbgate-sqltree'","cause":"Incorrect import; possibly default import instead of named export.","error":"TypeError: treeToSql is not a function"},{"fix":"Ensure query object follows the TypeScript interfaces; check for typos.","cause":"Invalid query tree structure, usually missing 'exprType' or invalid key.","error":"SyntaxError: Unexpected token ':' in JSON at position X"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://www.dbgate.io/","github":"https://github.com/dbgate/dbgate","docs":null,"changelog":null,"pypi":null,"npm":"dbgate-sqltree","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}