`:read`ing random entry
meow! i'd like to ask a possibly dumb question. i am currently making an inspirational quotes API. i have a
/api/quote
endpoint with :random
action which pretty much speaks for itself - returns a random quote and its author
how should i correctly define my :random
action? i know how to fetch a random entry using Ecto from q in "table", order_by: fragment("RANDOM()"), limit: 1, select: q
. how would i do this in my action? i am using sqlite btw and here's my resource definition
i use quote's text as primary key in my db to eliminate duplicates and to add new quotes from CSV-s and other stuff w/ ease. and i guess i won't need ids for them in the near future
as you can see, i have language code in my table's name, that's because i have multiple tables with quotes in different languages. so i need to choose which table to query too...13 Replies
Solution
Try that
you may need to
require Ash.Sort
thanks! it works. now i need to somehow deal with the multiple tables situation. also API responses are a bit off - i need to return
:text
field inside "attributes"
, and not as id, but i don't have an 'id'
column in my tables
The
id
attribute is actually non-optional in the JSON:API specification
so we take the primary key and we write it to the id
attribute and remove it from attributes
I forget if there is an option to tell it not to do that, lemme double check
Looks like there is not currently, but one could be added without too much hassle. In the meantime you could use a generic action to make the response look like whatever you wanted
Then you can use route :get, "/random_quote", :random_quote
that endpoint won't be JSON:API spec compliant but you probably don't care about that for this?so there's no option for
uuid_primary_key
/integer_primary_key
[if i add one of them to my attributes list] to be like an abstract field? so Ash won't query it from my data layer?If you don't mind
id
and text
being duplicated in the structs & response then you could do:
Then your response would have id
and text
populated
the primary key can't be a calculation though, no
You could also do
and then when you create something it will generate the UUID id and text will always be unique (or otherwise an error, unless you do an upsert)it'll do the job until i add ids to my tables, thanks! considering the table situation, i see that i can add multiple DSL
table
attributes
and Ash queries from the last defined table. if i define an argument in my :random
action like :lang
, is it possible to choose table from which to query?not using multiple
table
declarations no
you'd do something like this:
If you do that, you must validate the language list 🙂where should i place
validations
block?there is one at the top level
for validations across all create/update actions (and optionally destroy)
and you can place validations in create/update/destroy actions directly
See the validations guide for more
You can't use them on read actions, you have to roll your own validation code currently in preparations
much obliged for the help!