@warp-drive/legacy 5.8.0-alpha.4 → 5.8.0-alpha.40

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.
Files changed (213) hide show
  1. package/README.md +14 -27
  2. package/declarations/adapter/error.d.ts +5 -5
  3. package/declarations/adapter/json-api.d.ts +6 -8
  4. package/declarations/adapter/rest.d.ts +5 -8
  5. package/declarations/adapter.d.ts +4 -6
  6. package/declarations/compat/-private.d.ts +1 -1
  7. package/declarations/compat/builders/find-all.d.ts +6 -6
  8. package/declarations/compat/builders/find-record.d.ts +8 -8
  9. package/declarations/compat/builders/query.d.ts +12 -12
  10. package/declarations/compat/extensions.d.ts +1 -1
  11. package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts +7 -9
  12. package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +20 -30
  13. package/declarations/compat/utils.d.ts +17 -17
  14. package/declarations/compat.d.ts +35 -11
  15. package/declarations/index.d.ts +70 -0
  16. package/declarations/model/-private/attr.d.ts +5 -6
  17. package/declarations/model/-private/belongs-to.d.ts +4 -5
  18. package/declarations/model/-private/has-many.d.ts +4 -5
  19. package/declarations/model/-private/hooks.d.ts +1 -1
  20. package/declarations/model/-private/legacy-relationships-support.d.ts +2 -2
  21. package/declarations/model/-private/model.d.ts +8 -8
  22. package/declarations/model/-private/record-state.d.ts +1 -1
  23. package/declarations/model/-private/references/belongs-to.d.ts +5 -5
  24. package/declarations/model/-private/references/has-many.d.ts +3 -3
  25. package/declarations/model/migration-support.d.ts +46 -21
  26. package/declarations/model-fragments/extensions/fragment-array.d.ts +16 -0
  27. package/declarations/model-fragments/extensions/fragment.d.ts +15 -0
  28. package/declarations/model-fragments/hooks/model-for.d.ts +20 -0
  29. package/declarations/model-fragments/index.d.ts +5 -0
  30. package/declarations/model-fragments/instance-initializers/fragment-extensions.d.ts +9 -0
  31. package/declarations/model-fragments/utilities/with-array-defaults.d.ts +15 -0
  32. package/declarations/model-fragments/utilities/with-fragment-array-defaults.d.ts +20 -0
  33. package/declarations/model-fragments/utilities/with-fragment-defaults.d.ts +19 -0
  34. package/declarations/model-fragments/utilities/with-legacy.d.ts +3 -0
  35. package/declarations/model-fragments.d.ts +9 -0
  36. package/declarations/model.d.ts +2 -2
  37. package/declarations/serializer/-private/embedded-records-mixin.d.ts +1 -6
  38. package/declarations/serializer/-private/transforms/boolean.d.ts +2 -2
  39. package/declarations/serializer/-private/transforms/date.d.ts +2 -2
  40. package/declarations/serializer/-private/transforms/number.d.ts +1 -1
  41. package/declarations/serializer/-private/transforms/string.d.ts +1 -1
  42. package/declarations/serializer/json-api.d.ts +7 -9
  43. package/declarations/serializer/json.d.ts +7 -9
  44. package/declarations/serializer/rest.d.ts +4 -6
  45. package/declarations/serializer.d.ts +9 -12
  46. package/dist/{-private-8UmnAf9J.js → -private-B1pSSN52.js} +1 -1
  47. package/dist/adapter/-private.js +1 -1
  48. package/dist/adapter/error.js +12 -13
  49. package/dist/adapter/json-api.js +4 -1
  50. package/dist/adapter/rest.js +6 -9
  51. package/dist/adapter.js +4 -6
  52. package/dist/compat/-private.js +1 -1
  53. package/dist/compat/builders.js +26 -26
  54. package/dist/compat/utils.js +17 -18
  55. package/dist/compat.js +58 -41
  56. package/dist/{errors-8kD2mSe_.js → errors-CIGPcDvd.js} +87 -65
  57. package/dist/hooks-QqRnX108.js +74 -0
  58. package/dist/index.js +195 -0
  59. package/dist/{json-DziiodPf.js → json-BNrV8EYG.js} +12 -16
  60. package/dist/model/-private.js +1 -1
  61. package/dist/model/migration-support.js +54 -24
  62. package/dist/model-for-CqXsIKws.js +221 -0
  63. package/dist/model-fragments.js +76 -0
  64. package/dist/model.js +18 -90
  65. package/dist/{schema-provider-DQu4Rjco.js → schema-provider-g5MfTj8n.js} +18 -20
  66. package/dist/{serialize-into-hash-CS0MIv4F.js → serialize-into-hash-BnYvPex3.js} +1 -1
  67. package/dist/serializer/json-api.js +17 -44
  68. package/dist/serializer/json.js +1 -1
  69. package/dist/serializer/rest.js +14 -21
  70. package/dist/serializer/transform.js +15 -6
  71. package/dist/serializer.js +9 -13
  72. package/dist/store.js +3 -0
  73. package/dist/unpkg/dev/-private-DbaSCSym.js +1205 -0
  74. package/dist/unpkg/dev/adapter/-private.js +1 -0
  75. package/dist/unpkg/dev/adapter/error.js +335 -0
  76. package/dist/unpkg/dev/adapter/json-api.js +271 -0
  77. package/dist/unpkg/dev/adapter/rest.js +1255 -0
  78. package/dist/unpkg/dev/adapter.js +1252 -0
  79. package/dist/unpkg/dev/compat/-private.js +1 -0
  80. package/dist/unpkg/dev/compat/builders.js +275 -0
  81. package/dist/unpkg/dev/compat/extensions.js +242 -0
  82. package/dist/unpkg/dev/compat/utils.js +223 -0
  83. package/dist/unpkg/dev/compat.js +1146 -0
  84. package/dist/unpkg/dev/errors-DD96TBEs.js +2591 -0
  85. package/dist/unpkg/dev/hooks-CqWjNWeL.js +73 -0
  86. package/dist/unpkg/dev/index.js +197 -0
  87. package/dist/unpkg/dev/json-CCU-ZQ4b.js +1269 -0
  88. package/dist/unpkg/dev/model/-private.js +1 -0
  89. package/dist/unpkg/dev/model/migration-support.js +551 -0
  90. package/dist/unpkg/dev/model-for-CqXsIKws.js +221 -0
  91. package/dist/unpkg/dev/model-fragments.js +76 -0
  92. package/dist/unpkg/dev/model.js +678 -0
  93. package/dist/unpkg/dev/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  94. package/dist/unpkg/dev/schema-provider-B8jiJOYC.js +2229 -0
  95. package/dist/unpkg/dev/serialize-into-hash-CPAZXrQU.js +259 -0
  96. package/dist/unpkg/dev/serializer/json-api.js +648 -0
  97. package/dist/unpkg/dev/serializer/json.js +4 -0
  98. package/dist/unpkg/dev/serializer/rest.js +1242 -0
  99. package/dist/unpkg/dev/serializer/transform.js +278 -0
  100. package/dist/unpkg/dev/serializer.js +248 -0
  101. package/dist/unpkg/dev/store.js +636 -0
  102. package/dist/unpkg/dev/util-DvanW33H.js +20 -0
  103. package/dist/unpkg/dev/utils-BhvS1iTS.js +8 -0
  104. package/dist/unpkg/dev-deprecated/-private-DbaSCSym.js +1205 -0
  105. package/dist/unpkg/dev-deprecated/adapter/-private.js +1 -0
  106. package/dist/unpkg/dev-deprecated/adapter/error.js +335 -0
  107. package/dist/unpkg/dev-deprecated/adapter/json-api.js +271 -0
  108. package/dist/unpkg/dev-deprecated/adapter/rest.js +1255 -0
  109. package/dist/unpkg/dev-deprecated/adapter.js +1252 -0
  110. package/dist/unpkg/dev-deprecated/compat/-private.js +1 -0
  111. package/dist/unpkg/dev-deprecated/compat/builders.js +275 -0
  112. package/dist/unpkg/dev-deprecated/compat/extensions.js +242 -0
  113. package/dist/unpkg/dev-deprecated/compat/utils.js +223 -0
  114. package/dist/unpkg/dev-deprecated/compat.js +1146 -0
  115. package/dist/unpkg/dev-deprecated/errors-DEnabIZj.js +2594 -0
  116. package/dist/unpkg/dev-deprecated/hooks-CAll-Ets.js +73 -0
  117. package/dist/unpkg/dev-deprecated/index.js +196 -0
  118. package/dist/unpkg/dev-deprecated/json-CCU-ZQ4b.js +1269 -0
  119. package/dist/unpkg/dev-deprecated/model/-private.js +1 -0
  120. package/dist/unpkg/dev-deprecated/model/migration-support.js +568 -0
  121. package/dist/unpkg/dev-deprecated/model-for-CqXsIKws.js +221 -0
  122. package/dist/unpkg/dev-deprecated/model-fragments.js +76 -0
  123. package/dist/unpkg/dev-deprecated/model.js +682 -0
  124. package/dist/unpkg/dev-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  125. package/dist/unpkg/dev-deprecated/schema-provider-k2qXQTBg.js +2254 -0
  126. package/dist/unpkg/dev-deprecated/serialize-into-hash-CPAZXrQU.js +259 -0
  127. package/dist/unpkg/dev-deprecated/serializer/json-api.js +648 -0
  128. package/dist/unpkg/dev-deprecated/serializer/json.js +4 -0
  129. package/dist/unpkg/dev-deprecated/serializer/rest.js +1242 -0
  130. package/dist/unpkg/dev-deprecated/serializer/transform.js +278 -0
  131. package/dist/unpkg/dev-deprecated/serializer.js +248 -0
  132. package/dist/unpkg/dev-deprecated/store.js +636 -0
  133. package/dist/unpkg/dev-deprecated/util-CWr5WQOT.js +24 -0
  134. package/dist/unpkg/dev-deprecated/utils-C9PJehtL.js +12 -0
  135. package/dist/unpkg/prod/-private-Cvf_97EG.js +970 -0
  136. package/dist/unpkg/prod/adapter/-private.js +1 -0
  137. package/dist/unpkg/prod/adapter/error.js +330 -0
  138. package/dist/unpkg/prod/adapter/json-api.js +266 -0
  139. package/dist/unpkg/prod/adapter/rest.js +1218 -0
  140. package/dist/unpkg/prod/adapter.js +1219 -0
  141. package/dist/unpkg/prod/compat/-private.js +1 -0
  142. package/dist/unpkg/prod/compat/builders.js +210 -0
  143. package/dist/unpkg/prod/compat/extensions.js +232 -0
  144. package/dist/unpkg/prod/compat/utils.js +218 -0
  145. package/dist/unpkg/prod/compat.js +726 -0
  146. package/dist/unpkg/prod/errors-CXnfnBfQ.js +2343 -0
  147. package/dist/unpkg/prod/hooks-DvyWhLNg.js +41 -0
  148. package/dist/unpkg/prod/index.js +151 -0
  149. package/dist/unpkg/prod/json-BYrUP8ao.js +1256 -0
  150. package/dist/unpkg/prod/model/-private.js +1 -0
  151. package/dist/unpkg/prod/model/migration-support.js +544 -0
  152. package/dist/unpkg/prod/model-for-CqXsIKws.js +221 -0
  153. package/dist/unpkg/prod/model-fragments.js +76 -0
  154. package/dist/unpkg/prod/model.js +593 -0
  155. package/dist/unpkg/prod/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  156. package/dist/unpkg/prod/schema-provider-CHujJvA9.js +1904 -0
  157. package/dist/unpkg/prod/serialize-into-hash-DYU2egXl.js +215 -0
  158. package/dist/unpkg/prod/serializer/json-api.js +591 -0
  159. package/dist/unpkg/prod/serializer/json.js +4 -0
  160. package/dist/unpkg/prod/serializer/rest.js +1210 -0
  161. package/dist/unpkg/prod/serializer/transform.js +278 -0
  162. package/dist/unpkg/prod/serializer.js +248 -0
  163. package/dist/unpkg/prod/store.js +504 -0
  164. package/dist/unpkg/prod/util-DvanW33H.js +20 -0
  165. package/dist/unpkg/prod/utils-BhvS1iTS.js +8 -0
  166. package/dist/unpkg/prod-deprecated/-private-Cvf_97EG.js +970 -0
  167. package/dist/unpkg/prod-deprecated/adapter/-private.js +1 -0
  168. package/dist/unpkg/prod-deprecated/adapter/error.js +330 -0
  169. package/dist/unpkg/prod-deprecated/adapter/json-api.js +266 -0
  170. package/dist/unpkg/prod-deprecated/adapter/rest.js +1218 -0
  171. package/dist/unpkg/prod-deprecated/adapter.js +1219 -0
  172. package/dist/unpkg/prod-deprecated/compat/-private.js +1 -0
  173. package/dist/unpkg/prod-deprecated/compat/builders.js +210 -0
  174. package/dist/unpkg/prod-deprecated/compat/extensions.js +232 -0
  175. package/dist/unpkg/prod-deprecated/compat/utils.js +218 -0
  176. package/dist/unpkg/prod-deprecated/compat.js +726 -0
  177. package/dist/unpkg/prod-deprecated/errors-CG1SPYVg.js +2346 -0
  178. package/dist/unpkg/prod-deprecated/hooks-BIUBiNGR.js +41 -0
  179. package/dist/unpkg/prod-deprecated/index.js +150 -0
  180. package/dist/unpkg/prod-deprecated/json-BYrUP8ao.js +1256 -0
  181. package/dist/unpkg/prod-deprecated/model/-private.js +1 -0
  182. package/dist/unpkg/prod-deprecated/model/migration-support.js +561 -0
  183. package/dist/unpkg/prod-deprecated/model-for-CqXsIKws.js +221 -0
  184. package/dist/unpkg/prod-deprecated/model-fragments.js +76 -0
  185. package/dist/unpkg/prod-deprecated/model.js +596 -0
  186. package/dist/unpkg/prod-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  187. package/dist/unpkg/prod-deprecated/schema-provider-BJ4TWnZf.js +1947 -0
  188. package/dist/unpkg/prod-deprecated/serialize-into-hash-DYU2egXl.js +215 -0
  189. package/dist/unpkg/prod-deprecated/serializer/json-api.js +591 -0
  190. package/dist/unpkg/prod-deprecated/serializer/json.js +4 -0
  191. package/dist/unpkg/prod-deprecated/serializer/rest.js +1210 -0
  192. package/dist/unpkg/prod-deprecated/serializer/transform.js +278 -0
  193. package/dist/unpkg/prod-deprecated/serializer.js +248 -0
  194. package/dist/unpkg/prod-deprecated/store.js +504 -0
  195. package/dist/unpkg/prod-deprecated/util-B6cn-i93.js +23 -0
  196. package/dist/unpkg/prod-deprecated/utils-BUWwQwCh.js +11 -0
  197. package/logos/README.md +2 -2
  198. package/logos/logo-yellow-slab.svg +1 -0
  199. package/logos/word-mark-black.svg +1 -0
  200. package/logos/word-mark-white.svg +1 -0
  201. package/package.json +14 -6
  202. package/logos/NCC-1701-a-blue.svg +0 -4
  203. package/logos/NCC-1701-a-gold.svg +0 -4
  204. package/logos/NCC-1701-a-gold_100.svg +0 -1
  205. package/logos/NCC-1701-a-gold_base-64.txt +0 -1
  206. package/logos/NCC-1701-a.svg +0 -4
  207. package/logos/docs-badge.svg +0 -2
  208. package/logos/ember-data-logo-dark.svg +0 -12
  209. package/logos/ember-data-logo-light.svg +0 -12
  210. package/logos/social1.png +0 -0
  211. package/logos/social2.png +0 -0
  212. package/logos/warp-drive-logo-dark.svg +0 -4
  213. package/logos/warp-drive-logo-gold.svg +0 -4
@@ -0,0 +1,636 @@
1
+ import { defineSignal, assertPrivateStore, ensureStringId, recordIdentifierFor, constructResource, storeFor } 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 { getOrSetGlobal } from '@warp-drive/core/types/-private';
6
+
7
+ /*
8
+ When a find request is triggered on the store, the user can optionally pass in
9
+ attributes and relationships to be preloaded. These are meant to behave as if they
10
+ came back from the server, except the user obtained them out of band and is informing
11
+ the store of their existence. The most common use case is for supporting client side
12
+ nested URLs, such as `/posts/1/comments/2` so the user can do
13
+ `store.findRecord('comment', 2, { preload: { post: 1 } })` without having to fetch the post.
14
+
15
+ Preloaded data can be attributes and relationships passed in either as IDs or as actual
16
+ models.
17
+ */
18
+
19
+ function preloadData(store, identifier, preload) {
20
+ const jsonPayload = {};
21
+ //TODO(Igor) consider the polymorphic case
22
+ const schemas = store.schema;
23
+ const fields = schemas.fields(identifier);
24
+ Object.keys(preload).forEach(key => {
25
+ const preloadValue = preload[key];
26
+ const field = fields.get(key);
27
+ if (field && (field.kind === 'hasMany' || field.kind === 'belongsTo')) {
28
+ if (!jsonPayload.relationships) {
29
+ jsonPayload.relationships = {};
30
+ }
31
+ jsonPayload.relationships[key] = preloadRelationship(field, preloadValue);
32
+ } else {
33
+ if (!jsonPayload.attributes) {
34
+ jsonPayload.attributes = {};
35
+ }
36
+ jsonPayload.attributes[key] = preloadValue;
37
+ }
38
+ });
39
+ const cache = store.cache;
40
+ assertPrivateStore(store);
41
+ const hasRecord = Boolean(store._instanceCache.peek(identifier));
42
+ cache.upsert(identifier, jsonPayload, hasRecord);
43
+ }
44
+ function preloadRelationship(schema, preloadValue) {
45
+ const relatedType = schema.type;
46
+ if (schema.kind === 'hasMany') {
47
+ (test => {
48
+ if (!test) {
49
+ throw new Error('You need to pass in an array to set a hasMany property on a record');
50
+ }
51
+ })(Array.isArray(preloadValue));
52
+ return {
53
+ data: preloadValue.map(value => _convertPreloadRelationshipToJSON(value, relatedType))
54
+ };
55
+ }
56
+ (test => {
57
+ if (!test) {
58
+ throw new Error('You should not pass in an array to set a belongsTo property on a record');
59
+ }
60
+ })(!Array.isArray(preloadValue));
61
+ return {
62
+ data: preloadValue ? _convertPreloadRelationshipToJSON(preloadValue, relatedType) : null
63
+ };
64
+ }
65
+
66
+ /*
67
+ findRecord('user', '1', { preload: { friends: ['1'] }});
68
+ findRecord('user', '1', { preload: { friends: [record] }});
69
+ */
70
+ function _convertPreloadRelationshipToJSON(value, type) {
71
+ if (typeof value === 'string' || typeof value === 'number') {
72
+ return {
73
+ type,
74
+ id: ensureStringId(value)
75
+ };
76
+ }
77
+ // TODO if not a record instance assert it's an identifier
78
+ // and allow identifiers to be used
79
+ return recordIdentifierFor(value);
80
+ }
81
+
82
+ /**
83
+ * Minimum subset of static schema methods and properties on the
84
+ * "model" class.
85
+ *
86
+ * Only used when using the legacy schema-service implementation
87
+ * for @ember-data/model or when wrapping schema for legacy
88
+ * Adapters/Serializers.
89
+ *
90
+ */
91
+
92
+ function _resourceIsFullDeleted(identifier, cache) {
93
+ return cache.isDeletionCommitted(identifier) || cache.isNew(identifier) && cache.isDeleted(identifier);
94
+ }
95
+ function resourceIsFullyDeleted(instanceCache, identifier) {
96
+ const cache = instanceCache.cache;
97
+ return !cache || _resourceIsFullDeleted(identifier, cache);
98
+ }
99
+
100
+ /**
101
+ A `RecordReference` is a low-level API that allows users and
102
+ addon authors to perform meta-operations on a record.
103
+
104
+ @hideconstructor
105
+ @public
106
+ */
107
+ class RecordReference {
108
+ /** @internal */
109
+
110
+ /** @internal */
111
+ // unsubscribe token given to us by the notification manager
112
+ ___token;
113
+ /** @internal */
114
+ ___identifier;
115
+
116
+ /** @internal */
117
+
118
+ constructor(store, identifier) {
119
+ this.store = store;
120
+ this.___identifier = identifier;
121
+ this.___token = store.notifications.subscribe(identifier, (_, bucket, notifiedKey) => {
122
+ if (bucket === 'identity' || bucket === 'attributes' && notifiedKey === 'id') {
123
+ this._ref++;
124
+ }
125
+ });
126
+ }
127
+
128
+ /** @internal */
129
+ destroy() {
130
+ this.store.notifications.unsubscribe(this.___token);
131
+ }
132
+ get type() {
133
+ return this.identifier().type;
134
+ }
135
+
136
+ /**
137
+ The `id` of the record that this reference refers to.
138
+ Together, the `type` and `id` properties form a composite key for
139
+ the identity map.
140
+ Example
141
+ ```javascript
142
+ let userRef = store.getReference('user', 1);
143
+ userRef.id(); // '1'
144
+ ```
145
+ @public
146
+ @return The id of the record.
147
+ */
148
+ id() {
149
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
150
+ this._ref; // consume the tracked prop
151
+ return this.___identifier.id;
152
+ }
153
+
154
+ /**
155
+ The `identifier` of the record that this reference refers to.
156
+ Together, the `type` and `id` properties form a composite key for
157
+ the identity map.
158
+ Example
159
+ ```javascript
160
+ let userRef = store.getReference('user', 1);
161
+ userRef.identifier(); // '1'
162
+ ```
163
+ @public
164
+ @return The identifier of the record.
165
+ */
166
+ identifier() {
167
+ return this.___identifier;
168
+ }
169
+
170
+ /**
171
+ How the reference will be looked up when it is loaded. Currently
172
+ this always returns `identity` to signify that a record will be
173
+ loaded by its `type` and `id`.
174
+ Example
175
+ ```javascript
176
+ const userRef = store.getReference('user', 1);
177
+ userRef.remoteType(); // 'identity'
178
+ ```
179
+ @public
180
+ */
181
+ remoteType() {
182
+ return 'identity';
183
+ }
184
+
185
+ /**
186
+ This API allows you to provide a reference with new data. The
187
+ simplest usage of this API is similar to `store.push`: you provide a
188
+ normalized hash of data and the object represented by the reference
189
+ will update.
190
+ If you pass a promise to `push`, Ember Data will not ask the adapter
191
+ for the data if another attempt to fetch it is made in the
192
+ interim. When the promise resolves, the underlying object is updated
193
+ with the new data, and the promise returned by *this function* is resolved
194
+ with that object.
195
+ For example, `recordReference.push(promise)` will be resolved with a
196
+ record.
197
+ Example
198
+ ```javascript
199
+ let userRef = store.getReference('user', 1);
200
+ // provide data for reference
201
+ userRef.push({
202
+ data: {
203
+ id: "1",
204
+ type: "user",
205
+ attributes: {
206
+ username: "@user"
207
+ }
208
+ }
209
+ }).then(function(user) {
210
+ userRef.value() === user;
211
+ });
212
+ ```
213
+ @public
214
+ @param objectOrPromise a JSON:API ResourceDocument or a promise resolving to one
215
+ @return a promise for the value (record or relationship)
216
+ */
217
+ push(objectOrPromise) {
218
+ // TODO @deprecate pushing unresolved payloads
219
+ return Promise.resolve(objectOrPromise).then(data => {
220
+ return this.store.push(data);
221
+ });
222
+ }
223
+
224
+ /**
225
+ If the entity referred to by the reference is already loaded, it is
226
+ present as `reference.value`. Otherwise the value returned by this function
227
+ is `null`.
228
+ Example
229
+ ```javascript
230
+ let userRef = store.getReference('user', 1);
231
+ userRef.value(); // user
232
+ ```
233
+ @public
234
+ @return {Model} the record for this RecordReference
235
+ */
236
+ value() {
237
+ return this.store.peekRecord(this.___identifier);
238
+ }
239
+
240
+ /**
241
+ Triggers a fetch for the backing entity based on its `remoteType`
242
+ (see `remoteType` definitions per reference type).
243
+ Example
244
+ ```javascript
245
+ let userRef = store.getReference('user', 1);
246
+ // load user (via store.find)
247
+ userRef.load().then(...)
248
+ ```
249
+ @public
250
+ @return the record for this RecordReference
251
+ */
252
+ load() {
253
+ const id = this.id();
254
+ if (id !== null) {
255
+ return this.store.findRecord(this.type, id);
256
+ }
257
+ (test => {
258
+ {
259
+ throw new Error(`Unable to fetch record of type ${this.type} without an id`);
260
+ }
261
+ })();
262
+ }
263
+
264
+ /**
265
+ Reloads the record if it is already loaded. If the record is not
266
+ loaded it will load the record via `store.findRecord`
267
+ Example
268
+ ```javascript
269
+ let userRef = store.getReference('user', 1);
270
+ // or trigger a reload
271
+ userRef.reload().then(...)
272
+ ```
273
+ @public
274
+ @return the record for this RecordReference
275
+ */
276
+ reload() {
277
+ const id = this.id();
278
+ if (id !== null) {
279
+ return this.store.findRecord(this.type, id, {
280
+ reload: true
281
+ });
282
+ }
283
+ (test => {
284
+ {
285
+ throw new Error(`Unable to fetch record of type ${this.type} without an id`);
286
+ }
287
+ })();
288
+ }
289
+ }
290
+ defineSignal(RecordReference.prototype, '_ref');
291
+
292
+ // if modelFor turns out to be a bottleneck we should replace with a Map
293
+ // and clear it during store teardown.
294
+ const AvailableShims = getOrSetGlobal('AvailableShims', new WeakMap());
295
+ function getShimClass(store, modelName) {
296
+ let shims = AvailableShims.get(store);
297
+ if (!shims) {
298
+ shims = Object.create(null);
299
+ AvailableShims.set(store, shims);
300
+ }
301
+ let shim = shims[modelName];
302
+ if (shim === undefined) {
303
+ shim = shims[modelName] = new ShimModelClass(store, modelName);
304
+ }
305
+ return shim;
306
+ }
307
+
308
+ // Mimics the static apis of @ember-data/model
309
+ class ShimModelClass {
310
+ constructor(store, modelName) {
311
+ this.__store = store;
312
+ this.modelName = modelName;
313
+ }
314
+ get fields() {
315
+ const fields = new Map();
316
+ const fieldSchemas = this.__store.schema.fields({
317
+ type: this.modelName
318
+ });
319
+ fieldSchemas.forEach((schema, key) => {
320
+ if (schema.kind === 'attribute' || schema.kind === 'belongsTo' || schema.kind === 'hasMany') {
321
+ fields.set(key, schema.kind);
322
+ }
323
+ });
324
+ return fields;
325
+ }
326
+ get attributes() {
327
+ const attrs = new Map();
328
+ const fields = this.__store.schema.fields({
329
+ type: this.modelName
330
+ });
331
+ fields.forEach((schema, key) => {
332
+ if (schema.kind === 'attribute') {
333
+ attrs.set(key, schema);
334
+ }
335
+ });
336
+ return attrs;
337
+ }
338
+ get relationshipsByName() {
339
+ const rels = new Map();
340
+ const fields = this.__store.schema.fields({
341
+ type: this.modelName
342
+ });
343
+ fields.forEach((schema, key) => {
344
+ if (schema.kind === 'belongsTo' || schema.kind === 'hasMany') {
345
+ rels.set(key, schema);
346
+ }
347
+ });
348
+ return rels;
349
+ }
350
+ eachAttribute(callback, binding) {
351
+ this.__store.schema.fields({
352
+ type: this.modelName
353
+ }).forEach((schema, key) => {
354
+ if (schema.kind === 'attribute') {
355
+ callback.call(binding, key, schema);
356
+ }
357
+ });
358
+ }
359
+ eachRelationship(callback, binding) {
360
+ this.__store.schema.fields({
361
+ type: this.modelName
362
+ }).forEach((schema, key) => {
363
+ if (schema.kind === 'belongsTo' || schema.kind === 'hasMany') {
364
+ callback.call(binding, key, schema);
365
+ }
366
+ });
367
+ }
368
+ eachTransformedAttribute(callback, binding) {
369
+ this.__store.schema.fields({
370
+ type: this.modelName
371
+ }).forEach((schema, key) => {
372
+ if (schema.kind === 'attribute') {
373
+ const type = schema.type;
374
+ if (type) callback.call(binding, key, type);
375
+ }
376
+ });
377
+ }
378
+ }
379
+ function restoreDeprecatedStoreBehaviors(StoreKlass) {
380
+ StoreKlass.prototype.findRecord = function (resource, id, options) {
381
+ (test => {
382
+ if (!test) {
383
+ throw new Error(`Attempted to call store.findRecord(), but the store instance has already been destroyed.`);
384
+ }
385
+ })(!(this.isDestroying || this.isDestroyed));
386
+ (test => {
387
+ if (!test) {
388
+ throw new Error(`You need to pass a modelName or resource identifier as the first argument to the store's findRecord method`);
389
+ }
390
+ })(resource);
391
+ if (isMaybeIdentifier(resource)) {
392
+ options = id;
393
+ } else {
394
+ (test => {
395
+ if (!test) {
396
+ throw new Error(`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${resource}`);
397
+ }
398
+ })(typeof resource === 'string');
399
+ const type = normalizeModelName(resource);
400
+ const normalizedId = ensureStringId(id);
401
+ resource = constructResource(type, normalizedId);
402
+ }
403
+ const identifier = this.cacheKeyManager.getOrCreateRecordIdentifier(resource);
404
+ options = options || {};
405
+ assertPrivateStore(this);
406
+ if (options.preload) {
407
+ // force reload if we preload to ensure we don't resolve the promise
408
+ // until we are complete, else we will end up background-reloading
409
+ // even for initial load.
410
+ if (!this._instanceCache.recordIsLoaded(identifier)) {
411
+ options.reload = true;
412
+ }
413
+ this._join(() => {
414
+ preloadData(this, identifier, options.preload);
415
+ });
416
+ }
417
+ const promise = this.request({
418
+ op: 'findRecord',
419
+ data: {
420
+ record: identifier,
421
+ options
422
+ },
423
+ cacheOptions: {
424
+ [SkipCache]: true
425
+ }
426
+ });
427
+ return promise.then(document => {
428
+ return document.content;
429
+ });
430
+ };
431
+ StoreKlass.prototype.findAll = function (type, options = {}) {
432
+ (test => {
433
+ if (!test) {
434
+ throw new Error(`Attempted to call store.findAll(), but the store instance has already been destroyed.`);
435
+ }
436
+ })(!(this.isDestroying || this.isDestroyed));
437
+ (test => {
438
+ if (!test) {
439
+ throw new Error(`You need to pass a model name to the store's findAll method`);
440
+ }
441
+ })(type);
442
+ (test => {
443
+ if (!test) {
444
+ throw new Error(`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${type}`);
445
+ }
446
+ })(typeof type === 'string');
447
+ const promise = this.request({
448
+ op: 'findAll',
449
+ data: {
450
+ type: normalizeModelName(type),
451
+ options: options || {}
452
+ },
453
+ cacheOptions: {
454
+ [SkipCache]: true
455
+ }
456
+ });
457
+ return promise.then(document => document.content);
458
+ };
459
+ StoreKlass.prototype.query = function (type, query, options = {}) {
460
+ (test => {
461
+ if (!test) {
462
+ throw new Error(`Attempted to call store.query(), but the store instance has already been destroyed.`);
463
+ }
464
+ })(!(this.isDestroying || this.isDestroyed));
465
+ (test => {
466
+ if (!test) {
467
+ throw new Error(`You need to pass a model name to the store's query method`);
468
+ }
469
+ })(type);
470
+ (test => {
471
+ if (!test) {
472
+ throw new Error(`You need to pass a query hash to the store's query method`);
473
+ }
474
+ })(query);
475
+ (test => {
476
+ if (!test) {
477
+ throw new Error(`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${type}`);
478
+ }
479
+ })(typeof type === 'string');
480
+ const promise = this.request({
481
+ op: 'query',
482
+ data: {
483
+ type: normalizeModelName(type),
484
+ query,
485
+ options: options
486
+ },
487
+ cacheOptions: {
488
+ [SkipCache]: true
489
+ }
490
+ });
491
+ return promise.then(document => document.content);
492
+ };
493
+ StoreKlass.prototype.queryRecord = function (type, query, options) {
494
+ (test => {
495
+ if (!test) {
496
+ throw new Error(`Attempted to call store.queryRecord(), but the store instance has already been destroyed.`);
497
+ }
498
+ })(!(this.isDestroying || this.isDestroyed));
499
+ (test => {
500
+ if (!test) {
501
+ throw new Error(`You need to pass a model name to the store's queryRecord method`);
502
+ }
503
+ })(type);
504
+ (test => {
505
+ if (!test) {
506
+ throw new Error(`You need to pass a query hash to the store's queryRecord method`);
507
+ }
508
+ })(query);
509
+ (test => {
510
+ if (!test) {
511
+ throw new Error(`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${type}`);
512
+ }
513
+ })(typeof type === 'string');
514
+ const promise = this.request({
515
+ op: 'queryRecord',
516
+ data: {
517
+ type: normalizeModelName(type),
518
+ query,
519
+ options: options || {}
520
+ },
521
+ cacheOptions: {
522
+ [SkipCache]: true
523
+ }
524
+ });
525
+ return promise.then(document => document.content);
526
+ };
527
+
528
+ // do not put a ts-expect-error here, because typedoc will fail to
529
+ // build due to this error only occurring when references are in use in the editor
530
+ StoreKlass.prototype.getReference = function (resource, id) {
531
+ (test => {
532
+ if (!test) {
533
+ throw new Error(`Attempted to call store.getReference(), but the store instance has already been destroyed.`);
534
+ }
535
+ })(!(this.isDestroying || this.isDestroyed));
536
+ let resourceIdentifier;
537
+ if (arguments.length === 1 && isMaybeIdentifier(resource)) {
538
+ resourceIdentifier = resource;
539
+ } else {
540
+ const type = normalizeModelName(resource);
541
+ const normalizedId = ensureStringId(id);
542
+ resourceIdentifier = constructResource(type, normalizedId);
543
+ }
544
+ (test => {
545
+ if (!test) {
546
+ throw new Error('getReference expected to receive either a resource identifier or type and id as arguments');
547
+ }
548
+ })(isMaybeIdentifier(resourceIdentifier));
549
+ const identifier = this.cacheKeyManager.getOrCreateRecordIdentifier(resourceIdentifier);
550
+ assertPrivateStore(this);
551
+ const cache = upgradeInstanceCaches(this._instanceCache.__instances).reference;
552
+ let reference = cache.get(identifier);
553
+ if (!reference) {
554
+ reference = new RecordReference(this, identifier);
555
+ cache.set(identifier, reference);
556
+ }
557
+ return reference;
558
+ };
559
+ StoreKlass.prototype.modelFor = function (type) {
560
+ (test => {
561
+ if (!test) {
562
+ throw new Error(`Attempted to call store.modelFor(), but the store instance has already been destroyed.`);
563
+ }
564
+ })(!this.isDestroyed);
565
+ (test => {
566
+ if (!test) {
567
+ throw new Error(`You need to pass <type> to the store's modelFor method`);
568
+ }
569
+ })(typeof type === 'string' && type.length);
570
+ (test => {
571
+ if (!test) {
572
+ throw new Error(`No model was found for '${type}' and no schema handles the type`);
573
+ }
574
+ })(this.schema.hasResource({
575
+ type
576
+ }));
577
+ return getShimClass(this, type);
578
+ };
579
+ StoreKlass.prototype.saveRecord = function (record, options = {}) {
580
+ (test => {
581
+ if (!test) {
582
+ throw new Error(`Attempted to call store.saveRecord(), but the store instance has already been destroyed.`);
583
+ }
584
+ })(!(this.isDestroying || this.isDestroyed));
585
+ (test => {
586
+ if (!test) {
587
+ throw new Error(`Unable to initiate save for a record in a disconnected state`);
588
+ }
589
+ })(storeFor(record, true));
590
+ const identifier = recordIdentifierFor(record);
591
+ const cache = this.cache;
592
+ if (!identifier) {
593
+ // this commonly means we're disconnected
594
+ // but just in case we reject here to prevent bad things.
595
+ return Promise.reject(new Error(`Record Is Disconnected`));
596
+ }
597
+ assertPrivateStore(this);
598
+ (test => {
599
+ if (!test) {
600
+ throw new Error(`Cannot initiate a save request for an unloaded record: ${identifier.lid}`);
601
+ }
602
+ })(this._instanceCache.recordIsLoaded(identifier));
603
+ if (resourceIsFullyDeleted(this._instanceCache, identifier)) {
604
+ return Promise.resolve(record);
605
+ }
606
+ if (!options) {
607
+ options = {};
608
+ }
609
+ let operation = 'updateRecord';
610
+ if (cache.isNew(identifier)) {
611
+ operation = 'createRecord';
612
+ } else if (cache.isDeleted(identifier)) {
613
+ operation = 'deleteRecord';
614
+ }
615
+ const request = {
616
+ op: operation,
617
+ data: {
618
+ options,
619
+ record: identifier
620
+ },
621
+ records: [identifier],
622
+ cacheOptions: {
623
+ [SkipCache]: true
624
+ }
625
+ };
626
+ return this.request(request).then(document => document.content);
627
+ };
628
+ }
629
+ function upgradeInstanceCaches(cache) {
630
+ const withReferences = cache;
631
+ if (!withReferences.reference) {
632
+ withReferences.reference = new WeakMap();
633
+ }
634
+ return withReferences;
635
+ }
636
+ 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 };