Skip to content

Commit

Permalink
Merge pull request #24 from dangchinh25/cle/chore/ignore-relationship…
Browse files Browse the repository at this point in the history
…-attribute

fix: Ignore prisma relational attributes
  • Loading branch information
dangchinh25 authored Dec 10, 2023
2 parents 8f056ae + a30e1ac commit 2f67134
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
4 changes: 4 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ model User {
latLngGeography Unsupported("geography(Point,4326)")? @map("lat_lng_geography")
posts Post[]
@@map("users")
}

Expand All @@ -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")
}

Expand Down
12 changes: 10 additions & 2 deletions src/generator/helpers/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ export const parseDMMFModels = (
const parsedModels: ParsedModels = {};
const modelNameDbNameMap: Map<DMMF.Model['name'], string> = new Map();
const attributesDbNameMap: Map<string, string> = new Map();
const modelsSet: Set<DMMF.Model['name']> = 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;
Expand All @@ -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,
Expand All @@ -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 );

Expand Down
4 changes: 4 additions & 0 deletions tests/__fixtures__/sample.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ model User {
name String?
userTypeId Int @map("user_type_id") /// [[UserType.id]]
posts Post[]
@@map("users")
}

Expand All @@ -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")
}

Expand Down
21 changes: 15 additions & 6 deletions tests/helpers/parser.test.ts
Original file line number Diff line number Diff line change
@@ -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 );
} );

Expand All @@ -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 ) );
Expand Down

0 comments on commit 2f67134

Please sign in to comment.