Skip to content

Commit

Permalink
Merge pull request #24 from monolithst/fix-type
Browse files Browse the repository at this point in the history
feat(type): allow undefined for normalization property
  • Loading branch information
macornwell authored May 1, 2024
2 parents 4c63663 + 29b04f8 commit 2089143
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,12 @@ A value that is calculated and save if it doesn't exist, using other values for

NOTE: If the value is provided as part of the instance, it is not re-calculated. If you want to re-calculate it, you must use either the property's method `calculate()` method to get the value and replace the existing OR pass in undefined for the property.

<strong>Incremental data creation such as GUI forms:</strong>

If you are incrementally creating and validating model data, such as a GUI form, you should make your denormalization callback understand that there may be properties that are required, but are not present (yet). What this means, is if you need a particular property's value to be part of the denormalization value, but it isn't there, you should check for the value, and if not there, return undefined. This will allow it to be recalculated later.

<strong>A Strong Word of Caution</strong>

Generally, we would recommend not using this as a primary key in a database. However, if you want to use a DenormalizedProperty as primary key in a database and you want to make changes to an instance, you need to delete the previous entry and then recreate it for every update. A dynamic primary key is not tracked between changes.

<strong>Example</strong>
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "functional-models",
"version": "2.1.8",
"version": "2.1.9",
"description": "A library for creating JavaScript function based models.",
"main": "index.js",
"types": "index.d.ts",
Expand Down
5 changes: 4 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,10 @@ type OptionalModelOptions<
type CalculateDenormalization<
T extends FunctionalValue,
TModel extends FunctionalModel,
> = (modelData: TModel, modelInstance: ModelInstance<TModel>) => MaybePromise<T>
> = (
modelData: TModel,
modelInstance: ModelInstance<TModel>
) => MaybePromise<T | undefined>

export {
MaybeFunction,
Expand Down

0 comments on commit 2089143

Please sign in to comment.