#Set up Nestjs with Prisma

20 messages · Page 1 of 1 (latest)

teal lintel
#

Hi i am new to Nest and I am trying to follow the guide from https://docs.nestjs.com/recipes/prisma to test out this function, but after prisma generate, my app couldn't find the schema in prisma/client: "Module '"@prisma/client"' has no exported member 'User'.",

"Module '\"@prisma/client\"' has no exported member 'Post'.", ; Can someone help me with this problem I try to search stack overflow but no answer found and some said it could relate to the prisma version.
jovial cliff
#

Yea this is an issue in the official guide.

The command npx prisma init actually outputs the below as the schema.prisma file:

// learn more about it in the docs: https://pris.ly/d/prisma-schema

// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

generator client {
  provider = "prisma-client-js"
  output   = "../generated/prisma"
}

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

The key difference here is the "output". As soon as we set this then we are no longer importing the User or Post from @prisma/client, but instead from "generated/prisma".
#

So your controller et al should get the imports from that new generated file directory like so:

import {Controller, Get, Param, Post, Body, Put, Delete,
} from '@nestjs/common';
import { PostService } from './post/post.service';
import { UserService } from './user/user.service';
import { User as UserModel, Post as PostModel} from 'generated/prisma'

@Controller()
export class AppController {
  constructor(
    private readonly userService: UserService,
    private readonly postService: PostService,
  ) {}

  @Get('post/:id')
  async getPostById(@Param('id') id: string): Promise<PostModel | null> {
    return this.postService.post({ id: Number(id) });
  }

  @Get('feed')
  async getPublishedPosts(): Promise<PostModel[]> {
    return this.postService.posts({
      where: { published: true },
    });
  }
///etc...
}
teal lintel
#

How about the PrismaService, is the problem the same? Property "user" does not exist in type "PrismaService"

jovial cliff
#

No clue how you got that error to be totally honest.

Can you share your prisma.service.ts file? It should look like this:

import { Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from 'generated/prisma';


@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
  async onModuleInit() {
    await this.$connect();
  }
}
teal lintel
#

Yea i have fixed to that and now i have a new error: could it be relate to that there is no data in the database yet?

jovial cliff
#

Nah, that's just a very common NestJS specific error due to your modules missing service/controller imports.

In your app.module.ts file, you should ensure you have the services that module holds as "Providers"

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { PrismaService } from './prisma/prisma.service';
import { PostService } from './post/post.service';
import { UserService } from './user/user.service';

@Module({
  controllers: [AppController],
  providers: [AppService, PrismaService, UserService, PostService,],
})
export class AppModule {}

Take a look through https://docs.nestjs.com/modules for more information. You'll encounter this very often as you use NestJS...

-> Controllers are the endpoints for that module
-> Providers are the services (and other things but you'll get to those as you work with NestJS more I'm sure)
-> Imports are where you designate other services from other modules (or entire modules) that you want the existing services/controllers in the current module to work with
-> Exports are where you designate what parts of the current module you want to export into other modules/services.

Getting to grips with this structure is essential to using NestJS.

My personal reccomendation for a brief intro to all this is Dave Gray's tutorial series (https://www.youtube.com/watch?v=juNVinepwKA&list=PL0Zuz27SZ-6MexSAh5x1R3rU6Mg2zYBVr). Although its a little over a year old at this point the main part of it is still useful.

Web Dev Roadmap for Beginners (Free!): https://bit.ly/DaveGrayWebDevRoadmap

In this Nest.js tutorial for beginners, we will look at what Nest.js is, compare NestJS vs Node.js and NestJS vs Express, and get a high level overview of NestJS basics as we begin creating a REST API.

💖 Support me on Patreon ➜ https://patreon.com/davegray

⭐ Be...

▶ Play video
teal lintel
#

Thank you, i will look into that

tribal tuskBOT
#

Suggestion for: @teal lintel

Please do not screenshot code as it causes a number of issues: ⚠️

  • Ease of assistance: if someone wants to copy your code and correct it, they cannot. Making it easy for people to help you is in your best interests.
  • Editorializing: it's common to try to make images small, which means you're likely to crop out code relevant to your issue
  • Accessibility: wide images can be hard to read on mobile devices, and are impossible for screen readers.
  • Legibility: you cannot read screenshots of code directly, instead you have to open them in an enlarged context.
  • Bandwidth usage/clutter: some of our members use metered connections, and it is wasteful for them to download images of a text.
    For a small amount of code, please use Markdown code blocks.
hybrid monolith
#

hey guys, im using nestjs+prisma. im running db seed, but i encounter this Error: Cannot find module 'generated/prisma/client'. have you encountered this?

azure dirge
livid oriole
#

any update to this ? I also follow to documentation, but endup in a esmodule error

<>Object.defineProperty(exports, "__esModule", { value: true });
^

ReferenceError: exports is not defined in ES module scope<>

#

is this most like a framework bug ? got stcuk at the moment T_T

teal lintel
#

i can tell you is relate to framework version, i also spent for additional 2 months just to watch learn and try to make a project using nest. watch the series above and also do some research, worst case you can ask AI.

For the above i can hint that it is simply syntaxes

vernal hazel
livid oriole
vernal hazel
#

try that