在Prisma模式中有两个独立的模型。
我需要将这两种模型结合起来,并通过createdAt进行排序。
这两种模型都有createdAt。
这样我就想得到两个由createdAt排序的数据。
这有可能在普里斯马把两种模式结合起来吗?
model Feed {
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId Int
photos String[]
caption String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
likes Like[]
comments Comment[]
}
model Poem {
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId Int
poemTitle String
poemCaption String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
poemLikes Poemlike[]
poemComments Poemcomment[]
}还是我只能在前端做?
问题是,每个模型的前端数据为2。
(前)
Feed Data: 2022-06 / 2022-07 / 2022-08
Poem Data: 2022-09 / 2022-10 / 2022-11然后我需要对它们进行排序:诗11首->诗10首->诗09 -> feed 08 -> feed 07 -> feed 06
但是,由于我在2之前得到数据,所以在当前屏幕上我只得到数据。
Feed Data: 2022-07 / 2022-08
Poem Data: 2022-10 / 2022-11然后排序将是:诗11 ->诗10 ->提要08 -> feed 07
诗09消失了。
我该如何解决这个问题?
发布于 2022-07-14 13:35:28
如果这两个模型没有共享关系,则需要分别查询它们,组合数组,然后按createdAt排序。
const [feeds, poems] = await Promise.all([
prisma.feed.findMany(), // add whatever filters you need to the "findMany" call
prisma.poem.findMany()
]);
const dataToSort = [...feeds, ...poems];
const sortedData = dataToSort.sort((itemA, itemB) => {
return new Date(itemA.createdAt).getTime() - new Date(itemB.createdAt).getTime()
});
return sortedData;https://stackoverflow.com/questions/72980150
复制相似问题