WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 9f26a4d

Browse files
authored
Merge pull request #49 from Pastequee/fix/unique-double-generation
Fix double unique field generation when 1 element unique array
2 parents bfe7991 + 1fc51ed commit 9f26a4d

File tree

2 files changed

+47
-44
lines changed

2 files changed

+47
-44
lines changed

prisma/schema.prisma

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,14 @@ enum ConferenceRole {
120120
/// A user's membership in a conference, providing them with a role in the conference
121121
model ConferenceMember {
122122
id String @id @default(uuid())
123-
blah String @unique
123+
blah String
124124
conference Conference @relation(fields: [conferenceId], references: [id])
125125
conferenceId String
126126
user User? @relation(fields: [userId], references: [id])
127127
userId String?
128128
role ConferenceRole
129129
130+
@@unique([blah])
130131
@@unique([userId, conferenceId])
131132
}
132133

src/generators/where.ts

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -113,54 +113,56 @@ export function stringifyWhereUnique(data: DMMF.Model) {
113113
const annotations = extractAnnotations(data.documentation);
114114
if (annotations.isHidden) return undefined;
115115

116-
const uniqueCompositeFields = data.uniqueFields.map((fields) => {
117-
const compositeName = fields.join("_");
118-
const fieldObjects = fields.map(
119-
// biome-ignore lint/style/noNonNullAssertion: this must exist
120-
(f) => data.fields.find((field) => field.name === f)!,
121-
);
122-
123-
const stringifiedFieldObjects = fieldObjects.map((f) => {
124-
const annotations = extractAnnotations(f.documentation);
125-
if (annotations.isHidden) return undefined;
126-
let stringifiedType = "";
127-
128-
if (isPrimitivePrismaFieldType(f.type)) {
129-
const overwrittenType = annotations.annotations
130-
.filter(isTypeOverwriteVariant)
131-
.at(0)?.value;
132-
133-
if (overwrittenType) {
134-
stringifiedType = overwrittenType;
116+
const uniqueCompositeFields = data.uniqueFields
117+
.filter((fields) => fields.length > 1)
118+
.map((fields) => {
119+
const compositeName = fields.join("_");
120+
const fieldObjects = fields.map(
121+
// biome-ignore lint/style/noNonNullAssertion: this must exist
122+
(f) => data.fields.find((field) => field.name === f)!,
123+
);
124+
125+
const stringifiedFieldObjects = fieldObjects.map((f) => {
126+
const annotations = extractAnnotations(f.documentation);
127+
if (annotations.isHidden) return undefined;
128+
let stringifiedType = "";
129+
130+
if (isPrimitivePrismaFieldType(f.type)) {
131+
const overwrittenType = annotations.annotations
132+
.filter(isTypeOverwriteVariant)
133+
.at(0)?.value;
134+
135+
if (overwrittenType) {
136+
stringifiedType = overwrittenType;
137+
} else {
138+
stringifiedType = stringifyPrimitiveType({
139+
fieldType: f.type as PrimitivePrismaFieldType,
140+
options: generateTypeboxOptions({
141+
exludeAdditionalProperties: false,
142+
input: annotations,
143+
}),
144+
});
145+
}
146+
} else if (processedEnums.find((e) => e.name === f.type)) {
147+
// biome-ignore lint/style/noNonNullAssertion: we checked this manually
148+
stringifiedType = processedEnums.find(
149+
(e) => e.name === f.type,
150+
)!.stringRepresentation;
135151
} else {
136-
stringifiedType = stringifyPrimitiveType({
137-
fieldType: f.type as PrimitivePrismaFieldType,
138-
options: generateTypeboxOptions({
139-
exludeAdditionalProperties: false,
140-
input: annotations,
141-
}),
142-
});
152+
throw new Error("Invalid type for unique composite generation");
143153
}
144-
} else if (processedEnums.find((e) => e.name === f.type)) {
145-
// biome-ignore lint/style/noNonNullAssertion: we checked this manually
146-
stringifiedType = processedEnums.find(
147-
(e) => e.name === f.type,
148-
)!.stringRepresentation;
149-
} else {
150-
throw new Error("Invalid type for unique composite generation");
151-
}
152154

153-
return `${f.name}: ${stringifiedType}`;
154-
});
155+
return `${f.name}: ${stringifiedType}`;
156+
});
155157

156-
const compositeObject = `${
157-
getConfig().typeboxImportVariableName
158-
}.Object({${stringifiedFieldObjects.join(
159-
",",
160-
)}}, ${generateTypeboxOptions({ exludeAdditionalProperties: true })})`;
158+
const compositeObject = `${
159+
getConfig().typeboxImportVariableName
160+
}.Object({${stringifiedFieldObjects.join(
161+
",",
162+
)}}, ${generateTypeboxOptions({ exludeAdditionalProperties: true })})`;
161163

162-
return `${compositeName}: ${compositeObject}`;
163-
});
164+
return `${compositeName}: ${compositeObject}`;
165+
});
164166

165167
const allFields = data.fields
166168
.map((field) => {

0 commit comments

Comments
 (0)