breeze-client 2.2.2 → 3.0.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/LICENSE +1 -1
- package/README.md +83 -63
- package/adapters/abstract-data-service-adapter.d.ts +120 -0
- package/adapters/abstract-data-service-adapter.js +395 -0
- package/adapters/abstract-data-service-adapter.js.map +1 -0
- package/adapters/adapter-ajax-fetch.d.ts +12 -0
- package/adapters/adapter-ajax-fetch.js +13 -0
- package/adapters/adapter-ajax-fetch.js.map +1 -0
- package/{adapter-ajax-post → adapters}/adapter-ajax-post.d.ts +40 -36
- package/{fesm2022/breeze-client-adapter-ajax-post.mjs → adapters/adapter-ajax-post.js} +80 -82
- package/adapters/adapter-ajax-post.js.map +1 -0
- package/{adapter-ajax-fetch → adapters}/adapter-core.d.ts +4 -4
- package/{fesm2022/breeze-client-adapter-uri-builder-json.mjs → adapters/adapter-core.js} +48 -91
- package/adapters/adapter-core.js.map +1 -0
- package/adapters/adapter-data-service-webapi.d.ts +9 -0
- package/{fesm2022/breeze-client-adapter-data-service-webapi.mjs → adapters/adapter-data-service-webapi.js} +105 -107
- package/adapters/adapter-data-service-webapi.js.map +1 -0
- package/adapters/adapter-model-library-backing-store.d.ts +32 -0
- package/adapters/adapter-model-library-backing-store.js +321 -0
- package/adapters/adapter-model-library-backing-store.js.map +1 -0
- package/adapters/adapter-uri-builder-json.d.ts +11 -0
- package/adapters/adapter-uri-builder-json.js +40 -0
- package/adapters/adapter-uri-builder-json.js.map +1 -0
- package/adapters/http.d.ts +52 -0
- package/adapters/http.js +202 -0
- package/adapters/http.js.map +1 -0
- package/angular/adapter-angular-httpclient.d.ts +21 -0
- package/angular/adapter-angular-httpclient.js +70 -0
- package/angular/adapter-angular-httpclient.js.map +1 -0
- package/breeze.d.ts +215 -0
- package/breeze.js +141 -0
- package/breeze.js.map +1 -0
- package/config/config.d.ts +212 -0
- package/config/config.js +392 -0
- package/config/config.js.map +1 -0
- package/config/configure.d.ts +66 -0
- package/config/configure.js +46 -0
- package/config/configure.js.map +1 -0
- package/config/default-adapters.d.ts +1 -0
- package/config/default-adapters.js +40 -0
- package/config/default-adapters.js.map +1 -0
- package/config/interface-registry.d.ts +191 -0
- package/config/interface-registry.js +5 -0
- package/config/interface-registry.js.map +1 -0
- package/{src → core}/assert-param.d.ts +52 -60
- package/core/assert-param.js +390 -0
- package/core/assert-param.js.map +1 -0
- package/core/core.d.ts +285 -0
- package/core/core.js +772 -0
- package/core/core.js.map +1 -0
- package/core/enum.d.ts +116 -0
- package/core/enum.js +167 -0
- package/core/enum.js.map +1 -0
- package/core/event.d.ts +169 -0
- package/core/event.js +338 -0
- package/core/event.js.map +1 -0
- package/entity/array.d.ts +3 -0
- package/entity/array.js +4 -0
- package/entity/array.js.map +1 -0
- package/entity/complex-array.d.ts +17 -0
- package/entity/complex-array.js +104 -0
- package/entity/complex-array.js.map +1 -0
- package/entity/default-property-interceptor.d.ts +1 -0
- package/entity/default-property-interceptor.js +496 -0
- package/entity/default-property-interceptor.js.map +1 -0
- package/{src → entity}/entity-action.d.ts +36 -42
- package/entity/entity-action.js +45 -0
- package/entity/entity-action.js.map +1 -0
- package/entity/entity-aspect.d.ts +457 -0
- package/entity/entity-aspect.js +1262 -0
- package/entity/entity-aspect.js.map +1 -0
- package/entity/entity-base.d.ts +55 -0
- package/entity/entity-base.js +41 -0
- package/entity/entity-base.js.map +1 -0
- package/entity/entity-group.d.ts +1 -0
- package/entity/entity-group.js +224 -0
- package/entity/entity-group.js.map +1 -0
- package/entity/entity-key.d.ts +95 -0
- package/entity/entity-key.js +189 -0
- package/entity/entity-key.js.map +1 -0
- package/{src → entity}/entity-state.d.ts +112 -84
- package/entity/entity-state.js +135 -0
- package/entity/entity-state.js.map +1 -0
- package/entity/key-generator.d.ts +57 -0
- package/entity/key-generator.js +155 -0
- package/entity/key-generator.js.map +1 -0
- package/entity/observable-array.d.ts +25 -0
- package/entity/observable-array.js +230 -0
- package/entity/observable-array.js.map +1 -0
- package/entity/primitive-array.d.ts +12 -0
- package/entity/primitive-array.js +71 -0
- package/entity/primitive-array.js.map +1 -0
- package/entity/relation-array.d.ts +24 -0
- package/entity/relation-array.js +176 -0
- package/entity/relation-array.js.map +1 -0
- package/entity/unattached-children-map.d.ts +1 -0
- package/entity/unattached-children-map.js +105 -0
- package/entity/unattached-children-map.js.map +1 -0
- package/generate-entity-classes.js +1176 -0
- package/manager/entity-manager.d.ts +801 -0
- package/manager/entity-manager.js +2395 -0
- package/manager/entity-manager.js.map +1 -0
- package/{src → manager}/save-options.d.ts +48 -42
- package/manager/save-options.js +51 -0
- package/manager/save-options.js.map +1 -0
- package/{src → metadata}/data-service.d.ts +229 -184
- package/metadata/data-service.js +246 -0
- package/metadata/data-service.js.map +1 -0
- package/metadata/data-type.d.ts +122 -0
- package/metadata/data-type.js +465 -0
- package/metadata/data-type.js.map +1 -0
- package/metadata/entity-metadata.d.ts +1193 -0
- package/metadata/entity-metadata.js +2502 -0
- package/metadata/entity-metadata.js.map +1 -0
- package/{src → metadata}/local-query-comparison-options.d.ts +65 -59
- package/metadata/local-query-comparison-options.js +69 -0
- package/metadata/local-query-comparison-options.js.map +1 -0
- package/{src → metadata}/naming-convention.d.ts +75 -72
- package/metadata/naming-convention.js +97 -0
- package/metadata/naming-convention.js.map +1 -0
- package/mixins/mixin-get-entity-graph.d.ts +63 -0
- package/{fesm2022/breeze-client-mixin-get-entity-graph.mjs → mixins/mixin-get-entity-graph.js} +331 -253
- package/mixins/mixin-get-entity-graph.js.map +1 -0
- package/mixins/mixin-save-queuing.d.ts +98 -0
- package/{fesm2022/breeze-client-mixin-save-queuing.mjs → mixins/mixin-save-queuing.js} +493 -365
- package/mixins/mixin-save-queuing.js.map +1 -0
- package/package.json +63 -94
- package/query/entity-query.d.ts +521 -0
- package/query/entity-query.js +1149 -0
- package/query/entity-query.js.map +1 -0
- package/{src → query}/mapping-context.d.ts +44 -46
- package/query/mapping-context.js +412 -0
- package/query/mapping-context.js.map +1 -0
- package/query/predicate.d.ts +394 -0
- package/query/predicate.js +1294 -0
- package/query/predicate.js.map +1 -0
- package/query/property-path.d.ts +252 -0
- package/query/property-path.js +2 -0
- package/query/property-path.js.map +1 -0
- package/{src → query}/query-options.d.ts +143 -119
- package/query/query-options.js +180 -0
- package/query/query-options.js.map +1 -0
- package/rxjs/breeze-rxjs.d.ts +72 -0
- package/rxjs/breeze-rxjs.js +117 -0
- package/rxjs/breeze-rxjs.js.map +1 -0
- package/validation/validate.d.ts +583 -0
- package/validation/validate.js +961 -0
- package/validation/validate.js.map +1 -0
- package/{src → validation}/validation-options.d.ts +67 -63
- package/validation/validation-options.js +70 -0
- package/validation/validation-options.js.map +1 -0
- package/adapter-ajax-angularjs/adapter-ajax-angularjs.d.ts +0 -17
- package/adapter-ajax-angularjs/adapter-core.d.ts +0 -4
- package/adapter-ajax-angularjs/index.d.ts +0 -5
- package/adapter-ajax-fetch/adapter-ajax-fetch.d.ts +0 -18
- package/adapter-ajax-fetch/index.d.ts +0 -5
- package/adapter-ajax-httpclient/adapter-ajax-httpclient.d.ts +0 -13
- package/adapter-ajax-httpclient/adapter-core.d.ts +0 -4
- package/adapter-ajax-httpclient/index.d.ts +0 -5
- package/adapter-ajax-jquery/adapter-ajax-jquery.d.ts +0 -15
- package/adapter-ajax-jquery/index.d.ts +0 -5
- package/adapter-ajax-post/index.d.ts +0 -5
- package/adapter-data-service-odata/adapter-core.d.ts +0 -4
- package/adapter-data-service-odata/adapter-data-service-odata.d.ts +0 -17
- package/adapter-data-service-odata/index.d.ts +0 -5
- package/adapter-data-service-webapi/adapter-data-service-webapi.d.ts +0 -15
- package/adapter-data-service-webapi/index.d.ts +0 -5
- package/adapter-model-library-backing-store/adapter-model-library-backing-store.d.ts +0 -10
- package/adapter-model-library-backing-store/index.d.ts +0 -5
- package/adapter-model-library-ko/adapter-model-library-ko.d.ts +0 -10
- package/adapter-model-library-ko/index.d.ts +0 -5
- package/adapter-uri-builder-json/adapter-core.d.ts +0 -4
- package/adapter-uri-builder-json/adapter-uri-builder-json.d.ts +0 -8
- package/adapter-uri-builder-json/index.d.ts +0 -5
- package/adapter-uri-builder-odata/adapter-core.d.ts +0 -4
- package/adapter-uri-builder-odata/adapter-uri-builder-odata.d.ts +0 -8
- package/adapter-uri-builder-odata/index.d.ts +0 -5
- package/esm2022/adapter-ajax-angularjs/adapter-ajax-angularjs.mjs +0 -135
- package/esm2022/adapter-ajax-angularjs/adapter-core.mjs +0 -49
- package/esm2022/adapter-ajax-angularjs/breeze-client-adapter-ajax-angularjs.mjs +0 -5
- package/esm2022/adapter-ajax-fetch/adapter-ajax-fetch.mjs +0 -135
- package/esm2022/adapter-ajax-fetch/adapter-core.mjs +0 -49
- package/esm2022/adapter-ajax-fetch/breeze-client-adapter-ajax-fetch.mjs +0 -5
- package/esm2022/adapter-ajax-httpclient/adapter-ajax-httpclient.mjs +0 -139
- package/esm2022/adapter-ajax-httpclient/adapter-core.mjs +0 -49
- package/esm2022/adapter-ajax-httpclient/breeze-client-adapter-ajax-httpclient.mjs +0 -5
- package/esm2022/adapter-ajax-jquery/adapter-ajax-jquery.mjs +0 -107
- package/esm2022/adapter-ajax-jquery/breeze-client-adapter-ajax-jquery.mjs +0 -5
- package/esm2022/adapter-ajax-post/adapter-ajax-post.mjs +0 -77
- package/esm2022/adapter-ajax-post/breeze-client-adapter-ajax-post.mjs +0 -5
- package/esm2022/adapter-data-service-odata/adapter-core.mjs +0 -49
- package/esm2022/adapter-data-service-odata/adapter-data-service-odata.mjs +0 -461
- package/esm2022/adapter-data-service-odata/breeze-client-adapter-data-service-odata.mjs +0 -5
- package/esm2022/adapter-data-service-webapi/adapter-data-service-webapi.mjs +0 -100
- package/esm2022/adapter-data-service-webapi/breeze-client-adapter-data-service-webapi.mjs +0 -5
- package/esm2022/adapter-model-library-backing-store/adapter-model-library-backing-store.mjs +0 -269
- package/esm2022/adapter-model-library-backing-store/breeze-client-adapter-model-library-backing-store.mjs +0 -5
- package/esm2022/adapter-model-library-ko/adapter-model-library-ko.mjs +0 -241
- package/esm2022/adapter-model-library-ko/breeze-client-adapter-model-library-ko.mjs +0 -5
- package/esm2022/adapter-uri-builder-json/adapter-core.mjs +0 -49
- package/esm2022/adapter-uri-builder-json/adapter-uri-builder-json.mjs +0 -37
- package/esm2022/adapter-uri-builder-json/breeze-client-adapter-uri-builder-json.mjs +0 -5
- package/esm2022/adapter-uri-builder-odata/adapter-core.mjs +0 -49
- package/esm2022/adapter-uri-builder-odata/adapter-uri-builder-odata.mjs +0 -191
- package/esm2022/adapter-uri-builder-odata/breeze-client-adapter-uri-builder-odata.mjs +0 -5
- package/esm2022/breeze-client.mjs +0 -5
- package/esm2022/index.mjs +0 -2
- package/esm2022/mixin-get-entity-graph/breeze-client-mixin-get-entity-graph.mjs +0 -5
- package/esm2022/mixin-get-entity-graph/mixin-get-entity-graph.mjs +0 -269
- package/esm2022/mixin-save-queuing/breeze-client-mixin-save-queuing.mjs +0 -5
- package/esm2022/mixin-save-queuing/mixin-save-queuing.mjs +0 -421
- package/esm2022/src/abstract-data-service-adapter.mjs +0 -325
- package/esm2022/src/array.mjs +0 -4
- package/esm2022/src/assert-param.mjs +0 -330
- package/esm2022/src/breeze.mjs +0 -85
- package/esm2022/src/complex-array.mjs +0 -117
- package/esm2022/src/config.mjs +0 -232
- package/esm2022/src/core.mjs +0 -712
- package/esm2022/src/csdl-metadata-parser.mjs +0 -385
- package/esm2022/src/data-service.mjs +0 -221
- package/esm2022/src/data-type.mjs +0 -510
- package/esm2022/src/default-property-interceptor.mjs +0 -460
- package/esm2022/src/entity-action.mjs +0 -45
- package/esm2022/src/entity-aspect.mjs +0 -897
- package/esm2022/src/entity-group.mjs +0 -192
- package/esm2022/src/entity-key.mjs +0 -125
- package/esm2022/src/entity-manager.mjs +0 -2163
- package/esm2022/src/entity-metadata.mjs +0 -2242
- package/esm2022/src/entity-query.mjs +0 -1024
- package/esm2022/src/entity-state.mjs +0 -107
- package/esm2022/src/enum.mjs +0 -143
- package/esm2022/src/event.mjs +0 -264
- package/esm2022/src/interface-registry.mjs +0 -39
- package/esm2022/src/key-generator.mjs +0 -110
- package/esm2022/src/local-query-comparison-options.mjs +0 -65
- package/esm2022/src/mapping-context.mjs +0 -411
- package/esm2022/src/naming-convention.mjs +0 -92
- package/esm2022/src/observable-array.mjs +0 -183
- package/esm2022/src/predicate.mjs +0 -1259
- package/esm2022/src/primitive-array.mjs +0 -85
- package/esm2022/src/query-options.mjs +0 -152
- package/esm2022/src/relation-array.mjs +0 -173
- package/esm2022/src/save-options.mjs +0 -41
- package/esm2022/src/unattached-children-map.mjs +0 -56
- package/esm2022/src/validate.mjs +0 -995
- package/esm2022/src/validation-options.mjs +0 -64
- package/fesm2022/breeze-client-adapter-ajax-angularjs.mjs +0 -190
- package/fesm2022/breeze-client-adapter-ajax-angularjs.mjs.map +0 -1
- package/fesm2022/breeze-client-adapter-ajax-fetch.mjs +0 -190
- package/fesm2022/breeze-client-adapter-ajax-fetch.mjs.map +0 -1
- package/fesm2022/breeze-client-adapter-ajax-httpclient.mjs +0 -194
- package/fesm2022/breeze-client-adapter-ajax-httpclient.mjs.map +0 -1
- package/fesm2022/breeze-client-adapter-ajax-jquery.mjs +0 -114
- package/fesm2022/breeze-client-adapter-ajax-jquery.mjs.map +0 -1
- package/fesm2022/breeze-client-adapter-ajax-post.mjs.map +0 -1
- package/fesm2022/breeze-client-adapter-data-service-odata.mjs +0 -516
- package/fesm2022/breeze-client-adapter-data-service-odata.mjs.map +0 -1
- package/fesm2022/breeze-client-adapter-data-service-webapi.mjs.map +0 -1
- package/fesm2022/breeze-client-adapter-model-library-backing-store.mjs +0 -276
- package/fesm2022/breeze-client-adapter-model-library-backing-store.mjs.map +0 -1
- package/fesm2022/breeze-client-adapter-model-library-ko.mjs +0 -248
- package/fesm2022/breeze-client-adapter-model-library-ko.mjs.map +0 -1
- package/fesm2022/breeze-client-adapter-uri-builder-json.mjs.map +0 -1
- package/fesm2022/breeze-client-adapter-uri-builder-odata.mjs +0 -246
- package/fesm2022/breeze-client-adapter-uri-builder-odata.mjs.map +0 -1
- package/fesm2022/breeze-client-mixin-get-entity-graph.mjs.map +0 -1
- package/fesm2022/breeze-client-mixin-save-queuing.mjs.map +0 -1
- package/fesm2022/breeze-client.mjs +0 -14144
- package/fesm2022/breeze-client.mjs.map +0 -1
- package/index.d.ts +0 -1
- package/mixin-get-entity-graph/index.d.ts +0 -5
- package/mixin-get-entity-graph/mixin-get-entity-graph.d.ts +0 -31
- package/mixin-save-queuing/index.d.ts +0 -5
- package/mixin-save-queuing/mixin-save-queuing.d.ts +0 -32
- package/src/abstract-data-service-adapter.d.ts +0 -84
- package/src/array.d.ts +0 -3
- package/src/breeze.d.ts +0 -139
- package/src/complex-array.d.ts +0 -13
- package/src/config.d.ts +0 -104
- package/src/core.d.ts +0 -164
- package/src/csdl-metadata-parser.d.ts +0 -7
- package/src/data-type.d.ts +0 -89
- package/src/default-property-interceptor.d.ts +0 -4
- package/src/entity-aspect.d.ts +0 -332
- package/src/entity-group.d.ts +0 -28
- package/src/entity-key.d.ts +0 -77
- package/src/entity-manager.d.ts +0 -680
- package/src/entity-metadata.d.ts +0 -988
- package/src/entity-query.d.ts +0 -444
- package/src/enum.d.ts +0 -100
- package/src/event.d.ts +0 -142
- package/src/interface-registry.d.ts +0 -150
- package/src/key-generator.d.ts +0 -12
- package/src/observable-array.d.ts +0 -52
- package/src/predicate.d.ts +0 -376
- package/src/primitive-array.d.ts +0 -7
- package/src/relation-array.d.ts +0 -18
- package/src/unattached-children-map.d.ts +0 -19
- package/src/validate.d.ts +0 -650
|
@@ -0,0 +1,1262 @@
|
|
|
1
|
+
import { core } from '../core/core.js';
|
|
2
|
+
import { config } from '../config/config.js';
|
|
3
|
+
import { BreezeEvent } from '../core/event.js';
|
|
4
|
+
import { assertParam, paramError } from '../core/assert-param.js';
|
|
5
|
+
import { EntityState } from './entity-state.js';
|
|
6
|
+
import { EntityAction } from './entity-action.js';
|
|
7
|
+
import { EntityType } from '../metadata/entity-metadata.js';
|
|
8
|
+
import { observableArray } from './observable-array.js';
|
|
9
|
+
import { EntityKey } from './entity-key.js';
|
|
10
|
+
import { Validator, ValidationError } from '../validation/validate.js';
|
|
11
|
+
import { EntityQuery } from '../query/entity-query.js';
|
|
12
|
+
import { DataType } from '../metadata/data-type.js';
|
|
13
|
+
/**
|
|
14
|
+
An EntityAspect instance is associated with every attached entity and is accessed via the entity's 'entityAspect' property.
|
|
15
|
+
|
|
16
|
+
The EntityAspect itself provides properties to determine and modify the EntityState of the entity and has methods
|
|
17
|
+
that provide a variety of services including validation and change tracking.
|
|
18
|
+
|
|
19
|
+
An EntityAspect will almost never need to be constructed directly. You will usually get an EntityAspect by accessing
|
|
20
|
+
an entities 'entityAspect' property. This property will be automatically attached when an entity is created via either
|
|
21
|
+
a query, import or {@link EntityManager.createEntity} call.
|
|
22
|
+
```ts
|
|
23
|
+
// assume order is an order entity attached to an EntityManager.
|
|
24
|
+
var aspect = order.entityAspect;
|
|
25
|
+
var currentState = aspect.entityState;
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
*/
|
|
29
|
+
export class EntityAspect {
|
|
30
|
+
/** The Entity that this aspect is associated with. __Read Only__ */
|
|
31
|
+
entity;
|
|
32
|
+
/** The {@link EntityManager} that contains this entity. __Read Only__ */
|
|
33
|
+
entityManager;
|
|
34
|
+
/** @hidden @internal */
|
|
35
|
+
entityGroup;
|
|
36
|
+
/** @hidden @internal */
|
|
37
|
+
_entityState;
|
|
38
|
+
// An accessor rather than a plain field because the entity's group keeps the set of its
|
|
39
|
+
// entities that are not Unchanged, and this is the one place every state change passes through
|
|
40
|
+
// - setEntityState, _detach, the attach and merge paths, and anything an application or plugin
|
|
41
|
+
// assigns. Working the set out by scanning instead is what made hasChanges cost the size of the
|
|
42
|
+
// cache, once per state change. See "Cache lookups that were scans" in CHANGES-DEV.md.
|
|
43
|
+
/** The {@link EntityState} of this entity. __Read Only__ */
|
|
44
|
+
get entityState() {
|
|
45
|
+
return this._entityState;
|
|
46
|
+
}
|
|
47
|
+
set entityState(entityState) {
|
|
48
|
+
this._entityState = entityState;
|
|
49
|
+
const group = this.entityGroup;
|
|
50
|
+
if (group === undefined)
|
|
51
|
+
return; // detached: nothing is tracking it
|
|
52
|
+
if (entityState === EntityState.Unchanged || entityState === EntityState.Detached) {
|
|
53
|
+
group._changedEntities.delete(this.entity);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
group._changedEntities.add(this.entity);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/** Whether this entity is in the process of being saved. __Read Only__ */
|
|
60
|
+
isBeingSaved;
|
|
61
|
+
/** The 'original values' of this entity where they are different from the 'current values'.
|
|
62
|
+
This is a map where the key is a property name and the value is the 'original value' of the property. */
|
|
63
|
+
originalValues;
|
|
64
|
+
/** Whether this entity has any validation errors. __Read Only__ */
|
|
65
|
+
hasValidationErrors;
|
|
66
|
+
/** Whether this entity has a temporary {@link EntityKey}. */
|
|
67
|
+
hasTempKey;
|
|
68
|
+
/** Whether this entity was created by being loaded from the database */
|
|
69
|
+
wasLoaded;
|
|
70
|
+
/** Extra metadata about this entity such as the entity's etag.
|
|
71
|
+
You may extend this object with your own metadata information.
|
|
72
|
+
Breeze (de)serializes this object when importing/exporting the entity. */
|
|
73
|
+
extraMetadata;
|
|
74
|
+
/**
|
|
75
|
+
A {@link BreezeEvent} that fires whenever any of the validation errors on this entity change.
|
|
76
|
+
Note that this might be the removal of an error when some data on the entity is fixed.
|
|
77
|
+
@eventArgs -
|
|
78
|
+
- entity - The entity on which the validation errors are being added or removed.
|
|
79
|
+
- added - An array containing any newly added {@link ValidationError}s
|
|
80
|
+
- removed - An array containing any newly removed {@link ValidationError}s. This is those
|
|
81
|
+
errors that have been 'fixed'.
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
// assume order is an order entity attached to an EntityManager.
|
|
85
|
+
order.entityAspect.validationErrorsChanged.subscribe(
|
|
86
|
+
function (validationChangeArgs) {
|
|
87
|
+
// this code will be executed anytime a property value changes on the 'order' entity.
|
|
88
|
+
var entity == validationChangeArgs.entity; // Note: entity === order
|
|
89
|
+
var errorsAdded = validationChangeArgs.added;
|
|
90
|
+
var errorsCleared = validationChangeArgs.removed;
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
@event
|
|
94
|
+
*/
|
|
95
|
+
validationErrorsChanged;
|
|
96
|
+
/**
|
|
97
|
+
A {@link BreezeEvent} that fires whenever a value of one of this entity's properties change.
|
|
98
|
+
@eventArgs -
|
|
99
|
+
- entity - The entity whose property has changed.
|
|
100
|
+
- property - The {@link DataProperty} that changed.
|
|
101
|
+
- propertyName - The name of the property that changed. This value will be 'null' for operations that replace the entire entity. This includes
|
|
102
|
+
queries, imports and saves that require a merge. The remaining parameters will not exist in this case either. This will actually be a "property path"
|
|
103
|
+
for any properties of a complex type.
|
|
104
|
+
- oldValue - The old value of this property before the change.
|
|
105
|
+
- newValue - The new value of this property after the change.
|
|
106
|
+
- parent - The immediate parent object for the changed property. This will be a ComplexType instance as opposed to an Entity
|
|
107
|
+
for any complex type or nested complex type properties.
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
// assume order is an order entity attached to an EntityManager.
|
|
111
|
+
order.entityAspect.propertyChanged.subscribe(
|
|
112
|
+
function (propertyChangedArgs) {
|
|
113
|
+
// this code will be executed anytime a property value changes on the 'order' entity.
|
|
114
|
+
var entity = propertyChangedArgs.entity; // Note: entity === order
|
|
115
|
+
var propertyNameChanged = propertyChangedArgs.propertyName;
|
|
116
|
+
var oldValue = propertyChangedArgs.oldValue;
|
|
117
|
+
var newValue = propertyChangedArgs.newValue;
|
|
118
|
+
});
|
|
119
|
+
```
|
|
120
|
+
@event
|
|
121
|
+
*/
|
|
122
|
+
propertyChanged;
|
|
123
|
+
/** @hidden @internal */
|
|
124
|
+
_validationErrors;
|
|
125
|
+
/** @hidden @internal */
|
|
126
|
+
_pendingValidationResult;
|
|
127
|
+
/** Whether an async validator is still running for this entity - see
|
|
128
|
+
{@link EntityAspect.validateEntityAsync}. __Read Only__ */
|
|
129
|
+
get isValidating() {
|
|
130
|
+
return !!this._asyncChecks && this._asyncChecks.size > 0;
|
|
131
|
+
}
|
|
132
|
+
/** @hidden @internal */
|
|
133
|
+
_entityKey;
|
|
134
|
+
/** @hidden @internal */
|
|
135
|
+
_loadedNps;
|
|
136
|
+
/** @hidden @internal */
|
|
137
|
+
_initialized;
|
|
138
|
+
/** @hidden @internal */
|
|
139
|
+
_inProcess; // used in defaultPropertyInterceptor for temp storage.
|
|
140
|
+
/** @hidden @internal */
|
|
141
|
+
_inProcessEntity; // used in EntityManager
|
|
142
|
+
/** @hidden @internal */
|
|
143
|
+
constructor(entity) {
|
|
144
|
+
// if called without new
|
|
145
|
+
// if (!(this instanceof EntityAspect)) {
|
|
146
|
+
// return new EntityAspect(entity);
|
|
147
|
+
// }
|
|
148
|
+
this.entity = entity;
|
|
149
|
+
// TODO: keep public or not?
|
|
150
|
+
this.entityGroup = undefined;
|
|
151
|
+
this.entityManager = undefined;
|
|
152
|
+
this.entityState = EntityState.Detached;
|
|
153
|
+
this.isBeingSaved = false;
|
|
154
|
+
this.originalValues = {};
|
|
155
|
+
this.hasValidationErrors = false;
|
|
156
|
+
this._validationErrors = {};
|
|
157
|
+
// Uncomment when we implement entityAspect.isNavigationPropertyLoaded method
|
|
158
|
+
// this._loadedNavPropMap = {};
|
|
159
|
+
this.validationErrorsChanged = new BreezeEvent("validationErrorsChanged", this);
|
|
160
|
+
this.propertyChanged = new BreezeEvent("propertyChanged", this);
|
|
161
|
+
// in case this is the NULL entityAspect. - used with ComplexAspects that have no parent.
|
|
162
|
+
if (entity != null) {
|
|
163
|
+
// remove properties that should be on prototype but placed on instance by Babel
|
|
164
|
+
if (entity.hasOwnProperty('entityType')) {
|
|
165
|
+
// throw new Error("Entity instance has entityType property; should only be on prototype");
|
|
166
|
+
delete entity.entityType;
|
|
167
|
+
}
|
|
168
|
+
entity.entityAspect = this;
|
|
169
|
+
// entityType should already be on the entity from 'watch'
|
|
170
|
+
let entityType = entity.entityType || entity._$entityType;
|
|
171
|
+
if (!entityType) {
|
|
172
|
+
let typeName = entity.prototype._$typeName;
|
|
173
|
+
if (!typeName) {
|
|
174
|
+
throw new Error("This entity is not registered as a valid EntityType");
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
throw new Error("Metadata for this entityType has not yet been resolved: " + typeName);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
let entityCtor = entityType.getCtor();
|
|
181
|
+
config.interfaceRegistry.modelLibrary.getDefaultInstance().startTracking(entity, entityCtor.prototype);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/** @hidden */
|
|
185
|
+
// type-guard
|
|
186
|
+
static isEntity(obj) {
|
|
187
|
+
return obj.entityAspect != null;
|
|
188
|
+
}
|
|
189
|
+
// TODO: refactor this and the instance getPropertyValue method.
|
|
190
|
+
/**
|
|
191
|
+
Returns the value of a specified 'property path' for a specified entity.
|
|
192
|
+
|
|
193
|
+
The propertyPath can be either a string delimited with '.' or a string array.
|
|
194
|
+
*/
|
|
195
|
+
// used by EntityQuery and Predicate
|
|
196
|
+
static getPropertyPathValue(obj, propertyPath) {
|
|
197
|
+
let properties = Array.isArray(propertyPath) ? propertyPath : propertyPath.split(".");
|
|
198
|
+
if (properties.length === 1) {
|
|
199
|
+
// properties[0], not propertyPath. Given a single-element array this used to pass the array
|
|
200
|
+
// itself to getProperty, and reached the right property only because a property lookup
|
|
201
|
+
// coerces ['freight'] to 'freight'. Correct by accident, and slower than the real thing.
|
|
202
|
+
return obj.getProperty(properties[0]);
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
let nextValue = obj;
|
|
206
|
+
// hack use of some to perform mapFirst operation.
|
|
207
|
+
properties.some((prop) => {
|
|
208
|
+
nextValue = nextValue.getProperty(prop);
|
|
209
|
+
return nextValue == null;
|
|
210
|
+
});
|
|
211
|
+
return nextValue;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
Returns the {@link EntityKey} for this Entity.
|
|
216
|
+
```ts
|
|
217
|
+
// assume order is an order entity attached to an EntityManager.
|
|
218
|
+
var entityKey = order.entityAspect.getKey();
|
|
219
|
+
```
|
|
220
|
+
@param forceRefresh - (boolean=false) Forces the recalculation of the key. This should normally be unnecessary.
|
|
221
|
+
@returns The {@link EntityKey} associated with this Entity.
|
|
222
|
+
*/
|
|
223
|
+
getKey(forceRefresh = false) {
|
|
224
|
+
// Inline, not assertParam: getKey runs several times per entity created and again on
|
|
225
|
+
// every attach, merge and foreign key lookup. Same wording.
|
|
226
|
+
if (forceRefresh != null && typeof forceRefresh !== 'boolean') {
|
|
227
|
+
throw paramError('forceRefresh', "is optional or it must be a 'boolean'");
|
|
228
|
+
}
|
|
229
|
+
if (forceRefresh || !this._entityKey) {
|
|
230
|
+
let entityType = this.entity.entityType;
|
|
231
|
+
let keyProps = entityType.keyProperties;
|
|
232
|
+
let values = keyProps.map(function (p) {
|
|
233
|
+
return this.entity.getProperty(p.name);
|
|
234
|
+
}, this);
|
|
235
|
+
this._entityKey = new EntityKey(entityType, values);
|
|
236
|
+
}
|
|
237
|
+
return this._entityKey;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
Returns the entity to an {@link EntityState} of 'Unchanged' by committing all changes made since the entity was last queried
|
|
241
|
+
had 'acceptChanges' called on it.
|
|
242
|
+
```ts
|
|
243
|
+
// assume order is an order entity attached to an EntityManager.
|
|
244
|
+
order.entityAspect.acceptChanges();
|
|
245
|
+
// The 'order' entity will now be in an 'Unchanged' state with any changes committed.
|
|
246
|
+
```
|
|
247
|
+
*/
|
|
248
|
+
acceptChanges() {
|
|
249
|
+
if (!this.entity)
|
|
250
|
+
return;
|
|
251
|
+
this._checkOperation("acceptChanges");
|
|
252
|
+
let em = this.entityManager;
|
|
253
|
+
if (this.entityState.isDeleted()) {
|
|
254
|
+
em.detachEntity(this.entity);
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
this.setUnchanged();
|
|
258
|
+
}
|
|
259
|
+
em.entityChanged.publish({ entityAction: EntityAction.AcceptChanges, entity: this.entity });
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
Returns the entity to an {@link EntityState} of 'Unchanged' by rejecting all changes made to it since the entity was last queried
|
|
263
|
+
had 'rejectChanges' called on it.
|
|
264
|
+
```ts
|
|
265
|
+
// assume order is an order entity attached to an EntityManager.
|
|
266
|
+
order.entityAspect.rejectChanges();
|
|
267
|
+
// The 'order' entity will now be in an 'Unchanged' state with any changes rejected.
|
|
268
|
+
```
|
|
269
|
+
*/
|
|
270
|
+
rejectChanges() {
|
|
271
|
+
this._checkOperation("rejectChanges");
|
|
272
|
+
let entity = this.entity;
|
|
273
|
+
let entityManager = this.entityManager;
|
|
274
|
+
// we do not want PropertyChange or EntityChange events to occur here
|
|
275
|
+
core.using(entityManager, "isRejectingChanges", true, function () {
|
|
276
|
+
rejectChangesCore(entity);
|
|
277
|
+
});
|
|
278
|
+
if (this.entityState.isAdded()) {
|
|
279
|
+
// next line is needed because the following line will cause this.entityManager -> null;
|
|
280
|
+
entityManager.detachEntity(entity);
|
|
281
|
+
// need to tell em that an entity that needed to be saved no longer does.
|
|
282
|
+
entityManager._notifyStateChange(entity, false);
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
if (this.entityState.isDeleted()) {
|
|
286
|
+
entityManager._linkRelatedEntities(entity);
|
|
287
|
+
}
|
|
288
|
+
this.setUnchanged();
|
|
289
|
+
// propertyChanged propertyName is not specified because more than one property may have changed.
|
|
290
|
+
this.propertyChanged.publish({ entity: entity, propertyName: null });
|
|
291
|
+
entityManager.entityChanged.publish({ entityAction: EntityAction.RejectChanges, entity: entity });
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
/** @hidden @internal */
|
|
295
|
+
// TODO: rename - and use '_'; used on both EntityAspect and ComplexAspect for polymorphic reasons.
|
|
296
|
+
getPropertyPath(propName) {
|
|
297
|
+
return propName;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
Sets the entity to an EntityState of 'Added'. This is NOT the equivalent of calling {@link EntityManager.addEntity}
|
|
301
|
+
because no key generation will occur for autogenerated keys as a result of this operation. As a result this operation can be problematic
|
|
302
|
+
unless you are certain that the entity being marked 'Added' does not already exist in the database and does not have an autogenerated key.
|
|
303
|
+
The same operation can be performed by calling {@link EntityAspect.setEntityState}.
|
|
304
|
+
```ts
|
|
305
|
+
// assume order is an order entity attached to an EntityManager.
|
|
306
|
+
order.entityAspect.setAdded();
|
|
307
|
+
// The 'order' entity will now be in an 'Added' state.
|
|
308
|
+
```
|
|
309
|
+
*/
|
|
310
|
+
setAdded() {
|
|
311
|
+
return this.setEntityState(EntityState.Added);
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
Sets the entity to an EntityState of 'Unchanged'. This is also the equivalent of calling {@link EntityAspect.acceptChanges}.
|
|
315
|
+
The same operation can be performed by calling {@link EntityAspect.setEntityState}.
|
|
316
|
+
```ts
|
|
317
|
+
// assume order is an order entity attached to an EntityManager.
|
|
318
|
+
order.entityAspect.setUnchanged();
|
|
319
|
+
// The 'order' entity will now be in an 'Unchanged' state with any changes committed.
|
|
320
|
+
```
|
|
321
|
+
*/
|
|
322
|
+
setUnchanged = function () {
|
|
323
|
+
return this.setEntityState(EntityState.Unchanged);
|
|
324
|
+
};
|
|
325
|
+
/**
|
|
326
|
+
Sets the entity to an EntityState of 'Modified'. This can also be achieved by changing the value of any property on an 'Unchanged' entity.
|
|
327
|
+
The same operation can be performed by calling {@link EntityAspect.setEntityState}.
|
|
328
|
+
```ts
|
|
329
|
+
// assume order is an order entity attached to an EntityManager.
|
|
330
|
+
order.entityAspect.setModified();
|
|
331
|
+
// The 'order' entity will now be in a 'Modified' state.
|
|
332
|
+
```
|
|
333
|
+
*/
|
|
334
|
+
setModified = function () {
|
|
335
|
+
return this.setEntityState(EntityState.Modified);
|
|
336
|
+
};
|
|
337
|
+
/**
|
|
338
|
+
Sets the entity to an EntityState of 'Deleted'. This both marks the entity as being scheduled for deletion during the next 'Save' call
|
|
339
|
+
but also removes the entity from all of its related entities. If the current entityState is 'Added', then `setDeleted()` will mark it 'Detached'
|
|
340
|
+
The same operation can be performed by calling {@link EntityAspect.setEntityState}.
|
|
341
|
+
```ts
|
|
342
|
+
// assume order is an order entity attached to an EntityManager.
|
|
343
|
+
order.entityAspect.setDeleted();
|
|
344
|
+
// The 'order' entity will now be in a 'Deleted' state and it will no longer have any 'related' entities.
|
|
345
|
+
```
|
|
346
|
+
*/
|
|
347
|
+
setDeleted = function () {
|
|
348
|
+
return this.setEntityState(EntityState.Deleted);
|
|
349
|
+
};
|
|
350
|
+
/**
|
|
351
|
+
Sets the entity to an EntityState of 'Detached'. This removes the entity from all of its related entities, but does NOT change the EntityState of any existing entities.
|
|
352
|
+
The same operation can be performed by calling {@link EntityAspect.setEntityState}.
|
|
353
|
+
```ts
|
|
354
|
+
// assume order is an order entity attached to an EntityManager.
|
|
355
|
+
order.entityAspect.setDetached();
|
|
356
|
+
// The 'order' entity will now be in a 'Detached' state and it will no longer have any 'related' entities.
|
|
357
|
+
```
|
|
358
|
+
*/
|
|
359
|
+
setDetached = function () {
|
|
360
|
+
return this.setEntityState(EntityState.Detached);
|
|
361
|
+
};
|
|
362
|
+
/**
|
|
363
|
+
Sets the entity to the specified EntityState. See also 'setUnchanged', 'setModified', 'setDetached', etc.
|
|
364
|
+
```ts
|
|
365
|
+
// assume order is an order entity attached to an EntityManager.
|
|
366
|
+
order.entityAspect.setEntityState(EntityState.Unchanged);
|
|
367
|
+
// The 'order' entity will now be in a 'Unchanged' state.
|
|
368
|
+
```
|
|
369
|
+
*/
|
|
370
|
+
setEntityState(entityState) {
|
|
371
|
+
if (this.entityState === entityState)
|
|
372
|
+
return false;
|
|
373
|
+
this._checkOperation("setEntityState");
|
|
374
|
+
if (this.entityState.isDetached()) {
|
|
375
|
+
throw new Error("You cannot set the 'entityState' of an entity when it is detached - except by first attaching it to an EntityManager");
|
|
376
|
+
}
|
|
377
|
+
let entity = this.entity;
|
|
378
|
+
let em = this.entityManager;
|
|
379
|
+
let needsSave = true;
|
|
380
|
+
if (entityState === EntityState.Unchanged) {
|
|
381
|
+
clearOriginalValues(entity);
|
|
382
|
+
this.hasTempKey = undefined; // not delete: every entity that becomes Unchanged runs this
|
|
383
|
+
needsSave = false;
|
|
384
|
+
}
|
|
385
|
+
else if (entityState === EntityState.Added) {
|
|
386
|
+
clearOriginalValues(entity);
|
|
387
|
+
// TODO: more to do here... like regenerating key ???
|
|
388
|
+
}
|
|
389
|
+
else if (entityState === EntityState.Deleted) {
|
|
390
|
+
if (this.entityState.isAdded()) {
|
|
391
|
+
// turn it into a detach and exit early
|
|
392
|
+
this.setEntityState(EntityState.Detached);
|
|
393
|
+
return true;
|
|
394
|
+
}
|
|
395
|
+
else {
|
|
396
|
+
// TODO: think about cascade deletes
|
|
397
|
+
// entityState needs to be set it early in this one case to insure that fk's are not cleared.
|
|
398
|
+
this.entityState = EntityState.Deleted;
|
|
399
|
+
removeFromRelations(entity, EntityState.Deleted);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
else if (entityState === EntityState.Modified) {
|
|
403
|
+
// nothing extra needed
|
|
404
|
+
}
|
|
405
|
+
else if (entityState === EntityState.Detached) {
|
|
406
|
+
let group = this.entityGroup;
|
|
407
|
+
// no group === already detached.
|
|
408
|
+
if (!group)
|
|
409
|
+
return false;
|
|
410
|
+
group.detachEntity(entity);
|
|
411
|
+
// An entity whose foreign keys named parents that are not in the cache is parked in the
|
|
412
|
+
// unattached-children map, waiting for them. Detaching it has to take it back out, or the
|
|
413
|
+
// manager holds it for good: clear() replaces the whole map, but detachEntity does not.
|
|
414
|
+
em._unattachedChildrenMap.removeChild(entity);
|
|
415
|
+
// needs to occur early here - so this IS deliberately redundent with the same code later in this method.
|
|
416
|
+
this.entityState = entityState;
|
|
417
|
+
removeFromRelations(entity, EntityState.Detached);
|
|
418
|
+
this._detach();
|
|
419
|
+
em.entityChanged.publish({ entityAction: EntityAction.Detach, entity: entity });
|
|
420
|
+
needsSave = false;
|
|
421
|
+
}
|
|
422
|
+
this.entityState = entityState;
|
|
423
|
+
em._notifyStateChange(entity, needsSave);
|
|
424
|
+
return true;
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
Performs a query for the value of a specified {@link NavigationProperty}. __Async__
|
|
428
|
+
```ts
|
|
429
|
+
emp.entityAspect.loadNavigationProperty("Orders").then(function (data) {
|
|
430
|
+
var orders = data.results;
|
|
431
|
+
}).catch(function (exception) {
|
|
432
|
+
// handle exception here;
|
|
433
|
+
});
|
|
434
|
+
```
|
|
435
|
+
@param navigationProperty - The NavigationProperty or the name of the NavigationProperty to 'load'.
|
|
436
|
+
@param callback - Deprecated. Function to call on success.
|
|
437
|
+
@param errorCallback - Deprecated. Function to call on failure.
|
|
438
|
+
@returns Promise with shape
|
|
439
|
+
- results {Array of Entity}
|
|
440
|
+
- query {EntityQuery} The original query
|
|
441
|
+
- httpResponse {httpResponse} The HttpResponse returned from the server.
|
|
442
|
+
*/
|
|
443
|
+
loadNavigationProperty(navigationProperty, callback, errorCallback) {
|
|
444
|
+
let entity = this.entity;
|
|
445
|
+
let navProperty = entity.entityType._checkNavProperty(navigationProperty);
|
|
446
|
+
let query = EntityQuery.fromEntityNavigation(entity, navProperty);
|
|
447
|
+
// return entity.entityAspect.entityManager.executeQuery(query, callback, errorCallback);
|
|
448
|
+
let promise = entity.entityAspect.entityManager.executeQuery(query);
|
|
449
|
+
return promise.then((data) => {
|
|
450
|
+
this._markAsLoaded(navProperty.name);
|
|
451
|
+
if (callback)
|
|
452
|
+
callback(data);
|
|
453
|
+
return Promise.resolve(data);
|
|
454
|
+
}, (error) => {
|
|
455
|
+
if (errorCallback)
|
|
456
|
+
errorCallback(error);
|
|
457
|
+
return Promise.reject(error);
|
|
458
|
+
});
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
Marks this navigationProperty on this entity as already having been loaded.
|
|
462
|
+
```ts
|
|
463
|
+
emp.entityAspect.markNavigationPropertyAsLoaded("Orders");
|
|
464
|
+
```
|
|
465
|
+
@param navigationProperty - The NavigationProperty or name of NavigationProperty to 'load'.
|
|
466
|
+
*/
|
|
467
|
+
markNavigationPropertyAsLoaded(navigationProperty) {
|
|
468
|
+
if (!this.entity)
|
|
469
|
+
return;
|
|
470
|
+
let navProperty = this.entity.entityType._checkNavProperty(navigationProperty);
|
|
471
|
+
this._markAsLoaded(navProperty.name);
|
|
472
|
+
}
|
|
473
|
+
/**
|
|
474
|
+
Determines whether a navigationProperty on this entity has already been loaded.
|
|
475
|
+
|
|
476
|
+
A navigation property is considered loaded when any of the following three conditions applies:
|
|
477
|
+
|
|
478
|
+
1. It was fetched from the backend server.
|
|
479
|
+
<br/> This can be the result of an expand query or a call to the {@link EntityAspect.loadNavigationProperty} method.
|
|
480
|
+
<br/> Note that even if the fetch returns nothing the property is still marked as loaded in this case.
|
|
481
|
+
1. The property is scalar and has been set to a nonnull value.
|
|
482
|
+
1. The {@link EntityAspect.markNavigationPropertyAsLoaded} was called.
|
|
483
|
+
|
|
484
|
+
```ts
|
|
485
|
+
var wasLoaded = emp.entityAspect.isNavigationPropertyLoaded("Orders");
|
|
486
|
+
```
|
|
487
|
+
@param navigationProperty - The NavigationProperty or name of NavigationProperty to 'load'.
|
|
488
|
+
*/
|
|
489
|
+
isNavigationPropertyLoaded(navigationProperty) {
|
|
490
|
+
// Both returns below used to hand back `undefined` - a bare `return`, and `this._loadedNps &&`
|
|
491
|
+
// when nothing has been marked yet - while the overload signatures above promise `boolean`.
|
|
492
|
+
// TypeScript does not check an implementation signature against its own overloads, so callers
|
|
493
|
+
// were told `boolean` and could be given `undefined`.
|
|
494
|
+
if (!this.entity)
|
|
495
|
+
return false;
|
|
496
|
+
let navProperty = this.entity.entityType._checkNavProperty(navigationProperty);
|
|
497
|
+
if (navProperty.isScalar && this.entity.getProperty(navProperty.name) != null) {
|
|
498
|
+
return true;
|
|
499
|
+
}
|
|
500
|
+
return !!this._loadedNps && this._loadedNps.indexOf(navProperty.name) >= 0;
|
|
501
|
+
}
|
|
502
|
+
/** @hidden @internal */
|
|
503
|
+
_markAsLoaded(navPropName) {
|
|
504
|
+
this._loadedNps = this._loadedNps || [];
|
|
505
|
+
core.arrayAddItemUnique(this._loadedNps, navPropName);
|
|
506
|
+
}
|
|
507
|
+
/**
|
|
508
|
+
Performs validation on the entity, any errors encountered during the validation are available via the
|
|
509
|
+
{@link EntityAspect.getValidationErrors} method. Validating an entity means executing
|
|
510
|
+
all of the validators on both the entity itself as well as those on each of its properties.
|
|
511
|
+
```ts
|
|
512
|
+
// assume order is an order entity attached to an EntityManager.
|
|
513
|
+
var isOk = order.entityAspect.validateEntity();
|
|
514
|
+
// isOk will be 'true' if there are no errors on the entity.
|
|
515
|
+
if (!isOk) {
|
|
516
|
+
var errors = order.entityAspect.getValidationErrors();
|
|
517
|
+
}
|
|
518
|
+
```
|
|
519
|
+
@returns Whether the entity can be saved: every validator passes, and no error added with
|
|
520
|
+
{@link EntityAspect.addValidationError} remains. Errors from the server are not counted - a save
|
|
521
|
+
clears them before validating, and the server checks again. This is the check `saveChanges`
|
|
522
|
+
makes, so the two always agree.
|
|
523
|
+
*/
|
|
524
|
+
validateEntity() {
|
|
525
|
+
let ok = true;
|
|
526
|
+
this._processValidationOpAndPublish(function (that) {
|
|
527
|
+
ok = validateTarget(that.entity);
|
|
528
|
+
});
|
|
529
|
+
// validateTarget re-runs the validators, so on its own it answers only for the errors
|
|
530
|
+
// validators make. An error added with addValidationError is still an error, and the entity is
|
|
531
|
+
// not valid while it stands - it used to be ignored here, and so saveChanges, which asks this,
|
|
532
|
+
// sent the entity anyway. The server's errors are the exception: every save clears them before
|
|
533
|
+
// validating, because the server checks again, and counting them here would make this answer
|
|
534
|
+
// differ from the one a save gets.
|
|
535
|
+
return ok && !this._hasBlockingErrors();
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
Performs validation on a specific property of this entity, any errors encountered during the validation are available via the
|
|
539
|
+
{@link EntityAspect.getValidationErrors} method. Validating a property means executing
|
|
540
|
+
all of the validators on the specified property. This call is also made automatically anytime a property
|
|
541
|
+
of an entity is changed.
|
|
542
|
+
```ts
|
|
543
|
+
// assume order is an order entity attached to an EntityManager.
|
|
544
|
+
var isOk = order.entityAspect.validateProperty("Order");
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
or
|
|
548
|
+
```ts
|
|
549
|
+
var orderDateProperty = order.entityType.getProperty("OrderDate");
|
|
550
|
+
var isOk = order.entityAspect.validateProperty(OrderDateProperty);
|
|
551
|
+
```
|
|
552
|
+
@param property - The {@link DataProperty} or {@link NavigationProperty} to validate or a string
|
|
553
|
+
with the name of the property or a property path with the path to a property of a complex object.
|
|
554
|
+
@param context - A context object used to pass additional information to each {@link Validator}.
|
|
555
|
+
@returns Whether the property can be saved: its validators pass, and no error added with
|
|
556
|
+
{@link EntityAspect.addValidationError} about it remains. As with {@link EntityAspect.validateEntity},
|
|
557
|
+
errors from the server are not counted.
|
|
558
|
+
*/
|
|
559
|
+
validateProperty(property, context) {
|
|
560
|
+
let value = this.getPropertyValue(property); // performs validations
|
|
561
|
+
// As validateEntity: an error added with addValidationError for this property counts too.
|
|
562
|
+
const propertyName = typeof property === "string" ? property : property.name;
|
|
563
|
+
if (value && value.complexAspect) {
|
|
564
|
+
return validateTarget(value) && !this._hasBlockingErrors(propertyName);
|
|
565
|
+
}
|
|
566
|
+
context = context || {};
|
|
567
|
+
context.entity = this.entity;
|
|
568
|
+
if (typeof property === "string") {
|
|
569
|
+
context.property = this.entity.entityType.getProperty(property, true);
|
|
570
|
+
context.propertyName = property;
|
|
571
|
+
}
|
|
572
|
+
else {
|
|
573
|
+
context.property = property;
|
|
574
|
+
context.propertyName = property.name;
|
|
575
|
+
}
|
|
576
|
+
return this._validateProperty(value, context) && !this._hasBlockingErrors(context.propertyName);
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
Validates the entity as {@link EntityAspect.validateEntity} does, and also runs its async
|
|
580
|
+
validators - see {@link Validator.isAsync} - resolving when they have all answered. This is the
|
|
581
|
+
check `saveChanges` makes when any of the entities it saves has an async validator.
|
|
582
|
+
```ts
|
|
583
|
+
if (!await order.entityAspect.validateEntityAsync()) {
|
|
584
|
+
const errors = order.entityAspect.getValidationErrors();
|
|
585
|
+
}
|
|
586
|
+
```
|
|
587
|
+
While it runs, {@link EntityAspect.isValidating} is true. If a property changes while one of its
|
|
588
|
+
checks runs, the check is run again on the new value.
|
|
589
|
+
@returns Whether the entity can be saved, as {@link EntityAspect.validateEntity} answers, once the
|
|
590
|
+
async validators' answers are in.
|
|
591
|
+
*/
|
|
592
|
+
async validateEntityAsync() {
|
|
593
|
+
this.validateEntity();
|
|
594
|
+
await runAsyncChecks(this, collectAsyncChecks(this.entity));
|
|
595
|
+
return !this._hasBlockingErrors();
|
|
596
|
+
}
|
|
597
|
+
/**
|
|
598
|
+
Validates one property as {@link EntityAspect.validateProperty} does, and also runs its async
|
|
599
|
+
validators, resolving when they have all answered.
|
|
600
|
+
```ts
|
|
601
|
+
const isOk = await customer.entityAspect.validatePropertyAsync("companyName");
|
|
602
|
+
```
|
|
603
|
+
@param property - The property, by {@link DataProperty}, {@link NavigationProperty}, name, or path
|
|
604
|
+
to a property of a complex object.
|
|
605
|
+
@param context - Additional context for each {@link Validator}.
|
|
606
|
+
@returns Whether the property can be saved, once the async validators' answers are in.
|
|
607
|
+
*/
|
|
608
|
+
async validatePropertyAsync(property, context) {
|
|
609
|
+
this.validateProperty(property, context && { ...context });
|
|
610
|
+
await runAsyncChecks(this, collectAsyncPropertyChecks(this, property, context));
|
|
611
|
+
return !this._hasBlockingErrors(typeof property === "string" ? property : property.name);
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
Returns the validation errors associated with either the entire entity or any specified property.
|
|
615
|
+
|
|
616
|
+
This method can return all of the errors for an Entity
|
|
617
|
+
```ts
|
|
618
|
+
// assume order is an order entity attached to an EntityManager.
|
|
619
|
+
var valErrors = order.entityAspect.getValidationErrors();
|
|
620
|
+
```
|
|
621
|
+
|
|
622
|
+
as well as those for just a specific property.
|
|
623
|
+
```ts
|
|
624
|
+
// assume order is an order entity attached to an EntityManager.
|
|
625
|
+
var orderDateErrors = order.entityAspect.getValidationErrors("OrderDate");
|
|
626
|
+
```
|
|
627
|
+
|
|
628
|
+
which can also be expressed as
|
|
629
|
+
```ts
|
|
630
|
+
// assume order is an order entity attached to an EntityManager.
|
|
631
|
+
var orderDateProperty = order.entityType.getProperty("OrderDate");
|
|
632
|
+
var orderDateErrors = order.entityAspect.getValidationErrors(orderDateProperty);
|
|
633
|
+
```
|
|
634
|
+
@param property - The property for which validation errors should be retrieved.
|
|
635
|
+
If omitted, all of the validation errors for this entity will be returned.
|
|
636
|
+
@returns A array of validation errors.
|
|
637
|
+
*/
|
|
638
|
+
getValidationErrors(property) {
|
|
639
|
+
assertParam(property, "property").isOptional().isEntityProperty().or().isString().check();
|
|
640
|
+
let result = core.getOwnPropertyValues(this._validationErrors);
|
|
641
|
+
if (property) {
|
|
642
|
+
let propertyName = typeof (property) === 'string' ? property : property.name;
|
|
643
|
+
// By propertyName as well as by the property object: an error added with only a
|
|
644
|
+
// propertyName in its context - as the validation guide shows - has no property object, and
|
|
645
|
+
// was missing from this list while validateProperty reported the property invalid because
|
|
646
|
+
// of it. A form asking "why is this field wrong?" got an empty answer.
|
|
647
|
+
result = result.filter(function (ve) {
|
|
648
|
+
return (ve.property && ve.property.name === propertyName) || ve.propertyName === propertyName;
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
return result;
|
|
652
|
+
}
|
|
653
|
+
/**
|
|
654
|
+
The value a property had before the entity's pending changes: its original value if it has been
|
|
655
|
+
edited since the entity was last saved or accepted, and its current value if not. So it answers
|
|
656
|
+
"what was this?" without first asking whether it changed.
|
|
657
|
+
```ts
|
|
658
|
+
order.freight = 99;
|
|
659
|
+
order.entityAspect.getOriginalValue('freight'); // the freight before the edit
|
|
660
|
+
order.entityAspect.getOriginalValue('shipCity'); // unchanged, so its current value
|
|
661
|
+
```
|
|
662
|
+
Also takes a path into a complex property, such as `'location.city'`.
|
|
663
|
+
@param propertyName - A data property of this entity, or a path to one in a complex property.
|
|
664
|
+
*/
|
|
665
|
+
getOriginalValue(propertyName) {
|
|
666
|
+
let target = this.entity;
|
|
667
|
+
let name = propertyName;
|
|
668
|
+
const dot = name.lastIndexOf('.');
|
|
669
|
+
if (dot >= 0) {
|
|
670
|
+
target = this.getPropertyValue(name.slice(0, dot));
|
|
671
|
+
name = name.slice(dot + 1);
|
|
672
|
+
}
|
|
673
|
+
const originals = (target.entityAspect || target.complexAspect).originalValues;
|
|
674
|
+
return Object.prototype.hasOwnProperty.call(originals, name) ? originals[name] : target.getProperty(name);
|
|
675
|
+
}
|
|
676
|
+
/**
|
|
677
|
+
The data properties whose value now differs from the one the entity was last saved or accepted
|
|
678
|
+
with - as paths, such as `'location.city'`, for those of a complex property, and by name for an
|
|
679
|
+
array property whose contents changed. A property edited and then set back is not included,
|
|
680
|
+
although it stays in {@link EntityAspect.originalValues}. Dates are compared by time.
|
|
681
|
+
```ts
|
|
682
|
+
order.freight = 99;
|
|
683
|
+
order.shipCity = order.shipCity; // set, but to the same value
|
|
684
|
+
order.entityAspect.getChangedProperties(); // ['freight']
|
|
685
|
+
```
|
|
686
|
+
Always empty for an `Added` entity, which has no original values to differ from.
|
|
687
|
+
*/
|
|
688
|
+
getChangedProperties() {
|
|
689
|
+
return this.entity ? changedPropertyPaths(this.entity, this.originalValues, '') : [];
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
Adds a validation error.
|
|
693
|
+
|
|
694
|
+
An error added here stops the entity being saved - `validateEntity` returns false and
|
|
695
|
+
`saveChanges` rejects - until it is removed with {@link EntityAspect.removeValidationError} or
|
|
696
|
+
{@link EntityAspect.clearValidationErrors}. Editing the property does not remove it: Breeze
|
|
697
|
+
cannot re-check a rule it did not run. Give the error a key, and a failed save names it by that
|
|
698
|
+
key in `errorName`.
|
|
699
|
+
*/
|
|
700
|
+
addValidationError(validationError) {
|
|
701
|
+
assertParam(validationError, "validationError").isInstanceOf(ValidationError).check();
|
|
702
|
+
this._processValidationOpAndPublish(function (that) {
|
|
703
|
+
that._addValidationError(validationError);
|
|
704
|
+
});
|
|
705
|
+
}
|
|
706
|
+
/**
|
|
707
|
+
Removes a validation error.
|
|
708
|
+
@param validationErrorOrKey - A ValidationError, a ValidationError 'key' value, or a Validator -
|
|
709
|
+
in which case every error that validator produced on this entity is removed.
|
|
710
|
+
*/
|
|
711
|
+
removeValidationError(validationErrorOrKey) {
|
|
712
|
+
assertParam(validationErrorOrKey, "validationErrorOrKey").isString().or().isInstanceOf(ValidationError).or().isInstanceOf(Validator).check();
|
|
713
|
+
let keys;
|
|
714
|
+
if (typeof (validationErrorOrKey) === "string") {
|
|
715
|
+
keys = [validationErrorOrKey];
|
|
716
|
+
}
|
|
717
|
+
else if (validationErrorOrKey instanceof Validator) {
|
|
718
|
+
// A Validator has no key of its own - reading one used to remove nothing, silently.
|
|
719
|
+
const errors = this._validationErrors;
|
|
720
|
+
keys = Object.keys(errors).filter(k => errors[k]?.validator === validationErrorOrKey);
|
|
721
|
+
}
|
|
722
|
+
else {
|
|
723
|
+
keys = [validationErrorOrKey.key];
|
|
724
|
+
}
|
|
725
|
+
this._processValidationOpAndPublish(function (that) {
|
|
726
|
+
keys.forEach(key => that._removeValidationError(key));
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
/**
|
|
730
|
+
Removes all of the validation errors for a specified entity
|
|
731
|
+
*/
|
|
732
|
+
clearValidationErrors() {
|
|
733
|
+
this._processValidationOpAndPublish(function (that) {
|
|
734
|
+
core.objectForEach(that._validationErrors, function (key, valError) {
|
|
735
|
+
if (valError) {
|
|
736
|
+
delete that._validationErrors[key];
|
|
737
|
+
that._pendingValidationResult.removed.push(valError);
|
|
738
|
+
}
|
|
739
|
+
});
|
|
740
|
+
that.hasValidationErrors = !core.isEmpty(that._validationErrors);
|
|
741
|
+
});
|
|
742
|
+
}
|
|
743
|
+
/**
|
|
744
|
+
Returns an {@link EntityKey} for the entity pointed to by the specified scalar NavigationProperty.
|
|
745
|
+
This only returns an EntityKey if the current entity is a 'child' entity along the specified NavigationProperty.
|
|
746
|
+
i.e. has a single parent.
|
|
747
|
+
|
|
748
|
+
@param navigationProperty - The {@link NavigationProperty} ( pointing to a parent).
|
|
749
|
+
@returns Either a parent EntityKey if this is a 'child' entity or null;
|
|
750
|
+
*/
|
|
751
|
+
getParentKey(navigationProperty) {
|
|
752
|
+
if (!this.entity)
|
|
753
|
+
return null;
|
|
754
|
+
let fkNames = navigationProperty.foreignKeyNames;
|
|
755
|
+
if (fkNames.length === 0)
|
|
756
|
+
return null;
|
|
757
|
+
let that = this;
|
|
758
|
+
let fkValues = fkNames.map(function (fkn) {
|
|
759
|
+
return that.entity.getProperty(fkn);
|
|
760
|
+
});
|
|
761
|
+
return new EntityKey(navigationProperty.entityType, fkValues);
|
|
762
|
+
}
|
|
763
|
+
// TODO: refactor this and the static getPropertyPathValue.
|
|
764
|
+
/**
|
|
765
|
+
Returns the value of a specified DataProperty or NavigationProperty or 'property path'.
|
|
766
|
+
*/
|
|
767
|
+
getPropertyValue(property) {
|
|
768
|
+
assertParam(property, "property").isString().or().isEntityProperty().check();
|
|
769
|
+
let value;
|
|
770
|
+
if (typeof (property) === 'string') {
|
|
771
|
+
let propNames = property.trim().split(".");
|
|
772
|
+
let propName = propNames.shift();
|
|
773
|
+
value = this.entity;
|
|
774
|
+
value = value.getProperty(propName);
|
|
775
|
+
while (propNames.length > 0) {
|
|
776
|
+
propName = propNames.shift();
|
|
777
|
+
value = value.getProperty(propName);
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
else {
|
|
781
|
+
if (!(property.parentType instanceof EntityType)) {
|
|
782
|
+
throw new Error("The validateProperty method does not accept a 'property' parameter whose parentType is a ComplexType; " +
|
|
783
|
+
"Pass a 'property path' string as the 'property' parameter instead ");
|
|
784
|
+
}
|
|
785
|
+
value = this.entity.getProperty(property.name);
|
|
786
|
+
}
|
|
787
|
+
return value;
|
|
788
|
+
}
|
|
789
|
+
// internal methods
|
|
790
|
+
/** @hidden @internal */
|
|
791
|
+
_checkOperation(operationName) {
|
|
792
|
+
if (this.isBeingSaved) {
|
|
793
|
+
throw new Error("Cannot perform a '" + operationName + "' on an entity that is in the process of being saved");
|
|
794
|
+
}
|
|
795
|
+
// allows chaining
|
|
796
|
+
return this;
|
|
797
|
+
}
|
|
798
|
+
/** @hidden @internal */
|
|
799
|
+
_detach() {
|
|
800
|
+
this.entityGroup = undefined;
|
|
801
|
+
this.entityManager = undefined;
|
|
802
|
+
this.entityState = EntityState.Detached;
|
|
803
|
+
this.originalValues = {};
|
|
804
|
+
this._validationErrors = {};
|
|
805
|
+
this.hasValidationErrors = false;
|
|
806
|
+
this.validationErrorsChanged.clear();
|
|
807
|
+
this.propertyChanged.clear();
|
|
808
|
+
if (this._asyncChecks) {
|
|
809
|
+
this._asyncChecks.forEach(run => run.controller.abort());
|
|
810
|
+
this._asyncChecks = undefined;
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
// called from defaultInterceptor.
|
|
814
|
+
/** @hidden @internal */
|
|
815
|
+
_validateProperty(value, context) {
|
|
816
|
+
let ok = true;
|
|
817
|
+
this._processValidationOpAndPublish(function (that) {
|
|
818
|
+
context.property.getAllValidators().forEach(function (validator) {
|
|
819
|
+
ok = validate(that, validator, value, context) && ok;
|
|
820
|
+
});
|
|
821
|
+
});
|
|
822
|
+
return ok;
|
|
823
|
+
}
|
|
824
|
+
/** @hidden @internal */
|
|
825
|
+
_processValidationOpAndPublish(validationFn) {
|
|
826
|
+
if (this._pendingValidationResult) {
|
|
827
|
+
// only top level processValidations call publishes
|
|
828
|
+
validationFn(this);
|
|
829
|
+
}
|
|
830
|
+
else {
|
|
831
|
+
try {
|
|
832
|
+
this._pendingValidationResult = { entity: this.entity, added: [], removed: [] };
|
|
833
|
+
validationFn(this);
|
|
834
|
+
if (this._pendingValidationResult.added.length > 0 || this._pendingValidationResult.removed.length > 0) {
|
|
835
|
+
this.validationErrorsChanged.publish(this._pendingValidationResult);
|
|
836
|
+
// this might be a detached entity hence the guard below.
|
|
837
|
+
this.entityManager && this.entityManager.validationErrorsChanged.publish(this._pendingValidationResult);
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
finally {
|
|
841
|
+
this._pendingValidationResult = undefined;
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
/** @hidden @internal */
|
|
846
|
+
// Whether an error stands that should stop this entity being saved - every error except the
|
|
847
|
+
// server's, which a save clears before validating. With a property name, only errors about that
|
|
848
|
+
// property, or about a property inside it when it is a complex property.
|
|
849
|
+
_hasBlockingErrors(propertyName) {
|
|
850
|
+
if (!this.hasValidationErrors)
|
|
851
|
+
return false;
|
|
852
|
+
const errors = this._validationErrors;
|
|
853
|
+
for (const key in errors) {
|
|
854
|
+
const ve = errors[key];
|
|
855
|
+
if (!ve || ve.isServerError)
|
|
856
|
+
continue;
|
|
857
|
+
if (propertyName == null || isAbout(ve, propertyName))
|
|
858
|
+
return true;
|
|
859
|
+
}
|
|
860
|
+
return false;
|
|
861
|
+
}
|
|
862
|
+
/** @hidden @internal */
|
|
863
|
+
// Drops the errors about a property that its edit has made stale: the server's, and those of
|
|
864
|
+
// async validators, which cannot run again until the next save or validateEntityAsync. Both were
|
|
865
|
+
// about a value the entity no longer has. Runs on every edit that is not part of a load, so the
|
|
866
|
+
// entity with no errors - nearly all of them - pays one flag check.
|
|
867
|
+
_clearStaleErrors(propertyName) {
|
|
868
|
+
if (!this.hasValidationErrors)
|
|
869
|
+
return;
|
|
870
|
+
const keys = [];
|
|
871
|
+
const errors = this._validationErrors;
|
|
872
|
+
for (const key in errors) {
|
|
873
|
+
const ve = errors[key];
|
|
874
|
+
if (ve && (ve.isServerError || ve.validator?.isAsync) && isAbout(ve, propertyName))
|
|
875
|
+
keys.push(key);
|
|
876
|
+
}
|
|
877
|
+
if (keys.length === 0)
|
|
878
|
+
return;
|
|
879
|
+
this._processValidationOpAndPublish(function (that) {
|
|
880
|
+
keys.forEach(key => that._removeValidationError(key));
|
|
881
|
+
});
|
|
882
|
+
}
|
|
883
|
+
/** @hidden @internal */
|
|
884
|
+
_addValidationError(validationError) {
|
|
885
|
+
this._validationErrors[validationError.key] = validationError;
|
|
886
|
+
this.hasValidationErrors = true;
|
|
887
|
+
this._pendingValidationResult.added.push(validationError);
|
|
888
|
+
}
|
|
889
|
+
/** @hidden @internal */
|
|
890
|
+
_removeValidationError(key) {
|
|
891
|
+
let valError = this._validationErrors[key];
|
|
892
|
+
if (valError) {
|
|
893
|
+
delete this._validationErrors[key];
|
|
894
|
+
this.hasValidationErrors = !core.isEmpty(this._validationErrors);
|
|
895
|
+
this._pendingValidationResult.removed.push(valError);
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
BreezeEvent.bubbleEvent(EntityAspect.prototype, function () {
|
|
900
|
+
return this.entityManager;
|
|
901
|
+
});
|
|
902
|
+
function rejectChangesCore(target) {
|
|
903
|
+
let aspect = target.entityAspect || target.complexAspect;
|
|
904
|
+
let stype = target.entityType || target.complexType;
|
|
905
|
+
let originalValues = aspect.originalValues;
|
|
906
|
+
for (let propName in originalValues) {
|
|
907
|
+
target.setProperty(propName, originalValues[propName]);
|
|
908
|
+
}
|
|
909
|
+
stype.complexProperties.forEach(function (cp) {
|
|
910
|
+
let cos = target.getProperty(cp.name);
|
|
911
|
+
if (cp.isScalar) {
|
|
912
|
+
rejectChangesCore(cos);
|
|
913
|
+
}
|
|
914
|
+
else {
|
|
915
|
+
observableArray.rejectChanges(cos);
|
|
916
|
+
cos.forEach(rejectChangesCore);
|
|
917
|
+
}
|
|
918
|
+
});
|
|
919
|
+
}
|
|
920
|
+
/**
|
|
921
|
+
* The value of a property as stored, without creating anything. A collection navigation is built
|
|
922
|
+
* on first read, so code that only wants to look at *existing* related entities - attaching,
|
|
923
|
+
* deleting - asks with this. Falls back to getProperty for a model library that cannot peek,
|
|
924
|
+
* which just means the collection is created as it was before.
|
|
925
|
+
* @hidden @internal
|
|
926
|
+
*/
|
|
927
|
+
export function peekProperty(entity, propertyName) {
|
|
928
|
+
const modelLibrary = config.interfaceRegistry.modelLibrary.getDefaultInstance();
|
|
929
|
+
return modelLibrary.peekProperty
|
|
930
|
+
? modelLibrary.peekProperty(entity, propertyName)
|
|
931
|
+
: entity.getProperty(propertyName);
|
|
932
|
+
}
|
|
933
|
+
function removeFromRelations(entity, entityState) {
|
|
934
|
+
// remove this entity from any collections.
|
|
935
|
+
// mark the entity deleted or detached
|
|
936
|
+
let isDeleted = entityState.isDeleted();
|
|
937
|
+
if (isDeleted) {
|
|
938
|
+
removeFromRelationsCore(entity);
|
|
939
|
+
}
|
|
940
|
+
else {
|
|
941
|
+
core.using(entity.entityAspect.entityManager, "isLoading", true, function () {
|
|
942
|
+
removeFromRelationsCore(entity);
|
|
943
|
+
});
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
function removeFromRelationsCore(entity) {
|
|
947
|
+
entity.entityType.navigationProperties.forEach(function (np) {
|
|
948
|
+
let inverseNp = np.inverse;
|
|
949
|
+
// A collection that has never been read holds no related entities, and reading it here would
|
|
950
|
+
// create an array only to empty it.
|
|
951
|
+
let npValue = np.isScalar ? entity.getProperty(np.name) : peekProperty(entity, np.name);
|
|
952
|
+
if (np.isScalar) {
|
|
953
|
+
if (npValue) {
|
|
954
|
+
if (inverseNp) {
|
|
955
|
+
if (inverseNp.isScalar) {
|
|
956
|
+
npValue.setProperty(inverseNp.name, null);
|
|
957
|
+
}
|
|
958
|
+
else {
|
|
959
|
+
let collection = npValue.getProperty(inverseNp.name);
|
|
960
|
+
if (collection.length) {
|
|
961
|
+
core.arrayRemoveItem(collection, entity);
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
entity.setProperty(np.name, null);
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
else {
|
|
969
|
+
if (npValue == null)
|
|
970
|
+
return;
|
|
971
|
+
// Empty it in one go rather than letting each child splice itself out, which is O(n) per
|
|
972
|
+
// child and one event per child. clearAll nulls each child's reference to this entity
|
|
973
|
+
// first - so the child still gets its navigation property and foreign key cleared - then
|
|
974
|
+
// truncates and publishes a single arrayChanged. A many-to-many inverse has nothing to
|
|
975
|
+
// null on the other side, so it is emptied with no per-child step. (TODO: many to many.)
|
|
976
|
+
const clearChild = inverseNp != null && inverseNp.isScalar
|
|
977
|
+
? (v) => v.setProperty(inverseNp.name, null)
|
|
978
|
+
: undefined;
|
|
979
|
+
observableArray.clearAll(npValue, clearChild);
|
|
980
|
+
}
|
|
981
|
+
});
|
|
982
|
+
}
|
|
983
|
+
// note entityAspect only - ( no complex aspect allowed on the call).
|
|
984
|
+
/** Whether a validation error concerns a property: that property, or one inside it by path. */
|
|
985
|
+
function isAbout(ve, propertyName) {
|
|
986
|
+
const name = ve.propertyName;
|
|
987
|
+
return name === propertyName || (name != null && name.startsWith(propertyName + '.'));
|
|
988
|
+
}
|
|
989
|
+
function validate(entityAspect, validator, value, context) {
|
|
990
|
+
// Nothing here can wait for an async validator's answer, so it is not asked; its last settled
|
|
991
|
+
// answer stands. It runs from validateEntityAsync, and when the entity is saved.
|
|
992
|
+
if (validator.isAsync)
|
|
993
|
+
return true;
|
|
994
|
+
let ve = validator.validate(value, context);
|
|
995
|
+
if (ve) {
|
|
996
|
+
entityAspect._addValidationError(ve);
|
|
997
|
+
return false;
|
|
998
|
+
}
|
|
999
|
+
else {
|
|
1000
|
+
let key = ValidationError.getKey(validator, context ? context.propertyName : null);
|
|
1001
|
+
entityAspect._removeValidationError(key);
|
|
1002
|
+
return true;
|
|
1003
|
+
}
|
|
1004
|
+
}
|
|
1005
|
+
/** How many times a check is run again because its value keeps changing while it is checked. */
|
|
1006
|
+
const MAX_ASYNC_ROUNDS = 3;
|
|
1007
|
+
/** The async validators of an entity or complex object, walked as validateTarget walks the sync ones. */
|
|
1008
|
+
function collectAsyncChecks(target, coIndex) {
|
|
1009
|
+
const checks = [];
|
|
1010
|
+
const stype = target.entityType || target.complexType;
|
|
1011
|
+
const aspect = target.entityAspect || target.complexAspect;
|
|
1012
|
+
const entityAspect = target.entityAspect || target.complexAspect.getEntityAspect();
|
|
1013
|
+
stype.getProperties().forEach((p) => {
|
|
1014
|
+
const read = () => p.isNavigationProperty && !p.isScalar ? peekProperty(target, p.name) : target.getProperty(p.name);
|
|
1015
|
+
const value = read();
|
|
1016
|
+
const asyncValidators = p.getAllValidators().filter((v) => v.isAsync);
|
|
1017
|
+
if (asyncValidators.length > 0) {
|
|
1018
|
+
// A context of its own for each property: validateTarget reuses one, which an async check,
|
|
1019
|
+
// still reading it later, would see changed underneath it.
|
|
1020
|
+
const context = { entity: entityAspect.entity, property: p, propertyName: aspect.getPropertyPath(p.name) };
|
|
1021
|
+
if (coIndex !== undefined)
|
|
1022
|
+
context.index = coIndex;
|
|
1023
|
+
asyncValidators.forEach((validator) => checks.push({ validator, value, context, read }));
|
|
1024
|
+
}
|
|
1025
|
+
if (p.isComplexProperty) {
|
|
1026
|
+
if (p.isScalar) {
|
|
1027
|
+
checks.push(...collectAsyncChecks(value));
|
|
1028
|
+
}
|
|
1029
|
+
else {
|
|
1030
|
+
value.forEach((co, ix) => checks.push(...collectAsyncChecks(co, ix)));
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
});
|
|
1034
|
+
stype.getAllValidators().filter((v) => v.isAsync).forEach((validator) => {
|
|
1035
|
+
checks.push({ validator, value: target, read: () => target });
|
|
1036
|
+
});
|
|
1037
|
+
return checks;
|
|
1038
|
+
}
|
|
1039
|
+
/** The async validators of one property, found as validateProperty finds the sync ones. */
|
|
1040
|
+
function collectAsyncPropertyChecks(aspect, property, context) {
|
|
1041
|
+
const read = () => aspect.getPropertyValue(property);
|
|
1042
|
+
const value = read();
|
|
1043
|
+
if (value && value.complexAspect)
|
|
1044
|
+
return collectAsyncChecks(value);
|
|
1045
|
+
const prop = typeof property === "string" ? aspect.entity.entityType.getProperty(property, true) : property; // throws if not found
|
|
1046
|
+
const propertyName = typeof property === "string" ? property : property.name;
|
|
1047
|
+
const checkContext = { ...context, entity: aspect.entity, property: prop, propertyName };
|
|
1048
|
+
return prop.getAllValidators().filter((v) => v.isAsync)
|
|
1049
|
+
.map((validator) => ({ validator, value, context: checkContext, read }));
|
|
1050
|
+
}
|
|
1051
|
+
function runAsyncChecks(aspect, checks) {
|
|
1052
|
+
return Promise.all(checks.map(check => runAsyncCheck(aspect, check, 1)));
|
|
1053
|
+
}
|
|
1054
|
+
/**
|
|
1055
|
+
Runs one async check and applies its answer, unless the answer is no longer wanted: a newer run of
|
|
1056
|
+
the same check has started - whose answer is then the one waited for - or the entity was detached.
|
|
1057
|
+
If the value changed while it was checked, the check runs again on the new value.
|
|
1058
|
+
*/
|
|
1059
|
+
function runAsyncCheck(aspect, check, round) {
|
|
1060
|
+
const key = ValidationError.getKey(check.validator, check.context && check.context.propertyName);
|
|
1061
|
+
const runs = aspect._asyncChecks || (aspect._asyncChecks = new Map());
|
|
1062
|
+
runs.get(key)?.controller.abort();
|
|
1063
|
+
const controller = new AbortController();
|
|
1064
|
+
const run = { controller };
|
|
1065
|
+
runs.set(key, run);
|
|
1066
|
+
run.done = check.validator.validateAsync(check.value, { ...check.context, signal: controller.signal }).then(ve => {
|
|
1067
|
+
const current = aspect._asyncChecks?.get(key);
|
|
1068
|
+
if (current !== run)
|
|
1069
|
+
return current?.done; // replaced, or detached
|
|
1070
|
+
aspect._asyncChecks.delete(key);
|
|
1071
|
+
const valueNow = check.read();
|
|
1072
|
+
if (valueNow !== check.value) {
|
|
1073
|
+
return round < MAX_ASYNC_ROUNDS ? runAsyncCheck(aspect, { ...check, value: valueNow }, round + 1) : undefined;
|
|
1074
|
+
}
|
|
1075
|
+
aspect._processValidationOpAndPublish((that) => {
|
|
1076
|
+
if (ve) {
|
|
1077
|
+
that._addValidationError(ve);
|
|
1078
|
+
}
|
|
1079
|
+
else {
|
|
1080
|
+
that._removeValidationError(key);
|
|
1081
|
+
}
|
|
1082
|
+
});
|
|
1083
|
+
});
|
|
1084
|
+
return run.done;
|
|
1085
|
+
}
|
|
1086
|
+
/** The paths of target's data properties whose value differs from its original one. */
|
|
1087
|
+
function changedPropertyPaths(target, originalValues, prefix) {
|
|
1088
|
+
const stype = target.entityType || target.complexType;
|
|
1089
|
+
const paths = [];
|
|
1090
|
+
stype.dataProperties.forEach((dp) => {
|
|
1091
|
+
const path = prefix + dp.name;
|
|
1092
|
+
if (dp.isComplexProperty) {
|
|
1093
|
+
const value = target.getProperty(dp.name);
|
|
1094
|
+
if (dp.isScalar) {
|
|
1095
|
+
if (value)
|
|
1096
|
+
paths.push(...changedPropertyPaths(value, value.complexAspect.originalValues, path + '.'));
|
|
1097
|
+
}
|
|
1098
|
+
else if (arrayContentsChanged(value) ||
|
|
1099
|
+
(value || []).some((co) => changedPropertyPaths(co, co.complexAspect.originalValues, '').length > 0)) {
|
|
1100
|
+
paths.push(path);
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
else if (!dp.isScalar) {
|
|
1104
|
+
if (arrayContentsChanged(target.getProperty(dp.name)))
|
|
1105
|
+
paths.push(path);
|
|
1106
|
+
}
|
|
1107
|
+
else if (Object.prototype.hasOwnProperty.call(originalValues, dp.name)) {
|
|
1108
|
+
const comparable = DataType.getComparableFn(dp.dataType);
|
|
1109
|
+
if (comparable(originalValues[dp.name]) !== comparable(target.getProperty(dp.name)))
|
|
1110
|
+
paths.push(path);
|
|
1111
|
+
}
|
|
1112
|
+
});
|
|
1113
|
+
return paths;
|
|
1114
|
+
}
|
|
1115
|
+
/** Whether an array property's contents differ from those it had before its entity was changed. */
|
|
1116
|
+
function arrayContentsChanged(arr) {
|
|
1117
|
+
const original = observableArray.originalContents(arr);
|
|
1118
|
+
if (!original)
|
|
1119
|
+
return false;
|
|
1120
|
+
return original.length !== arr.length || original.some((item, ix) => item !== arr[ix]);
|
|
1121
|
+
}
|
|
1122
|
+
// coIndex is only used where target is a complex object that is part of an array of complex objects
|
|
1123
|
+
// in which case ctIndex is the index of the target within the array.
|
|
1124
|
+
function validateTarget(target, coIndex) {
|
|
1125
|
+
let ok = true;
|
|
1126
|
+
let stype = target.entityType || target.complexType;
|
|
1127
|
+
let aspect = target.entityAspect || target.complexAspect;
|
|
1128
|
+
let entityAspect = target.entityAspect || target.complexAspect.getEntityAspect();
|
|
1129
|
+
let context = { entity: entityAspect.entity };
|
|
1130
|
+
if (coIndex !== undefined) {
|
|
1131
|
+
context.index = coIndex;
|
|
1132
|
+
}
|
|
1133
|
+
stype.getProperties().forEach(function (p) {
|
|
1134
|
+
let value = p.isNavigationProperty && !p.isScalar
|
|
1135
|
+
? peekProperty(target, p.name)
|
|
1136
|
+
: target.getProperty(p.name);
|
|
1137
|
+
let validators = p.getAllValidators();
|
|
1138
|
+
if (validators.length > 0) {
|
|
1139
|
+
context.property = p;
|
|
1140
|
+
context.propertyName = aspect.getPropertyPath(p.name);
|
|
1141
|
+
ok = entityAspect._validateProperty(value, context) && ok;
|
|
1142
|
+
}
|
|
1143
|
+
if (p.isComplexProperty) {
|
|
1144
|
+
if (p.isScalar) {
|
|
1145
|
+
ok = validateTarget(value) && ok;
|
|
1146
|
+
}
|
|
1147
|
+
else {
|
|
1148
|
+
ok = value.reduce(function (pv, cv, ix) {
|
|
1149
|
+
return validateTarget(cv, ix) && pv;
|
|
1150
|
+
}, ok);
|
|
1151
|
+
}
|
|
1152
|
+
}
|
|
1153
|
+
});
|
|
1154
|
+
// then target level
|
|
1155
|
+
stype.getAllValidators().forEach(function (validator) {
|
|
1156
|
+
ok = validate(entityAspect, validator, target) && ok;
|
|
1157
|
+
});
|
|
1158
|
+
return ok;
|
|
1159
|
+
}
|
|
1160
|
+
/**
|
|
1161
|
+
An ComplexAspect instance is associated with every complex object instance and is accessed via the complex object's 'complexAspect' property.
|
|
1162
|
+
|
|
1163
|
+
The ComplexAspect itself provides properties to determine the parent object, parent property and original values for the complex object.
|
|
1164
|
+
|
|
1165
|
+
A ComplexAspect will almost never need to be constructed directly. You will usually get an ComplexAspect by accessing
|
|
1166
|
+
an entities 'complexAspect' property. This property will be automatically attached when an complex object is created as part of an
|
|
1167
|
+
entity via either a query, import or EntityManager.createEntity call.
|
|
1168
|
+
```ts
|
|
1169
|
+
// assume address is a complex property on the 'Customer' type
|
|
1170
|
+
var aspect = aCustomer.address.complexAspect;
|
|
1171
|
+
// aCustomer === aspect.parent;
|
|
1172
|
+
```
|
|
1173
|
+
*/
|
|
1174
|
+
export class ComplexAspect {
|
|
1175
|
+
/** The complex object that this aspect is associated with. __Read Only__ */
|
|
1176
|
+
complexObject;
|
|
1177
|
+
/** The 'original values' of this complex object where they are different from the 'current values'.
|
|
1178
|
+
This is a map where the key is a property name and the value is the 'original value' of the property.
|
|
1179
|
+
__Read Only__ */
|
|
1180
|
+
originalValues;
|
|
1181
|
+
/** The parent object that to which this aspect belongs; this will either be an entity or another complex object. __Read Only__ */
|
|
1182
|
+
parent;
|
|
1183
|
+
/** The {@link DataProperty} on the 'parent' that contains this complex object. __Read Only__ */
|
|
1184
|
+
parentProperty;
|
|
1185
|
+
/** Unlike {@link EntityAspect.extraMetadata}, Breeze does not fill this in and does not export it.
|
|
1186
|
+
An application may use it to hold its own data about the complex object. */
|
|
1187
|
+
extraMetadata;
|
|
1188
|
+
/** You will rarely, if ever, create a ComplexAspect directly. */
|
|
1189
|
+
constructor(complexObject, parent, parentProperty) {
|
|
1190
|
+
if (!complexObject) {
|
|
1191
|
+
throw new Error("The ComplexAspect ctor requires an entity as its only argument.");
|
|
1192
|
+
}
|
|
1193
|
+
if (complexObject.complexAspect) {
|
|
1194
|
+
return complexObject.complexAspect;
|
|
1195
|
+
}
|
|
1196
|
+
// if called without new
|
|
1197
|
+
if (!(this instanceof ComplexAspect)) {
|
|
1198
|
+
return new ComplexAspect(complexObject, parent, parentProperty);
|
|
1199
|
+
}
|
|
1200
|
+
// entityType should already be on the entity from 'watch'
|
|
1201
|
+
this.complexObject = complexObject;
|
|
1202
|
+
complexObject.complexAspect = this;
|
|
1203
|
+
// TODO: keep public or not?
|
|
1204
|
+
this.originalValues = {};
|
|
1205
|
+
// if a standalone complexObject
|
|
1206
|
+
if (parent != null) {
|
|
1207
|
+
this.parent = parent;
|
|
1208
|
+
this.parentProperty = parentProperty;
|
|
1209
|
+
}
|
|
1210
|
+
let complexType = complexObject.complexType;
|
|
1211
|
+
if (!complexType) {
|
|
1212
|
+
let typeName = complexObject.prototype._$typeName;
|
|
1213
|
+
if (!typeName) {
|
|
1214
|
+
throw new Error("This entity is not registered as a valid ComplexType");
|
|
1215
|
+
}
|
|
1216
|
+
else {
|
|
1217
|
+
throw new Error("Metadata for this complexType has not yet been resolved: " + typeName);
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
let complexCtor = complexType.getCtor();
|
|
1221
|
+
config.interfaceRegistry.modelLibrary.getDefaultInstance().startTracking(complexObject, complexCtor.prototype);
|
|
1222
|
+
}
|
|
1223
|
+
/**
|
|
1224
|
+
Returns the EntityAspect for the top level entity that contains this complex object.
|
|
1225
|
+
*/
|
|
1226
|
+
getEntityAspect() {
|
|
1227
|
+
let parent = this.parent;
|
|
1228
|
+
if (!parent)
|
|
1229
|
+
return new EntityAspect();
|
|
1230
|
+
let entityAspect = parent.entityAspect;
|
|
1231
|
+
while (parent && !entityAspect) {
|
|
1232
|
+
parent = parent.complexAspect && parent.complexAspect.parent;
|
|
1233
|
+
entityAspect = parent && parent.entityAspect;
|
|
1234
|
+
}
|
|
1235
|
+
return entityAspect || new EntityAspect();
|
|
1236
|
+
}
|
|
1237
|
+
/** @hidden @internal */
|
|
1238
|
+
// TODO: rename - and use '_'; used on both EntityAspect and ComplexAspect for polymorphic reasons.
|
|
1239
|
+
getPropertyPath(propName) {
|
|
1240
|
+
let parent = this.parent;
|
|
1241
|
+
if (!parent)
|
|
1242
|
+
return null;
|
|
1243
|
+
let aspect = parent.complexAspect || parent.entityAspect;
|
|
1244
|
+
return aspect.getPropertyPath(this.parentProperty.name + "." + propName);
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
function clearOriginalValues(target) {
|
|
1248
|
+
let aspect = target.entityAspect || target.complexAspect;
|
|
1249
|
+
aspect.originalValues = {};
|
|
1250
|
+
let stype = target.entityType || target.complexType;
|
|
1251
|
+
stype.complexProperties.forEach(function (cp) {
|
|
1252
|
+
let cos = target.getProperty(cp.name);
|
|
1253
|
+
if (cp.isScalar) {
|
|
1254
|
+
clearOriginalValues(cos);
|
|
1255
|
+
}
|
|
1256
|
+
else {
|
|
1257
|
+
observableArray.acceptChanges(cos);
|
|
1258
|
+
cos.forEach(clearOriginalValues);
|
|
1259
|
+
}
|
|
1260
|
+
});
|
|
1261
|
+
}
|
|
1262
|
+
//# sourceMappingURL=entity-aspect.js.map
|