Skip to content

Instantly share code, notes, and snippets.

@elpete
Last active May 18, 2017 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elpete/80d641b98025f16059f6476561d88202 to your computer and use it in GitHub Desktop.
Save elpete/80d641b98025f16059f6476561d88202 to your computer and use it in GitHub Desktop.
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