Skip to content
This repository has been archived by the owner on Jan 8, 2023. It is now read-only.

Commit

Permalink
Add support for multiple @unique field (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
yujonglee authored Dec 20, 2021
1 parent 66aa2f4 commit 161d7c2
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
22 changes: 18 additions & 4 deletions src/converters/convertScalar.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
8 changes: 5 additions & 3 deletions src/converters/rules/scalar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {PSL, Rule, SDL, Scalar} from '../types';
import extractId from 'extractors/extractId';
import extractUniques from 'extractors/extractUniques';

const rules: Rule[] = [
{
Expand Down Expand Up @@ -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,
},
Expand Down
20 changes: 20 additions & 0 deletions src/extractors/extractUniques.test.ts
Original file line number Diff line number Diff line change
@@ -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,
]);
});
});
7 changes: 7 additions & 0 deletions src/extractors/extractUniques.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 161d7c2

Please sign in to comment.