Skip to content

Instantly share code, notes, and snippets.

@stanistan
Created November 29, 2012 22:07
Show Gist options
  • Save stanistan/4172226 to your computer and use it in GitHub Desktop.
Save stanistan/4172226 to your computer and use it in GitHub Desktop.

AQL

One purpose of this (abbreviated query language) is to generate SQL queries so that you don't have to repeat yourself.

artist {
    name
}

Scoped SQL!

parse(statement).getSQL(); // select artist.name from artist

Default options.

var parser = new aql.Parser({ 
    contraints: ['active', 1],
    fields: function(table) { 
        return { id: table.name + '_id' }; 
    }
});
select artist.id as artist_id, artist.name from artist where artist.active = 1
artist {
    name
}
label on artist.id = artist_id {
    label as label_name
}
select 
    artist.id as artist_id,
    artist.name,
    label.id as label_id,
    label.name as label_name
from artist
left join label on artist.id = label.artist_id and label.active = 1
where artist.active = 1

Generating clauses

var q = parse(statment);
q.getSQL({
    where: {name: 'Pink Floyd'}
});

Generates:

select 
    artist.id as artist_id,
    artist.name,
    label.id as label_id,
    label.name as label_name
from artist
left join label on artist.id = label.artist_id and label.active = 1
where artist.active = 1 
    and artist.name = 'Pink Floyd'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment