Skip to content

Commit

Permalink
fix: Compound primary key generated type
Browse files Browse the repository at this point in the history
  • Loading branch information
unlight committed Aug 4, 2023
1 parent f07ba3d commit 64a9854
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 51 deletions.
14 changes: 11 additions & 3 deletions src/helpers/get-where-unique-at-least-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ export function getWhereUniqueAtLeastKeys(model: DMMF.Model) {
.filter(field => field.isUnique || field.isId)
.map(field => field.name);

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

names.push(name);
for (const uniqueIndex of model.uniqueIndexes) {
names.push(createFieldName(uniqueIndex));
}

return names.map(name => `'${name}'`).join(' | ');
}

function createFieldName(args: { name?: string | null; fields: string[] }) {
const { name, fields } = args;

return name || fields.join('_');
}
85 changes: 85 additions & 0 deletions src/test/compound.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import expect from 'expect';
import { Project, SourceFile } from 'ts-morph';

import { testSourceFile } from './helpers';
import { testGenerate } from './test-generate';

let project: Project;
let sourceFiles: SourceFile[];

describe('compound index', () => {
before(async () => {
({ project, sourceFiles } = await testGenerate({
schema: `
model User {
id Int @id
/// @Validator.MinLength(3)
name String
/// @PropertyType({ name: 'G.Email', from: 'graphql-type-email' })
email String?
@@unique([email, name])
}
model Us {
id Int @id
}
`,
options: [
`outputFilePattern = "{name}.{type}.ts"`,
`fields_Validator_from = "class-validator"`,
`fields_Validator_input = true`,
],
}));
});

it('user unique input compound', () => {
const s = testSourceFile({
project,
class: 'UserEmailNameCompoundUniqueInput',
});

const minLength = s.classFile.getProperty('name')?.getDecorator('MinLength');
expect(minLength?.getText()).toEqual('@Validator.MinLength(3)');
});

it('compound uniq where must be wrapped to prisma atleast', () => {
const s = testSourceFile({
project,
class: 'FindManyUserArgs',
property: 'cursor',
});

expect(s.property?.type).toEqual(
`Prisma.AtLeast<UserWhereUniqueInput, 'id' | 'email_name'>`,
);
});
});

describe('compound primary key', () => {
before(async () => {
({ project, sourceFiles } = await testGenerate({
schema: `
model User {
firstname String
surname String
email String
@@id([firstname, surname])
}
`,
options: [`outputFilePattern = "{name}.{type}.ts"`],
}));
});

it('compound primary key atleast keys', () => {
const s = testSourceFile({
project,
class: 'FindFirstUserArgs',
property: 'cursor',
});

expect(s.property?.type).toEqual(
"Prisma.AtLeast<UserWhereUniqueInput, 'firstname_surname'>",
);
});
});
48 changes: 0 additions & 48 deletions src/test/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1905,54 +1905,6 @@ describe('object model options', () => {
});
});

describe('compound index', () => {
before(async () => {
({ project, sourceFiles } = await testGenerate({
schema: `
model User {
id Int @id
/// @Validator.MinLength(3)
name String
/// @PropertyType({ name: 'G.Email', from: 'graphql-type-email' })
email String?
@@unique([email, name])
}
model Us {
id Int @id
}
`,
options: [
`outputFilePattern = "{name}.{type}.ts"`,
`fields_Validator_from = "class-validator"`,
`fields_Validator_input = true`,
],
}));
});

it('user unique input compound', () => {
const s = testSourceFile({
project,
class: 'UserEmailNameCompoundUniqueInput',
});

const minLength = s.classFile.getProperty('name')?.getDecorator('MinLength');
expect(minLength?.getText()).toEqual('@Validator.MinLength(3)');
});

it('compound uniq where must be wrapped to prisma atleast', () => {
const s = testSourceFile({
project,
class: 'FindManyUserArgs',
property: 'cursor',
});

expect(s.property?.type).toEqual(
`Prisma.AtLeast<UserWhereUniqueInput, 'id' | 'email_name'>`,
);
});
});

describe('field type', () => {
describe('it overwrites field type based on match expression', () => {
before(async () => {
Expand Down

0 comments on commit 64a9854

Please sign in to comment.