https://github.com/pg-nano/pg-parser Skip to content Navigation Menu Toggle navigation Sign in * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + GitHub Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code Explore + All features + Documentation + GitHub Skills + Blog * Solutions By size + Enterprise + Teams + Startups By industry + Healthcare + Financial services + Manufacturing By use case + CI/CD & Automation + DevOps + DevSecOps * Resources Topics + AI + DevOps + Security + Software Development + View all Explore + Learning Pathways + White papers, Ebooks, Webinars + Customer Stories + Partners * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Enterprise + Enterprise platform AI-powered developer platform Available add-ons + Advanced Security Enterprise-grade security features + GitHub Copilot Enterprise-grade AI features + Premium Support Enterprise-grade 24/7 support * Pricing Search or jump to... Search code, repositories, users, issues, pull requests... Search [ ] Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. [ ] [ ] Include my email address so I can be contacted Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly Name [ ] Query [ ] To see all available qualifiers, see our documentation. Cancel Create saved search Sign in Sign up Reseting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} pg-nano / pg-parser Public forked from launchql/libpg-query-node * Notifications You must be signed in to change notification settings * Fork 0 * Star 36 Parse your Postgres queries into a 100% type-safe AST (powered by libpg_query) 36 stars 20 forks Branches Tags Activity Star Notifications You must be signed in to change notification settings * Code * Issues 1 * Pull requests 0 * Actions * Projects 0 * Security * Insights Additional navigation options * Code * Issues * Pull requests * Actions * Projects * Security * Insights pg-nano/pg-parser This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. 16-latest BranchesTags Go to file Code Folders and files Name Name Last commit Last commit message date Latest commit History 67 Commits .github/workflows .github/workflows .vscode .vscode libpg_query @ libpg_query @ 43bad3c 43bad3c scripts scripts src src test test .gitignore .gitignore .gitmodules .gitmodules binding.gyp binding.gyp biome.json biome.json nodeFields.json nodeFields.json package.json package.json pnpm-lock.yaml pnpm-lock.yaml readme.md readme.md tsconfig.json tsconfig.json View all files Repository files navigation * README @pg-nano/pg-parser A fork of libpg-query with best-in-class type definitions and AST utilities. import { parseQuery } from "@pg-nano/pg-parser" const ast = await parseQuery("SELECT 1; SELECT 2") // ^? ParseResult ast.version // => 160001 ast.stmts // => [{ stmt: SelectStmt, stmt_len: 8 }, { stmt: SelectStmt, stmt_location: 9 }] Install pnpm add @pg-nano/pg-parser The major and minor version of this package is meant to be aligned with the supported PostgreSQL major and minor version. Older and newer versions may be compatible, but this is not guaranteed. Upon install, the pre-compiled binary for your operating system and architecture will be pulled from GitHub Releases. API This package exports the following functions: * parseQuery (for async parsing a SQL string of one or more statements) * parseQuerySync * parsePlPgSQL (for async parsing a plpgsql string) * parsePlPgSQLSync * fingerprint (for generating a unique string for a SQL string) * fingerprintSync * splitWithScannerSync (for splitting a SQL string into one or more statements) * walk (for traversing the AST) * select (for type-safe, deep field access through dot-notation) * $ (for type-safe node proxy and type guards) Note: There is no deparse function (for turning an AST back into a string) included, as this isn't needed for my use case. Walking the AST I've added a walk function for easy AST traversal. You can pass a callback or a visitor object. You can return false to not walk into the children of the current node. Each node passed to your visitor is wrapped in a NodePath instance, which tracks the parent node and provides type guards (e.g. isSelectStmt) for type narrowing. You can access the underlying node with path.node. import { parseQuerySync, walk, NodeTag } from "@pg-nano/pg-parser" walk(parseQuerySync(sql), (path) => { path.tag // string path.node // the node object path.parent // the parent node if (path.isSelectStmt()) { // The visitor pattern is also supported. walk(path.node.targetList, { ColumnRef(path) { const id = path.node.fields .map((f) => (NodeTag.isString(f) ? f.String.sval : "*")) .join(".") console.log(id) }, }) // don't walk into the children return false } }) I've also added a select function for type-safe, deep field access through dot-notation. You must not include the node types in the field path. import { select, Expr } from "@pg-nano/pg-parser" /** * Given an expression node of many possible types, * check for a `typeName` field. */ const typeName = select(expr as Expr, 'typeName') // ^? TypeName | undefined Similar to select, you may like the $ function for field access. It returns a proxy that makes field access less verbose. It also comes with type guards for all nodes. import { $, walk } from "@pg-nano/pg-parser" walk(ast, { SelectStmt(path) { for (const target of path.node.targetList) { const { name, val } = $(target) if ($.isColumnRef(val)) { console.log( name, $(val).fields.map(field => { return $.isA_Star(field) ? "*" : field.String.sval }).join("."), ) } } } }) Type definitions Every possible type that could be returned from libpg_query is defined in ast.ts. If a type is missing, it's probably because libpg_query didn't tell us about it (otherwise, please file an issue ). The type definitions are generated from the srcdata of libpg_query (the C library this package binds to). If you're interested in how they're generated, see scripts/generateTypes.ts and scripts/ inferFieldMetadata.ts. For some cases, type definitions are manually specified in scripts/typeMappings.ts. Other improvements * Uses prebuild-install to avoid bundling every platform's binaries into the package. * Added splitWithScannerSync for SQL statement splitting. * Generated unit tests (see snapshots of every SQL case supported by libpg_query). Contributing To generate the type definitions, you can use this command: pnpm prepare:types To compile the TypeScript bindings and the C++ addon (and recompile them on file changes), you can use this command: pnpm dev Otherwise, pnpm build will compile just once. If you're editing C++ code, you'll want to have compiledb installed and the clangd extension in VSCode. This enables the clangd language server for features like autocomplete, static analysis, and code navigation. brew install compiledb [?][?] Windows support: The binding.gyp file is currently broken for Windows builds. Any help would be appreciated! License MIT About Parse your Postgres queries into a 100% type-safe AST (powered by libpg_query) Topics parser postgres typescript Resources Readme Activity Custom properties Stars 36 stars Watchers 0 watching Forks 0 forks Report repository Releases 2 v16.1.1 Latest Sep 17, 2024 + 1 release Languages * TypeScript 96.2% * C++ 2.4% * Other 1.4% Footer (c) 2024 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact * Manage cookies * Do not share my personal information You can't perform that action at this time.