@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,504 @@
1
+ import { defineSignal, 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 { 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
+ return {
48
+ data: preloadValue.map(value => _convertPreloadRelationshipToJSON(value, relatedType))
49
+ };
50
+ }
51
+ return {
52
+ data: preloadValue ? _convertPreloadRelationshipToJSON(preloadValue, relatedType) : null
53
+ };
54
+ }
55
+
56
+ /*
57
+ findRecord('user', '1', { preload: { friends: ['1'] }});
58
+ findRecord('user', '1', { preload: { friends: [record] }});
59
+ */
60
+ function _convertPreloadRelationshipToJSON(value, type) {
61
+ if (typeof value === 'string' || typeof value === 'number') {
62
+ return {
63
+ type,
64
+ id: ensureStringId(value)
65
+ };
66
+ }
67
+ // TODO if not a record instance assert it's an identifier
68
+ // and allow identifiers to be used
69
+ return recordIdentifierFor(value);
70
+ }
71
+
72
+ /**
73
+ * Minimum subset of static schema methods and properties on the
74
+ * "model" class.
75
+ *
76
+ * Only used when using the legacy schema-service implementation
77
+ * for @ember-data/model or when wrapping schema for legacy
78
+ * Adapters/Serializers.
79
+ *
80
+ */
81
+
82
+ function _resourceIsFullDeleted(identifier, cache) {
83
+ return cache.isDeletionCommitted(identifier) || cache.isNew(identifier) && cache.isDeleted(identifier);
84
+ }
85
+ function resourceIsFullyDeleted(instanceCache, identifier) {
86
+ const cache = instanceCache.cache;
87
+ return !cache || _resourceIsFullDeleted(identifier, cache);
88
+ }
89
+
90
+ /**
91
+ A `RecordReference` is a low-level API that allows users and
92
+ addon authors to perform meta-operations on a record.
93
+
94
+ @hideconstructor
95
+ @public
96
+ */
97
+ class RecordReference {
98
+ /** @internal */
99
+
100
+ /** @internal */
101
+ // unsubscribe token given to us by the notification manager
102
+ ___token;
103
+ /** @internal */
104
+ ___identifier;
105
+
106
+ /** @internal */
107
+
108
+ constructor(store, identifier) {
109
+ this.store = store;
110
+ this.___identifier = identifier;
111
+ this.___token = store.notifications.subscribe(identifier, (_, bucket, notifiedKey) => {
112
+ if (bucket === 'identity' || bucket === 'attributes' && notifiedKey === 'id') {
113
+ this._ref++;
114
+ }
115
+ });
116
+ }
117
+
118
+ /** @internal */
119
+ destroy() {
120
+ this.store.notifications.unsubscribe(this.___token);
121
+ }
122
+ get type() {
123
+ return this.identifier().type;
124
+ }
125
+
126
+ /**
127
+ The `id` of the record that this reference refers to.
128
+ Together, the `type` and `id` properties form a composite key for
129
+ the identity map.
130
+ Example
131
+ ```javascript
132
+ let userRef = store.getReference('user', 1);
133
+ userRef.id(); // '1'
134
+ ```
135
+ @public
136
+ @return The id of the record.
137
+ */
138
+ id() {
139
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
140
+ this._ref; // consume the tracked prop
141
+ return this.___identifier.id;
142
+ }
143
+
144
+ /**
145
+ The `identifier` of the record that this reference refers to.
146
+ Together, the `type` and `id` properties form a composite key for
147
+ the identity map.
148
+ Example
149
+ ```javascript
150
+ let userRef = store.getReference('user', 1);
151
+ userRef.identifier(); // '1'
152
+ ```
153
+ @public
154
+ @return The identifier of the record.
155
+ */
156
+ identifier() {
157
+ return this.___identifier;
158
+ }
159
+
160
+ /**
161
+ How the reference will be looked up when it is loaded. Currently
162
+ this always returns `identity` to signify that a record will be
163
+ loaded by its `type` and `id`.
164
+ Example
165
+ ```javascript
166
+ const userRef = store.getReference('user', 1);
167
+ userRef.remoteType(); // 'identity'
168
+ ```
169
+ @public
170
+ */
171
+ remoteType() {
172
+ return 'identity';
173
+ }
174
+
175
+ /**
176
+ This API allows you to provide a reference with new data. The
177
+ simplest usage of this API is similar to `store.push`: you provide a
178
+ normalized hash of data and the object represented by the reference
179
+ will update.
180
+ If you pass a promise to `push`, Ember Data will not ask the adapter
181
+ for the data if another attempt to fetch it is made in the
182
+ interim. When the promise resolves, the underlying object is updated
183
+ with the new data, and the promise returned by *this function* is resolved
184
+ with that object.
185
+ For example, `recordReference.push(promise)` will be resolved with a
186
+ record.
187
+ Example
188
+ ```javascript
189
+ let userRef = store.getReference('user', 1);
190
+ // provide data for reference
191
+ userRef.push({
192
+ data: {
193
+ id: "1",
194
+ type: "user",
195
+ attributes: {
196
+ username: "@user"
197
+ }
198
+ }
199
+ }).then(function(user) {
200
+ userRef.value() === user;
201
+ });
202
+ ```
203
+ @public
204
+ @param objectOrPromise a JSON:API ResourceDocument or a promise resolving to one
205
+ @return a promise for the value (record or relationship)
206
+ */
207
+ push(objectOrPromise) {
208
+ // TODO @deprecate pushing unresolved payloads
209
+ return Promise.resolve(objectOrPromise).then(data => {
210
+ return this.store.push(data);
211
+ });
212
+ }
213
+
214
+ /**
215
+ If the entity referred to by the reference is already loaded, it is
216
+ present as `reference.value`. Otherwise the value returned by this function
217
+ is `null`.
218
+ Example
219
+ ```javascript
220
+ let userRef = store.getReference('user', 1);
221
+ userRef.value(); // user
222
+ ```
223
+ @public
224
+ @return {Model} the record for this RecordReference
225
+ */
226
+ value() {
227
+ return this.store.peekRecord(this.___identifier);
228
+ }
229
+
230
+ /**
231
+ Triggers a fetch for the backing entity based on its `remoteType`
232
+ (see `remoteType` definitions per reference type).
233
+ Example
234
+ ```javascript
235
+ let userRef = store.getReference('user', 1);
236
+ // load user (via store.find)
237
+ userRef.load().then(...)
238
+ ```
239
+ @public
240
+ @return the record for this RecordReference
241
+ */
242
+ load() {
243
+ const id = this.id();
244
+ if (id !== null) {
245
+ return this.store.findRecord(this.type, id);
246
+ }
247
+ }
248
+
249
+ /**
250
+ Reloads the record if it is already loaded. If the record is not
251
+ loaded it will load the record via `store.findRecord`
252
+ Example
253
+ ```javascript
254
+ let userRef = store.getReference('user', 1);
255
+ // or trigger a reload
256
+ userRef.reload().then(...)
257
+ ```
258
+ @public
259
+ @return the record for this RecordReference
260
+ */
261
+ reload() {
262
+ const id = this.id();
263
+ if (id !== null) {
264
+ return this.store.findRecord(this.type, id, {
265
+ reload: true
266
+ });
267
+ }
268
+ }
269
+ }
270
+ defineSignal(RecordReference.prototype, '_ref');
271
+
272
+ // if modelFor turns out to be a bottleneck we should replace with a Map
273
+ // and clear it during store teardown.
274
+ const AvailableShims = getOrSetGlobal('AvailableShims', new WeakMap());
275
+ function getShimClass(store, modelName) {
276
+ let shims = AvailableShims.get(store);
277
+ if (!shims) {
278
+ shims = Object.create(null);
279
+ AvailableShims.set(store, shims);
280
+ }
281
+ let shim = shims[modelName];
282
+ if (shim === undefined) {
283
+ shim = shims[modelName] = new ShimModelClass(store, modelName);
284
+ }
285
+ return shim;
286
+ }
287
+
288
+ // Mimics the static apis of @ember-data/model
289
+ class ShimModelClass {
290
+ constructor(store, modelName) {
291
+ this.__store = store;
292
+ this.modelName = modelName;
293
+ }
294
+ get fields() {
295
+ const fields = new Map();
296
+ const fieldSchemas = this.__store.schema.fields({
297
+ type: this.modelName
298
+ });
299
+ fieldSchemas.forEach((schema, key) => {
300
+ if (schema.kind === 'attribute' || schema.kind === 'belongsTo' || schema.kind === 'hasMany') {
301
+ fields.set(key, schema.kind);
302
+ }
303
+ });
304
+ return fields;
305
+ }
306
+ get attributes() {
307
+ const attrs = new Map();
308
+ const fields = this.__store.schema.fields({
309
+ type: this.modelName
310
+ });
311
+ fields.forEach((schema, key) => {
312
+ if (schema.kind === 'attribute') {
313
+ attrs.set(key, schema);
314
+ }
315
+ });
316
+ return attrs;
317
+ }
318
+ get relationshipsByName() {
319
+ const rels = new Map();
320
+ const fields = this.__store.schema.fields({
321
+ type: this.modelName
322
+ });
323
+ fields.forEach((schema, key) => {
324
+ if (schema.kind === 'belongsTo' || schema.kind === 'hasMany') {
325
+ rels.set(key, schema);
326
+ }
327
+ });
328
+ return rels;
329
+ }
330
+ eachAttribute(callback, binding) {
331
+ this.__store.schema.fields({
332
+ type: this.modelName
333
+ }).forEach((schema, key) => {
334
+ if (schema.kind === 'attribute') {
335
+ callback.call(binding, key, schema);
336
+ }
337
+ });
338
+ }
339
+ eachRelationship(callback, binding) {
340
+ this.__store.schema.fields({
341
+ type: this.modelName
342
+ }).forEach((schema, key) => {
343
+ if (schema.kind === 'belongsTo' || schema.kind === 'hasMany') {
344
+ callback.call(binding, key, schema);
345
+ }
346
+ });
347
+ }
348
+ eachTransformedAttribute(callback, binding) {
349
+ this.__store.schema.fields({
350
+ type: this.modelName
351
+ }).forEach((schema, key) => {
352
+ if (schema.kind === 'attribute') {
353
+ const type = schema.type;
354
+ if (type) callback.call(binding, key, type);
355
+ }
356
+ });
357
+ }
358
+ }
359
+ function restoreDeprecatedStoreBehaviors(StoreKlass) {
360
+ StoreKlass.prototype.findRecord = function (resource, id, options) {
361
+ if (isMaybeIdentifier(resource)) {
362
+ options = id;
363
+ } else {
364
+ const type = normalizeModelName(resource);
365
+ const normalizedId = ensureStringId(id);
366
+ resource = constructResource(type, normalizedId);
367
+ }
368
+ const identifier = this.cacheKeyManager.getOrCreateRecordIdentifier(resource);
369
+ options = options || {};
370
+ assertPrivateStore(this);
371
+ if (options.preload) {
372
+ // force reload if we preload to ensure we don't resolve the promise
373
+ // until we are complete, else we will end up background-reloading
374
+ // even for initial load.
375
+ if (!this._instanceCache.recordIsLoaded(identifier)) {
376
+ options.reload = true;
377
+ }
378
+ this._join(() => {
379
+ preloadData(this, identifier, options.preload);
380
+ });
381
+ }
382
+ const promise = this.request({
383
+ op: 'findRecord',
384
+ data: {
385
+ record: identifier,
386
+ options
387
+ },
388
+ cacheOptions: {
389
+ [SkipCache]: true
390
+ }
391
+ });
392
+ return promise.then(document => {
393
+ return document.content;
394
+ });
395
+ };
396
+ StoreKlass.prototype.findAll = function (type, options = {}) {
397
+ const promise = this.request({
398
+ op: 'findAll',
399
+ data: {
400
+ type: normalizeModelName(type),
401
+ options: options || {}
402
+ },
403
+ cacheOptions: {
404
+ [SkipCache]: true
405
+ }
406
+ });
407
+ return promise.then(document => document.content);
408
+ };
409
+ StoreKlass.prototype.query = function (type, query, options = {}) {
410
+ const promise = this.request({
411
+ op: 'query',
412
+ data: {
413
+ type: normalizeModelName(type),
414
+ query,
415
+ options: options
416
+ },
417
+ cacheOptions: {
418
+ [SkipCache]: true
419
+ }
420
+ });
421
+ return promise.then(document => document.content);
422
+ };
423
+ StoreKlass.prototype.queryRecord = function (type, query, options) {
424
+ const promise = this.request({
425
+ op: 'queryRecord',
426
+ data: {
427
+ type: normalizeModelName(type),
428
+ query,
429
+ options: options || {}
430
+ },
431
+ cacheOptions: {
432
+ [SkipCache]: true
433
+ }
434
+ });
435
+ return promise.then(document => document.content);
436
+ };
437
+
438
+ // do not put a ts-expect-error here, because typedoc will fail to
439
+ // build due to this error only occurring when references are in use in the editor
440
+ StoreKlass.prototype.getReference = function (resource, id) {
441
+ let resourceIdentifier;
442
+ if (arguments.length === 1 && isMaybeIdentifier(resource)) {
443
+ resourceIdentifier = resource;
444
+ } else {
445
+ const type = normalizeModelName(resource);
446
+ const normalizedId = ensureStringId(id);
447
+ resourceIdentifier = constructResource(type, normalizedId);
448
+ }
449
+ const identifier = this.cacheKeyManager.getOrCreateRecordIdentifier(resourceIdentifier);
450
+ assertPrivateStore(this);
451
+ const cache = upgradeInstanceCaches(this._instanceCache.__instances).reference;
452
+ let reference = cache.get(identifier);
453
+ if (!reference) {
454
+ reference = new RecordReference(this, identifier);
455
+ cache.set(identifier, reference);
456
+ }
457
+ return reference;
458
+ };
459
+ StoreKlass.prototype.modelFor = function (type) {
460
+ return getShimClass(this, type);
461
+ };
462
+ StoreKlass.prototype.saveRecord = function (record, options = {}) {
463
+ const identifier = recordIdentifierFor(record);
464
+ const cache = this.cache;
465
+ if (!identifier) {
466
+ // this commonly means we're disconnected
467
+ // but just in case we reject here to prevent bad things.
468
+ return Promise.reject(new Error(`Record Is Disconnected`));
469
+ }
470
+ assertPrivateStore(this);
471
+ if (resourceIsFullyDeleted(this._instanceCache, identifier)) {
472
+ return Promise.resolve(record);
473
+ }
474
+ if (!options) {
475
+ options = {};
476
+ }
477
+ let operation = 'updateRecord';
478
+ if (cache.isNew(identifier)) {
479
+ operation = 'createRecord';
480
+ } else if (cache.isDeleted(identifier)) {
481
+ operation = 'deleteRecord';
482
+ }
483
+ const request = {
484
+ op: operation,
485
+ data: {
486
+ options,
487
+ record: identifier
488
+ },
489
+ records: [identifier],
490
+ cacheOptions: {
491
+ [SkipCache]: true
492
+ }
493
+ };
494
+ return this.request(request).then(document => document.content);
495
+ };
496
+ }
497
+ function upgradeInstanceCaches(cache) {
498
+ const withReferences = cache;
499
+ if (!withReferences.reference) {
500
+ withReferences.reference = new WeakMap();
501
+ }
502
+ return withReferences;
503
+ }
504
+ 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 };