Skip to content

Commit

Permalink
✨ feat: differentiate between fields for create and update
Browse files Browse the repository at this point in the history
  • Loading branch information
m1212e committed Mar 25, 2024
1 parent e3e89c4 commit 09022f9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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())`
7 changes: 4 additions & 3 deletions src/generator/dataModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export function enableDataModel() {

export function DataModel(
data: Pick<DMMF.Model, "fields" | "documentation">,
referenceableEnums: Models
referenceableEnums: Models,
optional = false
) {
if (!enabled) return undefined;
const modelDoc = parseDocumentation(data.documentation);
Expand Down Expand Up @@ -42,7 +43,7 @@ export function DataModel(
fieldType: field.type,
list: field.isList,
name: field.name,
optional: true,
optional,
options: doc.options,
referenceableModels: referenceableEnums,
});
Expand Down Expand Up @@ -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,
});
})
Expand Down
17 changes: 17 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ generatorHandler({
})
);

const optionalDataTasks: Promise<void>[] = [];
const optionalDataTypes: Models = new Map<string, string>();

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<void>[] = [];
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 09022f9

Please sign in to comment.