VictorTimi
VictorTimi
DTDrizzle Team
Created by VictorTimi on 10/31/2023 in #help
Infinite scroll on Drizzle ORM not working
Please I haven't gotten the solution on how to solve the Cursor issue on Drizzle ORM. I was able to use a custom limit by using the Slice method on Javascript, however for the cursor scroll I still dont know how to add a cursor here:
const selectedMessage = limitUserMessage.map((_message) => ({
isUserMessage: _message.isUserMessage!,
createdAt: _message.createdAt!,
text: _message.text!,
id: _message.id === cursor ? _message.id : undefined,
}));
const selectedMessage = limitUserMessage.map((_message) => ({
isUserMessage: _message.isUserMessage!,
createdAt: _message.createdAt!,
text: _message.text!,
id: _message.id === cursor ? _message.id : undefined,
}));
If it were to be Prisma I'd have done it this way:
cursor: cursor ? { id: cursor } : undefined,
cursor: cursor ? { id: cursor } : undefined,
However, cursor and Pagination feel's strange on Drizzle ORM, here is full code below:
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(eq(_message.fileId, fileId))
.orderBy(desc(_message.createdAt));

console.log("USER_MESSAGE", userMessage);

function limitMessage(messages: DBMessage[]) {
const messagesToLimit = limit + 1;
const lastIndex = Math.max(messagesToLimit - messages.length, 0);
return messages.slice(lastIndex);
}

const limitUserMessage = limitMessage(userMessage);

const selectedMessage = limitUserMessage.map((_message) => ({
isUserMessage: _message.isUserMessage!,
createdAt: _message.createdAt!,
text: _message.text!,
id: _message.id === cursor ? _message.id : undefined,
}));

console.log("SELECTED_MESSAGES", selectedMessage);
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(eq(_message.fileId, fileId))
.orderBy(desc(_message.createdAt));

console.log("USER_MESSAGE", userMessage);

function limitMessage(messages: DBMessage[]) {
const messagesToLimit = limit + 1;
const lastIndex = Math.max(messagesToLimit - messages.length, 0);
return messages.slice(lastIndex);
}

const limitUserMessage = limitMessage(userMessage);

const selectedMessage = limitUserMessage.map((_message) => ({
isUserMessage: _message.isUserMessage!,
createdAt: _message.createdAt!,
text: _message.text!,
id: _message.id === cursor ? _message.id : undefined,
}));

console.log("SELECTED_MESSAGES", selectedMessage);
12 replies