Hey hey, is there any way to orderBy string value ...
# orm-help
u
Hey hey, is there any way to orderBy string value for example with SQL
Copy code
.orderBy('user.state = "pending" ', 'DESC')
First show users with state pending then the rest
1
n
Hey, it seems you would need to split it into two queries, one query would get the results which has pending user state records and the second query would return the records which doesn’t have user pending state which are ordered in descending sequence. If you could share your schema file I can write a query for this case.
u
@Nurul Thanks a lot for the reply. It's an interesting idea, but I ended up writing the raw query like the following:
Copy code
[queryResult,countResult] = await prisma.$transaction(
              [
                prisma.$queryRaw<sesUserType[]>`SELECT * FROM keyweon_dev.tb_ses_user AS susr
          WHERE susr.sesId = ${Buffer.from(sesId,'base64url')}
          AND susr.keyRingKeyId IS NOT NULL AND susr.state != 'deleted' AND susr.updateTime > ${FABRICMIGRATIONDATE}
          order by susr.state = 'pending' desc, susr.userName desc
          LIMIT ${pageSize}
          OFFSET ${skip}
          `,
            prisma.$queryRaw<sesUserCount>`SELECT COUNT(*) as count FROM keyweon_dev.tb_ses_user AS susr
            WHERE susr.sesId = ${Buffer.from(sesId,'base64url')}
            AND susr.keyRingKeyId IS NOT NULL AND susr.state != 'deleted' AND susr.updateTime > ${FABRICMIGRATIONDATE}
            order by susr.state = 'pending' desc, susr.userName desc
            `
              ]
            )
Thanks again for the reply!
🙌 1