VictorTimi
VictorTimi
DTDrizzle Team
Created by VictorTimi on 10/31/2023 in #help
Infinite scroll on Drizzle ORM not working
Good evening @Luxaritas , I am very sorry to disturb you sir. I tried following your instruction yesterday however it went reloading the same data that was on the page instead of fetching a new 10 data, please help me see what I am doing wrong. Thanks so much! Here is the redefined code:
getFileMessages: privateProcedure
.input(
z.object({
limit: z.number().min(1).max(100).nullish(),
cursor: z.string().nullish(),
fileId: z.string(),
})
)
.query(async ({ ctx, input }) => {
const { userId } = ctx;
const { fileId, cursor } = input;
const limit = input.limit ?? INFINITE_QUERY_LIMIT;

const userFile = await db
.select()
.from(_file)
.where(eq(_file.userId, userId));

const selectedFile = userFile.find((_file) => _file.id === fileId);

if (!selectedFile) throw new TRPCError({ code: "NOT_FOUND" });

const userMessage = await db
.select()
.from(_message)
.where(or(eq(_message.fileId, fileId), gt(_message.id, cursor!)))
.limit(limit + 1)
.orderBy(desc(_message.createdAt));

console.log("USER_MESSAGE", userMessage);

const selectedMessage = userMessage.map((_message) => ({
isUserMessage: _message.isUserMessage!,
createdAt: _message.createdAt!,
text: _message.text!,
id: _message.id!,
}));

console.log("SELECTED_MESSAGES", selectedMessage);

let nextCursor: typeof cursor | undefined = undefined;
if (selectedMessage.length > limit) {
const nextItem = selectedMessage.pop();
nextCursor = nextItem?.id;
}
return {
selectedMessage,
nextCursor,
};
}),
getFileMessages: privateProcedure
.input(
z.object({
limit: z.number().min(1).max(100).nullish(),
cursor: z.string().nullish(),
fileId: z.string(),
})
)
.query(async ({ ctx, input }) => {
const { userId } = ctx;
const { fileId, cursor } = input;
const limit = input.limit ?? INFINITE_QUERY_LIMIT;

const userFile = await db
.select()
.from(_file)
.where(eq(_file.userId, userId));

const selectedFile = userFile.find((_file) => _file.id === fileId);

if (!selectedFile) throw new TRPCError({ code: "NOT_FOUND" });

const userMessage = await db
.select()
.from(_message)
.where(or(eq(_message.fileId, fileId), gt(_message.id, cursor!)))
.limit(limit + 1)
.orderBy(desc(_message.createdAt));

console.log("USER_MESSAGE", userMessage);

const selectedMessage = userMessage.map((_message) => ({
isUserMessage: _message.isUserMessage!,
createdAt: _message.createdAt!,
text: _message.text!,
id: _message.id!,
}));

console.log("SELECTED_MESSAGES", selectedMessage);

let nextCursor: typeof cursor | undefined = undefined;
if (selectedMessage.length > limit) {
const nextItem = selectedMessage.pop();
nextCursor = nextItem?.id;
}
return {
selectedMessage,
nextCursor,
};
}),
12 replies
DTDrizzle Team
Created by VictorTimi on 10/31/2023 in #help
Infinite scroll on Drizzle ORM not working
Thank you very much sir
12 replies
TTCTheo's Typesafe Cult
Created by BarMemes on 5/10/2023 in #questions
Best database for a social network if starting from scratch today?
Redis, because it's fast, especially if you're building a chat app
75 replies