@warp-drive/legacy 5.8.0-beta.0 → 5.8.0-beta.2

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