AQL Parser
Abbreviated Query Language for generating data models (possibly for an ORM) and making writing select queries easier.
(Using jison as a parser generator.)
Install
npm install aql-parser
AQL Syntax
A basic introduction with corresponding SQL.
artist { name } -> select artist.name from artist
artist { name, bio } -> select artist.name, artist.bio from artist
artist {
name
where name ilike 'pink %'
}
label on artist.id = artist_id {
name as label_name
order by name
}
select artist.name, label.name as label_namefrom artistleft join label on artist.id = label.artist_idwhere artist.name ilike 'pink %'order by label.name
see test/sqlSpec
in tests for more structure/usage.
JS Usage
Simple
var aql = ; var statement = 'artist { name, count(*) as num_artists }'; var query = aql; // will be a Query object query; // select artist.name, count(*) as num_artists from artistquery; // a JSON representation of the query object // some data query; // ['name', 'num_artists']query; // ['name']query; // { 'name' : 'artist.name' }
SQL Options
Table constraints:
// given the previous AQL statement and query object var options = constraints: 'active' 1 // field, val; query;// select artist.name, count(*) as num_artists from artist where artist.active = 1 // with joinsvar statement = '\ artist {\ name\ }\ label on artist.id = artist_id {\ name as label_name\ }\'; aql;// select artist.name, label.name as label_name// from artist// left join label on artist.id = label.artist_id and label.active = 1// where artist.active = 1
Default fields:
var options = constraints: 'active' 1 { return id: tablename + '_id' ; }; aql;
Output:
select artist.id as artist_id, artist.name, label.id as label_id, label.name as label_namefrom artistleft join label on artist.id = label.artist_id and label.active = 1where artist.active = 1
A parser with options built in:
var options = constraints: 'active' 1 { return id: tablename + '_id' ; }; var p = options { return p; };
Modifying the query with clauses:
// this uses the above parse function var statement = '\ artist {\ name\ }\ label on artist.id = artist_id {\ name as label_name\ }\'; var query = ; query;
Output:
select artist.id as artist_id, artist.name, label.id as label_id, label.name as label_namefrom artistleft join label on artist.id = label.artist_id and label.active = 1where artist.active = 1 and artist.id = 10
var clauses = limit: 10 offset: 20; query;
Output:
select artist.id as artist_id, artist.name, label.id as label_id, label.name as label_namefrom artistleft join label on artist.id = label.artist_id and label.active = 1where artist.active = 1limit 10offset 20
See test/optionSpec
for other ways of manipulating clauses.
Currently the only clauses supported are where|order_by|group_by|having|limit|offset
.
Other Uses:
I'll get here eventually :)
Buildling the Parser
In the root project directory:
This will generate src/parser.js
and report any conflicts
npm run-script gen-parser
This will generate the parser and run the tests.
Note: This overrides the current parser, so when changing the grammar, do this in another branch.
npm run-script build-test