Skip to content

Commit

Permalink
fix: Emit single with Prisma type
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed Jul 24, 2023
1 parent 471c405 commit 94df9cf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/handlers/emit-single.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AwaitEventEmitter from 'await-event-emitter';
import { partition } from 'lodash';
import { PropertyDeclarationStructure } from 'ts-morph';

import { DMMF } from '../types';
Expand All @@ -17,11 +18,12 @@ function classProperty(
) {
const { location, isList, propertyType } = eventArguments;
if (['inputObjectTypes', 'outputObjectTypes'].includes(location) && !isList) {
const types = propertyType.filter(t => t !== 'null');
property.type = types.map(t => `InstanceType<typeof ${t}>`).join(' | ');
if (types.length !== propertyType.length) {
// If null was removed
property.type += ' | null';
}
const [safeTypes, instanceofTypes] = partition(
propertyType,
t => t === 'null' || t.startsWith('Prisma.'),
);
const mappedInstanceofTypes = instanceofTypes.map(t => `InstanceType<typeof ${t}>`);

property.type = [...mappedInstanceofTypes, ...safeTypes].join(' | ');
}
}
19 changes: 19 additions & 0 deletions src/test/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1531,6 +1531,25 @@ describe('emit single', () => {
}));
expect(types).toHaveLength(0);
});

it('type started with Prisma should not be wrapped to instanceof', async () => {
const types = sourceFile
.getClasses()
.flatMap(c => c.getProperties().map(p => ({ c, p })))
.map(({ c, p }) => ({
c,
p,
propertyType: p.getStructure().type,
}))
.map(({ propertyType }) => String(propertyType))
.filter(
propertyType =>
propertyType.includes('Prisma.') &&
propertyType.startsWith('InstanceType<'),
);

expect(types).toHaveLength(0);
});
});

describe('emit single second gen', () => {
Expand Down

0 comments on commit 94df9cf

Please sign in to comment.