@warp-drive/legacy 5.8.0-alpha.9 → 5.8.0-beta.1
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/README.md +14 -27
- package/declarations/adapter/error.d.ts +7 -7
- package/declarations/adapter/json-api.d.ts +2 -2
- package/declarations/adapter/rest.d.ts +21 -104
- package/declarations/adapter.d.ts +2 -2
- package/declarations/compat/builders/find-all.d.ts +6 -6
- package/declarations/compat/builders/find-record.d.ts +8 -8
- package/declarations/compat/builders/query.d.ts +12 -12
- package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +16 -24
- package/declarations/compat/utils.d.ts +13 -13
- package/declarations/compat.d.ts +31 -5
- package/declarations/index.d.ts +25 -8
- package/declarations/model/-private/legacy-relationships-support.d.ts +2 -2
- package/declarations/model/-private/model.d.ts +10 -51
- package/declarations/model/-private/promise-many-array.d.ts +0 -18
- package/declarations/model/-private/references/belongs-to.d.ts +18 -28
- package/declarations/model/-private/references/has-many.d.ts +13 -15
- package/declarations/model/migration-support.d.ts +42 -19
- package/declarations/model-fragments/extensions/fragment-array.d.ts +16 -0
- package/declarations/model-fragments/extensions/fragment.d.ts +15 -0
- package/declarations/model-fragments/hooks/model-for.d.ts +20 -0
- package/declarations/model-fragments/index.d.ts +5 -0
- package/declarations/model-fragments/instance-initializers/fragment-extensions.d.ts +9 -0
- package/declarations/model-fragments/utilities/with-array-defaults.d.ts +15 -0
- package/declarations/model-fragments/utilities/with-fragment-array-defaults.d.ts +20 -0
- package/declarations/model-fragments/utilities/with-fragment-defaults.d.ts +19 -0
- package/declarations/model-fragments/utilities/with-legacy.d.ts +3 -0
- package/declarations/model-fragments.d.ts +9 -0
- package/declarations/model.d.ts +2 -2
- package/declarations/serializer/-private/embedded-records-mixin.d.ts +1 -1
- package/declarations/serializer/json-api.d.ts +7 -6
- package/declarations/serializer/json.d.ts +3 -3
- package/declarations/serializer.d.ts +5 -6
- package/dist/{-private-B1pSSN52.js → -private-BG3bMiKp.js} +2 -1
- package/dist/adapter/-private.js +1 -1
- package/dist/adapter/error.js +9 -9
- package/dist/adapter/json-api.js +1 -1
- package/dist/adapter/rest.js +33 -117
- package/dist/adapter.js +2 -2
- package/dist/compat/-private.js +1 -1
- package/dist/compat/builders.js +26 -26
- package/dist/compat/utils.js +13 -14
- package/dist/compat.js +28 -8
- package/dist/{errors-COviC59J.js → errors-Cz5KrzBk.js} +113 -120
- package/dist/{hooks-Bp8SIQBU.js → hooks-D6diaM34.js} +1 -1
- package/dist/index.js +25 -8
- package/dist/{json-ksWOHRfq.js → json-ChdEfB0X.js} +12 -27
- package/dist/model/-private.js +1 -1
- package/dist/model/migration-support.js +55 -25
- package/dist/model-for-CqXsIKws.js +221 -0
- package/dist/model-fragments.js +76 -0
- package/dist/model.js +3 -3
- package/dist/{schema-provider-JlCneqZH.js → schema-provider-DJCV_6AF.js} +44 -87
- package/dist/{serialize-into-hash-BnYvPex3.js → serialize-into-hash-DPZYoF-i.js} +1 -1
- package/dist/serializer/json-api.js +18 -45
- package/dist/serializer/json.js +1 -1
- package/dist/serializer/rest.js +10 -10
- package/dist/serializer.js +5 -6
- package/dist/store.js +2 -1
- package/dist/unpkg/dev/-private-DtjBbEgy.js +1206 -0
- package/dist/unpkg/dev/adapter/-private.js +1 -0
- package/dist/unpkg/dev/adapter/error.js +335 -0
- package/dist/unpkg/dev/adapter/json-api.js +271 -0
- package/dist/unpkg/dev/adapter/rest.js +1171 -0
- package/dist/unpkg/dev/adapter.js +1252 -0
- package/dist/unpkg/dev/compat/-private.js +1 -0
- package/dist/unpkg/dev/compat/builders.js +275 -0
- package/dist/unpkg/dev/compat/extensions.js +242 -0
- package/dist/unpkg/dev/compat/utils.js +223 -0
- package/dist/unpkg/dev/compat.js +1147 -0
- package/dist/unpkg/dev/errors-DmGGJr3T.js +2562 -0
- package/dist/unpkg/dev/hooks-CkYiE6Ud.js +73 -0
- package/dist/unpkg/dev/index.js +197 -0
- package/dist/unpkg/dev/json-Cu1LNgmQ.js +1256 -0
- package/dist/unpkg/dev/model/-private.js +1 -0
- package/dist/unpkg/dev/model/migration-support.js +553 -0
- package/dist/unpkg/dev/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/dev/model-fragments.js +76 -0
- package/dist/unpkg/dev/model.js +678 -0
- package/dist/unpkg/dev/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/dev/schema-provider-DDVYxmUV.js +2186 -0
- package/dist/unpkg/dev/serialize-into-hash-B2xDbuo5.js +259 -0
- package/dist/unpkg/dev/serializer/json-api.js +649 -0
- package/dist/unpkg/dev/serializer/json.js +4 -0
- package/dist/unpkg/dev/serializer/rest.js +1242 -0
- package/dist/unpkg/dev/serializer/transform.js +278 -0
- package/dist/unpkg/dev/serializer.js +248 -0
- package/dist/unpkg/dev/store.js +637 -0
- package/dist/unpkg/dev/util-DvanW33H.js +20 -0
- package/dist/unpkg/dev/utils-BhvS1iTS.js +8 -0
- package/dist/unpkg/dev-deprecated/-private-DtjBbEgy.js +1206 -0
- package/dist/unpkg/dev-deprecated/adapter/-private.js +1 -0
- package/dist/unpkg/dev-deprecated/adapter/error.js +335 -0
- package/dist/unpkg/dev-deprecated/adapter/json-api.js +271 -0
- package/dist/unpkg/dev-deprecated/adapter/rest.js +1171 -0
- package/dist/unpkg/dev-deprecated/adapter.js +1252 -0
- package/dist/unpkg/dev-deprecated/compat/-private.js +1 -0
- package/dist/unpkg/dev-deprecated/compat/builders.js +275 -0
- package/dist/unpkg/dev-deprecated/compat/extensions.js +242 -0
- package/dist/unpkg/dev-deprecated/compat/utils.js +223 -0
- package/dist/unpkg/dev-deprecated/compat.js +1147 -0
- package/dist/unpkg/dev-deprecated/errors-Spt6ubMd.js +2565 -0
- package/dist/unpkg/dev-deprecated/hooks-DOXegvhL.js +73 -0
- package/dist/unpkg/dev-deprecated/index.js +196 -0
- package/dist/unpkg/dev-deprecated/json-Cu1LNgmQ.js +1256 -0
- package/dist/unpkg/dev-deprecated/model/-private.js +1 -0
- package/dist/unpkg/dev-deprecated/model/migration-support.js +570 -0
- package/dist/unpkg/dev-deprecated/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/dev-deprecated/model-fragments.js +76 -0
- package/dist/unpkg/dev-deprecated/model.js +682 -0
- package/dist/unpkg/dev-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/dev-deprecated/schema-provider-BP6_8N-V.js +2211 -0
- package/dist/unpkg/dev-deprecated/serialize-into-hash-B2xDbuo5.js +259 -0
- package/dist/unpkg/dev-deprecated/serializer/json-api.js +649 -0
- package/dist/unpkg/dev-deprecated/serializer/json.js +4 -0
- package/dist/unpkg/dev-deprecated/serializer/rest.js +1242 -0
- package/dist/unpkg/dev-deprecated/serializer/transform.js +278 -0
- package/dist/unpkg/dev-deprecated/serializer.js +248 -0
- package/dist/unpkg/dev-deprecated/store.js +637 -0
- package/dist/unpkg/dev-deprecated/util-CWr5WQOT.js +24 -0
- package/dist/unpkg/dev-deprecated/utils-C9PJehtL.js +12 -0
- package/dist/unpkg/prod/-private-BdyZaGEh.js +971 -0
- package/dist/unpkg/prod/adapter/-private.js +1 -0
- package/dist/unpkg/prod/adapter/error.js +330 -0
- package/dist/unpkg/prod/adapter/json-api.js +266 -0
- package/dist/unpkg/prod/adapter/rest.js +1134 -0
- package/dist/unpkg/prod/adapter.js +1219 -0
- package/dist/unpkg/prod/compat/-private.js +1 -0
- package/dist/unpkg/prod/compat/builders.js +210 -0
- package/dist/unpkg/prod/compat/extensions.js +232 -0
- package/dist/unpkg/prod/compat/utils.js +218 -0
- package/dist/unpkg/prod/compat.js +727 -0
- package/dist/unpkg/prod/errors-BGVFCBmi.js +2314 -0
- package/dist/unpkg/prod/hooks-BztVA_x0.js +41 -0
- package/dist/unpkg/prod/index.js +151 -0
- package/dist/unpkg/prod/json-BWrZ5546.js +1243 -0
- package/dist/unpkg/prod/model/-private.js +1 -0
- package/dist/unpkg/prod/model/migration-support.js +546 -0
- package/dist/unpkg/prod/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/prod/model-fragments.js +76 -0
- package/dist/unpkg/prod/model.js +593 -0
- package/dist/unpkg/prod/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/prod/schema-provider-DJtD_8jZ.js +1861 -0
- package/dist/unpkg/prod/serialize-into-hash-DGlzQteF.js +215 -0
- package/dist/unpkg/prod/serializer/json-api.js +592 -0
- package/dist/unpkg/prod/serializer/json.js +4 -0
- package/dist/unpkg/prod/serializer/rest.js +1210 -0
- package/dist/unpkg/prod/serializer/transform.js +278 -0
- package/dist/unpkg/prod/serializer.js +248 -0
- package/dist/unpkg/prod/store.js +505 -0
- package/dist/unpkg/prod/util-DvanW33H.js +20 -0
- package/dist/unpkg/prod/utils-BhvS1iTS.js +8 -0
- package/dist/unpkg/prod-deprecated/-private-BdyZaGEh.js +971 -0
- package/dist/unpkg/prod-deprecated/adapter/-private.js +1 -0
- package/dist/unpkg/prod-deprecated/adapter/error.js +330 -0
- package/dist/unpkg/prod-deprecated/adapter/json-api.js +266 -0
- package/dist/unpkg/prod-deprecated/adapter/rest.js +1134 -0
- package/dist/unpkg/prod-deprecated/adapter.js +1219 -0
- package/dist/unpkg/prod-deprecated/compat/-private.js +1 -0
- package/dist/unpkg/prod-deprecated/compat/builders.js +210 -0
- package/dist/unpkg/prod-deprecated/compat/extensions.js +232 -0
- package/dist/unpkg/prod-deprecated/compat/utils.js +218 -0
- package/dist/unpkg/prod-deprecated/compat.js +727 -0
- package/dist/unpkg/prod-deprecated/errors-CdDaK81x.js +2317 -0
- package/dist/unpkg/prod-deprecated/hooks-yId87yyG.js +41 -0
- package/dist/unpkg/prod-deprecated/index.js +150 -0
- package/dist/unpkg/prod-deprecated/json-BWrZ5546.js +1243 -0
- package/dist/unpkg/prod-deprecated/model/-private.js +1 -0
- package/dist/unpkg/prod-deprecated/model/migration-support.js +563 -0
- package/dist/unpkg/prod-deprecated/model-for-CqXsIKws.js +221 -0
- package/dist/unpkg/prod-deprecated/model-fragments.js +76 -0
- package/dist/unpkg/prod-deprecated/model.js +596 -0
- package/dist/unpkg/prod-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
- package/dist/unpkg/prod-deprecated/schema-provider-CjX55uSY.js +1904 -0
- package/dist/unpkg/prod-deprecated/serialize-into-hash-DGlzQteF.js +215 -0
- package/dist/unpkg/prod-deprecated/serializer/json-api.js +592 -0
- package/dist/unpkg/prod-deprecated/serializer/json.js +4 -0
- package/dist/unpkg/prod-deprecated/serializer/rest.js +1210 -0
- package/dist/unpkg/prod-deprecated/serializer/transform.js +278 -0
- package/dist/unpkg/prod-deprecated/serializer.js +248 -0
- package/dist/unpkg/prod-deprecated/store.js +505 -0
- package/dist/unpkg/prod-deprecated/util-B6cn-i93.js +23 -0
- package/dist/unpkg/prod-deprecated/utils-BUWwQwCh.js +11 -0
- package/logos/README.md +2 -2
- package/logos/logo-yellow-slab.svg +1 -0
- package/logos/word-mark-black.svg +1 -0
- package/logos/word-mark-white.svg +1 -0
- package/package.json +14 -6
- package/logos/NCC-1701-a-blue.svg +0 -4
- package/logos/NCC-1701-a-gold.svg +0 -4
- package/logos/NCC-1701-a-gold_100.svg +0 -1
- package/logos/NCC-1701-a-gold_base-64.txt +0 -1
- package/logos/NCC-1701-a.svg +0 -4
- package/logos/docs-badge.svg +0 -2
- package/logos/ember-data-logo-dark.svg +0 -12
- package/logos/ember-data-logo-light.svg +0 -12
- package/logos/social1.png +0 -0
- package/logos/social2.png +0 -0
- package/logos/warp-drive-logo-dark.svg +0 -4
- package/logos/warp-drive-logo-gold.svg +0 -4
|
@@ -0,0 +1,505 @@
|
|
|
1
|
+
import { assertPrivateStore, ensureStringId, recordIdentifierFor, constructResource } from '@warp-drive/core/store/-private';
|
|
2
|
+
import { SkipCache } from '@warp-drive/core/types/request';
|
|
3
|
+
import { i as isMaybeIdentifier } from "./utils-BhvS1iTS.js";
|
|
4
|
+
import { n as normalizeModelName } from "./util-DvanW33H.js";
|
|
5
|
+
import { defineSignal } from '@warp-drive/core/signals/-leaked';
|
|
6
|
+
import { getOrSetGlobal } from '@warp-drive/core/types/-private';
|
|
7
|
+
|
|
8
|
+
/*
|
|
9
|
+
When a find request is triggered on the store, the user can optionally pass in
|
|
10
|
+
attributes and relationships to be preloaded. These are meant to behave as if they
|
|
11
|
+
came back from the server, except the user obtained them out of band and is informing
|
|
12
|
+
the store of their existence. The most common use case is for supporting client side
|
|
13
|
+
nested URLs, such as `/posts/1/comments/2` so the user can do
|
|
14
|
+
`store.findRecord('comment', 2, { preload: { post: 1 } })` without having to fetch the post.
|
|
15
|
+
|
|
16
|
+
Preloaded data can be attributes and relationships passed in either as IDs or as actual
|
|
17
|
+
models.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
function preloadData(store, identifier, preload) {
|
|
21
|
+
const jsonPayload = {};
|
|
22
|
+
//TODO(Igor) consider the polymorphic case
|
|
23
|
+
const schemas = store.schema;
|
|
24
|
+
const fields = schemas.fields(identifier);
|
|
25
|
+
Object.keys(preload).forEach(key => {
|
|
26
|
+
const preloadValue = preload[key];
|
|
27
|
+
const field = fields.get(key);
|
|
28
|
+
if (field && (field.kind === 'hasMany' || field.kind === 'belongsTo')) {
|
|
29
|
+
if (!jsonPayload.relationships) {
|
|
30
|
+
jsonPayload.relationships = {};
|
|
31
|
+
}
|
|
32
|
+
jsonPayload.relationships[key] = preloadRelationship(field, preloadValue);
|
|
33
|
+
} else {
|
|
34
|
+
if (!jsonPayload.attributes) {
|
|
35
|
+
jsonPayload.attributes = {};
|
|
36
|
+
}
|
|
37
|
+
jsonPayload.attributes[key] = preloadValue;
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
const cache = store.cache;
|
|
41
|
+
assertPrivateStore(store);
|
|
42
|
+
const hasRecord = Boolean(store._instanceCache.peek(identifier));
|
|
43
|
+
cache.upsert(identifier, jsonPayload, hasRecord);
|
|
44
|
+
}
|
|
45
|
+
function preloadRelationship(schema, preloadValue) {
|
|
46
|
+
const relatedType = schema.type;
|
|
47
|
+
if (schema.kind === 'hasMany') {
|
|
48
|
+
return {
|
|
49
|
+
data: preloadValue.map(value => _convertPreloadRelationshipToJSON(value, relatedType))
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
return {
|
|
53
|
+
data: preloadValue ? _convertPreloadRelationshipToJSON(preloadValue, relatedType) : null
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/*
|
|
58
|
+
findRecord('user', '1', { preload: { friends: ['1'] }});
|
|
59
|
+
findRecord('user', '1', { preload: { friends: [record] }});
|
|
60
|
+
*/
|
|
61
|
+
function _convertPreloadRelationshipToJSON(value, type) {
|
|
62
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
63
|
+
return {
|
|
64
|
+
type,
|
|
65
|
+
id: ensureStringId(value)
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
// TODO if not a record instance assert it's an identifier
|
|
69
|
+
// and allow identifiers to be used
|
|
70
|
+
return recordIdentifierFor(value);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Minimum subset of static schema methods and properties on the
|
|
75
|
+
* "model" class.
|
|
76
|
+
*
|
|
77
|
+
* Only used when using the legacy schema-service implementation
|
|
78
|
+
* for @ember-data/model or when wrapping schema for legacy
|
|
79
|
+
* Adapters/Serializers.
|
|
80
|
+
*
|
|
81
|
+
*/
|
|
82
|
+
|
|
83
|
+
function _resourceIsFullDeleted(identifier, cache) {
|
|
84
|
+
return cache.isDeletionCommitted(identifier) || cache.isNew(identifier) && cache.isDeleted(identifier);
|
|
85
|
+
}
|
|
86
|
+
function resourceIsFullyDeleted(instanceCache, identifier) {
|
|
87
|
+
const cache = instanceCache.cache;
|
|
88
|
+
return !cache || _resourceIsFullDeleted(identifier, cache);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
A `RecordReference` is a low-level API that allows users and
|
|
93
|
+
addon authors to perform meta-operations on a record.
|
|
94
|
+
|
|
95
|
+
@hideconstructor
|
|
96
|
+
@public
|
|
97
|
+
*/
|
|
98
|
+
class RecordReference {
|
|
99
|
+
/** @internal */
|
|
100
|
+
|
|
101
|
+
/** @internal */
|
|
102
|
+
// unsubscribe token given to us by the notification manager
|
|
103
|
+
___token;
|
|
104
|
+
/** @internal */
|
|
105
|
+
___identifier;
|
|
106
|
+
|
|
107
|
+
/** @internal */
|
|
108
|
+
|
|
109
|
+
constructor(store, identifier) {
|
|
110
|
+
this.store = store;
|
|
111
|
+
this.___identifier = identifier;
|
|
112
|
+
this.___token = store.notifications.subscribe(identifier, (_, bucket, notifiedKey) => {
|
|
113
|
+
if (bucket === 'identity' || bucket === 'attributes' && notifiedKey === 'id') {
|
|
114
|
+
this._ref++;
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** @internal */
|
|
120
|
+
destroy() {
|
|
121
|
+
this.store.notifications.unsubscribe(this.___token);
|
|
122
|
+
}
|
|
123
|
+
get type() {
|
|
124
|
+
return this.identifier().type;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
The `id` of the record that this reference refers to.
|
|
129
|
+
Together, the `type` and `id` properties form a composite key for
|
|
130
|
+
the identity map.
|
|
131
|
+
Example
|
|
132
|
+
```javascript
|
|
133
|
+
let userRef = store.getReference('user', 1);
|
|
134
|
+
userRef.id(); // '1'
|
|
135
|
+
```
|
|
136
|
+
@public
|
|
137
|
+
@return The id of the record.
|
|
138
|
+
*/
|
|
139
|
+
id() {
|
|
140
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
|
|
141
|
+
this._ref; // consume the tracked prop
|
|
142
|
+
return this.___identifier.id;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
The `identifier` of the record that this reference refers to.
|
|
147
|
+
Together, the `type` and `id` properties form a composite key for
|
|
148
|
+
the identity map.
|
|
149
|
+
Example
|
|
150
|
+
```javascript
|
|
151
|
+
let userRef = store.getReference('user', 1);
|
|
152
|
+
userRef.identifier(); // '1'
|
|
153
|
+
```
|
|
154
|
+
@public
|
|
155
|
+
@return The identifier of the record.
|
|
156
|
+
*/
|
|
157
|
+
identifier() {
|
|
158
|
+
return this.___identifier;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
How the reference will be looked up when it is loaded. Currently
|
|
163
|
+
this always returns `identity` to signify that a record will be
|
|
164
|
+
loaded by its `type` and `id`.
|
|
165
|
+
Example
|
|
166
|
+
```javascript
|
|
167
|
+
const userRef = store.getReference('user', 1);
|
|
168
|
+
userRef.remoteType(); // 'identity'
|
|
169
|
+
```
|
|
170
|
+
@public
|
|
171
|
+
*/
|
|
172
|
+
remoteType() {
|
|
173
|
+
return 'identity';
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
This API allows you to provide a reference with new data. The
|
|
178
|
+
simplest usage of this API is similar to `store.push`: you provide a
|
|
179
|
+
normalized hash of data and the object represented by the reference
|
|
180
|
+
will update.
|
|
181
|
+
If you pass a promise to `push`, Ember Data will not ask the adapter
|
|
182
|
+
for the data if another attempt to fetch it is made in the
|
|
183
|
+
interim. When the promise resolves, the underlying object is updated
|
|
184
|
+
with the new data, and the promise returned by *this function* is resolved
|
|
185
|
+
with that object.
|
|
186
|
+
For example, `recordReference.push(promise)` will be resolved with a
|
|
187
|
+
record.
|
|
188
|
+
Example
|
|
189
|
+
```javascript
|
|
190
|
+
let userRef = store.getReference('user', 1);
|
|
191
|
+
// provide data for reference
|
|
192
|
+
userRef.push({
|
|
193
|
+
data: {
|
|
194
|
+
id: "1",
|
|
195
|
+
type: "user",
|
|
196
|
+
attributes: {
|
|
197
|
+
username: "@user"
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}).then(function(user) {
|
|
201
|
+
userRef.value() === user;
|
|
202
|
+
});
|
|
203
|
+
```
|
|
204
|
+
@public
|
|
205
|
+
@param objectOrPromise a JSON:API ResourceDocument or a promise resolving to one
|
|
206
|
+
@return a promise for the value (record or relationship)
|
|
207
|
+
*/
|
|
208
|
+
push(objectOrPromise) {
|
|
209
|
+
// TODO @deprecate pushing unresolved payloads
|
|
210
|
+
return Promise.resolve(objectOrPromise).then(data => {
|
|
211
|
+
return this.store.push(data);
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
If the entity referred to by the reference is already loaded, it is
|
|
217
|
+
present as `reference.value`. Otherwise the value returned by this function
|
|
218
|
+
is `null`.
|
|
219
|
+
Example
|
|
220
|
+
```javascript
|
|
221
|
+
let userRef = store.getReference('user', 1);
|
|
222
|
+
userRef.value(); // user
|
|
223
|
+
```
|
|
224
|
+
@public
|
|
225
|
+
@return {Model} the record for this RecordReference
|
|
226
|
+
*/
|
|
227
|
+
value() {
|
|
228
|
+
return this.store.peekRecord(this.___identifier);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
Triggers a fetch for the backing entity based on its `remoteType`
|
|
233
|
+
(see `remoteType` definitions per reference type).
|
|
234
|
+
Example
|
|
235
|
+
```javascript
|
|
236
|
+
let userRef = store.getReference('user', 1);
|
|
237
|
+
// load user (via store.find)
|
|
238
|
+
userRef.load().then(...)
|
|
239
|
+
```
|
|
240
|
+
@public
|
|
241
|
+
@return the record for this RecordReference
|
|
242
|
+
*/
|
|
243
|
+
load() {
|
|
244
|
+
const id = this.id();
|
|
245
|
+
if (id !== null) {
|
|
246
|
+
return this.store.findRecord(this.type, id);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
Reloads the record if it is already loaded. If the record is not
|
|
252
|
+
loaded it will load the record via `store.findRecord`
|
|
253
|
+
Example
|
|
254
|
+
```javascript
|
|
255
|
+
let userRef = store.getReference('user', 1);
|
|
256
|
+
// or trigger a reload
|
|
257
|
+
userRef.reload().then(...)
|
|
258
|
+
```
|
|
259
|
+
@public
|
|
260
|
+
@return the record for this RecordReference
|
|
261
|
+
*/
|
|
262
|
+
reload() {
|
|
263
|
+
const id = this.id();
|
|
264
|
+
if (id !== null) {
|
|
265
|
+
return this.store.findRecord(this.type, id, {
|
|
266
|
+
reload: true
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
defineSignal(RecordReference.prototype, '_ref');
|
|
272
|
+
|
|
273
|
+
// if modelFor turns out to be a bottleneck we should replace with a Map
|
|
274
|
+
// and clear it during store teardown.
|
|
275
|
+
const AvailableShims = getOrSetGlobal('AvailableShims', new WeakMap());
|
|
276
|
+
function getShimClass(store, modelName) {
|
|
277
|
+
let shims = AvailableShims.get(store);
|
|
278
|
+
if (!shims) {
|
|
279
|
+
shims = Object.create(null);
|
|
280
|
+
AvailableShims.set(store, shims);
|
|
281
|
+
}
|
|
282
|
+
let shim = shims[modelName];
|
|
283
|
+
if (shim === undefined) {
|
|
284
|
+
shim = shims[modelName] = new ShimModelClass(store, modelName);
|
|
285
|
+
}
|
|
286
|
+
return shim;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// Mimics the static apis of @ember-data/model
|
|
290
|
+
class ShimModelClass {
|
|
291
|
+
constructor(store, modelName) {
|
|
292
|
+
this.__store = store;
|
|
293
|
+
this.modelName = modelName;
|
|
294
|
+
}
|
|
295
|
+
get fields() {
|
|
296
|
+
const fields = new Map();
|
|
297
|
+
const fieldSchemas = this.__store.schema.fields({
|
|
298
|
+
type: this.modelName
|
|
299
|
+
});
|
|
300
|
+
fieldSchemas.forEach((schema, key) => {
|
|
301
|
+
if (schema.kind === 'attribute' || schema.kind === 'belongsTo' || schema.kind === 'hasMany') {
|
|
302
|
+
fields.set(key, schema.kind);
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
return fields;
|
|
306
|
+
}
|
|
307
|
+
get attributes() {
|
|
308
|
+
const attrs = new Map();
|
|
309
|
+
const fields = this.__store.schema.fields({
|
|
310
|
+
type: this.modelName
|
|
311
|
+
});
|
|
312
|
+
fields.forEach((schema, key) => {
|
|
313
|
+
if (schema.kind === 'attribute') {
|
|
314
|
+
attrs.set(key, schema);
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
return attrs;
|
|
318
|
+
}
|
|
319
|
+
get relationshipsByName() {
|
|
320
|
+
const rels = new Map();
|
|
321
|
+
const fields = this.__store.schema.fields({
|
|
322
|
+
type: this.modelName
|
|
323
|
+
});
|
|
324
|
+
fields.forEach((schema, key) => {
|
|
325
|
+
if (schema.kind === 'belongsTo' || schema.kind === 'hasMany') {
|
|
326
|
+
rels.set(key, schema);
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
return rels;
|
|
330
|
+
}
|
|
331
|
+
eachAttribute(callback, binding) {
|
|
332
|
+
this.__store.schema.fields({
|
|
333
|
+
type: this.modelName
|
|
334
|
+
}).forEach((schema, key) => {
|
|
335
|
+
if (schema.kind === 'attribute') {
|
|
336
|
+
callback.call(binding, key, schema);
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
eachRelationship(callback, binding) {
|
|
341
|
+
this.__store.schema.fields({
|
|
342
|
+
type: this.modelName
|
|
343
|
+
}).forEach((schema, key) => {
|
|
344
|
+
if (schema.kind === 'belongsTo' || schema.kind === 'hasMany') {
|
|
345
|
+
callback.call(binding, key, schema);
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
eachTransformedAttribute(callback, binding) {
|
|
350
|
+
this.__store.schema.fields({
|
|
351
|
+
type: this.modelName
|
|
352
|
+
}).forEach((schema, key) => {
|
|
353
|
+
if (schema.kind === 'attribute') {
|
|
354
|
+
const type = schema.type;
|
|
355
|
+
if (type) callback.call(binding, key, type);
|
|
356
|
+
}
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
function restoreDeprecatedStoreBehaviors(StoreKlass) {
|
|
361
|
+
StoreKlass.prototype.findRecord = function (resource, id, options) {
|
|
362
|
+
if (isMaybeIdentifier(resource)) {
|
|
363
|
+
options = id;
|
|
364
|
+
} else {
|
|
365
|
+
const type = normalizeModelName(resource);
|
|
366
|
+
const normalizedId = ensureStringId(id);
|
|
367
|
+
resource = constructResource(type, normalizedId);
|
|
368
|
+
}
|
|
369
|
+
const identifier = this.cacheKeyManager.getOrCreateRecordIdentifier(resource);
|
|
370
|
+
options = options || {};
|
|
371
|
+
assertPrivateStore(this);
|
|
372
|
+
if (options.preload) {
|
|
373
|
+
// force reload if we preload to ensure we don't resolve the promise
|
|
374
|
+
// until we are complete, else we will end up background-reloading
|
|
375
|
+
// even for initial load.
|
|
376
|
+
if (!this._instanceCache.recordIsLoaded(identifier)) {
|
|
377
|
+
options.reload = true;
|
|
378
|
+
}
|
|
379
|
+
this._join(() => {
|
|
380
|
+
preloadData(this, identifier, options.preload);
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
const promise = this.request({
|
|
384
|
+
op: 'findRecord',
|
|
385
|
+
data: {
|
|
386
|
+
record: identifier,
|
|
387
|
+
options
|
|
388
|
+
},
|
|
389
|
+
cacheOptions: {
|
|
390
|
+
[SkipCache]: true
|
|
391
|
+
}
|
|
392
|
+
});
|
|
393
|
+
return promise.then(document => {
|
|
394
|
+
return document.content;
|
|
395
|
+
});
|
|
396
|
+
};
|
|
397
|
+
StoreKlass.prototype.findAll = function (type, options = {}) {
|
|
398
|
+
const promise = this.request({
|
|
399
|
+
op: 'findAll',
|
|
400
|
+
data: {
|
|
401
|
+
type: normalizeModelName(type),
|
|
402
|
+
options: options || {}
|
|
403
|
+
},
|
|
404
|
+
cacheOptions: {
|
|
405
|
+
[SkipCache]: true
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
return promise.then(document => document.content);
|
|
409
|
+
};
|
|
410
|
+
StoreKlass.prototype.query = function (type, query, options = {}) {
|
|
411
|
+
const promise = this.request({
|
|
412
|
+
op: 'query',
|
|
413
|
+
data: {
|
|
414
|
+
type: normalizeModelName(type),
|
|
415
|
+
query,
|
|
416
|
+
options: options
|
|
417
|
+
},
|
|
418
|
+
cacheOptions: {
|
|
419
|
+
[SkipCache]: true
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
return promise.then(document => document.content);
|
|
423
|
+
};
|
|
424
|
+
StoreKlass.prototype.queryRecord = function (type, query, options) {
|
|
425
|
+
const promise = this.request({
|
|
426
|
+
op: 'queryRecord',
|
|
427
|
+
data: {
|
|
428
|
+
type: normalizeModelName(type),
|
|
429
|
+
query,
|
|
430
|
+
options: options || {}
|
|
431
|
+
},
|
|
432
|
+
cacheOptions: {
|
|
433
|
+
[SkipCache]: true
|
|
434
|
+
}
|
|
435
|
+
});
|
|
436
|
+
return promise.then(document => document.content);
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
// do not put a ts-expect-error here, because typedoc will fail to
|
|
440
|
+
// build due to this error only occurring when references are in use in the editor
|
|
441
|
+
StoreKlass.prototype.getReference = function (resource, id) {
|
|
442
|
+
let resourceIdentifier;
|
|
443
|
+
if (arguments.length === 1 && isMaybeIdentifier(resource)) {
|
|
444
|
+
resourceIdentifier = resource;
|
|
445
|
+
} else {
|
|
446
|
+
const type = normalizeModelName(resource);
|
|
447
|
+
const normalizedId = ensureStringId(id);
|
|
448
|
+
resourceIdentifier = constructResource(type, normalizedId);
|
|
449
|
+
}
|
|
450
|
+
const identifier = this.cacheKeyManager.getOrCreateRecordIdentifier(resourceIdentifier);
|
|
451
|
+
assertPrivateStore(this);
|
|
452
|
+
const cache = upgradeInstanceCaches(this._instanceCache.__instances).reference;
|
|
453
|
+
let reference = cache.get(identifier);
|
|
454
|
+
if (!reference) {
|
|
455
|
+
reference = new RecordReference(this, identifier);
|
|
456
|
+
cache.set(identifier, reference);
|
|
457
|
+
}
|
|
458
|
+
return reference;
|
|
459
|
+
};
|
|
460
|
+
StoreKlass.prototype.modelFor = function (type) {
|
|
461
|
+
return getShimClass(this, type);
|
|
462
|
+
};
|
|
463
|
+
StoreKlass.prototype.saveRecord = function (record, options = {}) {
|
|
464
|
+
const identifier = recordIdentifierFor(record);
|
|
465
|
+
const cache = this.cache;
|
|
466
|
+
if (!identifier) {
|
|
467
|
+
// this commonly means we're disconnected
|
|
468
|
+
// but just in case we reject here to prevent bad things.
|
|
469
|
+
return Promise.reject(new Error(`Record Is Disconnected`));
|
|
470
|
+
}
|
|
471
|
+
assertPrivateStore(this);
|
|
472
|
+
if (resourceIsFullyDeleted(this._instanceCache, identifier)) {
|
|
473
|
+
return Promise.resolve(record);
|
|
474
|
+
}
|
|
475
|
+
if (!options) {
|
|
476
|
+
options = {};
|
|
477
|
+
}
|
|
478
|
+
let operation = 'updateRecord';
|
|
479
|
+
if (cache.isNew(identifier)) {
|
|
480
|
+
operation = 'createRecord';
|
|
481
|
+
} else if (cache.isDeleted(identifier)) {
|
|
482
|
+
operation = 'deleteRecord';
|
|
483
|
+
}
|
|
484
|
+
const request = {
|
|
485
|
+
op: operation,
|
|
486
|
+
data: {
|
|
487
|
+
options,
|
|
488
|
+
record: identifier
|
|
489
|
+
},
|
|
490
|
+
records: [identifier],
|
|
491
|
+
cacheOptions: {
|
|
492
|
+
[SkipCache]: true
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
return this.request(request).then(document => document.content);
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
function upgradeInstanceCaches(cache) {
|
|
499
|
+
const withReferences = cache;
|
|
500
|
+
if (!withReferences.reference) {
|
|
501
|
+
withReferences.reference = new WeakMap();
|
|
502
|
+
}
|
|
503
|
+
return withReferences;
|
|
504
|
+
}
|
|
505
|
+
export { restoreDeprecatedStoreBehaviors };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import '@warp-drive/utilities/string';
|
|
2
|
+
function isElementDescriptor(args) {
|
|
3
|
+
const [maybeTarget, maybeKey, maybeDesc] = args;
|
|
4
|
+
return (
|
|
5
|
+
// Ensure we have the right number of args
|
|
6
|
+
args.length === 3 && (
|
|
7
|
+
// Make sure the target is a class or object (prototype)
|
|
8
|
+
typeof maybeTarget === 'function' || typeof maybeTarget === 'object' && maybeTarget !== null) &&
|
|
9
|
+
// Make sure the key is a string
|
|
10
|
+
typeof maybeKey === 'string' && (
|
|
11
|
+
// Make sure the descriptor is the right shape
|
|
12
|
+
typeof maybeDesc === 'object' && maybeDesc !== null && 'enumerable' in maybeDesc && 'configurable' in maybeDesc ||
|
|
13
|
+
// TS compatibility
|
|
14
|
+
maybeDesc === undefined)
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
function normalizeModelName(type) {
|
|
18
|
+
return type;
|
|
19
|
+
}
|
|
20
|
+
export { isElementDescriptor as i, normalizeModelName as n };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import '@warp-drive/utilities/string';
|
|
2
|
+
function isMaybeIdentifier(maybeIdentifier) {
|
|
3
|
+
return Boolean(maybeIdentifier !== null && typeof maybeIdentifier === 'object' && ('id' in maybeIdentifier && 'type' in maybeIdentifier && maybeIdentifier.id && maybeIdentifier.type || maybeIdentifier.lid));
|
|
4
|
+
}
|
|
5
|
+
function normalizeModelName(type) {
|
|
6
|
+
return type;
|
|
7
|
+
}
|
|
8
|
+
export { isMaybeIdentifier as i, normalizeModelName as n };
|