From 09022f9edf2925d305f238a7dbb836291d0c1b31 Mon Sep 17 00:00:00 2001 From: m1212e <14091540+m1212e@users.noreply.github.com> Date: Mon, 25 Mar 2024 19:15:40 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20differentiate=20between=20f?= =?UTF-8?q?ields=20for=20create=20and=20update?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 +++++-- src/generator/dataModel.ts | 7 ++++--- src/index.ts | 17 +++++++++++++++++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5cdf205..cb4e48b 100644 --- a/README.md +++ b/README.md @@ -84,12 +84,15 @@ export const PostWhere = ... // can be used for entity creation // respects the @prismabox.hide.data annotation to ignore fields specifically for the data model export const PostData = ... +// just like the above but allows each field to be nullable (null or undefined) +export const PostDataOptional = ... + ``` ### Data models To simplify the validation of input data, prismabox is able to generate schemas specifically for input data. -These are called "DataModels" and need to be explicitly enabled in the generator settings (`dataModel = true`) because they expect some conventions/field naming pattern to work properly. If you want to see the specifics on how the model works, see [the code](./src/generator/dataModel.ts). +These are called "DataModels" and need to be explicitly enabled in the generator settings (`dataModel = true`) because they expect some conventions/field naming patterns to work properly. If you want to see the specifics on how the model works, see [the code](./src/generator/dataModel.ts). 1. Foreign Ids need to end in Id (case is ignored) 2. To be detected as foreign key id, a relation field with a matching name must exist (case is ignored): @@ -100,7 +103,7 @@ These are called "DataModels" and need to be explicitly enabled in the generator // we change the name to something other than post myCoolPost Post? @relation(fields: [postId], references: [id]) - // will NOT be detected (and therefore ignored) + // will NOT be detected postId Int? ``` 3. createdAt will be detected and ignored if it follows exactly this pattern: `createdAt DateTime @default(now())` \ No newline at end of file diff --git a/src/generator/dataModel.ts b/src/generator/dataModel.ts index abfa83c..459d18b 100644 --- a/src/generator/dataModel.ts +++ b/src/generator/dataModel.ts @@ -12,7 +12,8 @@ export function enableDataModel() { export function DataModel( data: Pick, - referenceableEnums: Models + referenceableEnums: Models, + optional = false ) { if (!enabled) return undefined; const modelDoc = parseDocumentation(data.documentation); @@ -42,7 +43,7 @@ export function DataModel( fieldType: field.type, list: field.isList, name: field.name, - optional: true, + optional, options: doc.options, referenceableModels: referenceableEnums, }); @@ -76,7 +77,7 @@ export function DataModel( fieldType: field.type as any, // we checked earlier if it's a primitive type list: field.isList, name: field.name, - optional: true, + optional, options: parseDocumentation(field.documentation).options, }); }) diff --git a/src/index.ts b/src/index.ts index 3956ca5..e760984 100644 --- a/src/index.ts +++ b/src/index.ts @@ -108,6 +108,18 @@ generatorHandler({ }) ); + const optionalDataTasks: Promise[] = []; + const optionalDataTypes: Models = new Map(); + + optionalDataTasks.push( + ...options.dmmf.datamodel.models.map(async (e) => { + const model = DataModel(e, enumTypes, true); + if (model) { + optionalDataTypes.set(e.name, model); + } + }) + ); + await Promise.all([...plainTasks, ...enumTasks]); const relationTasks: Promise[] = []; @@ -147,6 +159,11 @@ generatorHandler({ models.set(`${name}Data`, dataTypeForThisName); } + const optionalDataTypeForThisName = optionalDataTypes.get(name); + if (optionalDataTypeForThisName) { + models.set(`${name}DataOptional`, optionalDataTypeForThisName); + } + await writeFile( join(outputDirectory, `${name}.ts`), await format(Compose(models))