Pagination for multiple items

Let's say I have 100 000 rows of data in my database and I have a REST API to query these. Obviously I can't query the 100 000 rows at once so ideally I want to have around 25 rows in my UI present and 25 on the next page and so on. Question is, how to do this in an efficient way? How to request the right amount from the server and so on Thanks
56 Replies
Keef
Keef•2y ago
Check out serverside pagination You can either do it using cursors or offsets
julius
julius•2y ago
using react query you have useInfiniteQuery. trpc has some examples of this (the pattern is the same even if you're on REST): "normal": https://nextjs.trpc.io (src https://github.com/trpc/examples-next-prisma-starter) with RSC: https://rsc.trpc.io
rre
rreOP•2y ago
Interesting, are the infinite posts saved where?
rre
rreOP•2y ago
No I meant on the client
julius
julius•2y ago
when you load more the new ones gets put into the querycache
rre
rreOP•2y ago
So RQ handles caching
julius
julius•2y ago
yea
rre
rreOP•2y ago
Okay maybe this is enough for me to go on
Keef
Keef•2y ago
The term I mentioned is what that query is doing 🥺
rre
rreOP•2y ago
So by serverside pagination the endpoint would be something like /myendpoint&pages=1 Or like /mynedpoint&entries=25 Or..?
julius
julius•2y ago
yea exactly. ?page=5&page_size=20 i think are more common params or skip/take, first/after etc
rre
rreOP•2y ago
the question mark probably is the divider here? so if I do const { page, page_size } = params; it knows to split from the question mark? and julius, https://rsc.trpc.io/, this page queries like this: ?page=5&page_size=20, or along the lines I'm assuming when you scroll to the bottom, the params go up one and it fetches more
julius
julius•2y ago
yea, query param parsing can prob be a bit different in different frameworks though
rre
rreOP•2y ago
I'm using node here an express app
julius
julius•2y ago
Mastering JS
Query Parameters in Express
Express automatically parses the URL query string and stores the parsed parameters in req.query. Here's what you need to know.
julius
julius•2y ago
yea that's the gist
rre
rreOP•2y ago
Okay cool, a follow up question to my issue. I read the 100k rows from a local csv file, is there a quick an easy way to check it my database is iniatlized (so the file is already read once) so I don't have to read it again on every npm start First thought is to check if database.db file exists
julius
julius•2y ago
you can use the csv as your db xd
rre
rreOP•2y ago
nah not an option
julius
julius•2y ago
aight, well you can have a seed script or smth which loads the csv into your db
rre
rreOP•2y ago
what's a seed script
julius
julius•2y ago
Database seeding
Database seeding is populating a database with an initial set of data. It's common to load seed data such as initial user accounts or dummy data upon initial setup of an application.
rre
rreOP•2y ago
I'm loading the csv into my db in any case
Keef
Keef•2y ago
Are you using any database software?
rre
rreOP•2y ago
yes that is what I need but that script cannot be run if the db already has the rows
Keef
Keef•2y ago
Usually they'll have import functionality and you can just give it ur csv then it'll do its thing
rre
rreOP•2y ago
I'm using sqlite
Keef
Keef•2y ago
Idk the right term for it but something like datagrip Like an SQL ide
rre
rreOP•2y ago
the csv lines wont go as they are to the db so can't really do it like that
Keef
Keef•2y ago
Yeah it has functionality for that too You can map it to columns based on the column in the csv You can also just seed it with a portion of the data instead of all 100k of it If you don't want to persist/having trouble persisting it
rre
rreOP•2y ago
I'd like the data to persist in the db but I want to avoid reading it more than once if the DB exists
julius
julius•2y ago
if you know that if row1 exist then row 100k exist you can just do a single read and run seed script if it comes back empty on application launch if there could be "holes" its trickier
rre
rreOP•2y ago
that's what chatgpt told me but I'm thinking there's got to be something more better but I guess I can do it like that
Keef
Keef•2y ago
So why do you have to keep re-seeding the database?
julius
julius•2y ago
or just setup the seed script so that it stops if it finds duplicates
Keef
Keef•2y ago
Shouldn't it already be seeded since it ran once I'm just curious
rre
rreOP•2y ago
I guess you're missing my point I only need to seed it once
Keef
Keef•2y ago
I'm just confused why its a difficult thing is all. As nice as that can be
rre
rreOP•2y ago
what I want is that if the 100k rows aren't in the db, then insert them, otherwise just continue
julius
julius•2y ago
then just pnpm db-seed once and the rest of the time pnpm start?
Keef
Keef•2y ago
I want that last message to come across as nice as possible I'm not trying to be antagonistic
rre
rreOP•2y ago
I'm just wondering how to perform the check the that the seed has been done
julius
julius•2y ago
it's a bit hard to follow what you want to do. you say that you need a single seed but still want to check if it exists in runtime?
rre
rreOP•2y ago
maybe I'm explaining it horribly, let me rephrase
julius
julius•2y ago
cause ideally you'd just have a load-csv/seed script you can run once (or whenever you have reset the db for some reason)
rre
rreOP•2y ago
so I have 100k csv lines. I need to check that these are okay etc validate them, then add these to my database. in my scenario, these files are only iterated and validated if they are not already in the database. meaning this csv file is checked once and then they are not checked again UNTIL database is permantently deleted and script is restarted.
julius
julius•2y ago
then the rest of the times you just start the app and the db will be seeded from before
rre
rreOP•2y ago
yep exactly like that I run my script for the 1st time, seed happens. I close my app then I run my app again, it's already seeded and it uses the same db, no need to seed so ideally my command run app is actually "command seed && command run app"
julius
julius•2y ago
yep so
# init
pnpm db-seed
pnpm start

# then
pnpm start

pnpm start

pnpm start

# reset db
pnpm db-reset

# next time you want to run the app
pnpm db-seed
pnpm start
# init
pnpm db-seed
pnpm start

# then
pnpm start

pnpm start

pnpm start

# reset db
pnpm db-reset

# next time you want to run the app
pnpm db-seed
pnpm start
rre
rreOP•2y ago
yes, but ideally I just have npm run which is actually npm seed && npm run app, and the npm seed command checks if seed is needed.
Keef
Keef•2y ago
setting up environments is really a separate thing entirely and your app shouldn't really be responsible for it
rre
rreOP•2y ago
on the topic of npm and pnpm, what's the pros and cons of pnpm? agreed
Keef
Keef•2y ago
Having to query once to check then maybe do it is probably what you are gonna have to do if you want to be able to seed everytime or handle duplicates in ur seeding query
julius
julius•2y ago
if you want that then just read if the file exists, or try read a single line from the db
rre
rreOP•2y ago
yep
Want results from more Discord servers?
Add your server