Skip to content

Commit 1771afe

Browse files
fix: filter all relation fields on keepRelationFields: 'false' (#1337)
* feat: Added forceAnyOf prop * docs: Added docs coverage * feat: Added keepRelationFields option * fix: Updated logic to filter all relations with keepRelationFields: false * docs: Updated README
1 parent 426c5bf commit 1771afe

File tree

3 files changed

+46
-92
lines changed

3 files changed

+46
-92
lines changed

README.md

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -391,21 +391,11 @@ Output:
391391
keywords: { items: { type: 'string' }, type: 'array' },
392392
name: { type: ['string', 'null'] },
393393
number: { type: 'integer', default: '34534535435353' },
394-
posts: {
395-
items: { $ref: '#/definitions/Post' },
396-
type: 'array',
397-
},
398394
bytes: {
399395
description: 'Triple Slash Inline Comment: Will show up in JSON schema [BYTES]',
400396
type: 'string'
401397
},
402398
favouriteDecimal: { type: 'number' },
403-
predecessor: {
404-
anyOf: [
405-
{ $ref: '#/definitions/User' },
406-
{ type: 'null' },
407-
],
408-
},
409399
role: { enum: ['USER', 'ADMIN'], type: 'string', default: 'USER' },
410400
successorId: { type: ['integer', 'null'] },
411401
weight: { type: ['integer', 'null'] },

src/generator/model.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ function getRelationScalarFields(model: DMMF.Model): string[] {
99

1010
const getRelationFieldNames = (model: DMMF.Model): string[] => {
1111
return model.fields
12-
.filter(
13-
(field) =>
14-
field.relationFromFields?.length ||
15-
field.relationToFields?.length,
16-
)
12+
.filter((field) => field.relationFromFields || field.relationToFields)
1713
.map((field) => field.name)
1814
}
1915

src/tests/generator.test.ts

Lines changed: 45 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,59 @@ import addFormats from 'ajv-formats'
55

66
const datamodelPostGresQL = /* Prisma */ `
77
datasource db {
8-
provider = "postgresql"
9-
url = env("DATABASE_URL")
10-
}
8+
provider = "postgresql"
9+
url = env("DATABASE_URL")
10+
}
1111
12-
model User {
13-
id Int @id @default(autoincrement())
14-
// Double Slash Comment: Will NOT show up in JSON schema
15-
createdAt DateTime @default(now())
16-
/// Triple Slash Comment: Will show up in JSON schema [EMAIL]
17-
email String @unique
18-
number BigInt @default(34534535435353)
19-
favouriteDecimal Decimal @default(22.222222)
20-
bytes Bytes /// Triple Slash Inline Comment: Will show up in JSON schema [BYTES]
21-
weight Float? @default(333.33)
22-
is18 Boolean? @default(false)
23-
name String? @default("Bela B")
24-
successorId Int? @default(123) @unique
25-
successor User? @relation("BlogOwnerHistory", fields: [successorId], references: [id])
26-
predecessor User? @relation("BlogOwnerHistory")
27-
role Role @default(USER)
28-
posts Post[]
29-
keywords String[]
30-
biography Json
31-
}
12+
model User {
13+
id Int @id @default(autoincrement())
14+
// Double Slash Comment: Will NOT show up in JSON schema
15+
createdAt DateTime @default(now())
16+
/// Triple Slash Comment: Will show up in JSON schema [EMAIL]
17+
email String @unique
18+
number BigInt @default(34534535435353)
19+
favouriteDecimal Decimal @default(22.222222)
20+
bytes Bytes /// Triple Slash Inline Comment: Will show up in JSON schema [BYTES]
21+
weight Float? @default(333.33)
22+
is18 Boolean? @default(false)
23+
name String? @default("Bela B")
24+
successorId Int? @unique @default(123)
25+
successor User? @relation("BlogOwnerHistory", fields: [successorId], references: [id])
26+
predecessor User? @relation("BlogOwnerHistory")
27+
role Role @default(USER)
28+
posts Post[]
29+
keywords String[]
30+
biography Json
31+
}
3232
33-
model Post {
34-
id Int @id @default(autoincrement())
35-
user User? @relation(fields: [userId], references: [id])
36-
userId Int?
37-
}
33+
model Post {
34+
id Int @id @default(autoincrement())
35+
user User? @relation(fields: [userId], references: [id])
36+
userId Int?
37+
}
3838
39-
enum Role {
40-
USER
41-
ADMIN
42-
}
39+
enum Role {
40+
USER
41+
ADMIN
42+
}
4343
`
4444

4545
const datamodelMongoDB = /* Prisma */ `
46-
datasource db {
47-
provider = "mongodb"
48-
url = env("DATABASE_URL")
49-
}
46+
datasource db {
47+
provider = "mongodb"
48+
url = env("DATABASE_URL")
49+
}
5050
51-
model User {
52-
id String @id @default(auto()) @map("_id") @db.ObjectId
53-
photos Photo[]
54-
}
51+
model User {
52+
id String @id @default(auto()) @map("_id") @db.ObjectId
53+
photos Photo[]
54+
}
5555
56-
type Photo {
57-
height Int @default(200)
58-
width Int @default(100)
59-
url String
60-
}
56+
type Photo {
57+
height Int @default(200)
58+
width Int @default(100)
59+
url String
60+
}
6161
`
6262

6363
describe('JSON Schema Generator', () => {
@@ -391,22 +391,6 @@ describe('JSON Schema Generator', () => {
391391
type: 'integer',
392392
default: '34534535435353',
393393
},
394-
posts: {
395-
items: {
396-
$ref: '#/definitions/Post',
397-
},
398-
type: 'array',
399-
},
400-
predecessor: {
401-
anyOf: [
402-
{
403-
$ref: '#/definitions/User',
404-
},
405-
{
406-
type: 'null',
407-
},
408-
],
409-
},
410394
bytes: {
411395
description:
412396
'Triple Slash Inline Comment: Will show up in JSON schema [BYTES]',
@@ -488,22 +472,6 @@ describe('JSON Schema Generator', () => {
488472
type: 'integer',
489473
default: '34534535435353',
490474
},
491-
posts: {
492-
items: {
493-
$ref: '#/definitions/Post',
494-
},
495-
type: 'array',
496-
},
497-
predecessor: {
498-
anyOf: [
499-
{
500-
$ref: '#/definitions/User',
501-
},
502-
{
503-
type: 'null',
504-
},
505-
],
506-
},
507475
bytes: {
508476
description:
509477
'Triple Slash Inline Comment: Will show up in JSON schema [BYTES]',

0 commit comments

Comments
 (0)