IMG_3059.PNG


플라즈마 스키마 추가

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

model User {
  id         Int        @id @default(autoincrement())
  username   String     @unique
  email      String?    @unique
  password   String?
  phone      String?    @unique
  github_id  String?    @unique
  avatar     String?
  created_at DateTime   @default(now())
  updated_at DateTime   @updatedAt
  tokens     SMSToken[]
  products   Product[]
  posts      Post[]
  Comment    Comment[]
  Like       Like[]
}

model SMSToken {
  id         Int      @id @default(autoincrement())
  token      String   @unique
  created_at DateTime @default(now())
  updated_at DateTime @updatedAt
  user       User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId     Int
}

model Product {
  id          Int    @id @default(autoincrement())
  title       String
  price       Float
  photo       String
  description String

  created_at DateTime @default(now())
  updated_at DateTime @updatedAt
  user       User     @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId     Int
}

model Post {
  id          Int     @id @default(autoincrement())
  title       String
  description String?
  views       Int     @default(0)

  created_at DateTime @default(now())
  updated_at DateTime @updatedAt

  user     User      @relation(fields: [userId], references: [id], onDelete: Cascade)
  userId   Int
  comments Comment[]
  likes    Like[]
}

model Comment {
  id      Int    @id @default(autoincrement())
  payload String

  created_at DateTime @default(now())
  updated_at DateTime @updatedAt

  user   User @relation(fields: [userId], references: [id], onDelete: Cascade)
  post   Post @relation(fields: [postId], references: [id], onDelete: Cascade)
  userId Int
  postId Int
}

model Like {
  created_at DateTime @default(now())
  updated_at DateTime @updatedAt

  user   User @relation(fields: [userId], references: [id], onDelete: Cascade)
  post   Post @relation(fields: [postId], references: [id], onDelete: Cascade)
  userId Int
  postId Int

  @@id(name: "id", [userId, postId])
}

atomic operation

async function getPost(id: number) {
  try {
    const post = await db.post.update({
      where: {
        id,
      },
      data: {
        views: {
          **increment: 1,**
        },
      },
      include: {
        user: {
          select: {
            username: true,
            avatar: true,
          },
        },
        _count: {
          select: {
            comments: true,
            likes: true,
          },
        },
      },
    });
    return post;
  } catch (e) {
    return null;
  }
}


revaildatePath


Cache Tags

function getCachedLikeStatus(postId: number) {
  const cachedOperation = nextCache(getLikeStatus, ["product-like-status"], {
    tags: [`like-status-${postId}`],
  });
  return cachedOperation(postId);
}