Instantly share code, notes, and snippets.

Embed
What would you like to do?
Showing how qb makes a filter object a possibility
// Any Query Filter will loop through the request context
// see if any of the keys match functions on the concrete class,
// and if they do, call the function with the value in the request context.
component {
function apply(rc, query) {
variables.query = arguments.query;
for (var key in rc) {
if (structKeyExists(variables, key) && isCustomFunction(variables[key]) {
var func = variables[key];
func(rc[key]);
}
}
return variables.query;
}
}
// Handler
component {
property name='PostsFilters' inject='id';
function index(event, rc, prc) {
var query = wirebox.getInstance('Builder@qb');
PostsFilters.apply(rc, query.from('posts'));
prc.posts = query.get();
}
}
// Every function we define here is a parameter
// in the query string that we can respond to.
// So, '/posts?popular&limit=5' would trigger both
// the `popular` method and the `limit` method.
component extends='AbstractQueryFilters' {
function popular(order = 'desc') {
query.orderBy('views', order);
}
function author(authorName) {
query.join('authors', 'authors.id', '=', 'posts.author_id')
.whereAuthor(authorName);
}
function publishedAt(day) {
query.where('published_at', '=', day);
}
function limit(entries) {
query.limit(entries);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment