Registry / database / postgres-semicolons

postgres-semicolons

JSON →
library0.1.2jsnpmunverified

Splits Postgres SQL strings into statements by semicolons, correctly handling string literals, dollar-quoting, nested comments, and identifiers. Version 0.1.2 is stable. Uses sticky RegExps for efficient parsing. Ships TypeScript types. Minimal alternative to full SQL parsers for statement splitting.

npm install postgres-semicolons
INSTALL
IMPORT
SIG · POSTGRES-SEMICOLON
P
postgres-semicolons
databasejavascriptv0.1.2
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.

parseSplits
import { parseSplits } from 'postgres-semicolons'
Named import; TypeScript types included.
nonEmptyStatements
import { nonEmptyStatements } from 'postgres-semicolons'
Named import; filters out empty/whitespace statements.
splitStatements
import { splitStatements } from 'postgres-semicolons'
Named import; returns combined result as string array.

Splits SQL containing tricky semicolons (nested comments, dollar-quoting, escape strings) into non-empty statements.

import { parseSplits, nonEmptyStatements } from 'postgres-semicolons'; const sql = `BEGIN; /*/* SELECT 1; */ SELECT 2; */; SELECT ';'';'; SELECT $x$;$x$; -- COMMIT;`; const splits = parseSplits(sql, true); const queries = nonEmptyStatements(sql, splits.positions); console.log(queries); // [ 'BEGIN', "SELECT ';'';'", 'SELECT $x$;$x$' ]
Debug
Known issues
gotchaThe second parameter `standardConformingStrings` affects how backslash escapes are interpreted in strings. If set incorrectly, parsing of escape strings may be wrong.
fix
Set to `true` unless you have `standard_conforming_strings = off` in Postgres.
affects: >=0.0.0
gotchaDisallowed Unicode escapes inside strings with `unicodeEscape` parameter not yet implemented. May cause incorrect parse in rare cases.
fix
Manually ensure Unicode escapes are not confused with string boundaries.
affects: <=0.1.2
gotchaNested dollar-quoting (e.g., `$a$ $b$ $a$ $b$`) is not fully supported and may produce incorrect splits.
fix
Avoid nested dollar tags; use distinct tag names.
affects: <=0.1.2
gotchaThe parsed positions in `SplitPositions` may include trailing whitespace after the last statement's semicolon.
fix
Use `nonEmptyStatements` to trim whitespace or manually trim.
affects: >=0.0.0
Errors
Common errors & fixes
SyntaxError: Unterminated string constant
Input SQL has a string that starts but doesn't end (e.g., missing closing quote).
fix
Ensure input SQL is syntactically correct, or catch errors from parseSplits.
TypeError: Cannot read properties of undefined (reading 'positions')
Using result of parseSplits incorrectly, or parseSplits wasn't called with correct arguments.
fix
Check that parseSplits returns an object with a 'positions' property. Verify arguments.
Error: Dollar-quote tag must start with a letter
Invalid dollar-quote tag (e.g., starts with digit or special char).
fix
Use valid Postgres dollar-quote tags (alpha start, alphanumeric+underscore).
Upgrade
Version history
0.1.2latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
8 hits · last 30 days
node
6
Resources
postgres-semicolons — npm install postgres-semicolons · libregistry