Skip to content

Commit

Permalink
feat: do not execute update query if value bindings object has no keys
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Jul 27, 2019
1 parent 7f2baa6 commit eccc31d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/routines/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export default async (
// eslint-disable-next-line flowtype/no-weak-types
booleanExpressionValues: Object = null
) => {
if (Object.keys(namedValueBindings).length === 0) {
return;
}

const assignmentList = sql.assignmentList(namedValueBindings);

if (booleanExpressionValues) {
Expand Down
12 changes: 12 additions & 0 deletions test/slonik-utilities/routines/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ const createConnection = () => {
return connection;
};

test('does not execute UPDATE query if named value bindings object has no keys', async (t) => {
const connection = createConnection();

await update(
connection,
'foo',
{}
);

t.is(connection.query.callCount, 0);
});

test('executes UPDATE query without WHERE condition (single column)', async (t) => {
const connection = createConnection();

Expand Down

0 comments on commit eccc31d

Please sign in to comment.