Skip to content

Commit e55767b

Browse files
committed
fix: No atomic operations for scalar input list
1 parent 4be5d49 commit e55767b

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

src/handlers/no-atomic-operations.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import AwaitEventEmitter from 'await-event-emitter';
22

3-
import { isListInput } from '../helpers/is-list-input';
43
import { EventArguments, InputType } from '../types';
54

65
export function noAtomicOperations(eventEmitter: AwaitEventEmitter) {
@@ -12,12 +11,14 @@ function beforeInputType(args: EventArguments & { inputType: InputType }) {
1211
const { inputType, getModelName } = args;
1312

1413
for (const field of inputType.fields) {
14+
const fieldName = field.name;
1515
field.inputTypes = field.inputTypes.filter(inputType => {
1616
const inputTypeName = String(inputType.type);
1717
const modelName = getModelName(inputTypeName);
18+
1819
if (
1920
isAtomicOperation(inputTypeName) ||
20-
(modelName && isListInput(inputTypeName, modelName))
21+
(modelName && isListInput(inputTypeName, modelName, fieldName))
2122
) {
2223
return false;
2324
}
@@ -44,3 +45,10 @@ function isAtomicOperation(typeName: string) {
4445
}
4546
return false;
4647
}
48+
49+
function isListInput(typeName: string, model: string, field: string) {
50+
return (
51+
typeName === `${model}Create${field}Input` ||
52+
typeName === `${model}Update${field}Input`
53+
);
54+
}

src/helpers/get-model-name.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { first, memoize } from 'lodash';
22

3-
import { isListInput } from './is-list-input';
4-
53
export function createGetModelName(modelNames: string[]) {
64
return memoize(tryGetName);
75

@@ -55,14 +53,6 @@ function getModelName(args: {
5553
}
5654
}
5755

58-
if (name.slice(-5) === 'Input') {
59-
for (const model of modelNames) {
60-
if (isListInput(name, model)) {
61-
return model;
62-
}
63-
}
64-
}
65-
6656
// eslint-disable-next-line consistent-return, unicorn/no-useless-undefined
6757
return undefined;
6858
}
@@ -107,6 +97,8 @@ const splitKeywords = [
10797
'MinOrderBy',
10898
'MaxOrderBy',
10999
'AvgOrderBy',
100+
'Create',
101+
'Update',
110102
].sort((a, b) => b.length - a.length);
111103

112104
const endsWithKeywords = [

src/helpers/is-list-input.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/test/generate.spec.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,9 +1284,15 @@ describe('scalar arrays with noAtomicOperations', () => {
12841284
before(async () => {
12851285
({ project } = await testGenerate({
12861286
schema: `
1287-
model Dummy {
1287+
model User {
12881288
id String @id
12891289
ints Int[]
1290+
articles Article[] @relation("ArticleAuthor")
1291+
}
1292+
model Article {
1293+
id String @id
1294+
author User @relation(name: "ArticleAuthor", fields: [authorId], references: [id])
1295+
authorId String
12901296
}
12911297
`,
12921298
options: [
@@ -1298,7 +1304,7 @@ describe('scalar arrays with noAtomicOperations', () => {
12981304
});
12991305

13001306
describe('ints should be array', () => {
1301-
for (const className of ['DummyCreateInput']) {
1307+
for (const className of ['UserCreateInput']) {
13021308
it(className, () => {
13031309
const s = testSourceFile({
13041310
project,
@@ -1310,6 +1316,16 @@ describe('scalar arrays with noAtomicOperations', () => {
13101316
});
13111317
}
13121318
});
1319+
1320+
it('create many inputs should not be deleted', () => {
1321+
const s = testSourceFile({
1322+
project,
1323+
class: 'UserCreateInput',
1324+
property: 'articles',
1325+
});
1326+
1327+
expect(s.property?.type).toBe('ArticleCreateNestedManyWithoutAuthorInput');
1328+
});
13131329
});
13141330

13151331
describe('combine scalar filters', () => {

0 commit comments

Comments
 (0)