From a30e1acb67c80249fe44d4f4bd8414458eb919a9 Mon Sep 17 00:00:00 2001 From: dangchinh25 Date: Sun, 10 Dec 2023 17:00:34 -0600 Subject: [PATCH] fix: Ignore prisma relational attributes --- prisma/schema.prisma | 4 ++++ src/generator/helpers/parser.ts | 12 ++++++++++-- tests/__fixtures__/sample.prisma | 4 ++++ tests/helpers/parser.test.ts | 21 +++++++++++++++------ 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index e4135c4..1e9f329 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -22,6 +22,8 @@ model User { latLngGeography Unsupported("geography(Point,4326)")? @map("lat_lng_geography") + posts Post[] + @@map("users") } @@ -31,6 +33,8 @@ model Post { body String userId Int @map("user_id") /// [[User.id]] + user User @relation(fields: [userId], references: [id], onUpdate: Cascade, onDelete: Cascade) + @@map("posts") } diff --git a/src/generator/helpers/parser.ts b/src/generator/helpers/parser.ts index 5679468..9e2f7e6 100644 --- a/src/generator/helpers/parser.ts +++ b/src/generator/helpers/parser.ts @@ -10,8 +10,12 @@ export const parseDMMFModels = ( const parsedModels: ParsedModels = {}; const modelNameDbNameMap: Map = new Map(); const attributesDbNameMap: Map = new Map(); + const modelsSet: Set = new Set(); - // First pass to init parsedModels object with all model + // First pass to register all models + models.forEach( model => modelsSet.add( model.name ) ); + + // Second pass to init parsedModels object with all model // and its attribute with empty relations for ( const model of models ) { const modelDbName = model.dbName || model.name; @@ -25,6 +29,10 @@ export const parseDMMFModels = ( const unsupportedFields = getModelUnsupportedFields( options, model.name ); for ( const field of model.fields ) { + if ( modelsSet.has( field.type ) ) { + continue; + } + const attribute = field.dbName || field.name; parsedModel.attributes.push( { name: attribute, @@ -44,7 +52,7 @@ export const parseDMMFModels = ( parsedModels[ modelDbName ] = parsedModel; } - // Second pass to populate bi-directional relations + // Third pass to populate bi-directional relations for ( const model of models ) { const modelDbName = modelNameDbNameMap.get( model.name ); diff --git a/tests/__fixtures__/sample.prisma b/tests/__fixtures__/sample.prisma index c8540fb..aa0212e 100644 --- a/tests/__fixtures__/sample.prisma +++ b/tests/__fixtures__/sample.prisma @@ -9,6 +9,8 @@ model User { name String? userTypeId Int @map("user_type_id") /// [[UserType.id]] + posts Post[] + @@map("users") } @@ -18,6 +20,8 @@ model Post { body String userId Int @map("user_id") /// [[User.id]] + user User @relation(fields: [userId], references: [id], onUpdate: Cascade, onDelete: Cascade) + @@map("posts") } diff --git a/tests/helpers/parser.test.ts b/tests/helpers/parser.test.ts index bc4a8c8..6746db5 100644 --- a/tests/helpers/parser.test.ts +++ b/tests/helpers/parser.test.ts @@ -1,16 +1,18 @@ import { parseDMMFModels } from '../../src/generator/helpers'; import { getSampleDMMF, getSampleSchema } from '../__fixtures__/getSampleDMMF'; -import { ParsedModels } from '../../src/generator/types'; +import { ParsedModels, ParsedModelAttribute } from '../../src/generator/types'; import { DMMF, GeneratorOptions } from '@prisma/generator-helper'; describe( 'parseDMMFModels', () => { let parsedModels: ParsedModels; let dmmfModels: DMMF.Model[]; + let modelName: DMMF.Model['name'][]; beforeAll( async () => { const sampleDMMF = await getSampleDMMF(); const samepleSchema = await getSampleSchema(); dmmfModels = sampleDMMF.datamodel.models; + modelName = dmmfModels.map( model => model.name ); parsedModels = parseDMMFModels( { dmmf: sampleDMMF, datamodel: samepleSchema } as GeneratorOptions ); } ); @@ -25,17 +27,24 @@ describe( 'parseDMMFModels', () => { ); } ); - it( 'generates all attributes of each models', () => { + it( 'generates all attributes of each models except relational attributes', () => { dmmfModels.map( model => { const modelDbName = model.dbName || model.name; const parsedModel = parsedModels[ modelDbName ]; + const modelAttributes: ParsedModelAttribute[] = []; - const modelAttributes = model.fields.map( field => { - return { + // Create expected value from sampleDMMF + // TODO Hardcode the expected value for more accurate unit test + for ( const field of model.fields ) { + if ( modelName.includes( field.type ) ) { + continue; + } + + modelAttributes.push( { name: field.dbName || field.name, type: field.type - }; - } ); + } ); + } expect( parsedModel.attributes ) .toStrictEqual( expect.arrayContaining( modelAttributes ) );