diff --git a/src/converters/convertScalar.test.ts b/src/converters/convertScalar.test.ts index 15c72ae..fc2cc0e 100644 --- a/src/converters/convertScalar.test.ts +++ b/src/converters/convertScalar.test.ts @@ -92,19 +92,33 @@ describe('convertScalar', () => { ).toBe(SDL.ID); }); - it('converts @unique to ID if no @id exists', () => { + it('converts solo @unique to ID if no @id exists', () => { expect( convertScalar( {type: PSL.String, isUnique: true, isId: false} as DMMF.Field, - {name: 'User', fields: [{isId: false}]} as DMMF.Model, + { + name: 'User', + fields: [ + {type: PSL.String, isUnique: true, isId: false}, + {type: PSL.Int, isUnique: false, isId: false}, + ], + } as DMMF.Model, ), ).toBe(SDL.ID); + }); + it('does nothing for multiple @unique even if no @id exists', () => { expect( convertScalar( {type: PSL.String, isUnique: true, isId: false} as DMMF.Field, - {name: 'User', fields: [{isId: true}]} as DMMF.Model, + { + name: 'User', + fields: [ + {type: PSL.String, isUnique: true, isId: false} as DMMF.Field, + {type: PSL.Int, isUnique: true, isId: false} as DMMF.Field, + ], + } as DMMF.Model, ), - ).toBe(SDL.String); + ).toBe(PSL.String); }); }); diff --git a/src/converters/rules/scalar.ts b/src/converters/rules/scalar.ts index 0d7602f..90532bd 100644 --- a/src/converters/rules/scalar.ts +++ b/src/converters/rules/scalar.ts @@ -1,5 +1,6 @@ import {PSL, Rule, SDL, Scalar} from '../types'; import extractId from 'extractors/extractId'; +import extractUniques from 'extractors/extractUniques'; const rules: Rule[] = [ { @@ -60,11 +61,12 @@ const rules: Rule[] = [ }, { matcher: (field, model) => { - const idField = extractId(model); - const {isUnique} = field; - return !idField && isUnique; + const idField = extractId(model); + const uniqueFields = extractUniques(model); + + return !idField && uniqueFields.length === 1 && isUnique; }, transformer: () => SDL.ID, }, diff --git a/src/extractors/extractUniques.test.ts b/src/extractors/extractUniques.test.ts new file mode 100644 index 0000000..37979c5 --- /dev/null +++ b/src/extractors/extractUniques.test.ts @@ -0,0 +1,20 @@ +import {DMMF} from '@prisma/generator-helper'; + +import extractUniques from './extractUniques'; + +describe('extractUniques', () => { + it('extracts all fields that is @unique', () => { + const model = { + fields: [ + {name: '1', isUnique: true} as DMMF.Field, + {name: '2', isUnique: false} as DMMF.Field, + {name: '3', isUnique: true} as DMMF.Field, + ], + } as DMMF.Model; + + expect(extractUniques(model)).toEqual([ + {name: '1', isUnique: true} as DMMF.Field, + {name: '3', isUnique: true} as DMMF.Field, + ]); + }); +}); diff --git a/src/extractors/extractUniques.ts b/src/extractors/extractUniques.ts new file mode 100644 index 0000000..cfedae4 --- /dev/null +++ b/src/extractors/extractUniques.ts @@ -0,0 +1,7 @@ +import {DMMF} from '@prisma/generator-helper'; + +const extractUniques = (model: DMMF.Model): DMMF.Field[] => { + return model.fields.filter(({isUnique}) => isUnique); +}; + +export default extractUniques;