index added to db, pagination working
This commit is contained in:
1 parent
eada8b3eb1
commit
98c9328a3d
13 files changed
+428
-77
No files matched your search
@@ -0,0 +1,17 @@
|
||||
-- DropForeignKey
|
||||
ALTER TABLE "Note" DROP CONSTRAINT "Note_userId_fkey";
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE "Note" ALTER COLUMN "userId" DROP NOT NULL;
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "Article_url_idx" ON "Article"("url");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "Collection_userId_idx" ON "Collection"("userId");
|
||||
|
||||
-- CreateIndex
|
||||
CREATE INDEX "QuickNote_collectionId_idx" ON "QuickNote"("collectionId");
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "Note" ADD CONSTRAINT "Note_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
@@ -1,3 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (e.g., Git)
|
||||
provider = "postgresql"
|
||||
provider = "postgresql"
|
||||
@@ -18,6 +18,8 @@ model Collection {
|
||||
notes Note[]
|
||||
userId String
|
||||
user User @relation(fields: [userId], references: [id])
|
||||
|
||||
@@index([userId])
|
||||
}
|
||||
|
||||
model Article {
|
||||
@@ -34,6 +36,8 @@ model Article {
|
||||
categories String[]
|
||||
authors String[]
|
||||
Note Note[]
|
||||
|
||||
@@index([url])
|
||||
}
|
||||
|
||||
model QuickNote {
|
||||
@@ -50,6 +54,8 @@ model QuickNote {
|
||||
articleId String?
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
|
||||
@@index([collectionId])
|
||||
}
|
||||
|
||||
model Note {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Controller, Get } from '@nestjs/common';
|
||||
import { Controller, Get, Query } from '@nestjs/common';
|
||||
import { ArticlesService } from './articles.service';
|
||||
|
||||
@Controller('api/articles')
|
||||
@@ -6,7 +6,18 @@ export class ArticlesController {
|
||||
constructor(private readonly articlesService: ArticlesService) {}
|
||||
|
||||
@Get()
|
||||
async findAll() {
|
||||
return this.articlesService.findAll();
|
||||
async findAll(
|
||||
@Query('limit') limit?: number,
|
||||
@Query('cursor') cursor?: string,
|
||||
) {
|
||||
return this.articlesService.findAll(
|
||||
limit ? Number(limit) : undefined,
|
||||
cursor,
|
||||
);
|
||||
}
|
||||
|
||||
@Get('count')
|
||||
async getCount() {
|
||||
return { count: await this.articlesService.count() };
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,52 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { PaginatedArticles } from './types';
|
||||
|
||||
@Injectable()
|
||||
export class ArticlesService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async findAll() {
|
||||
return this.prisma.article.findMany({
|
||||
orderBy: { publishedAt: 'desc' },
|
||||
take: 100,
|
||||
async findAll(
|
||||
limit: number = 20,
|
||||
cursor?: string,
|
||||
): Promise<PaginatedArticles> {
|
||||
const articles = await this.prisma.article.findMany({
|
||||
take: limit + 1,
|
||||
skip: cursor ? 1 : 0,
|
||||
cursor: cursor
|
||||
? {
|
||||
id: cursor,
|
||||
}
|
||||
: undefined,
|
||||
orderBy: [
|
||||
{ publishedAt: 'desc' },
|
||||
{ id: 'desc' }, // Secondary sort for stability
|
||||
],
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
content: true,
|
||||
source: true,
|
||||
url: true,
|
||||
publishedAt: true,
|
||||
categories: true,
|
||||
authors: true,
|
||||
},
|
||||
});
|
||||
|
||||
let nextCursor: string | null = null;
|
||||
if (articles.length > limit) {
|
||||
const nextItem = articles.pop()!;
|
||||
nextCursor = nextItem.id;
|
||||
}
|
||||
|
||||
return {
|
||||
articles,
|
||||
nextCursor,
|
||||
};
|
||||
}
|
||||
|
||||
async count(): Promise<number> {
|
||||
return this.prisma.article.count();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
export interface PaginatedArticles {
|
||||
articles: any[];
|
||||
nextCursor: string | null;
|
||||
}
|
||||
Reference in new issue
Block a user