Last active
August 15, 2020 19:09
-
-
Save YannickFricke/ed68b56d0803722c0535730e844a93c6 to your computer and use it in GitHub Desktop.
Select mutliple IDs with TypeORM and Knex.JS from a SQLite database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let queryBuilder = knex<Message>('') | |
.from('message'); | |
if (!user.isGameMaster) { | |
queryBuilder = queryBuilder | |
.whereIn( | |
'recipient', | |
all.concat('Alle'), | |
) | |
.or | |
.whereIn( | |
'sender', | |
all, | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let queryBuilder = await this.messageRepository.createQueryBuilder('message') | |
.select('message.id', 'id') | |
.addSelect('message.sender', 'sender') | |
.addSelect('message.recipient', 'recipient') | |
.addSelect('message.content', 'content') | |
.addSelect('message.timestamp', 'timestamp') | |
.where('recipient = :all_recipients', { | |
all_recipients: 'Alle', | |
}) | |
.andWhere('sender = :user', { | |
user: user.username, | |
}) | |
.orWhere('recipient = :recipient', { | |
recipient: user.username, | |
}) | |
.orWhere('sender = :sender', { | |
sender: user.username, | |
}); | |
for (const contact of user.contacts) { | |
queryBuilder = queryBuilder.orWhere( | |
`recipient = :recipient_${contact.name}`, | |
{ | |
[`recipient_${contact.name}`]: contact.name, | |
}, | |
); | |
queryBuilder = queryBuilder.orWhere( | |
`sender = :sender_${contact.name}`, | |
{ | |
[`sender_${contact.name}`]: contact.name, | |
}, | |
); | |
} | |
return await queryBuilder.execute(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment