Skip to content

What is the best approach for building dynamic queries in .drift files (if there is one)? #3305

Closed Answered by simolus3
RedyAu asked this question in Q&A
Discussion options

You must be logged in to vote

This sounds like a perfect fit for Dart components in SQL. Drift re-interprets the $ variable syntax as a point where expressions generated by the query builder at runtime can be injected. So if you define a query like this:

custom_filter: SELECT * FROM songs_fts
  WHERE
    songs_fts MATCH 'hello'
  AND $filter;

Drift should generate a method like this:

Selectable<Song> customFilter({required Expression<bool> Function(SongsFts songsFts) filter})

Which you can then use like this (or use custom expressions for this part only if you need ->>, that operator is not available through the query API yet):

await customFilter(filter: (songsFts) {
  final contentTags = songsFts.contentMap.jsonExtract<

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
1 reply
@RedyAu
Comment options

Answer selected by RedyAu
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants