pgsql-ast-parser
Advanced tools
Comparing version 1.0.5 to 1.0.6
{ | ||
"name": "pgsql-ast-parser", | ||
"version": "1.0.5", | ||
"description": "Yet another simple Postgres SQL parser", | ||
"version": "1.0.6", | ||
"description": "Yet another simple Postgres SQL parser/modifier", | ||
"main": "index.js", | ||
@@ -27,3 +27,6 @@ "repository": "https://github.com/oguimbal/pgsql-ast-parser", | ||
"pgsql", | ||
"postgresql" | ||
"postgresql", | ||
"node", | ||
"parser", | ||
"ast" | ||
], | ||
@@ -30,0 +33,0 @@ "dependencies": { |
@@ -1,2 +0,2 @@ | ||
🏃♀️ `pgsql-ast-parser` is a Postgres SQL syntax parser. It produces a typed AST tree, covering the most common syntaxes of pgsql. | ||
🏃♀️ `pgsql-ast-parser` is a Postgres SQL syntax parser. It produces a typed AST (Abstract Syntax Tree), covering the most common syntaxes of pgsql. | ||
@@ -33,3 +33,3 @@ **⚠** This parser does not support (yet) PL/pgSQL. It might not even cover some funky syntaxes. | ||
Parse sql to an AST (Abstract Syntax Tree) like this: | ||
Parse sql to an AST like this: | ||
@@ -52,3 +52,3 @@ ```typescript | ||
There is a helper for that: `astVisitor`. | ||
There is a helper for that: [astVisitor](/src/ast-visitor.ts). | ||
@@ -101,9 +101,10 @@ Here is an example which lists all the tables used in a request, and which counts how many joins it contains: | ||
ℹ Like with visitor, you can also convert subparts of AST to SQL (not necessarily a whole statement) by calling other methods of toSql. | ||
Like with `astVisitor()` or `astModifier()`, you can also convert subparts of AST to SQL (not necessarily a whole statement) by calling other methods of toSql. | ||
# 📝 Modifying SQL AST | ||
There is a special kind of visitor, which I called `astMapper`, which allows you to traverse & modify ASTs on the fly. | ||
There is a special kind of visitor, which I called [astMapper](/src/ast-mapper.ts), which allows you to traverse & modify ASTs on the fly. | ||
@@ -140,3 +141,22 @@ For instance, you could rename a table in a request like this: | ||
Good to know: If you use Typescript, return types will force you to return something compatible with a valid AST. | ||
However, if you wish to remove a node from a tree, you can return null. For instance, this sample removes all references to column `'foo'`: | ||
```typescript | ||
// create a mapper | ||
const mapper = astMapper(map => ({ | ||
ref: c => c.name === 'foo' ? null : c, | ||
})) | ||
// process sql | ||
const result = mapper.statement(parseFirst('select foo, bar from test')); | ||
// Prints: SELECT "bar" FROM "test" | ||
console.log(toSql.statement(result!)); | ||
``` | ||
If no valid AST can be produced after having removed it, `result` will be null. | ||
## A note on `astMapper` performance: | ||
@@ -143,0 +163,0 @@ |
@@ -172,2 +172,15 @@ import 'mocha'; | ||
it('removes node', () => { | ||
// create a mapper | ||
const mapper = astMapper(map => ({ | ||
ref: c => c.name === 'foo' ? null : c, | ||
})) | ||
// process sql | ||
const result = mapper.statement(parseFirst('select foo, bar from test')); | ||
expect(toSql.statement(result!)).to.deep.equal('SELECT "bar" FROM "test"'); | ||
}) | ||
}); |
651722
7088
219