{"id":43934,"library":"sql-query-builder","title":"sql-query-builder","description":"Qb is a JavaScript SQL query builder for Node.js that generates SQL from JSON objects instead of method chaining. Current stable version is 0.3.2. It builds on brianc/node-sql and supports PostgreSQL, MySQL, and SQLite. Key differentiator: designed for BI applications where non-technical users send JSON specifications via an API. It enforces security by only allowing defined tables/columns, supports automatic joins based on model associations, and provides context-aware aliases. The library is minimal and has no active releases since 2017, suggesting maintenance status.","status":"maintenance","version":"0.3.2","language":"javascript","source_language":"en","source_url":"https://github.com/jtschoonhoven/qb","tags":["javascript","query","BI","query builder","sql builder","sql","postgresql","postgres","mysql"],"install":[{"cmd":"npm install sql-query-builder","lang":"bash","label":"npm"},{"cmd":"yarn add sql-query-builder","lang":"bash","label":"yarn"},{"cmd":"pnpm add sql-query-builder","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core dependency that provides the underlying SQL generation (node-sql).","package":"sql","optional":false}],"imports":[{"note":"Default export from package; CommonJS require works with 'const Qb = require('sql-query-builder')'.","wrong":"const Qb = require('sql-query-builder').Qb","symbol":"Qb","correct":"import Qb from 'sql-query-builder'"},{"note":"Only default export; named destructure fails.","wrong":"const { Qb } = require('sql-query-builder')","symbol":"Qb","correct":"const Qb = require('sql-query-builder')"},{"note":"Must instantiate Qb with definitions and dialect before calling query. Dialect is optional but defaults to 'postgres'.","wrong":"const sql = Qb.query(spec);","symbol":"Qb.prototype.query","correct":"const qb = new Qb(definitions, dialect); const sql = qb.query(spec);"}],"quickstart":{"code":"const Qb = require('sql-query-builder');\n\n// Define tables with columns and associations\nconst definitions = {\n  users: {\n    id: {},\n    name: {},\n    posts: { association: 'hasMany', table: 'posts', key: 'user_id' }\n  },\n  posts: {\n    id: {},\n    title: {},\n    user_id: {}\n  }\n};\n\nconst qb = new Qb(definitions, 'postgres');\n\nconst spec = {\n  select: 'name',\n  from: 'users',\n  where: { field: 'id', match: { value: 1 } }\n};\n\nconst sql = qb.query(spec);\nconsole.log(sql);\n// SELECT \"users\".\"name\" AS \"name\" FROM \"users\" WHERE (\"users\".\"id\" = 1)","lang":"javascript","description":"Shows how to define tables, instantiate Qb, and generate a SELECT query with a WHERE clause."},"warnings":[{"fix":"npm install sql-query-builder sql --save","message":"The package requires the 'sql' peer dependency (node-sql) which must be installed separately.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Always pass the dialect as second argument to the constructor, e.g. new Qb(definitions, 'mysql')","message":"If you omit the dialect, it defaults to 'postgres', which may produce incompatible SQL for MySQL or SQLite.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Use consistent casing in your definitions and spec field names, or override via aliases.","message":"Table and column names are automatically quoted (double quotes for postgres), which can cause issues with case-sensitive matching.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Ensure each table property has at least an empty object for its columns (e.g., 'id': {}).","message":"The API expects the definitions object structure to exactly match the library's expectations; invalid definitions cause silent failures.","severity":"breaking","affected_versions":">=0.1.0"}],"env_vars":null,"search_vec":"'0.3.2':28 '2017':87 'activ':84 'alias':76 'allow':61 'api':55 'applic':44 'associ':70 'automat':65 'awar':75 'base':67 'bi':43,93 'brianc/node-sql':32 'build':30 'builder':4,11,95,97 'chain':23 'context':74 'context-awar':73 'current':24 'defin':62 'design':41 'differenti':40 'enforc':57 'generat':15 'instead':20 'javascript':8,91 'join':66 'json':18,51 'key':39 'librari':78 'mainten':89 'method':22 'minim':80 'model':69 'mysql':36,101 'node.js':13 'non':47 'non-techn':46 'object':19 'postgr':100 'postgresql':35,99 'provid':72 'qb':5 'queri':3,10,92,94 'releas':85 'secur':58 'send':50 'sinc':86 'specif':52 'sql':2,9,16,96,98 'sql-query-build':1 'sqlite':38 'stabl':25 'status':90 'suggest':88 'support':34,64 'tables/columns':63 'technic':48 'user':49 'version':26 'via':53","created_at":"2026-06-05T17:02:10.612498+00:00","updated_at":"2026-06-05T17:02:10.612498+00:00","problems":[{"fix":"npm install sql","cause":"Missing peer dependency 'sql' (node-sql).","error":"Cannot find module 'sql'"},{"fix":"const Qb = require('sql-query-builder'); then new Qb(...)","cause":"Importing incorrectly, e.g., using named import or destructuring the result.","error":"TypeError: Qb is not a constructor"},{"fix":"const qb = new Qb(definitions, 'postgres'); qb.query(spec);","cause":"Trying to use qb without constructing it, or calling query() on the class instead of instance.","error":"ReferenceError: dialect is not defined"}],"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://github.com/jtschoonhoven/qb","github":"https://github.com/jtschoonhoven/qb","docs":null,"changelog":null,"pypi":null,"npm":"sql-query-builder","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}}