简体   繁体   English

Typescript 与棱镜

[英]Typescript with Prisma

I'm completely new to Typescript.我是 Typescript 的新手。

I've started to work with Prisma and, coming from Sequelize, I miss simply writing stuff eg user.update({... }) .我已经开始使用 Prisma,并且来自 Sequelize,我怀念简单地编写一些东西,例如user.update({... }) It seems that Prisma returns just plain objects, and not classes, so I came up with this:似乎 Prisma 只返回普通对象,而不是类,所以我想出了这个:

import { Prisma, User as PrismaUser } from '@prisma/client';
import prisma from './path-to-prisma-instance';

class User {
  constructor(data: PrismaUser) {
    Object.assign(this, data);
  }

  update = async (props: Prisma.UserUpdateArgs) => {
    const data = await prisma.user.update({
      ...props,
      where: { id: this.id } // Property 'id' does not exist on type 'User'
    });
    return new User(data);
  };
}

I'm getting that Property 'id' does not exist on type 'User' .我得到Property 'id' does not exist on type 'User'

How am I able to fix this?我该如何解决这个问题?

I don't understand why you need to return a class instead of an object but if you want to achieve that you should create your class User with the same properties of the plain object returned by我不明白为什么你需要返回class而不是object但如果你想实现这一点,你应该创建你的class用户,其properties与返回的普通 object 相同

await prisma.user.update()

your class User does not have any property .您的class用户没有任何property

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM