Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Nullable scalar field #51

Merged
merged 1 commit into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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