Registry / database / sql-query-identifier

sql-query-identifier

JSON →
library3.1.0jsnpmunverified

A SQL query identifier that parses SQL statements to determine their type (e.g., SELECT, INSERT, CREATE_TABLE) by scanning and parsing tokens. Version 3.1.0 supports a wide range of SQL dialects including MySQL, PostgreSQL, BigQuery, and Oracle, with over 50 recognized statement types. Unlike full SQL parsers, it focuses on fast identification by checking only the initial keywords and structure, making it suitable for applications that need quick classification without full validation. It is actively maintained with regular releases and includes TypeScript type definitions. The package works in both Node.js and browser environments.

npm install sql-query-identifier
INSTALL
IMPORT
SIG · SQL-QUERY-IDENTIFI
S
sql-query-identifier
databasejavascriptv3.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

identify
import { identify } from 'sql-query-identifier'
import identify from 'sql-query-identifier'
The function is a named export, not a default export.
identify (CommonJS)
const { identify } = require('sql-query-identifier')
const identify = require('sql-query-identifier')
CommonJS require returns an object with the named export.
TypeScript types
import type { IdentifiedQuery } from 'sql-query-identifier'
import { IdentifiedQuery } from 'sql-query-identifier'
Use import type for type-only imports to avoid runtime overhead.

Demonstrates basic usage of identify() to classify SQL statements, including handling multiple queries and dialect-specific options.

import { identify } from 'sql-query-identifier'; const queries = [ 'SELECT * FROM users', 'INSERT INTO logs (id, message) VALUES (1, \'test\')', 'CREATE TABLE items (id INT PRIMARY KEY)', 'DROP VIEW active_users', ]; for (const query of queries) { const results = identify(query); console.log(results); // Example output: [{ type: 'SELECT', start: 0, end: 20, text: 'SELECT * FROM users', executionType: 'LISTING' }] } // With options for MySQL dialect const mysqlQuery = 'SHOW TABLES'; const results = identify(mysqlQuery, { strict: false, dialect: 'mysql' }); console.log(results); // Output: [{ type: 'SHOW_TABLES', start: 0, end: 10, text: 'SHOW TABLES', executionType: 'LISTING' }]
Debug
Known issues
gotchaThe identifier does not validate SQL syntax; it only scans initial tokens. Invalid SQL may produce incorrect classification.
fix
Always execute the query against a database server before relying on the identifier for classification, especially for complex or dialect-specific statements.
affects: >=1.0.0
gotchaSHOW statements are only recognized for MySQL and generic dialects by default. Using other dialects will return UNKNOWN for SHOW queries.
fix
Set the dialect option to 'mysql' when working with MySQL SHOW statements, or use 'generic' for basic support.
affects: >=3.0.0
breakingIn v3.0.0, the function signature changed: the second parameter is now an options object instead of a boolean for strict mode.
fix
Update calls from identify(query, strictMode) to identify(query, { strict: strictMode }).
affects: >=3.0.0
deprecatedThe old 'type' property on results is deprecated in favor of the 'type' string directly.
fix
Access queryResult.type directly instead of queryResult.type.type.
affects: >=3.0.0
Errors
Common errors & fixes
TypeError: Cannot destructure property 'type' of 'undefined' or 'null'
Passing an empty string or null query to identify()
fix
Ensure the query string is non-empty before calling identify() or wrap in a try-catch.
Error: Unsupported SQL dialect: 'mssql'
Using a dialect string not in the supported list (mysql, postgresql, bigquery, oracle, generic)
fix
Use one of the supported dialects: 'mysql', 'postgresql', 'bigquery', 'oracle', or 'generic'.
TS2345: Argument of type 'string' is not assignable to parameter of type 'Options'
Passing strict mode as a second boolean argument instead of an options object in v3
fix
Change from identify(query, true) to identify(query, { strict: true })
Upgrade
Version history
3.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
15 hits · last 30 days
node
12
Meta
1
Amazon
1
OpenAI (training)
1
Resources
sql-query-identifier — npm install sql-query-identifier · libregistry