Skip to content

Commit 64a9854

Browse files
committed
fix: Compound primary key generated type
close #182
1 parent f07ba3d commit 64a9854

File tree

3 files changed

+96
-51
lines changed

3 files changed

+96
-51
lines changed

src/helpers/get-where-unique-at-least-keys.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@ export function getWhereUniqueAtLeastKeys(model: DMMF.Model) {
55
.filter(field => field.isUnique || field.isId)
66
.map(field => field.name);
77

8-
for (const uniqueIndex of model.uniqueIndexes) {
9-
const name = uniqueIndex.name || uniqueIndex.fields.join('_');
8+
if (model.primaryKey) {
9+
names.push(createFieldName(model.primaryKey));
10+
}
1011

11-
names.push(name);
12+
for (const uniqueIndex of model.uniqueIndexes) {
13+
names.push(createFieldName(uniqueIndex));
1214
}
1315

1416
return names.map(name => `'${name}'`).join(' | ');
1517
}
18+
19+
function createFieldName(args: { name?: string | null; fields: string[] }) {
20+
const { name, fields } = args;
21+
22+
return name || fields.join('_');
23+
}

src/test/compound.spec.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import expect from 'expect';
2+
import { Project, SourceFile } from 'ts-morph';
3+
4+
import { testSourceFile } from './helpers';
5+
import { testGenerate } from './test-generate';
6+
7+
let project: Project;
8+
let sourceFiles: SourceFile[];
9+
10+
describe('compound index', () => {
11+
before(async () => {
12+
({ project, sourceFiles } = await testGenerate({
13+
schema: `
14+
model User {
15+
id Int @id
16+
/// @Validator.MinLength(3)
17+
name String
18+
/// @PropertyType({ name: 'G.Email', from: 'graphql-type-email' })
19+
email String?
20+
21+
@@unique([email, name])
22+
}
23+
model Us {
24+
id Int @id
25+
}
26+
`,
27+
options: [
28+
`outputFilePattern = "{name}.{type}.ts"`,
29+
`fields_Validator_from = "class-validator"`,
30+
`fields_Validator_input = true`,
31+
],
32+
}));
33+
});
34+
35+
it('user unique input compound', () => {
36+
const s = testSourceFile({
37+
project,
38+
class: 'UserEmailNameCompoundUniqueInput',
39+
});
40+
41+
const minLength = s.classFile.getProperty('name')?.getDecorator('MinLength');
42+
expect(minLength?.getText()).toEqual('@Validator.MinLength(3)');
43+
});
44+
45+
it('compound uniq where must be wrapped to prisma atleast', () => {
46+
const s = testSourceFile({
47+
project,
48+
class: 'FindManyUserArgs',
49+
property: 'cursor',
50+
});
51+
52+
expect(s.property?.type).toEqual(
53+
`Prisma.AtLeast<UserWhereUniqueInput, 'id' | 'email_name'>`,
54+
);
55+
});
56+
});
57+
58+
describe('compound primary key', () => {
59+
before(async () => {
60+
({ project, sourceFiles } = await testGenerate({
61+
schema: `
62+
model User {
63+
firstname String
64+
surname String
65+
email String
66+
67+
@@id([firstname, surname])
68+
}
69+
`,
70+
options: [`outputFilePattern = "{name}.{type}.ts"`],
71+
}));
72+
});
73+
74+
it('compound primary key atleast keys', () => {
75+
const s = testSourceFile({
76+
project,
77+
class: 'FindFirstUserArgs',
78+
property: 'cursor',
79+
});
80+
81+
expect(s.property?.type).toEqual(
82+
"Prisma.AtLeast<UserWhereUniqueInput, 'firstname_surname'>",
83+
);
84+
});
85+
});

src/test/generate.spec.ts

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,54 +1905,6 @@ describe('object model options', () => {
19051905
});
19061906
});
19071907

1908-
describe('compound index', () => {
1909-
before(async () => {
1910-
({ project, sourceFiles } = await testGenerate({
1911-
schema: `
1912-
model User {
1913-
id Int @id
1914-
/// @Validator.MinLength(3)
1915-
name String
1916-
/// @PropertyType({ name: 'G.Email', from: 'graphql-type-email' })
1917-
email String?
1918-
1919-
@@unique([email, name])
1920-
}
1921-
model Us {
1922-
id Int @id
1923-
}
1924-
`,
1925-
options: [
1926-
`outputFilePattern = "{name}.{type}.ts"`,
1927-
`fields_Validator_from = "class-validator"`,
1928-
`fields_Validator_input = true`,
1929-
],
1930-
}));
1931-
});
1932-
1933-
it('user unique input compound', () => {
1934-
const s = testSourceFile({
1935-
project,
1936-
class: 'UserEmailNameCompoundUniqueInput',
1937-
});
1938-
1939-
const minLength = s.classFile.getProperty('name')?.getDecorator('MinLength');
1940-
expect(minLength?.getText()).toEqual('@Validator.MinLength(3)');
1941-
});
1942-
1943-
it('compound uniq where must be wrapped to prisma atleast', () => {
1944-
const s = testSourceFile({
1945-
project,
1946-
class: 'FindManyUserArgs',
1947-
property: 'cursor',
1948-
});
1949-
1950-
expect(s.property?.type).toEqual(
1951-
`Prisma.AtLeast<UserWhereUniqueInput, 'id' | 'email_name'>`,
1952-
);
1953-
});
1954-
});
1955-
19561908
describe('field type', () => {
19571909
describe('it overwrites field type based on match expression', () => {
19581910
before(async () => {

0 commit comments

Comments
 (0)