Skip to content

Commit

Permalink
Use prepared statement for limit/offset when getting multiple users a…
Browse files Browse the repository at this point in the history
…nd order them by userId.
  • Loading branch information
k3ypusher committed Aug 2, 2024
1 parent 4cb1d79 commit ecc1de1
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion config.js.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const config = {
database: "ircbot",
connectTimeout: 60000
},
listPerPage: 100,
listPerPage: '100',
};

module.exports = config;
2 changes: 1 addition & 1 deletion helper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function getOffset(currentPage = 1, listPerPage) {
return (currentPage - 1) * [listPerPage];
return '' + (currentPage - 1) * [listPerPage];
}

function emptyOrRows(rows) {
Expand Down
3 changes: 2 additions & 1 deletion services/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ async function getMultiple(page = 1){
const offset = helper.getOffset(page, config.listPerPage);
const rows = await db.query(
`SELECT userId, nickname
FROM user LIMIT ${offset},${config.listPerPage}`
FROM user ORDER BY userId LIMIT ?,?`,
[offset, config.listPerPage]
);
const data = helper.emptyOrRows(rows);
const meta = {page};
Expand Down

0 comments on commit ecc1de1

Please sign in to comment.