2.8 KiB
Server Side Pagination
The cross search table uses server side pagination to avoid loading tens of thousands of rows at once. Currently this is not needed for the other tables, but if this changes you can use the information on this page as a rough guide and to avoid some of the issues that might appear.
The server side pagination for cross search is implemented in the following way:
- The backend route is given values for limit, offset, column filters, and sorting.
limitandoffsetshould be numbers, and bothcolumnFiltersandsortingshould be an array of objects containing the keysidandvalue.sortingis currently an array because Material React Table supports multiple sorting values. The backend only uses the first value in the array for the SQL query.
- The values are validated. This is done in two places: the parsing and validation function, and the getCrossSearchRawSql function.
- The checks inside getCrossSearchRawSql are very important since they check if the values given to the
WHEREandORDER BYclauses are valid column ID's. Without these the query is vulnerable to SQL injection (see below)
- The checks inside getCrossSearchRawSql are very important since they check if the values given to the
- Since the SQL query uses aliases, some values used in the query need to be converted first. This is done with the convertIdToFieldName function.
- The values are given to generateCrossSearchSql function, which generates the SQL query. The values are used in the
LIMIT,OFFSET,WHEREandORDER BYclauses, respectively. To allow TableView to show and change pages correctly, we also add afull_countcolumn to every row which contains the amount of rows in the entire cross-search table. - The
getCrossSearchRawSqlfunction executes the query and returns the result.
TableView has a serverSidePagination boolean prop that should be set to True when the table in question is using server side pagination. Remember to check out the documentation for TableView too!
IMPORTANT! The WHERE and ORDER BY clauses use dynamic column names to make changing column filtering and sorting possible. This opens up a vulnerability for SQL injection, because this part of the query has to be built using the Prisma.raw function. It's essential to check that the values given by the user are proper column id's before using them in the query. Other dynamic values use the Prisma.sql function, which is safe from SQL injection.
More information about making raw queries and avoiding SQL injections here: https://www.prisma.io/docs/orm/prisma-client/using-raw-sql/raw-queries