Skip to content

Commit

Permalink
Merge pull request #51 from Quramy/fix_nullable_field
Browse files Browse the repository at this point in the history
fix: Nullable scalar field
  • Loading branch information
Quramy committed Nov 28, 2022
2 parents 93247ec + cb0c906 commit c299c5c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/prisma-fabbrica/src/templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ function isInputObjectTypeField(field: DMMF.SchemaArg) {
}

function filterRequiredScalarOrEnumFields(inputType: DMMF.InputType) {
return filterRequiredFields(inputType).filter(isScalarOrEnumField);
return filterRequiredFields(inputType)
.filter(inputType => !inputType.isNullable)
.filter(isScalarOrEnumField);
}

function filterRequiredInputObjectTypeField(inputType: DMMF.InputType) {
Expand Down Expand Up @@ -94,6 +96,8 @@ export const scalarFieldType = (
throw new Error("Invalid call. This function is allowed for only scalar field.");
}
switch (inputType.type) {
case "Null":
return ast.literalTypeNode(ast.null());
case "Boolean":
return ast.keywordTypeNode(ts.SyntaxKind.BooleanKeyword);
case "String":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ describe(modelScalarOrEnumFields, () => {
}
`,
},
{
pattern: "Nullable field",
datamodel: `
model TestModel {
id Int @id
nullableField String?
}
`,
expected: `
type TestModelScalarOrEnumFields = {
id: number;
}
`,
},
{
pattern: "Complex id",
datamodel: `
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ datasource db {

model User {
id String @id
nullableStr String?
role Role
roleDefault Role @default(USER)
roles Role[]
Expand Down
4 changes: 2 additions & 2 deletions packages/ts-compile-testing/scripts/generateFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ async function main() {
for (const fixtureDir of dirs) {
const schemaPath = path.resolve(fixturesDir, fixtureDir, "schema.prisma");
if (!existsSync(schemaPath)) continue;
const { stdout } = await exec(`npx prisma generate --schema=${schemaPath}`);
process.stdout.write(stdout);
const schemaContents = await fs.readFile(schemaPath, "utf8");
const dmmfDocument = await getDMMF({
datamodel: schemaContents,
Expand All @@ -24,6 +22,8 @@ async function main() {
JSON.stringify(dmmfDocument, null, 2),
"utf8",
);
const { stdout } = await exec(`npx prisma generate --schema=${schemaPath}`);
process.stdout.write(stdout);
}
}

Expand Down

0 comments on commit c299c5c

Please sign in to comment.