hello, do you know if i can get a random object du...
# off-topic
t
hello, do you know if i can get a random object during the request ? right now my request get an array of all the object in the table, and after i just generate a random number between 1 and the length of the Array and only show the object where id = the random number
w
Why not get a count of the table and then do your random number generator and only get the id you choose randomly? Guessing you're worried about payload size
t
Copy code
async function getRandomQuote() {
  try {
    const { data, count } = await supabase
      .from<definitions['quotes']>('quotes')
      .select(`
        quote,
        songs (
          listen_url,
          title,
          albums (
            cover_url,
            name
          )
        )
      `, { count: 'exact'}) 

    const randomQuoteId = getRandomId(0, count!)
    return data![randomQuoteId]
  }
  catch(error) {
    console.error(error)
    return error
  };
}

function getRandomId(min: number, max: number){
  return Math.floor(Math.random()*(max-min+1)+min)
}

export default getRandomQuote
that is what i do, in fact i don't have a lot of data so i don't think it's an issue for now i just try to optimize the way to do it
k
U can try adding order by random() in request. If that doesn't work, then please create a function that does this query from postgres procedure functions
supabase.from(..).select(...).order("random()")
s
This is an example of why I dislike the Discord platform as a place to ask for help, this was answered yesterday by someone else and now that thread is gone, so the same question has to be answered again today.
t
oh too bad thread just stay 24hours on free plan, i just arrived today on the discord do you remember who answered or who asked so i could i ask him, thanks
s
I don't actually, I was just looking for the thread and noticed its gone
The best place to ask these sort of question is on the Supabase GitHub discussion, I just found similar question answered there just now https://github.com/supabase/supabase/discussions/2299
t
Thanks i will look there, didn't know about github discussions