Search code examples
postgresqljsonbsupabase

JSONB filter on select via Supabase


I have such a logic (attributes column's type is JSONB - array of objects) that works:

enter image description here

But I want to implement logical OR here if trait_type is equal ... not AND:

enter image description here

JSONB's column structure:

[
   {
      "value":"Standard Issue Armor 1 (Purple)",
      "trait_type":"Clothes"
   },
   {
      "value":"Standard Issue Helmet 1 (Red)",
      "trait_type":"Full Helmet"
   },
   {
      "value":"Chrome",
      "trait_type":"SmartSkin"
   },
   {
      "value":"Base Drone (Blue)",
      "trait_type":"Drone"
   },
   {
      "value":"Thick",
      "trait_type":"Eyebrows"
   }
]

How that could be done?

Thanks in advance!


Solution

  • I didn't verify the code, so might not work, but I believe at least is in the right direction. You can use the .or() filter to connect multiple filters with logical or operator. For contains(), you can use the cs keyword inside the or filter like this:

    const { data, error } = await supabase.from('NTFs')
      .select('name, id_in_collection, owner_address')
      .eq('collection_id', Number(id))
      .contains('attributes', JSON.stringify([{trait_type: 'SmartSkin', value: 'Chrome'}]))
      .or(`attributes.cs.${JSON.stringify([{trait_type: 'Drone', value: 'Armed Drone (Blue)'}])}`, `attributes.cs.${JSON.stringify([{trait_type: 'Drone', value: 'Armed Drone (Green)'}])}`)
      .order('id_in_collection')
      .range(fromIndex, toIndex)