8000 GitHub - pnlinh-it/nestjs-prisma: Play with NestJS and Prisma
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

pnlinh-it/nestjs-prisma

Repository files navigation

Prisma schema

Typically called schema.prisma and consists of the following parts:

  • Data source: Specifies your database connection (via an environment variable)
  • Generator: Indicates that you want to generate Prisma Client
  • Data model: Defines your application models
// Data source
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL") // DATABASE_URL="mysql://root:@mysql:3306/nestjs_prisma"
}

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

// Data model
model User {
  id        Int      @id @default(autoincrement())
  email     String   @unique
  name      String
  profileId Int      @map("profile_id")
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @default(now()) @map("update_at")

  posts   Post[]
  profile Profile @relation(fields: [profileId], references: [id])

  @@map("users")
}

model Profile {
  id        Int       @id @default(autoincrement())
  birthdate DateTime?
  nickname  String?
  createdAt DateTime  @default(now()) @map("created_at")
  updatedAt DateTime  @default(now()) @map("update_at")

  user User?

  @@map("profiles")
}

model Post {
  id        Int      @id @default(autoincrement())
  authorId  Int      @map("author_id")
  content   String   @db.LongText
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @default(now()) @map("update_at")

  author     User           @relation(fields: [authorId], references: [id])
  categories CategoryPost[]

  @@map("posts")
}

model Category {
  id        Int      @id @default(autoincrement())
  name      String
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @default(now()) @map("update_at")

  posts CategoryPost[]

  @@map("categories")
}

model CategoryPost {
  id         Int      @id @default(autoincrement())
  categoryId Int      @map("category_id")
  postId     Int      @map("post_id")
  createdAt  DateTime @default(now()) @map("created_at")
  updatedAt  DateTime @default(now()) @map("update_at")

  category Category @relation(fields: [categoryId], references: [id])
  post     Post     @relation(fields: [postId], references: [id])

  @@unique([postId, categoryId])
  @@map("category_post")
}

Data model

The properties of a model are called fields, which consist of:

Optional attributes, including native database type attributes

Name Type Scalar vs Relation Type modifier Attributes
id Int Scalar - @id and @default(autoincrement())
email String Scalar - @unique
name String Scalar ? -
role Role Scalar (enum) - @default(USER)
posts Post Relation (Prisma-level field) [] -
profile Profile Relation (Prisma-level field) ? -

Prisma client

CRUD

[Nest] 89398  - 09/03/2021, 3:28:32 PM   ERROR [ExceptionsHandler] 
Invalid `prisma.user.create()` invocation:

{
  data: {
    email: 'linh@gmail.com',
    name: 'Linh Pham',
    nickname: 'Nick name',
    ~~~~~~~~
    profile: {
      create: {
        nickname: 'Nick name'
      }
    }
  }
}

Unknown arg `nickname` in data.nickname for type UserCreateInput. Did you mean `name`? Available args:
type UserCreateInput {
  email: String
  name: String
  createdAt?: DateTime
  updatedAt?: DateTime
  posts?: PostCreateNestedManyWithoutAuthorInput
  profile: ProfileCreateNestedOneWithoutUserInput
}
  • Handle exception
if ('name' in exception && exception.name === 'NotFoundError') {
     return super.catch(new NotFoundException('Resource not found'), host);
}
  • Type will be generated by Prisma. Such as: Prisma.UserCreateInput
const userPayload: Prisma.UserCreateInput = {
  email: createUser.email,
  name: createUser.name,
  profile: {
    create: {
      nickname: createUser.nickname,
    },
  },
};

About

Play with NestJS and Prisma

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0