diff --git a/server/utils/baaPagination.ts b/server/utils/baaPagination.ts index dc80ff5..76bf4cc 100644 --- a/server/utils/baaPagination.ts +++ b/server/utils/baaPagination.ts @@ -63,37 +63,40 @@ export default class BaaPagination = [], ) { + const sqlwhere = where ? `AND (${where})` : ""; switch (queryType.type) { case "before": { const [data] = await database.query( - `SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE \`${this.key}\` < ? ORDER BY \`${this.key}\` DESC LIMIT ?`, - [queryType.id, limit], + `SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE \`${this.key}\` < ? ${sqlwhere} ORDER BY \`${this.key}\` DESC LIMIT ?`, + [queryType.id, ...bind, limit], ) as unknown as data; return data; } case "after": { const [data] = await database.query( - `SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE \`${this.key}\` > ? ORDER BY \`${this.key}\` DESC LIMIT ?`, - [queryType.id, limit], + `SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE \`${this.key}\` > ? ${sqlwhere} ORDER BY \`${this.key}\` DESC LIMIT ?`, + [queryType.id, ...bind, limit], ) as unknown as data; return data; } case "around": { const [data] = await database.query( ` SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM (\n` + - `(SELECT * FROM \`${this.table}\` WHERE \`${this.key}\` >= ? ORDER BY \`${this.key}\` ASC LIMIT ?)\n` + + `(SELECT * FROM \`${this.table}\` WHERE \`${this.key}\` >= ? ${sqlwhere} ORDER BY \`${this.key}\` ASC LIMIT ?)\n` + "UNION ALL\n" + - `(SELECT * FROM \`${this.table}\` WHERE \`${this.key}\` < ? ORDER BY \`${this.key}\` DESC LIMIT ?)\n` + + `(SELECT * FROM \`${this.table}\` WHERE \`${this.key}\` < ? ${sqlwhere} ORDER BY \`${this.key}\` DESC LIMIT ?)\n` + `) as \`x\` ORDER BY \`${this.key}\` DESC`, - [queryType.id, Math.ceil(limit / 2), queryType.id, Math.floor(limit / 2)], + [queryType.id, ...bind, Math.ceil(limit / 2), queryType.id, ...bind, Math.floor(limit / 2)], ) as unknown as data; return data; } case null: { const [data] = await database.query( - `SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` ORDER BY \`${this.key}\` DESC LIMIT ?`, - [limit], + `SELECT *, CONVERT(\`${this.key}\`, CHAR) AS \`${this.key}\` FROM \`${this.table}\` WHERE TRUE ${sqlwhere} ORDER BY \`${this.key}\` DESC LIMIT ?`, + [...bind, limit], ) as unknown as data; return data; } @@ -102,7 +105,13 @@ export default class BaaPagination = [], + ) { const query = getQuery(e); let limit = defaultLimit; @@ -122,7 +131,7 @@ export default class BaaPagination>( @@ -217,9 +226,15 @@ export default class BaaPagination = [], + ) { + const sqlwhere = where ? `WHERE ${where}` : ""; const [[data]] = await database.query( - `SELECT COUNT(*) as \`count\` FROM \`${this.table}\``, + `SELECT COUNT(*) as \`count\` FROM \`${this.table}\` ${sqlwhere}`, + bind, ) as data<{count: number}>; if (!data) throw createError("Database returned no rows");