Last active
June 1, 2024 00:41
-
-
Save elpete/80d641b98025f16059f6476561d88202 to your computer and use it in GitHub Desktop.
Showing how qb makes a filter object a possibility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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