Skip to content

Commit f44aa85

Browse files
committed
fix: Get model name for CompoundUniqueInput
close: #53
1 parent eb68ca6 commit f44aa85

7 files changed

+66
-5
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Field } from '@nestjs/graphql';
2+
import { InputType } from '@nestjs/graphql';
3+
import * as Scalars from 'graphql-scalars';
4+
import * as Validator from 'class-validator';
5+
6+
@InputType()
7+
export class UserEmailNameCompoundUniqueInput {
8+
@Field(() => Scalars.GraphQLEmailAddress, { nullable: false })
9+
email!: string;
10+
11+
@Field(() => String, { nullable: false })
12+
@Validator.MinLength(3)
13+
@Validator.MaxLength(50)
14+
name!: string;
15+
}

@generated/user/user-where-unique.input.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Field } from '@nestjs/graphql';
22
import { InputType } from '@nestjs/graphql';
33
import * as Scalars from 'graphql-scalars';
44
import * as Validator from 'class-validator';
5+
import { UserEmailNameCompoundUniqueInput } from './user-email-name-compound-unique.input';
56

67
@InputType()
78
export class UserWhereUniqueInput {
@@ -15,4 +16,7 @@ export class UserWhereUniqueInput {
1516
@Validator.MinLength(3)
1617
@Validator.MaxLength(50)
1718
name?: string;
19+
20+
@Field(() => UserEmailNameCompoundUniqueInput, { nullable: true })
21+
email_name?: UserEmailNameCompoundUniqueInput;
1822
}

prisma/schema.prisma

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ model User {
5757
rating Float?
5858
role Role?
5959
profile Profile?
60+
61+
@@unique([email, name])
6062
}
6163

6264
model Tag {

src/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export async function generate(
112112
eventEmitter,
113113
typeNames: new Set<string>(),
114114
enums: mapKeys(datamodel.enums, x => x.name),
115-
getModelName: createGetModelName(modelNames),
115+
getModelName,
116116
removeTypes,
117117
};
118118

src/helpers/get-model-name.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { memoize } from 'lodash';
1+
import { first, memoize } from 'lodash';
22

33
export function createGetModelName(modelNames: string[]) {
44
return memoize(tryGetName);
@@ -35,6 +35,16 @@ function getModelName(args: {
3535
return test;
3636
}
3737
}
38+
39+
// test for {Model}{UniqueName}CompoundUniqueInput
40+
if (name.slice(-19) === 'CompoundUniqueInput') {
41+
const test = name.slice(0, -19);
42+
const models = modelNames
43+
.filter(x => test.startsWith(x))
44+
.sort((a, b) => b.length - a.length);
45+
return first(models);
46+
}
47+
3848
// test for {Model}Count
3949
if (name.slice(-5) === 'Count') {
4050
const test = name.slice(0, -5);

src/helpers/object-settings.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import JSON5 from 'json5';
22
import { isObject, merge, omit, trim } from 'lodash';
33
import outmatch from 'outmatch';
4+
import { PlainObject } from 'simplytyped';
45

56
import { GeneratorConfiguration } from '../types';
67

@@ -53,7 +54,7 @@ export class ObjectSettings extends Array<ObjectSetting> {
5354
const resultArguments: any[] = [objectTypeOptions];
5455
const objectType = this.find(s => s.kind === 'ObjectType');
5556
if (objectType && isObject(objectType.arguments)) {
56-
const name = objectType.arguments.name;
57+
const name = (objectType.arguments as PlainObject).name;
5758
merge(objectTypeOptions, omit(objectType.arguments, 'name'));
5859
if (name) {
5960
resultArguments.unshift(name);

src/test/generate.spec.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ const t = (name: string) =>
3434
getPropertyStructure(sourceFile, name)?.decorators?.find(d => d.name === 'Field')
3535
?.arguments?.[0];
3636
const setSourceFile = (name: string) => {
37-
sourceFile = project.getSourceFile(s => s.getFilePath().endsWith(name))!;
38-
classFile = sourceFile.getClass(() => true)!;
37+
sourceFile = project.getSourceFileOrThrow(s => s.getFilePath().endsWith(name));
3938
sourceText = sourceFile.getText();
39+
classFile = sourceFile.getClass(() => true)!;
4040
importDeclarations = sourceFile.getImportDeclarations().map(d => d.getStructure());
4141
imports = importDeclarations.flatMap(d =>
4242
(d.namedImports as ImportSpecifierStructure[]).map(x => ({
@@ -2051,3 +2051,32 @@ describe('object model options', () => {
20512051
});
20522052
});
20532053
});
2054+
2055+
describe('compound index', () => {
2056+
it('user unique input compound', async () => {
2057+
({ project, sourceFiles } = await testGenerate({
2058+
schema: `
2059+
model User {
2060+
id Int @id
2061+
/// @Validator.MinLength(3)
2062+
name String
2063+
/// @PropertyType({ name: 'G.Email', from: 'graphql-type-email' })
2064+
email String?
2065+
2066+
@@unique([email, name])
2067+
}
2068+
model Us {
2069+
id Int @id
2070+
}
2071+
`,
2072+
options: [
2073+
`outputFilePattern = "{name}.{type}.ts"`,
2074+
`fields_Validator_from = "class-validator"`,
2075+
`fields_Validator_input = true`,
2076+
],
2077+
}));
2078+
setSourceFile('user-email-name-compound-unique.input.ts');
2079+
const minLength = classFile.getProperty('name')?.getDecorator('MinLength');
2080+
expect(minLength?.getText()).toEqual('@Validator.MinLength(3)');
2081+
});
2082+
});

0 commit comments

Comments
 (0)