asynclogin(email: string, password: string): Promise<AuthEntity> { // Step 1: Fetch a user with the given email const user = awaitthis.prisma.user.findUnique({ where: { email: email } });
// If no user is found, throw an error if (!user) { thrownewNotFoundException(`No user found for email: ${email}`); }
// Step 2: Check if the password is correct const isPasswordValid = user.password === password;
// If password does not match, throw an error if (!isPasswordValid) { thrownewUnauthorizedException('Invalid password'); }
// Step 3: Generate a JWT containing the user's ID and return it return { accessToken: this.jwtService.sign({ userId: user.id }), }; } }
model User { id Int @id @default(autoincrement()) name String? email String @unique password String createAt DateTime @default(now()) updateAt DateTime @updatedAt articles Article[]
@@map("user") }
保存文件之后,prisma 插件会自动帮我生成 Article model 内对应的字段,内容如下
1 2 3 4 5 6 7 8 9 10 11 12 13
model Article { id Int @id @default(autoincrement()) title String @unique description String? body String published Boolean @default(false) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt User User? @relation(fields: [userId], references: [id]) userId Int?
@@map("article") }
可以看到 Article model 新增了 User和userId 字段, 它们是可选的,也就是说我们可以创建没有作者的文章。我们可以修改一下字段的名称,最终结果如下:
model Article { id Int @id @default(autoincrement()) title String @unique description String? body String published Boolean @default(false) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt author User? @relation(fields: [authorId], references: [id]) authorId Int? }
model User { id Int @id @default(autoincrement()) name String? email String @unique password String createdAt DateTime @default(now()) updatedAt DateTime @updatedAt articles Article[] }
// 创建2个虚拟文章 const post1 = await prisma.article.upsert({ where: { title: 'Prisma Adds Support for MongoDB' }, update: { authorId: user1.id, }, create: { title: 'Prisma Adds Support for MongoDB', body: 'Support for MongoDB has been one of the most requested features since the initial release of...', description: "We are excited to share that today's Prisma ORM release adds stable support for MongoDB!", published: false, authorId: user1.id, }, }); console.log(post1);
// upsert:用于创建或更新,确保在满足 where 条件时更新,否则创建新记录。 const post2 = await prisma.article.upsert({ where: { title: "What's new in Prisma? (Q1/22)" }, update: { authorId: user2.id, }, create: { title: "What's new in Prisma? (Q1/22)", body: 'Our engineers have been working hard, issuing new releases with many improvements...', description: 'Learn about everything in the Prisma ecosystem and community from January to March 2022.', published: true, authorId: user2.id, }, }); console.log(post2);
const post3 = await prisma.article.upsert({ where: { title: 'Prisma Client Just Became a Lot More Flexible' }, update: {}, create: { title: 'Prisma Client Just Became a Lot More Flexible', body: 'Prisma Client extensions provide a powerful new way to add functionality to Prisma in a type-safe manner...', description: 'This article will explore various ways you can use Prisma Client extensions to add custom functionality to Prisma Client..', published: true, }, }); console.log(post3); }
Data model(数据模型):定义数据库模型。每个模型将被映射到底层数据库中的一个表。现在您的模式中还没有模型,您将在下一节中探索这一部分
数据模型
在 prisma/schema.prisma 文件中添加以下内容
1 2 3 4 5 6 7 8 9 10 11
model Article { id Int @id @default(autoincrement()) title String @unique description String? body String published Boolean @default(false) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @@map("article") }
asyncfunctionmain() { // 创建2篇虚拟文章 const post1 = await prisma.article.create({ data: { title: 'Prisma Adds Support for MongoDB', body: 'Support for MongoDB has been one of the most requested features since the initial release of...', description: "We are excited to share that today's Prisma ORM release adds stable support for MongoDB!", published: false, }, });
// upsert:用于创建或更新,确保在满足 where 条件时更新,否则创建新记录。 const post2 = await prisma.article.upsert({ where: { title: "What's new in Prisma ? (Q1 / 22)", }, update: {}, create: { title: "What's new in Prisma? (Q1/22)", body: 'Our engineers have been working hard, issuing new releases with many improvements...', description: 'Learn about everything in the Prisma ecosystem and community from January to March 2022.', published: true, }, }); console.log(post1, post2); }
样式有点丑,不过我们更在意的是功能。这里展示了 unocss, vue-router,pinia, vue-jsx 组件的使用。点击按钮触发 store 里面的 count++,点击 Go to About 触发 router-link 的页面跳转。很简单,你直接看源码的 src/app.vue 就行。