Skip to content

Commit

Permalink
Allow ignoring tables
Browse files Browse the repository at this point in the history
  • Loading branch information
octo-topi committed Sep 26, 2023
1 parent 5e08f3f commit 9c7278a
Show file tree
Hide file tree
Showing 9 changed files with 294 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .idea/GitLink.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

161 changes: 161 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,22 @@ include the library itself (`knex`), but also transaction variables (`trx`,

### `knex/avoid-injections`

Avoid some issues related to SQL injection by disallowing plain strings as the query argument to the raw queries. Check out [the tests](https://github.com/AntonNiklasson/eslint-plugin-knex/blob/master/rules/avoid-injections.test.js) to get a sense for what is valid and not.
Avoid some issues related to SQL injection by disallowing plain strings as the query argument to the raw queries. Check
out [the tests](https://github.com/AntonNiklasson/eslint-plugin-knex/blob/master/rules/avoid-injections.test.js) to get
a sense for what is valid and not.

### `knex/avoid-updating-all-rows`

Avoid updating all rows of a table when unwanted.
You can ignore tables for which it is a legitimate usage.

```
{
settings: {
knex: {
rule: { "avoid-updating-all-rows": { tablesToIgnore: ["author"] } },
},
}
}
```

15 changes: 12 additions & 3 deletions rules/avoid-updating-all-rows.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,27 @@ const meta = {
},
};

const create = function (context) {
const create = function(context) {
return {
[`CallExpression[callee.property.name='update'][callee.object.callee.name='knex']`](node) {
check(context, node);
},
};
};
const check = function (context, node) {
const check = function(context, node) {

if (context.settings && context.settings.knex && context.settings.knex.rule) {
const ruleSettings = context.settings.knex.rule["avoid-updating-all-rows"];
const tablesToIgnore = ruleSettings.tablesToIgnore;
const tableToUpdate = node.callee.object.arguments[0].value;
if (tablesToIgnore.includes(tableToUpdate)) {
return;
}
}

context.report({
node,
messageId: "avoid"
messageId: "avoid",
});
};

Expand Down
21 changes: 20 additions & 1 deletion rules/avoid-updating-all-rows.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,29 @@ const tester = new RuleTester({
});

tester.run("avoid-updating-all-rows", rule, {
valid: ["knex('books').where({id:1}).update({'status': 'archived'})"],
valid: ["knex('books').where({id:1}).update({'status': 'archived'})",
{
code: "knex('books').update({'status': 'archived'})",
settings: {
knex: {
rule: { "avoid-updating-all-rows": { tablesToIgnore: ["books"] } },
},
},
}],
invalid: [
invalidCase("knex('books').update({'status': 'archived'})", [
{ messageId: "avoid" },
]),
invalidCase("knex('books').update({'status': 'archived'})", [
{ messageId: "avoid" }],
{
settings: {
knex: {
rule: { "avoid-updating-all-rows": { tablesToIgnore: ["author"] } },
},
},
},
),

],
});

0 comments on commit 9c7278a

Please sign in to comment.