Skip to content
Advertisement

How to return a result with duplicate id using whereIn in AdonisJs

I have a list of IDs and I want to query the database.

I press that it returns in order of index the data that appears in the list of Id.

I intend to return a list compatible with the same ID’s duplicated and in order.

Is there how?

Thanks for listening

 const result = await Estoque.query().preload('armazem').whereIn('siaId',  ['1122', '4455', '55664', '1122', '008', '4455']).orderBy('validade', 'asc')

Expected outcome

[
{siaId:'1122', ...},
{siaId:'4455', ...},
{siaId:'55664', ...},
{siaId:'1122', ...},
{siaId:'008', ...},
{siaId:'4455', ...}
]

Updated

How to convert this to lucid Adonis?

SELECT *
FROM estoques
JOIN ( -- This is your "IN" list
          SELECT '1122' AS ID
UNION ALL SELECT '4455' AS ID
UNION ALL SELECT '55664' AS ID
UNION ALL SELECT '1122' AS ID
UNION ALL SELECT '008' AS ID
UNION ALL SELECT '4455' AS ID
) x ON x.ID = estoques.siaId

Advertisement

Answer

Resolved.

const result = await Estoque.query().whereNull('siaId').union( (query)=> {
      _.forEach(_.flatten(cods), async (v, i)=>{
        if(i<=2) {
          query.from('estoques').whereNull('sia_id').unionAll(Database.from('estoques').where('sia_id', v))
        }
        })
    }, true).preload('armazem').orderBy('validade', 'asc')
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement