@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,726 @@
1
+ import { getOwner } from '@ember/application';
2
+ import { recordIdentifierFor } from '@warp-drive/core';
3
+ import { assertPrivateStore, _deprecatingNormalize } from '@warp-drive/core/store/-private';
4
+ import '@warp-drive/core/reactive/-private';
5
+ import { n as normalizeResponseHelper, F as FetchManager, S as SaveOp, a as SnapshotRecordArray } from "./-private-Cvf_97EG.js";
6
+ function iterateData(data, fn) {
7
+ if (Array.isArray(data)) {
8
+ return data.map(fn);
9
+ } else {
10
+ return fn(data, 0);
11
+ }
12
+ }
13
+ function _findHasMany(adapter, store, identifier, link, relationship, options) {
14
+ const promise = Promise.resolve().then(() => {
15
+ const snapshot = store._fetchManager.createSnapshot(identifier, options);
16
+ const useLink = !link || typeof link === 'string';
17
+ const relatedLink = useLink ? link : link.href;
18
+ return adapter.findHasMany(store, snapshot, relatedLink, relationship);
19
+ });
20
+ return promise.then(adapterPayload => {
21
+ const modelClass = store.modelFor(relationship.type);
22
+ const serializer = store.serializerFor(relationship.type);
23
+ let payload = normalizeResponseHelper(serializer, store, modelClass, adapterPayload, null, 'findHasMany');
24
+ payload = syncRelationshipDataFromLink(store, payload, identifier, relationship);
25
+ return store._push(payload, true);
26
+ }, null);
27
+ }
28
+ function _findBelongsTo(store, identifier, link, relationship, options) {
29
+ const promise = Promise.resolve().then(() => {
30
+ const adapter = store.adapterFor(identifier.type);
31
+ const snapshot = store._fetchManager.createSnapshot(identifier, options);
32
+ const useLink = !link || typeof link === 'string';
33
+ const relatedLink = useLink ? link : link.href;
34
+ return adapter.findBelongsTo(store, snapshot, relatedLink, relationship);
35
+ });
36
+ return promise.then(adapterPayload => {
37
+ const modelClass = store.modelFor(relationship.type);
38
+ const serializer = store.serializerFor(relationship.type);
39
+ let payload = normalizeResponseHelper(serializer, store, modelClass, adapterPayload, null, 'findBelongsTo');
40
+ if (!payload.data && !payload.links && !payload.meta) {
41
+ return null;
42
+ }
43
+ payload = syncRelationshipDataFromLink(store, payload, identifier, relationship);
44
+ return store._push(payload, true);
45
+ }, null);
46
+ }
47
+
48
+ // sync
49
+ // iterate over records in payload.data
50
+ // for each record
51
+ // assert that record.relationships[inverse] is either undefined (so we can fix it)
52
+ // or provide a data: {id, type} that matches the record that requested it
53
+ // return the relationship data for the parent
54
+ function syncRelationshipDataFromLink(store, payload, parentIdentifier, relationship) {
55
+ // ensure the right hand side (incoming payload) points to the parent record that
56
+ // requested this relationship
57
+ const relationshipData = payload.data ? iterateData(payload.data, (data, index) => {
58
+ const {
59
+ id,
60
+ type
61
+ } = data;
62
+ ensureRelationshipIsSetToParent(data, parentIdentifier, store, relationship);
63
+ return {
64
+ id,
65
+ type
66
+ };
67
+ }) : null;
68
+ const relatedDataHash = {};
69
+ if ('meta' in payload) {
70
+ relatedDataHash.meta = payload.meta;
71
+ }
72
+ if ('links' in payload) {
73
+ relatedDataHash.links = payload.links;
74
+ }
75
+ if ('data' in payload) {
76
+ relatedDataHash.data = relationshipData;
77
+ }
78
+
79
+ // now, push the left hand side (the parent record) to ensure things are in sync, since
80
+ // the payload will be pushed with store._push
81
+ const parentPayload = {
82
+ id: parentIdentifier.id,
83
+ type: parentIdentifier.type,
84
+ relationships: {
85
+ [relationship.name]: relatedDataHash
86
+ }
87
+ };
88
+ if (!Array.isArray(payload.included)) {
89
+ payload.included = [];
90
+ }
91
+ payload.included.push(parentPayload);
92
+ return payload;
93
+ }
94
+ function ensureRelationshipIsSetToParent(payload, parentIdentifier, store, parentRelationship, index) {
95
+ const {
96
+ id,
97
+ type
98
+ } = payload;
99
+ if (!payload.relationships) {
100
+ payload.relationships = {};
101
+ }
102
+ const {
103
+ relationships
104
+ } = payload;
105
+ const inverse = getInverse(store, parentIdentifier, parentRelationship, type);
106
+ if (inverse) {
107
+ const {
108
+ inverseKey,
109
+ kind
110
+ } = inverse;
111
+ const relationshipData = relationships[inverseKey]?.data;
112
+ if (kind !== 'hasMany' || typeof relationshipData !== 'undefined') {
113
+ relationships[inverseKey] = relationships[inverseKey] || {};
114
+ relationships[inverseKey].data = fixRelationshipData(relationshipData ?? null, kind, parentIdentifier);
115
+ }
116
+ }
117
+ }
118
+ function inverseForRelationship(store, identifier, key) {
119
+ const definition = store.schema.fields(identifier).get(key);
120
+ if (!definition) {
121
+ return null;
122
+ }
123
+ return definition.options.inverse;
124
+ }
125
+ function getInverse(store, parentIdentifier, parentRelationship, type) {
126
+ const {
127
+ name: lhs_relationshipName
128
+ } = parentRelationship;
129
+ const {
130
+ type: parentType
131
+ } = parentIdentifier;
132
+ const inverseKey = inverseForRelationship(store, {
133
+ type: parentType
134
+ }, lhs_relationshipName);
135
+ if (inverseKey) {
136
+ const definition = store.schema.fields({
137
+ type
138
+ }).get(inverseKey);
139
+ return {
140
+ inverseKey,
141
+ kind: definition.kind
142
+ };
143
+ }
144
+ }
145
+ function fixRelationshipData(relationshipData, relationshipKind, {
146
+ id,
147
+ type
148
+ }) {
149
+ const parentRelationshipData = {
150
+ id,
151
+ type
152
+ };
153
+ let payload = null;
154
+ if (relationshipKind === 'hasMany') {
155
+ const relData = relationshipData || [];
156
+ if (relationshipData) {
157
+ // these arrays could be massive so this is better than filter
158
+ // Note: this is potentially problematic if type/id are not in the
159
+ // same state of normalization.
160
+ const found = relationshipData.find(v => {
161
+ return v.type === parentRelationshipData.type && v.id === parentRelationshipData.id;
162
+ });
163
+ if (!found) {
164
+ relData.push(parentRelationshipData);
165
+ }
166
+ } else {
167
+ relData.push(parentRelationshipData);
168
+ }
169
+ payload = relData;
170
+ } else {
171
+ const relData = relationshipData || {};
172
+ Object.assign(relData, parentRelationshipData);
173
+ payload = relData;
174
+ }
175
+ return payload;
176
+ }
177
+ const PotentialLegacyOperations = new Set(['findRecord', 'findAll', 'query', 'queryRecord', 'findBelongsTo', 'findHasMany', 'updateRecord', 'createRecord', 'deleteRecord']);
178
+ const LegacyNetworkHandler = {
179
+ request(context, next) {
180
+ // if we are not a legacy request, move on
181
+ if (context.request.url || !context.request.op || !PotentialLegacyOperations.has(context.request.op)) {
182
+ return next(context.request);
183
+ }
184
+ const {
185
+ store
186
+ } = context.request;
187
+ if (!store._fetchManager) {
188
+ store._fetchManager = new FetchManager(store);
189
+ }
190
+ switch (context.request.op) {
191
+ case 'findRecord':
192
+ return findRecord(context);
193
+ case 'findAll':
194
+ return findAll(context);
195
+ case 'query':
196
+ return query(context);
197
+ case 'queryRecord':
198
+ return queryRecord(context);
199
+ case 'findBelongsTo':
200
+ return findBelongsTo(context);
201
+ case 'findHasMany':
202
+ return findHasMany(context);
203
+ case 'updateRecord':
204
+ return saveRecord(context);
205
+ case 'createRecord':
206
+ return saveRecord(context);
207
+ case 'deleteRecord':
208
+ return saveRecord(context);
209
+ default:
210
+ return next(context.request);
211
+ }
212
+ }
213
+ };
214
+ function findBelongsTo(context) {
215
+ const {
216
+ store,
217
+ data,
218
+ records: identifiers
219
+ } = context.request;
220
+ const {
221
+ options,
222
+ record,
223
+ links,
224
+ useLink,
225
+ field
226
+ } = data;
227
+ const identifier = identifiers?.[0];
228
+
229
+ // short circuit if we are already loading
230
+ const pendingRequest = identifier && store._fetchManager.getPendingFetch(identifier, options);
231
+ if (pendingRequest) {
232
+ return pendingRequest;
233
+ }
234
+ if (useLink) {
235
+ return _findBelongsTo(store, record, links.related, field, options);
236
+ }
237
+ const manager = store._fetchManager;
238
+ return options.reload ? manager.scheduleFetch(identifier, options, context.request) : manager.fetchDataIfNeededForIdentifier(identifier, options, context.request);
239
+ }
240
+ function findHasMany(context) {
241
+ const {
242
+ store,
243
+ data,
244
+ records: identifiers
245
+ } = context.request;
246
+ const {
247
+ options,
248
+ record,
249
+ links,
250
+ useLink,
251
+ field
252
+ } = data;
253
+
254
+ // link case
255
+ if (useLink) {
256
+ const adapter = store.adapterFor(record.type);
257
+ return _findHasMany(adapter, store, record, links.related, field, options);
258
+ }
259
+ const fetches = new Array(identifiers.length);
260
+ const manager = store._fetchManager;
261
+ for (let i = 0; i < identifiers.length; i++) {
262
+ const identifier = identifiers[i];
263
+ fetches[i] = options.reload ? manager.scheduleFetch(identifier, options, context.request) : manager.fetchDataIfNeededForIdentifier(identifier, options, context.request);
264
+ }
265
+ return Promise.all(fetches);
266
+ }
267
+ function saveRecord(context) {
268
+ const {
269
+ store,
270
+ data,
271
+ op: operation
272
+ } = context.request;
273
+ const {
274
+ options,
275
+ record: identifier
276
+ } = data;
277
+ assertPrivateStore(store);
278
+ store.cache.willCommit(identifier, context);
279
+ const saveOptions = Object.assign({
280
+ [SaveOp]: operation
281
+ }, options);
282
+ const fetchManagerPromise = store._fetchManager.scheduleSave(identifier, saveOptions);
283
+ return fetchManagerPromise.then(payload => {
284
+ let result;
285
+ store._join(() => {
286
+ // @ts-expect-error we don't have access to a response in legacy
287
+ result = store.cache.didCommit(identifier, {
288
+ request: context.request,
289
+ content: payload
290
+ });
291
+ });
292
+
293
+ // blatantly lie if we were a createRecord request
294
+ // to give some semblance of cache-control to the
295
+ // CachePolicy while legacy is still around
296
+ if (store.lifetimes?.didRequest && operation === 'createRecord') {
297
+ store.lifetimes.didRequest(context.request, {
298
+ status: 201
299
+ }, null, store);
300
+ }
301
+ return store.peekRecord(result.data);
302
+ }).catch(e => {
303
+ let err = e;
304
+ if (!e) {
305
+ err = new Error(`Unknown Error Occurred During Request`);
306
+ } else if (typeof e === 'string') {
307
+ err = new Error(e);
308
+ }
309
+ adapterDidInvalidate(store, identifier, err);
310
+ throw err;
311
+ });
312
+ }
313
+ function adapterDidInvalidate(store, identifier, error) {
314
+ if (error && error.isAdapterError === true && error.code === 'InvalidError') {
315
+ const serializer = store.serializerFor(identifier.type);
316
+
317
+ // TODO @deprecate extractErrors being called
318
+ // TODO remove extractErrors from the default serializers.
319
+ if (serializer && typeof serializer.extractErrors === 'function') {
320
+ const errorsHash = serializer.extractErrors(store, store.modelFor(identifier.type), error, identifier.id);
321
+ error.errors = errorsHashToArray(errorsHash);
322
+ }
323
+ }
324
+ const cache = store.cache;
325
+ if (error.errors) {
326
+ let jsonApiErrors = error.errors;
327
+ if (jsonApiErrors.length === 0) {
328
+ jsonApiErrors = [{
329
+ title: 'Invalid Error',
330
+ detail: '',
331
+ source: {
332
+ pointer: '/data'
333
+ }
334
+ }];
335
+ }
336
+ cache.commitWasRejected(identifier, jsonApiErrors);
337
+ } else {
338
+ cache.commitWasRejected(identifier);
339
+ }
340
+ }
341
+ function makeArray(value) {
342
+ return Array.isArray(value) ? value : [value];
343
+ }
344
+ const PRIMARY_ATTRIBUTE_KEY = 'base';
345
+ function errorsHashToArray(errors) {
346
+ const out = [];
347
+ if (errors) {
348
+ Object.keys(errors).forEach(key => {
349
+ const messages = makeArray(errors[key]);
350
+ for (let i = 0; i < messages.length; i++) {
351
+ let title = 'Invalid Attribute';
352
+ let pointer = `/data/attributes/${key}`;
353
+ if (key === PRIMARY_ATTRIBUTE_KEY) {
354
+ title = 'Invalid Document';
355
+ pointer = `/data`;
356
+ }
357
+ out.push({
358
+ title: title,
359
+ detail: messages[i],
360
+ source: {
361
+ pointer: pointer
362
+ }
363
+ });
364
+ }
365
+ });
366
+ }
367
+ return out;
368
+ }
369
+ function findRecord(context) {
370
+ const {
371
+ store,
372
+ data
373
+ } = context.request;
374
+ const {
375
+ record: identifier,
376
+ options
377
+ } = data;
378
+ assertPrivateStore(store);
379
+ let promise;
380
+
381
+ // if not loaded start loading
382
+ if (!store._instanceCache.recordIsLoaded(identifier)) {
383
+ promise = store._fetchManager.fetchDataIfNeededForIdentifier(identifier, options, context.request);
384
+
385
+ // Refetch if the reload option is passed
386
+ } else if (options.reload) {
387
+ promise = store._fetchManager.scheduleFetch(identifier, options, context.request);
388
+ } else {
389
+ let snapshot = null;
390
+ const adapter = store.adapterFor(identifier.type);
391
+
392
+ // Refetch the record if the adapter thinks the record is stale
393
+ if (typeof options.reload === 'undefined' && adapter.shouldReloadRecord && adapter.shouldReloadRecord(store, snapshot = store._fetchManager.createSnapshot(identifier, options))) {
394
+ {
395
+ options.reload = true;
396
+ promise = store._fetchManager.scheduleFetch(identifier, options, context.request);
397
+ }
398
+ } else {
399
+ // Trigger the background refetch if backgroundReload option is passed
400
+ if (options.backgroundReload !== false && (options.backgroundReload || !adapter.shouldBackgroundReloadRecord || adapter.shouldBackgroundReloadRecord(store, snapshot = snapshot || store._fetchManager.createSnapshot(identifier, options)))) {
401
+ {
402
+ options.backgroundReload = true;
403
+ void store._fetchManager.scheduleFetch(identifier, options, context.request);
404
+ }
405
+ }
406
+
407
+ // Return the cached record
408
+ promise = Promise.resolve(identifier);
409
+ }
410
+ }
411
+ return promise.then(i => store.peekRecord(i));
412
+ }
413
+ function findAll(context) {
414
+ const {
415
+ store,
416
+ data
417
+ } = context.request;
418
+ const {
419
+ type,
420
+ options
421
+ } = data;
422
+ assertPrivateStore(store);
423
+ const adapter = store.adapterFor(type);
424
+
425
+ // avoid initializing the liveArray just to set `isUpdating`
426
+ const maybeRecordArray = store.recordArrayManager._live.get(type);
427
+ const snapshotArray = new SnapshotRecordArray(store, type, options);
428
+ const shouldReload = options.reload || options.reload !== false && (adapter.shouldReloadAll && adapter.shouldReloadAll(store, snapshotArray) || !adapter.shouldReloadAll && snapshotArray.length === 0);
429
+ let fetch;
430
+ if (shouldReload) {
431
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
432
+ maybeRecordArray && (maybeRecordArray.isUpdating = true);
433
+ fetch = _findAll(adapter, store, type, snapshotArray, context.request, true);
434
+ } else {
435
+ fetch = Promise.resolve(store.peekAll(type));
436
+ if (options.backgroundReload || options.backgroundReload !== false && (!adapter.shouldBackgroundReloadAll || adapter.shouldBackgroundReloadAll(store, snapshotArray))) {
437
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
438
+ maybeRecordArray && (maybeRecordArray.isUpdating = true);
439
+ void _findAll(adapter, store, type, snapshotArray, context.request, false);
440
+ }
441
+ }
442
+ return fetch;
443
+ }
444
+ function _findAll(adapter, store, type, snapshotArray, request, isAsyncFlush) {
445
+ const schema = store.modelFor(type);
446
+ let promise = Promise.resolve().then(() => adapter.findAll(store, schema, null, snapshotArray));
447
+ promise = promise.then(adapterPayload => {
448
+ const serializer = store.serializerFor(type);
449
+ const payload = normalizeResponseHelper(serializer, store, schema, adapterPayload, null, 'findAll');
450
+ store._push(payload, isAsyncFlush);
451
+ snapshotArray._recordArray.isUpdating = false;
452
+ return snapshotArray._recordArray;
453
+ });
454
+ return promise;
455
+ }
456
+ function query(context) {
457
+ const {
458
+ store,
459
+ data
460
+ } = context.request;
461
+ assertPrivateStore(store);
462
+ let {
463
+ options
464
+ } = data;
465
+ // eslint-disable-next-line @typescript-eslint/no-shadow
466
+ const {
467
+ type,
468
+ query
469
+ } = data;
470
+ const adapter = store.adapterFor(type);
471
+ const recordArray = options._recordArray || store.recordArrayManager.getCollection({
472
+ type,
473
+ query
474
+ });
475
+ {
476
+ delete options._recordArray;
477
+ }
478
+ const schema = store.modelFor(type);
479
+ const promise = Promise.resolve().then(() => adapter.query(store, schema, query, recordArray, options));
480
+ return promise.then(adapterPayload => {
481
+ const serializer = store.serializerFor(type);
482
+ const payload = normalizeResponseHelper(serializer, store, schema, adapterPayload, null, 'query');
483
+ const identifiers = store._push(payload, true);
484
+ store.recordArrayManager.populateManagedArray(recordArray, identifiers, payload);
485
+ return recordArray;
486
+ });
487
+ }
488
+ function queryRecord(context) {
489
+ const {
490
+ store,
491
+ data
492
+ } = context.request;
493
+ // eslint-disable-next-line @typescript-eslint/no-shadow
494
+ const {
495
+ type,
496
+ query,
497
+ options
498
+ } = data;
499
+ const adapter = store.adapterFor(type);
500
+ const schema = store.modelFor(type);
501
+ const promise = Promise.resolve().then(() => adapter.queryRecord(store, schema, query, options));
502
+ return promise.then(adapterPayload => {
503
+ const serializer = store.serializerFor(type);
504
+ const payload = normalizeResponseHelper(serializer, store, schema, adapterPayload, null, 'queryRecord');
505
+ const identifier = store._push(payload, true);
506
+ return identifier ? store.peekRecord(identifier) : null;
507
+ });
508
+ }
509
+
510
+ /**
511
+ * Extends the signature of {@link Store} with additional
512
+ * methods available when using the legacy network layer.
513
+ *
514
+ * @public
515
+ * @noInheritDoc
516
+ * @legacy
517
+ */
518
+
519
+ /**
520
+ * @deprecated - use {@link LegacyStoreCompat} instead
521
+ */
522
+
523
+ /**
524
+ Returns an instance of the adapter for a given type. For
525
+ example, `adapterFor('person')` will return an instance of
526
+ the adapter located at `app/adapters/person.js`
527
+
528
+ If no `person` adapter is found, this method will look
529
+ for an `application` adapter (the default adapter for
530
+ your entire application).
531
+
532
+ @public
533
+ @param modelName
534
+ */
535
+
536
+ function adapterFor(modelName, _allowMissing) {
537
+ this._adapterCache = this._adapterCache || Object.create(null);
538
+ const normalizedModelName = _deprecatingNormalize(modelName);
539
+ const {
540
+ _adapterCache
541
+ } = this;
542
+ let adapter = _adapterCache[normalizedModelName];
543
+ if (adapter) {
544
+ return adapter;
545
+ }
546
+ const owner = getOwner(this);
547
+
548
+ // name specific adapter
549
+ adapter = owner.lookup(`adapter:${normalizedModelName}`);
550
+ if (adapter !== undefined) {
551
+ _adapterCache[normalizedModelName] = adapter;
552
+ return adapter;
553
+ }
554
+
555
+ // no adapter found for the specific name, fallback and check for application adapter
556
+ adapter = _adapterCache.application || owner.lookup('adapter:application');
557
+ if (adapter !== undefined) {
558
+ _adapterCache[normalizedModelName] = adapter;
559
+ _adapterCache.application = adapter;
560
+ return adapter;
561
+ }
562
+ }
563
+
564
+ /**
565
+ Returns an instance of the serializer for a given type. For
566
+ example, `serializerFor('person')` will return an instance of
567
+ `App.PersonSerializer`.
568
+
569
+ If no `App.PersonSerializer` is found, this method will look
570
+ for an `App.ApplicationSerializer` (the default serializer for
571
+ your entire application).
572
+
573
+ If a serializer cannot be found on the adapter, it will fall back
574
+ to an instance of `JSONSerializer`.
575
+
576
+ @public
577
+ @param modelName the record to serialize
578
+ */
579
+ function serializerFor(modelName) {
580
+ this._serializerCache = this._serializerCache || Object.create(null);
581
+ const normalizedModelName = _deprecatingNormalize(modelName);
582
+ const {
583
+ _serializerCache
584
+ } = this;
585
+ let serializer = _serializerCache[normalizedModelName];
586
+ if (serializer) {
587
+ return serializer;
588
+ }
589
+
590
+ // by name
591
+ const owner = getOwner(this);
592
+ serializer = owner.lookup(`serializer:${normalizedModelName}`);
593
+ if (serializer !== undefined) {
594
+ _serializerCache[normalizedModelName] = serializer;
595
+ return serializer;
596
+ }
597
+
598
+ // no serializer found for the specific model, fallback and check for application serializer
599
+ serializer = _serializerCache.application || owner.lookup('serializer:application');
600
+ if (serializer !== undefined) {
601
+ _serializerCache[normalizedModelName] = serializer;
602
+ _serializerCache.application = serializer;
603
+ return serializer;
604
+ }
605
+ return null;
606
+ }
607
+
608
+ /**
609
+ `normalize` converts a json payload into the normalized form expected by
610
+ {@link Store.push | push} using the serializer specified by `modelName`
611
+
612
+ :::warning
613
+ Generally it would be better to invoke the serializer yourself directly,
614
+ or write a more specialized normalization utility.
615
+ :::
616
+
617
+ Example
618
+
619
+ ```js
620
+ socket.on('message', function(message) {
621
+ let modelName = message.model;
622
+ let data = message.data;
623
+ store.push(store.normalize(modelName, data));
624
+ });
625
+ ```
626
+
627
+ @legacy
628
+ @public
629
+ @param modelName The name of the model type for this payload
630
+ @return The normalized payload
631
+ */
632
+ // TODO @runspired @deprecate users should call normalize on the associated serializer directly
633
+ function normalize(modelName, payload) {
634
+ const normalizedModelName = _deprecatingNormalize(modelName);
635
+ const serializer = this.serializerFor(normalizedModelName);
636
+ const schema = this.modelFor(normalizedModelName);
637
+ return serializer.normalize(schema, payload);
638
+ }
639
+
640
+ /**
641
+ Push some raw data into the store.
642
+
643
+ This method can be used both to push in brand new
644
+ records, as well as to update existing records. You
645
+ can push in more than one type of object at once.
646
+ All objects should be in the format expected by the
647
+ serializer.
648
+
649
+ ```js [app/serializers/application.js]
650
+ import RESTSerializer from '@warp-drive/legacy/serializer/rest';
651
+
652
+ export default class ApplicationSerializer extends RESTSerializer;
653
+ ```
654
+
655
+ ```js
656
+ let pushData = {
657
+ posts: [
658
+ { id: 1, postTitle: "Great post", commentIds: [2] }
659
+ ],
660
+ comments: [
661
+ { id: 2, commentBody: "Insightful comment" }
662
+ ]
663
+ }
664
+
665
+ store.pushPayload(pushData);
666
+ ```
667
+
668
+ By default, the data will be deserialized using a default
669
+ serializer (the application serializer if it exists).
670
+
671
+ Alternatively, `pushPayload` will accept a model type which
672
+ will determine which serializer will process the payload.
673
+
674
+ ```js [app/serializers/application.js]
675
+ import RESTSerializer from '@warp-drive/legacy/serializer/rest';
676
+
677
+ export default class ApplicationSerializer extends RESTSerializer;
678
+ ```
679
+
680
+ ```js [app/serializers/post.js]
681
+ import JSONSerializer from '@warp-drive/legacy/serializer/json';
682
+
683
+ export default JSONSerializer;
684
+ ```
685
+
686
+ ```js
687
+ store.pushPayload(pushData); // Will use the application serializer
688
+ store.pushPayload('post', pushData); // Will use the post serializer
689
+ ```
690
+
691
+ @public
692
+ @param modelName Optionally, a model type used to determine which serializer will be used
693
+ @param inputPayload
694
+ */
695
+ // TODO @runspired @deprecate pushPayload in favor of looking up the serializer
696
+ function pushPayload(modelName, inputPayload) {
697
+ const payload = inputPayload || modelName;
698
+ const normalizedModelName = inputPayload ? _deprecatingNormalize(modelName) : 'application';
699
+ const serializer = this.serializerFor(normalizedModelName);
700
+ serializer.pushPayload(this, payload);
701
+ }
702
+
703
+ // TODO @runspired @deprecate records should implement their own serialization if desired
704
+ function serializeRecord(record, options) {
705
+ // TODO we used to check if the record was destroyed here
706
+ if (!this._fetchManager) {
707
+ this._fetchManager = new FetchManager(this);
708
+ }
709
+ return this._fetchManager.createSnapshot(recordIdentifierFor(record)).serialize(options);
710
+ }
711
+ function cleanup() {
712
+ // enqueue destruction of any adapters/serializers we have created
713
+ for (const adapterName in this._adapterCache) {
714
+ const adapter = this._adapterCache[adapterName];
715
+ if (typeof adapter.destroy === 'function') {
716
+ adapter.destroy();
717
+ }
718
+ }
719
+ for (const serializerName in this._serializerCache) {
720
+ const serializer = this._serializerCache[serializerName];
721
+ if (typeof serializer.destroy === 'function') {
722
+ serializer.destroy();
723
+ }
724
+ }
725
+ }
726
+ export { LegacyNetworkHandler, adapterFor, cleanup, normalize, pushPayload, serializeRecord, serializerFor };