notiz-dev / nestjs-prisma-starter Goto Github PK
View Code? Open in Web Editor NEWStarter template for NestJS 😻 includes GraphQL with Prisma Client, Passport-JWT authentication, Swagger Api and Docker
License: MIT License
Starter template for NestJS 😻 includes GraphQL with Prisma Client, Passport-JWT authentication, Swagger Api and Docker
License: MIT License
Hi, the example is using graphql, is threre an example showing how to integrate nestjs with prisma2 using rest api?
I feel this Prisma + nexus is better, can there be a CODE-FIRST second method, or integration?
i got the following error
npm run start
[email protected] start
nest start
node_modules/@nestjs/graphql/dist/interfaces/gql-gateway-module-options.interface.d.ts:1:58 - error TS2307: Cannot find module '@apollo/gateway' or its corresponding type declarations.
1 import { GatewayConfig, ServiceEndpointDefinition } from '@apollo/gateway';
~~~~~~~~~~~~~~~~~
node_modules/@nestjs/graphql/dist/interfaces/gql-gateway-module-options.interface.d.ts:2:35 - error TS2307: Cannot find module '@apollo/gateway/src/datasources/types' or its corresponding type declarations.
2 import { GraphQLDataSource } from '@apollo/gateway/src/datasources/types';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Found 2 error(s).
i run with :
npm version 7.11.2
node version 15.12.0
Why did you decide not to implement the Logout functionality?
"Remember me" feature is missing, any plans for that ?
e2e tests passed
$ jest --config ./test/jest-e2e.json
FAIL test/app.resolver.e2e-spec.ts
● AppResolver (e2e) › helloWorld (Query)
Undefined type error. Make sure you are providing an explicit type for the "user" of the "AuthResolver" class.
at TypeMetadataStorageHost.compileExternalFieldResolverMetadata (../node_modules/@nestjs/graphql/dist/schema-builder/storages/type-metadata.storage.js:256:23)
at ../node_modules/@nestjs/graphql/dist/schema-builder/storages/type-metadata.storage.js:240:22
at Array.forEach (<anonymous>)
at TypeMetadataStorageHost.compileFieldResolverMetadata (../node_modules/@nestjs/graphql/dist/schema-builder/storages/type-metadata.storage.js:229:18)
at TypeMetadataStorageHost.compile (../node_modules/@nestjs/graphql/dist/schema-builder/storages/type-metadata.storage.js:144:14)
at GraphQLSchemaFactory.<anonymous> (../node_modules/@nestjs/graphql/dist/schema-builder/graphql-schema.factory.js:38:57)
at ../node_modules/@nestjs/graphql/node_modules/tslib/tslib.js:114:75
at Object.__awaiter (../node_modules/@nestjs/graphql/node_modules/tslib/tslib.js:110:16)
● AppController (e2e) › /hello/:name (GET)
Undefined type error. Make sure you are providing an explicit type for the "user" of the "AuthResolver" class.
[...]
How can I fix this? Thank you!
Any hints keeping in mind the current directory structure?
I'm using this example to build my own solution, but I stumbled into something that I'm not sure how to solve.
This is very much the same as the user.model.ts
present in this example:
import { Field, ObjectType, registerEnumType } from "@nestjs/graphql"
export enum AccountStatus {
Created = "Created",
Verified = "Verified",
}
registerEnumType(AccountStatus, {
name: "AccountStatus",
})
@ObjectType()
export class Account {
@Field()
public readonly slug: string
@Field(() => AccountStatus)
public status: AccountStatus
}
Then I have a service. In the return account
I have a error message: Types of property 'status' are incompatible.
because enums inside @prisma/client
are defined as a const
.
public async create(input: AccountCreate): Promise<Account> {
const account = await this.prismaService.account.create({
data: {
// whatever
},
})
return account
}
I can cast account to Account
, but I prefer not to do that.
Any plan to upgrade to 10.x?
totalCount
is currently not supported by prisma2
Hi,
thanks a lot for this starter, really appreciate it!
I'm trying to put this starter together with same frontend project in the same repo using yarn workspaces.
My approach so far:
apps/api/
package.json
like this:{
"private": true,
"name": "example-monorepo",
"workspaces": ["apps/api"],
"scripts": {}
}
yarn install
yarn workspace nestjs-prisma-client-starter start:dev
That works for the starter. However, for my existing project with frontend included already I'm experiencing several issues (e.g. nest-cli-plugins not working, VSCode configuration getting tricky, ...). I haven't really found out why (maybe the hoisting is problem...) and it is difficult to debug (for me).
I would assume having a frontend as well is kind of usual and I'm not the only one facing this problem ;-)
What is the standard way to approach this challenge?
I know, that Nestjs has some its own support for monorepos (https://docs.nestjs.com/cli/monorepo). However, this approach does not allow to have a different dependencies (package.json
) etc. per app (which I would like to have to avoid dependency problems...).
Is there some middle ground between keeping everything in separate repos and completely sharing all dependencies, that is proven and well established in the community?
If you create more than 10 modules in the providers of which "PrismaService" is located, the error "Possible EventEmitter memory leak detected." The more modules are added, the more error instances are displayed. Is there a way to fix this?
Error
(node:15126) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 beforeExit listeners added to [process]. Use emitter.setMaxListeners() to increase limit (Use
node --trace-warnings ... to show where the warning was created) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 beforeExit listeners added to [process]. Use emitter.setMaxListeners() to increase limit at _addListener (events.js:390:17) at process.addListener (events.js:406:10) at process.once (events.js:437:8) at hookProcess (/Users/Rikka/iMac/Projects/apps/nestGQLError2/node_modules/@prisma/client/runtime/index.js:27586:13) at initHooks (/Users/Rikka/iMac/Projects/apps/nestGQLError2/node_modules/@prisma/client/runtime/index.js:27609:9) at new NodeEngine (/Users/Rikka/iMac/Projects/apps/nestGQLError2/node_modules/@prisma/client/runtime/index.js:26911:7) at PrismaService.getEngine (/Users/Rikka/iMac/Projects/apps/nestGQLError2/node_modules/@prisma/client/runtime/index.js:34057:16) at new NewPrismaClient (/Users/Rikka/iMac/Projects/apps/nestGQLError2/node_modules/@prisma/client/runtime/index.js:34030:29) at new PrismaService (/Users/Rikka/iMac/Projects/apps/nestGQLError2/dist/services/prisma.service.js:17:9) at Injector.instantiateClass (/Users/Rikka/iMac/Projects/apps/nestGQLError2/node_modules/@nestjs/core/injector/injector.js:286:19)
WARNING
The installSubscriptionHandlers configuration option has been removed from the latest version of Apollo server and will be soon deprecated in this package as well. By default, installSubscriptionHandlers will fallback to use the subscriptions-transport-ws (read more) but we strongly recommend using the graphql-ws(read more) library instead.
NOTE
PubSub is a class that exposes a simple publish and subscribe API. Read more about it here. Note that the Apollo docs warn that the default implementation is not suitable for production (read more here). Production apps should use a PubSub implementation backed by an external store (read more here).
Maybe I can get this done.
Use node-alpine as docker image if prisma2 provides a binaries for Alpine Linux prisma/prisma#702
Hey there!
I've been trying to run the project with docker by running: npm run docker
or docker-compose -f docker-compose.yml up --build
And it gives me an error which I can't find a solution to. The error is:
> [email protected] start:prod
nest-api | > node dist/main
nest-api |
nest-api | /node_modules/flaschenpost/dist/readPackageJson.js:16
nest-api | throw new Error('Could not find package.json.');
nest-api | ^
nest-api |
nest-api | Error: Could not find package.json.
nest-api | at readPackageJson (/node_modules/flaschenpost/dist/readPackageJson.js:16:11)
nest-api | at Object.flaschenpost.initialize (/node_modules/flaschenpost/dist/flaschenpost.js:23:36)
nest-api | at Object.<anonymous> (/node_modules/flaschenpost/dist/flaschenpost.js:111:14)
nest-api | at Module._compile (node:internal/modules/cjs/loader:1126:14)
nest-api | at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
nest-api | at Module.load (node:internal/modules/cjs/loader:1004:32)
nest-api | at Function.Module._load (node:internal/modules/cjs/loader:839:12)
nest-api | at Module.require (node:internal/modules/cjs/loader:1028:19)
nest-api | at require (node:internal/modules/cjs/helpers:102:18)
nest-api | at Object.<anonymous> (/node_modules/node-poeditor/lib/workers/request/httprequest.js:4:16)
nest-api exited with code 1
Any idea what I could do to fix this? It seems that the package.json is in copied in the Dockerfile, but when running npm run start:prod
it does not find it.
Executing npm install
gives found 7344 vulnerabilities (7332 low, 12 moderate)
.
Any known constraint to avoid npm audit fix
?
When I am trying to run docker-compose up
I get this error:
Step 4/10 : COPY ./prisma/schema.prisma ./
ERROR: Service 'nest-api' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder161410517/prisma/schema.prisma: no such file or directory
I am using Docker in Mac. I have also tried the same with my RockPro64
server runs on Ubuntu 18.04
. still having the same issue.
I am completely new to both NestJS and Prisma so forgive me if I am making a silly mistake. 🤦
I've run through the instructions a couple of times and can't seem to get past Step 3.
To reproduce:
git clone https://github.com/fivethree-team/nestjs-prisma-starter
cd nestjs-prisma-starter
npm install
cp example.env .env
npm run docker:db
npm run migrate:dev
Which results in the following error: Error querying the database: db error: FATAL: role "prisma" does not exist
What are the files for? Where are they used?
graphql/auth.graphql
graphql/post.graphql
graphql/user.graphql
as the title says.
Sorry for bothering again.
I was wondering if there is a way to make code coverage 100% with unit tests for this project. I don't know if this is possible but, it would be nice to have a good set of unit tests.
Do you think that this is a good idea? I cannot image how to test something like the classes in pagination.ts as they are only graphql type definitions.
But maybe, this is not a problem at all. I wish to discuss this with you.
Now, code coverage is getting very low score:
$ yarn test:cov
yarn run v1.22.5
$ jest --coverage
PASS src/controllers/app.controller.spec.ts
PASS src/common/scalars/date.scalar.spec.ts
PASS src/resolvers/app.resolver.spec.ts
PASS src/configs/config.spec.ts
---------------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------------------|---------|----------|---------|---------|-------------------
All files | 8.94 | 2.44 | 14.63 | 8.12 |
src | 0 | 0 | 0 | 0 |
app.module.ts | 0 | 0 | 0 | 0 | 1-41
main.ts | 0 | 0 | 0 | 0 | 1-38
src/common/order | 0 | 0 | 0 | 0 |
order-direction.ts | 0 | 0 | 0 | 0 | 1-10
order.ts | 0 | 100 | 0 | 0 | 1-7
src/common/pagination | 0 | 100 | 0 | 0 |
page-info.model.ts | 0 | 100 | 100 | 0 | 1-4
pagination.args.ts | 0 | 100 | 100 | 0 | 1-4
pagination.ts | 0 | 100 | 0 | 0 | 1-30
src/common/scalars | 83.33 | 50 | 80 | 80 |
date.scalar.ts | 83.33 | 50 | 80 | 80 | 4,20
src/configs | 100 | 100 | 100 | 100 |
config.ts | 100 | 100 | 100 | 100 |
src/controllers | 100 | 100 | 100 | 100 |
app.controller.ts | 100 | 100 | 100 | 100 |
src/decorators | 0 | 100 | 0 | 0 |
user.decorator.ts | 0 | 100 | 0 | 0 | 1-6
src/guards | 0 | 100 | 0 | 0 |
gql-auth.guard.ts | 0 | 100 | 0 | 0 | 1-9
src/models | 0 | 0 | 0 | 0 |
auth.model.ts | 0 | 100 | 100 | 0 | 1-6
model.model.ts | 0 | 100 | 0 | 0 | 1-6
post.model.ts | 0 | 100 | 100 | 0 | 1-6
token.model.ts | 0 | 100 | 100 | 0 | 1-9
user.model.ts | 0 | 0 | 0 | 0 | 1-28
src/models/args | 0 | 100 | 100 | 0 |
post-id.args.ts | 0 | 100 | 100 | 0 | 1-7
user-id.args.ts | 0 | 100 | 100 | 0 | 1-7
src/models/inputs | 0 | 0 | 0 | 0 |
post-order.input.ts | 0 | 0 | 0 | 0 | 1-20
src/models/pagination | 0 | 100 | 100 | 0 |
post-connection.model.ts | 0 | 100 | 100 | 0 | 1-6
src/resolvers | 80 | 100 | 50 | 75 |
app.resolver.ts | 80 | 100 | 50 | 75 | 5,9
src/resolvers/auth | 0 | 0 | 0 | 0 |
auth.module.ts | 0 | 100 | 0 | 0 | 1-39
auth.resolver.ts | 0 | 100 | 0 | 0 | 1-48
jwt.strategy.ts | 0 | 0 | 0 | 0 | 2-26
src/resolvers/auth/dto | 0 | 100 | 100 | 0 |
login.input.ts | 0 | 100 | 100 | 0 | 1-13
signup.input.ts | 0 | 100 | 100 | 0 | 1-19
src/resolvers/post | 0 | 0 | 0 | 0 |
post.module.ts | 0 | 100 | 100 | 0 | 1-8
post.resolver.ts | 0 | 0 | 0 | 0 | 1-72
src/resolvers/user | 0 | 100 | 0 | 0 |
user.module.ts | 0 | 100 | 100 | 0 | 1-10
user.resolver.ts | 0 | 100 | 0 | 0 | 1-55
src/resolvers/user/dto | 0 | 100 | 100 | 0 |
change-password.input.ts | 0 | 100 | 100 | 0 | 1-14
update-user.input.ts | 0 | 100 | 100 | 0 | 1-8
src/services | 8 | 0 | 10.53 | 6.15 |
app.service.ts | 100 | 100 | 100 | 100 |
auth.service.ts | 0 | 0 | 0 | 0 | 1-100
password.service.ts | 0 | 0 | 0 | 0 | 1-24
prisma.service.ts | 0 | 100 | 0 | 0 | 1-15
user.service.ts | 0 | 0 | 0 | 0 | 1-41
---------------------------|---------|----------|---------|---------|-------------------
Test Suites: 4 passed, 4 total
Tests: 8 passed, 8 total
Snapshots: 0 total
Time: 4.629 s
Ran all test suites.
Done in 5.09s.
i some confuse, could please explain
schema.prisma
file what is the purpose of this fileschema.prisma
already.thanks
Hi!
First of all thanks for this starter. It is really awesome 😃
I've noticed that some of the code is duplicated between prisma schema definitions and nest js models.
In prisma.schema
we have a User
definition, which looks like below:
model User {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
email String @unique
password String
firstname String?
lastname String?
posts Post[]
role Role
}
And also in user.model
we define the same 'shape' but with typescript:
@ObjectType()
export class User extends BaseModel {
email: string;
firstname?: string;
lastname?: string;
role: Role;
@HideField()
password: string;
}
Why is that necessary? Can't we somehow generate schema from models, or other way around?
This code involves 3 things: a. GraphQL support b. Nest JS c. Prisma All of them have their own built-in support for exceptions and errors.
Can we write the Exception handling together? I'd write the basic code and expect that someone among you help take the code to a 'production-ready" state?
Replacing Prisma with Prisma2
https://github.com/prisma/prisma2#readme
In the User Resolver, a call to the prisma client is invoked directly. Instead, we should invoke the Prisma in the db service layer and access that method in the resolver.. as seen above in the code snippet.
you can add an example of subscription
After upgrading to @nestjs/graphql v7
, the schema generated by @nestjs/graphql v7
is not compatible with the schema generated by prisma
Implement pagination with PageInfo with prisma2
See helper function:
https://github.com/prisma/photonjs/issues/321#issuecomment-574659033
https://gist.github.com/ctrlplusb/17b5a1bd1736b5ba547bb15b3dd5be29#file-findmanycursor-ts
Hi,
I just used this repo first time and I found problems with arguments of queries not showing in grapqh playground
For example userPosts query has userId
argument in resolver:
@ArgsType() export class UserIdArgs { @IsNotEmpty() userId: string; }
Generated queries in schema:
type Query { ... userPosts: [Post!]! }
So the playground would not execute the query. I am just learning GraphQL, so I was confused of the error.
"message": "Unknown argument "userId" on field "Query.userPosts".",
After adding @field() decorator, all works fine.
Solved:
@ArgsType() export class UserIdArgs { @Field() @IsNotEmpty() userId: string; }
Am I missing something? Thanks.
I noticed at the start of the web server that it would take 5+ seconds and was taking longer and longer with every module I added to my project. It seems it tried to connect oninit for each service that uses this service. When I removed this line the project started under 1 second and Prisma has lazy loading connection that auto connects if no connection is established.
I see this in other docs and am wondering why its setup this way if it tries to front-load so many connections?
I have a question relating to JWT, could you pl. answer?
There is a mutation named, refreshToken() which is exposed as an endpoint. In the documentation, there is no mention about when to make a call to this endpoint? Over there, it is just stated that send the token in the Authorization header.
Kindly make things clear by writing the generic code flow at client side. Is there any need to use a library like jwt-decode there?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.