Installations
npm install prisma-generator-nestjs-graphql-crud
Developer Guide
Typescript
No
Module System
N/A
Node Version
20.5.0
NPM Version
10.1.0
Score
61.5
Supply Chain
88.4
Quality
79
Maintenance
50
Vulnerability
96.7
License
Releases
Unable to fetch releases
Total Downloads
Cumulative downloads
Total Downloads
3,170
Last day
100%
4
Compared to previous day
Last week
2,200%
46
Compared to previous week
Last month
60.6%
53
Compared to previous month
Last year
-53.3%
1,009
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
4
Peer Dependencies
4
Dev Dependencies
26
prisma-generator-nestjs-graphql-crud
This prisma generator will create all the @nestjs/graphql classes you'll need for basic CRUD operations; based on your prisma schema. It includes dataloader to prevent the infamous n+1 problem in graphql.
1npm i prisma-generator-nestjs-graphql-crud
Getting Started
- Inside your prisma schema add the following:
generator nestJsGraphQlCrud {
provider = "prisma-generator-nestjs-graphql-crud"
}
Options:
Feature | Description | Default | Example |
---|---|---|---|
excludes | prisma model names to exclude from generator | excludes = ["Ignore"] | |
includeMutations | include mutation resolvers | includeMutations = "true" | |
output | cwd relative path for the output | node_modules/@generated/graphql | output = "./example/src" |
- Configure your Graphql Service In NestJs
The generated code relies on the context
object for graphql to contain a
reference to the prisma
client. See the use of useFactory
in the GraphQLModule
below.
PrismaModule and PrismaService are generated; if you want your own custom implementation, use this doc as a guide.
* generated code is compatible with @nestjs/mercurius, @nestjs/apollo and @graphql-yoga/nestjs
1import { 2 DataLoaderService, 3 PrismaModule, 4 prismaProviders, 5 PrismaService 6} from '@generated/graphql'; 7import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo'; 8import { Module } from '@nestjs/common'; 9import { GraphQLModule } from '@nestjs/graphql'; 10 11@Module({ 12 imports: [ 13 GraphQLModule.forRootAsync<ApolloDriverConfig>({ 14 driver: ApolloDriver, 15 inject: [DataLoaderService, PrismaService], 16 useFactory: (loaders: DataLoaderService, prisma: PrismaService) => ({ 17 autoSchemaFile: 'schema.gql', 18 context: (req: FastifyRequest, res: FastifyReply) => { 19 return { 20 loaders: loaders.create(), 21 prisma, 22 req, 23 res 24 }; 25 }, 26 graphiql: true 27 }) 28 }), 29 PrismaModule.forRoot({ 30 isGlobal: true 31 }) 32 ], 33 providers: [...prismaProviders] 34}) 35export class AppModule { 36}
Prisma Features
Feature | Available | Notes |
---|---|---|
create | ✅ | |
createMany | ✅ | |
delete | ✅ | |
deleteMany | ✅ | |
findUnique | ✅ | by primary key or composite keys |
findMany | ✅ | pagination included (skip/take only for now) |
filters | ✅ | gt,gte,lt,lte,notIn,in,not, etc. (DateTime, Int, String supported) |
orderBy | ✅ | |
update | ✅ | |
updateMany | ✅ |
The following are the various filters and generated resolvers. Note the table name is Upper-CamelCased;
e.g. user_session
would become UserSession
.
Resolvers
All the following are based on the example project found here.
create
Create a new entity. Here's an example:
1mutation { 2 createTodo(title: "Next Todo", userId: 1) { 3 id 4 title 5 userId 6 } 7}
This would return the following:
1{ 2 "data": { 3 "createTodo": { 4 "id": 6, 5 "title": "Next Todo", 6 "userId": 1 7 } 8 } 9}
createMany
Create multiple new entities. Here's an example:
mutation {
createManyTodo([{"title": "Another Todo", "userId": 1}, {"title": "Final Todo", "userId": 2}]) {
id
title
userId
}
}
This would return the following:
1{ 2 "data": { 3 "createManyTodo": [ 4 { 5 "id": 7, 6 "title": "Another Todo", 7 "userId": 1 8 }, 9 { 10 "id": 8, 11 "title": "Final Todo", 12 "userId": 2 13 } 14 ] 15 } 16}
delete
Delete an entity. Here's an example:
1mutation { 2 deleteTodo(id: 1) { 3 id 4 title 5 userId 6 } 7}
This would return the following:
1{ 2 "data": { 3 "deleteTodo": { 4 "id": 1, 5 "title": "First Todo", 6 "userId": 1 7 } 8 } 9}
deleteMany
Delete multiple entities. Here's an example:
mutation {
deleteManyTodo(userId: 1) {
id
title
userId
}
}
This returns the list of deleted todos:
1{ 2 "data": { 3 "deleteManyTodo": [ 4 { 5 "id": 1, 6 "title": "First Todo", 7 "userId": 1 8 }, 9 { 10 "id": 2, 11 "title": "Second Todo", 12 "userId": 1 13 } 14 ] 15 } 16}
findFirst
Find first should be used when you want to query and find a single row without using any of the unique keys for filters;
this means that any filters you use for a findMany
can be used here. Here's an example:
1query { 2 findFirstTodo(title: "That Todo") { 3 id 4 title 5 userId 6 } 7}
This would return the following:
1{ 2 "data": { 3 "findFirstTodo": { 4 "id": 1, 5 "title": "That Todo", 6 "userId": 1 7 } 8 } 9}
findMany
Find many is used to return either filtered or unfiltered rows from a table, and can optionally provide skip
(offset)
and take
(limit) as parameters to control pagination. Here's an example:
1query { 2 findManyTodo(skip: 0, take: 5) { 3 id 4 title 5 userId 6 } 7}
This would return the following:
1{ 2 "data": { 3 "findManyTodo": [ 4 { 5 "id": 1, 6 "title": "Todo 1", 7 "userId": 1 8 }, 9 { 10 "id": 2, 11 "title": "Todo 2", 12 "userId": 1 13 }, 14 { 15 "id": 3, 16 "title": "Todo 3", 17 "userId": 1 18 }, 19 { 20 "id": 4, 21 "title": "Todo 4", 22 "userId": 2 23 }, 24 { 25 "id": 5, 26 "title": "Todo 5", 27 "userId": 2 28 } 29 ] 30 } 31}
findUnique
Find unique should be used when you want to query against a primary key or composite key. Here's an example:
1query { 2 findUniqueTodo(id: 1) { 3 id 4 title 5 userId 6 } 7}
This would return the following:
1{ 2 "data": { 3 "findUniqueTodo": { 4 "id": 1, 5 "title": "Todo 1", 6 "userId": 1 7 } 8 } 9}
update
Updates a single entity. Here's an example:
1mutation { 2 updateTodo(data: {"title": "First Todo", "userId": 2}, where: {"id": 1}) { 3 id 4 title 5 userId 6 } 7}
This would return the following:
1{ 2 "data": { 3 "updateTodo": { 4 "id": 1, 5 "title": "First Todo", 6 "userId": 2 7 } 8 } 9}
updateMany
Updates a single entity. Here's an example:
1mutation { 2 updateManyTodo(data: {"userId": 2}, where: {"userId": 1}) { 3 id 4 title 5 userId 6 } 7}
This would return the following:
1{ 2 "data": { 3 "updateManyTodo": [ 4 { 5 "id": 1, 6 "title": "First Todo", 7 "userId": 2 8 }, 9 { 10 "id": 2, 11 "title": "Second Todo", 12 "userId": 2 13 } 14 ] 15 } 16}
Filters
The following filters are available are:
DateFilter
Type | Supported |
---|---|
findFirst | ✅ |
findMany | ✅ |
findUnique | ❌ |
Options
Type | Description | Example |
---|---|---|
gt | where the column value is greater than the given {value} | gt: "2022-01-10" |
gte | where the column value is greater than or equal to the given {value} | gte:"2022-01-10" |
lt | where the column value is less than the given {value} | lt: "2022-01-10" |
lte | where the column value is less than or equal to the given {value} | lte:"2022-01-10" |
not | where the column value is not equal to the given {value} | not: "2022-01-10" |
Example:
1query { 2 findManyTodo(createdAt: { gt: "2022-01-10", lt: "2023-01-10" }) { 3 id 4 title 5 } 6}
NumberFilter
Type | Supported |
---|---|
findFirst | ✅ |
findMany | ✅ |
findUnique | ❌ |
Options
Type | Description | Example |
---|---|---|
gt | where the column value is greater than the given {value} | gt: 1 |
gte | where the column value is greater than or equal to the given {value} | gte: 1 |
lt | where the column value is less than the given {value} | lt: 1 |
lte | where the column value is less than or equal to the given {value} | lte: 1 |
not | where the column value is not equal to the given {value} | not: 1 |
Example:
1query { 2 findManyTodo(userId: { gt: 1, lt: 2 }) { 3 id 4 title 5 userId 6 } 7}
OrderBy
Type | Supported |
---|---|
findFirst | ✅ |
findMany | ✅ |
findUnique | ❌ |
Example:
1query { 2 findManyTodo(skip: 0, take: 5, orderBy: {title: "desc"}) { 3 id 4 title 5 } 6}
StringFilter
Type | Supported |
---|---|
findFirst | ✅ |
findMany | ✅ |
findUnique | ❌ |
Options
Type | Description | Example |
---|---|---|
contains | where the column contains the given {value} | contains: "Robert" |
endsWith | where the column value ends with the {value} | endsWith: "ert" |
in | where the column contains a value matching the array of {values}s | in: ["Robert", "Rob"] |
not | where the column value does not not equal the {value} | not: "Matt" |
notIn | where the column does not contain a value matching the array of {values}s | notIn: ["robb", "Robb"] |
startsWith | where the column value starts with the {value} | startsWith: "Rob" |
*all are case-insensitive
Example:
1query { 2 findManyTodo(title: { contains: "First" }) { 3 id 4 title 5 } 6}
Skip (offset)
Type | Supported |
---|---|
findFirst | ❌ |
findMany | ✅ |
findUnique | ❌ |
Example:
1query { 2 findManyTodo(skip: 0) { 3 id 4 title 5 } 6}
Take (limit)
Type | Supported |
---|---|
findFirst | ❌ |
findMany | ✅ |
findUnique | ❌ |
Example:
1query { 2 findManyTodo(take: 10) { 3 id 4 title 5 } 6}
Graphql Features
Feature | Available | Notes |
---|---|---|
FieldResolvers | ✅ | both up and down relationships supported |
Mutations | ✅ | |
Query | ✅ | |
Resolvers | ✅ |
Example
See a generated example here; note, when the output is in a node_modules directory, it will automatically transpile cjs and mjs versions.
Road Map
- authentication guard integration
- cursor-based pagination
- expand
gt
,gte
,lt
,lte
,notIn
,in
,not
"where" filtering to types other than DateTime, Int, and String
TODO
- fix
orderBy
ArgTypes casting toPrisma.{Model}FindManyArgs['orderBy']
- fix
data
ArgTypes casting toPrisma.{Model}CreateArgs['data']
- fix
where
ArgTypes casting toPrisma.{Model}FindManyArgs['where']
No vulnerabilities found.
No security vulnerabilities found.
Gathering detailed insights and metrics for prisma-generator-nestjs-graphql-crud