Skip to content

Commit

Permalink
fix: Switched to replace mode
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
Switched to replace mode generation of files, all extra field which are not exists in model will be
removed
  • Loading branch information
unlight committed Dec 1, 2020
1 parent f662efb commit d04c3ef
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 60 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ Generate object types, inputs, args, etc. from prisma schema file for usage with

- Generates only necessary imports
- Combines zoo of nested/nullable filters
- Updates source code of existing files
- Do not generate resolvers, since it's application specific
- Does not generate resolvers, since it's application specific

## Install

Expand Down
8 changes: 4 additions & 4 deletions src/@generated/prisma/int-filter.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ export class IntFilter {
@Field(() => Int, {
nullable: true,
})
equals?: number | null;
equals?: number;

@Field(() => [Int], {
nullable: true,
})
in?: Array<number> | null;
in?: Array<number>;

@Field(() => [Int], {
nullable: true,
})
notIn?: Array<number> | null;
notIn?: Array<number>;

@Field(() => Int, {
nullable: true,
Expand All @@ -40,5 +40,5 @@ export class IntFilter {
@Field(() => IntFilter, {
nullable: true,
})
not?: number | IntFilter | null;
not?: number | IntFilter;
}
13 changes: 4 additions & 9 deletions src/@generated/prisma/string-filter.input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ export class StringFilter {
@Field(() => String, {
nullable: true,
})
equals?: string;
equals?: string | null;

@Field(() => [String], {
nullable: true,
})
in?: Array<string>;
in?: Array<string> | null;

@Field(() => [String], {
nullable: true,
})
notIn?: Array<string>;
notIn?: Array<string> | null;

@Field(() => String, {
nullable: true,
Expand Down Expand Up @@ -54,13 +54,8 @@ export class StringFilter {
})
endsWith?: string;

@Field(() => QueryMode, {
nullable: true,
})
mode?: QueryMode;

@Field(() => StringFilter, {
nullable: true,
})
not?: string | StringFilter;
not?: string | StringFilter | null;
}
49 changes: 21 additions & 28 deletions src/generate-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ export function generateClass(args: GenerateClassArgs) {
let classDeclaration = sourceFile
.getClasses()
.find((classDeclaration) => classDeclaration.getName() === name);
if (!classDeclaration) {
classDeclaration = sourceFile.addClass({
name,
isExported: true,
decorators: [{ name: decorator.name, arguments: [] }],
});

if (classDeclaration) {
classDeclaration.remove();
}
let decoratorDeclaration = classDeclaration

classDeclaration = sourceFile.addClass({
name,
isExported: true,
decorators: [{ name: decorator.name, arguments: [] }],
});

const decoratorDeclaration = classDeclaration
.getDecorators()
.find((d) => d.getName() === decorator.name);
if (!decoratorDeclaration) {
decoratorDeclaration = classDeclaration.addDecorator({
name: decorator.name,
arguments: [],
});
}

assert(decoratorDeclaration);

const callExpression = decoratorDeclaration.getCallExpression();
assert(callExpression);
if (decorator.properties) {
Expand Down Expand Up @@ -81,19 +81,12 @@ type GenerateClassProperty = {

export function generateClassProperty(args: GenerateClassProperty) {
const { type, isRequired, name, classDeclaration, isReadOnly } = args;
let propertyDeclaration = classDeclaration
.getProperties()
.find((propertyDeclaration) => propertyDeclaration.getName() === name);
if (!propertyDeclaration) {
propertyDeclaration = classDeclaration.addProperty({
leadingTrivia: '\n',
name,
type,
hasQuestionToken: !isRequired,
hasExclamationToken: isRequired,
isReadonly: isReadOnly,
});
}
assert(propertyDeclaration);
return propertyDeclaration;
return classDeclaration.addProperty({
leadingTrivia: '\n',
name,
type,
hasQuestionToken: !isRequired,
hasExclamationToken: isRequired,
isReadonly: isReadOnly,
});
}
21 changes: 9 additions & 12 deletions src/generate-decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,23 @@ type GenerateDecoratorArgs = {
description?: string;
};

/**
* Generates `@Field` decorator.
*/
export function generateDecorator(args: GenerateDecoratorArgs) {
const { description, nullable, defaultValue, fieldType, propertyDeclaration } = args;
let decorator = propertyDeclaration.getDecorator('Field');
if (!decorator) {
decorator = propertyDeclaration.addDecorator({
name: 'Field',
arguments: [`() => ${fieldType}`, '{}'],
});
}
assert(decorator);
const decorator = propertyDeclaration.addDecorator({
name: 'Field',
arguments: [`() => ${fieldType}`, '{}'],
});
const callExpression = decorator.getCallExpression();
assert(callExpression);
let optionsExpression = callExpression
const optionsExpression = callExpression
.getArguments()
.find((node) => Node.isObjectLiteralExpression(node)) as
| ObjectLiteralExpression
| undefined;
if (!optionsExpression) {
[optionsExpression] = callExpression.addArguments(['{}']) as ObjectLiteralExpression[];
}
assert(optionsExpression);
updateObjectProperty({
expression: optionsExpression,
name: 'nullable',
Expand Down
2 changes: 1 addition & 1 deletion src/generate-model/generate-model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe('generate models', () => {
});
const objectTypeArgs = sourceFile.getClass('User')?.getStructure()?.decorators?.[0]
?.arguments?.[0];
expect(objectTypeArgs).toContain('{ }');
expect(objectTypeArgs).toContain('{}');
});

it('model import scalar types', async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/testing/compatibility.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Prisma,PrismaClient } from '@prisma/client';
import { Prisma, PrismaClient } from '@prisma/client';

import { ArticleListRelationFilter } from '../@generated/article/article-list-relation-filter.input';
import { DateTimeFilter } from '../@generated/prisma/date-time-filter.input';
Expand Down
5 changes: 2 additions & 3 deletions src/utils/generate-file-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ type GenerateFileNameArgs = {
type: string;
name: string;
models: string[];
template?: string;
template: string;
feature?: string;
};

export function generateFileName(args: GenerateFileNameArgs) {
const { type, name, models } = args;
const template = args.template || '{feature}/{name}.{type}.ts';
const { template, type, name, models } = args;

return pupa(template, {
type,
Expand Down

0 comments on commit d04c3ef

Please sign in to comment.