Skip to content

Instantly share code, notes, and snippets.

@YannickFricke
Last active August 15, 2020 19:09
Show Gist options
  • Save YannickFricke/ed68b56d0803722c0535730e844a93c6 to your computer and use it in GitHub Desktop.
Save YannickFricke/ed68b56d0803722c0535730e844a93c6 to your computer and use it in GitHub Desktop.
Select mutliple IDs with TypeORM and Knex.JS from a SQLite database
let queryBuilder = knex<Message>('')
.from('message');
if (!user.isGameMaster) {
queryBuilder = queryBuilder
.whereIn(
'recipient',
all.concat('Alle'),
)
.or
.whereIn(
'sender',
all,
);
}
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