Run sub queries for update and delete with joins

This commit is contained in:
Alexandre Bodin 2021-09-16 22:18:13 +02:00
parent 86a26da896
commit bb1b3c2c6e

View File

@ -191,6 +191,18 @@ const createQueryBuilder = (uid, db) => {
this.select('*');
}
if (['delete', 'update'].includes(state.type) && state.joins.length > 0) {
this.select('id');
const subQB = this.getKnexQuery();
const nestedSubQuery = db.connection.select('id').from(subQB.as('subQuery'));
return db
.connection(tableName)
[state.type]()
.whereIn('id', nestedSubQuery);
}
switch (state.type) {
case 'select': {
if (state.select.length === 0) {
@ -223,12 +235,14 @@ const createQueryBuilder = (uid, db) => {
}
case 'update': {
qb.update(state.data);
break;
}
case 'delete': {
qb.del();
break;
}
case 'truncate': {
db.truncate();
break;
}
}