Fix user list pagination SQL
This commit is contained in:
@@ -172,17 +172,19 @@ export function createLocalUserAuth(pool) {
|
|||||||
}
|
}
|
||||||
const where = clauses.length ? `WHERE ${clauses.join(' AND ')}` : '';
|
const where = clauses.length ? `WHERE ${clauses.join(' AND ')}` : '';
|
||||||
const [[countRow]] = await pool.execute(`SELECT COUNT(*) AS total FROM auth_users ${where}`, params);
|
const [[countRow]] = await pool.execute(`SELECT COUNT(*) AS total FROM auth_users ${where}`, params);
|
||||||
const offset = (Math.max(page, 1) - 1) * Math.max(pageSize, 1);
|
const safePage = Math.max(Number(page) || 1, 1);
|
||||||
|
const safePageSize = Math.max(Number(pageSize) || 20, 1);
|
||||||
|
const offset = (safePage - 1) * safePageSize;
|
||||||
const [rows] = await pool.execute(
|
const [rows] = await pool.execute(
|
||||||
`SELECT * FROM auth_users ${where} ORDER BY id DESC LIMIT ? OFFSET ?`,
|
`SELECT * FROM auth_users ${where} ORDER BY id DESC LIMIT ${safePageSize} OFFSET ${offset}`,
|
||||||
[...params, Math.max(pageSize, 1), offset],
|
params,
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
items: rows.map((row) => rowToUser(row)),
|
items: rows.map((row) => rowToUser(row)),
|
||||||
total: Number(countRow.total ?? 0),
|
total: Number(countRow.total ?? 0),
|
||||||
page,
|
page: safePage,
|
||||||
pageSize,
|
pageSize: safePageSize,
|
||||||
totalPages: Math.max(1, Math.ceil(Number(countRow.total ?? 0) / pageSize)),
|
totalPages: Math.max(1, Math.ceil(Number(countRow.total ?? 0) / safePageSize)),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user