@acodeninja/persist 3.2.0 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/model-properties.md +43 -8
- package/docs/models-as-properties.md +35 -0
- package/package.json +1 -1
- package/src/data/Model.js +3 -0
- package/src/data/properties/Type.js +27 -0
package/docs/model-properties.md
CHANGED
@@ -162,14 +162,49 @@ Most types support the `.required` modifier, which will alter validation to enfo
|
|
162
162
|
```javascript
|
163
163
|
class RequiredStringModel extends Persist.Model {
|
164
164
|
static {
|
165
|
-
RequiredStringModel.requiredString =
|
166
|
-
|
167
|
-
RequiredStringModel.
|
168
|
-
|
169
|
-
RequiredStringModel.
|
170
|
-
|
171
|
-
RequiredStringModel.
|
172
|
-
|
165
|
+
RequiredStringModel.requiredString =
|
166
|
+
Persist.Property.String.required;
|
167
|
+
RequiredStringModel.requiredNumber =
|
168
|
+
Persist.Property.Number.required;
|
169
|
+
RequiredStringModel.requiredBoolean =
|
170
|
+
Persist.Property.Boolean.required;
|
171
|
+
RequiredStringModel.requiredDate =
|
172
|
+
Persist.Property.Date.required;
|
173
|
+
RequiredStringModel.requiredArrayOfString =
|
174
|
+
Persist.Property.Array.of(Persist.Property.String).required;
|
175
|
+
RequiredStringModel.requiredArrayOfNumber =
|
176
|
+
Persist.Property.Array.of(Persist.Property.Number).required;
|
177
|
+
RequiredStringModel.requiredArrayOfBoolean =
|
178
|
+
Persist.Property.Array.of(Persist.Property.Boolean).required;
|
179
|
+
RequiredStringModel.requiredArrayOfDate =
|
180
|
+
Persist.Property.Array.of(Persist.Property.Date).required;
|
181
|
+
}
|
182
|
+
}
|
183
|
+
```
|
184
|
+
|
185
|
+
### `.default(defaultValue)`
|
186
|
+
|
187
|
+
Most types support the `.default(defaultValue)` modifier, which allows fields to have a default value when the user has not explicitly set one on an instance of the model.
|
188
|
+
|
189
|
+
```javascript
|
190
|
+
class ModelWithDefaults extends Persist.Model {
|
191
|
+
static {
|
192
|
+
ModelWithDefaults.requiredString =
|
193
|
+
Persist.Property.String.required.default('string');
|
194
|
+
ModelWithDefaults.requiredNumber =
|
195
|
+
Persist.Property.Number.required.default(67);
|
196
|
+
ModelWithDefaults.requiredBoolean =
|
197
|
+
Persist.Property.Boolean.required.default(false);
|
198
|
+
ModelWithDefaults.requiredDate =
|
199
|
+
Persist.Property.Date.required.default(new Date);
|
200
|
+
ModelWithDefaults.requiredArrayOfString =
|
201
|
+
Persist.Property.Array.of(Persist.Property.String).required.default(['string']);
|
202
|
+
ModelWithDefaults.requiredArrayOfNumber =
|
203
|
+
Persist.Property.Array.of(Persist.Property.Number).required.default([45, 22]);
|
204
|
+
ModelWithDefaults.requiredArrayOfBoolean =
|
205
|
+
Persist.Property.Array.of(Persist.Property.Boolean).required.default([true, false]);
|
206
|
+
ModelWithDefaults.requiredArrayOfDate =
|
207
|
+
Persist.Property.Array.of(Persist.Property.Date).required.default([new Date]);
|
173
208
|
}
|
174
209
|
}
|
175
210
|
```
|
@@ -157,3 +157,38 @@ In this setup:
|
|
157
157
|
- A `Person` can have multiple `Abode` entries (i.e., where they lived and when they moved in).
|
158
158
|
- Each `Abode` links a `Person` to an `Address`, while also recording the move-in date.
|
159
159
|
- An `Address` can still reference multiple people, making this a flexible and more complex relationship model.
|
160
|
+
|
161
|
+
## Polymorphic relationships
|
162
|
+
|
163
|
+
When a property of a model can be any of a list of models, you can use a polymorphic relationship.
|
164
|
+
|
165
|
+
```javascript
|
166
|
+
import Persist from "@acodeninja/persist";
|
167
|
+
|
168
|
+
export class Person extends Persist.Model {
|
169
|
+
static {
|
170
|
+
Person.name = Persist.Property.String.required;
|
171
|
+
Person.address = () => Persist.Property.Any.of(GBAddress, USAddress);
|
172
|
+
}
|
173
|
+
}
|
174
|
+
|
175
|
+
export class GBAddress extends Persist.Model {
|
176
|
+
static {
|
177
|
+
GBAddress.address = Persist.Property.String.required;
|
178
|
+
GBAddress.postcode = Persist.Property.String.required;
|
179
|
+
}
|
180
|
+
}
|
181
|
+
|
182
|
+
export class USAddress extends Persist.Model {
|
183
|
+
static {
|
184
|
+
USAddress.address = Persist.Property.String.required;
|
185
|
+
USAddress.zipcode = Persist.Property.String.required;
|
186
|
+
}
|
187
|
+
}
|
188
|
+
```
|
189
|
+
|
190
|
+
In this setup:
|
191
|
+
|
192
|
+
- A `Person` can have either a `GBAddress` or a `USAddress` depending on which country they live in.
|
193
|
+
- A `GBAddress` has a postcode.
|
194
|
+
- A `USAddress` has a zip code.
|
package/package.json
CHANGED
package/src/data/Model.js
CHANGED
@@ -73,6 +73,33 @@ class Type {
|
|
73
73
|
return Required;
|
74
74
|
}
|
75
75
|
|
76
|
+
/**
|
77
|
+
* Returns a version of the type with a default value set.
|
78
|
+
*
|
79
|
+
* @param {*} defaultValue
|
80
|
+
* @return {Type} A subclass of the current type with `_default` set to `defaultValue`.
|
81
|
+
*/
|
82
|
+
static default(defaultValue) {
|
83
|
+
const ThisType = this;
|
84
|
+
|
85
|
+
/**
|
86
|
+
* A subclass of the current type with the `_defaults` flag set to the default value.
|
87
|
+
* Used to indicate that the property will default to the value given if none is provided.
|
88
|
+
*
|
89
|
+
* @class
|
90
|
+
* @extends {Type}
|
91
|
+
* @private
|
92
|
+
*/
|
93
|
+
class WithDefault extends ThisType {
|
94
|
+
static _default = defaultValue;
|
95
|
+
}
|
96
|
+
|
97
|
+
// Define the class name as "OriginalTypeName"
|
98
|
+
Object.defineProperty(WithDefault, 'name', {value: ThisType.name});
|
99
|
+
|
100
|
+
return WithDefault;
|
101
|
+
}
|
102
|
+
|
76
103
|
static {
|
77
104
|
Object.defineProperty(Type, 'name', {value: 'Type'});
|
78
105
|
}
|