forked from oguimbal/pgsql-ast-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deno-test.ts
30 lines (26 loc) · 895 Bytes
/
deno-test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { toSql, parseFirst, astMapper } from './.deno/mod.ts';
// create a mapper
const mapper = astMapper(map => ({
tableRef: t => {
if (t.name === 'foo') {
return {
// Dont do that... see below
// (I wrote this like that for the sake of explainability)
...t,
name: 'bAr',
}
}
// call the default implementation of 'tableRef'
// this will ensure that the subtree is also traversed.
const sup = map.super();
return sup.tableRef(t);
}
}))
// parse + map + reconvert to sql
const parsed = parseFirst('select * from foo');
const modified = mapper.statement(parsed);
const modif = toSql.statement(modified!);
console.log('Modified', modif); // => SELECT * FROM "bar"
if (modif !== 'SELECT * FROM "bAr"') {
throw new Error('💀 ' + modif);
}