Skip to content

Commit

Permalink
Add IN support
Browse files Browse the repository at this point in the history
  • Loading branch information
AliusDieMorietur committed Jul 16, 2021
1 parent 3e63d44 commit 12f3b76
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@ const whereValue = (value) => {
return [op, value.substring(len)];
}
}

if (value.includes('*') || value.includes('?')) {
const mask = value.replace(/\*/g, '%').replace(/\?/g, '_');
return ['LIKE', mask];
}
}
if (value instanceof Array) {
return ['IN', value];
}
return ['=', value];
};

Expand All @@ -38,8 +42,16 @@ const buildWhere = (conditions, firstArgIndex = 1) => {
const keys = Object.keys(where);
for (const key of keys) {
const [operator, value] = whereValue(where[key]);
conjunction.push(`"${key}" ${operator} $${i++}`);
args.push(value);
if (value instanceof Array) {
conjunction.push(
`"${key}" ${operator} (${value
.map((_item, index) => index + 1)
.join(',')})`
);
} else {
conjunction.push(`"${key}" ${operator} $${i++}`);
args.push(value);
}
}
disjunction.push(conjunction.join(' AND '));
}
Expand Down

0 comments on commit 12f3b76

Please sign in to comment.