@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,1147 @@
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 { p as payloadIsNotBlank, n as normalizeResponseHelper, i as iterateData, F as FetchManager, S as SaveOp, a as assertIdentifierHasId, b as SnapshotRecordArray } from "./-private-DtjBbEgy.js";
6
+ import { waitFor } from '@warp-drive/core/signals/-leaked';
7
+ function _findHasMany(adapter, store, identifier, link, relationship, options) {
8
+ const promise = Promise.resolve().then(() => {
9
+ const snapshot = store._fetchManager.createSnapshot(identifier, options);
10
+ const useLink = !link || typeof link === 'string';
11
+ const relatedLink = useLink ? link : link.href;
12
+ (test => {
13
+ if (!test) {
14
+ throw new Error(`Attempted to load a hasMany relationship from a specified 'link' in the original payload, but the specified link is empty. You must provide a valid 'link' in the original payload to use 'findHasMany'`);
15
+ }
16
+ })(relatedLink);
17
+ (test => {
18
+ if (!test) {
19
+ throw new Error(`Expected the adapter to implement 'findHasMany' but it does not`);
20
+ }
21
+ })(typeof adapter.findHasMany === 'function');
22
+ return adapter.findHasMany(store, snapshot, relatedLink, relationship);
23
+ });
24
+ return promise.then(adapterPayload => {
25
+ (test => {
26
+ if (!test) {
27
+ throw new Error(`You made a 'findHasMany' request for a ${identifier.type}'s '${relationship.name}' relationship, using link '${JSON.stringify(link)}' , but the adapter's response did not have any data`);
28
+ }
29
+ })(payloadIsNotBlank(adapterPayload));
30
+ const modelClass = store.modelFor(relationship.type);
31
+ const serializer = store.serializerFor(relationship.type);
32
+ let payload = normalizeResponseHelper(serializer, store, modelClass, adapterPayload, null, 'findHasMany');
33
+ (test => {
34
+ if (!test) {
35
+ throw new Error(`fetched the hasMany relationship '${relationship.name}' for ${identifier.type}:${identifier.id} with link '${JSON.stringify(link)}', but no data member is present in the response. If no data exists, the response should set { data: [] }`);
36
+ }
37
+ })('data' in payload && Array.isArray(payload.data));
38
+ payload = syncRelationshipDataFromLink(store, payload, identifier, relationship);
39
+ return store._push(payload, true);
40
+ }, null);
41
+ }
42
+ function _findBelongsTo(store, identifier, link, relationship, options) {
43
+ const promise = Promise.resolve().then(() => {
44
+ const adapter = store.adapterFor(identifier.type);
45
+ (test => {
46
+ if (!test) {
47
+ throw new Error(`You tried to load a belongsTo relationship but you have no adapter (for ${identifier.type})`);
48
+ }
49
+ })(adapter);
50
+ (test => {
51
+ if (!test) {
52
+ throw new Error(`You tried to load a belongsTo relationship from a specified 'link' in the original payload but your adapter does not implement 'findBelongsTo'`);
53
+ }
54
+ })(typeof adapter.findBelongsTo === 'function');
55
+ const snapshot = store._fetchManager.createSnapshot(identifier, options);
56
+ const useLink = !link || typeof link === 'string';
57
+ const relatedLink = useLink ? link : link.href;
58
+ (test => {
59
+ if (!test) {
60
+ throw new Error(`Attempted to load a belongsTo relationship from a specified 'link' in the original payload, but the specified link is empty. You must provide a valid 'link' in the original payload to use 'findBelongsTo'`);
61
+ }
62
+ })(relatedLink);
63
+ return adapter.findBelongsTo(store, snapshot, relatedLink, relationship);
64
+ });
65
+ return promise.then(adapterPayload => {
66
+ const modelClass = store.modelFor(relationship.type);
67
+ const serializer = store.serializerFor(relationship.type);
68
+ let payload = normalizeResponseHelper(serializer, store, modelClass, adapterPayload, null, 'findBelongsTo');
69
+ (test => {
70
+ if (!test) {
71
+ throw new Error(`fetched the belongsTo relationship '${relationship.name}' for ${identifier.type}:${identifier.id} with link '${JSON.stringify(link)}', but no data member is present in the response. If no data exists, the response should set { data: null }`);
72
+ }
73
+ })('data' in payload && (payload.data === null || typeof payload.data === 'object' && !Array.isArray(payload.data)));
74
+ if (!payload.data && !payload.links && !payload.meta) {
75
+ return null;
76
+ }
77
+ payload = syncRelationshipDataFromLink(store, payload, identifier, relationship);
78
+ return store._push(payload, true);
79
+ }, null);
80
+ }
81
+
82
+ // sync
83
+ // iterate over records in payload.data
84
+ // for each record
85
+ // assert that record.relationships[inverse] is either undefined (so we can fix it)
86
+ // or provide a data: {id, type} that matches the record that requested it
87
+ // return the relationship data for the parent
88
+ function syncRelationshipDataFromLink(store, payload, parentIdentifier, relationship) {
89
+ // ensure the right hand side (incoming payload) points to the parent record that
90
+ // requested this relationship
91
+ const relationshipData = payload.data ? iterateData(payload.data, (data, index) => {
92
+ const {
93
+ id,
94
+ type
95
+ } = data;
96
+ ensureRelationshipIsSetToParent(data, parentIdentifier, store, relationship, index);
97
+ return {
98
+ id,
99
+ type
100
+ };
101
+ }) : null;
102
+ const relatedDataHash = {};
103
+ if ('meta' in payload) {
104
+ relatedDataHash.meta = payload.meta;
105
+ }
106
+ if ('links' in payload) {
107
+ relatedDataHash.links = payload.links;
108
+ }
109
+ if ('data' in payload) {
110
+ relatedDataHash.data = relationshipData;
111
+ }
112
+
113
+ // now, push the left hand side (the parent record) to ensure things are in sync, since
114
+ // the payload will be pushed with store._push
115
+ const parentPayload = {
116
+ id: parentIdentifier.id,
117
+ type: parentIdentifier.type,
118
+ relationships: {
119
+ [relationship.name]: relatedDataHash
120
+ }
121
+ };
122
+ if (!Array.isArray(payload.included)) {
123
+ payload.included = [];
124
+ }
125
+ payload.included.push(parentPayload);
126
+ return payload;
127
+ }
128
+ function ensureRelationshipIsSetToParent(payload, parentIdentifier, store, parentRelationship, index) {
129
+ const {
130
+ id,
131
+ type
132
+ } = payload;
133
+ if (!payload.relationships) {
134
+ payload.relationships = {};
135
+ }
136
+ const {
137
+ relationships
138
+ } = payload;
139
+ const inverse = getInverse(store, parentIdentifier, parentRelationship, type);
140
+ if (inverse) {
141
+ const {
142
+ inverseKey,
143
+ kind
144
+ } = inverse;
145
+ const relationshipData = relationships[inverseKey]?.data;
146
+ {
147
+ if (typeof relationshipData !== 'undefined' && !relationshipDataPointsToParent(relationshipData, parentIdentifier)) {
148
+ const inspect = function inspect(thing) {
149
+ return `'${JSON.stringify(thing)}'`;
150
+ };
151
+ const quotedType = inspect(type);
152
+ const quotedInverse = inspect(inverseKey);
153
+ const expected = inspect({
154
+ id: parentIdentifier.id,
155
+ type: parentIdentifier.type
156
+ });
157
+ const expectedModel = `${parentIdentifier.type}:${parentIdentifier.id}`;
158
+ const got = inspect(relationshipData);
159
+ const prefix = typeof index === 'number' ? `data[${index}]` : `data`;
160
+ const path = `${prefix}.relationships.${inverseKey}.data`;
161
+ const data = Array.isArray(relationshipData) ? relationshipData[0] : relationshipData;
162
+ const other = data ? `<${data.type}:${data.id}>` : null;
163
+ const relationshipFetched = `${expectedModel}.${parentRelationship.kind}("${parentRelationship.name}")`;
164
+ const includedRecord = `<${type}:${id}>`;
165
+ const message = [`Encountered mismatched relationship: WarpDrive expected ${path} in the payload from ${relationshipFetched} to include ${expected} but got ${got} instead.\n`, `The ${includedRecord} record loaded at ${prefix} in the payload specified ${other} as its ${quotedInverse}, but should have specified ${expectedModel} (the record the relationship is being loaded from) as its ${quotedInverse} instead.`, `This could mean that the response for ${relationshipFetched} may have accidentally returned ${quotedType} records that aren't related to ${expectedModel} and could be related to a different ${parentIdentifier.type} record instead.`, `WarpDrive has corrected the ${includedRecord} record's ${quotedInverse} relationship to ${expectedModel} so that ${relationshipFetched} will include ${includedRecord}.`, `Please update the response from the server or change your serializer to either ensure that the response for only includes ${quotedType} records that specify ${expectedModel} as their ${quotedInverse}, or omit the ${quotedInverse} relationship from the response.`].join('\n');
166
+ (test => {
167
+ {
168
+ throw new Error(message);
169
+ }
170
+ })();
171
+ }
172
+ }
173
+ if (kind !== 'hasMany' || typeof relationshipData !== 'undefined') {
174
+ relationships[inverseKey] = relationships[inverseKey] || {};
175
+ relationships[inverseKey].data = fixRelationshipData(relationshipData ?? null, kind, parentIdentifier);
176
+ }
177
+ }
178
+ }
179
+ function inverseForRelationship(store, identifier, key) {
180
+ const definition = store.schema.fields(identifier).get(key);
181
+ if (!definition) {
182
+ return null;
183
+ }
184
+ (test => {
185
+ if (!test) {
186
+ throw new Error(`Expected the field definition to be a relationship`);
187
+ }
188
+ })(definition.kind === 'hasMany' || definition.kind === 'belongsTo');
189
+ (test => {
190
+ if (!test) {
191
+ throw new Error(`Expected the relationship defintion to specify the inverse type or null.`);
192
+ }
193
+ })(definition.options?.inverse === null || typeof definition.options?.inverse === 'string' && definition.options.inverse.length > 0);
194
+ return definition.options.inverse;
195
+ }
196
+ function getInverse(store, parentIdentifier, parentRelationship, type) {
197
+ const {
198
+ name: lhs_relationshipName
199
+ } = parentRelationship;
200
+ const {
201
+ type: parentType
202
+ } = parentIdentifier;
203
+ const inverseKey = inverseForRelationship(store, {
204
+ type: parentType
205
+ }, lhs_relationshipName);
206
+ if (inverseKey) {
207
+ const definition = store.schema.fields({
208
+ type
209
+ }).get(inverseKey);
210
+ (test => {
211
+ if (!test) {
212
+ throw new Error(`Expected the field definition to be a relationship`);
213
+ }
214
+ })(definition && (definition.kind === 'hasMany' || definition.kind === 'belongsTo'));
215
+ return {
216
+ inverseKey,
217
+ kind: definition.kind
218
+ };
219
+ }
220
+ }
221
+ function relationshipDataPointsToParent(relationshipData, identifier) {
222
+ if (relationshipData === null) {
223
+ return false;
224
+ }
225
+ if (Array.isArray(relationshipData)) {
226
+ if (relationshipData.length === 0) {
227
+ return false;
228
+ }
229
+ for (let i = 0; i < relationshipData.length; i++) {
230
+ const entry = relationshipData[i];
231
+ if (validateRelationshipEntry(entry, identifier)) {
232
+ return true;
233
+ }
234
+ }
235
+ } else {
236
+ return validateRelationshipEntry(relationshipData, identifier);
237
+ }
238
+ return false;
239
+ }
240
+ function fixRelationshipData(relationshipData, relationshipKind, {
241
+ id,
242
+ type
243
+ }) {
244
+ const parentRelationshipData = {
245
+ id,
246
+ type
247
+ };
248
+ let payload = null;
249
+ if (relationshipKind === 'hasMany') {
250
+ const relData = relationshipData || [];
251
+ if (relationshipData) {
252
+ (test => {
253
+ if (!test) {
254
+ throw new Error('expected the relationship data to be an array');
255
+ }
256
+ })(Array.isArray(relationshipData));
257
+ // these arrays could be massive so this is better than filter
258
+ // Note: this is potentially problematic if type/id are not in the
259
+ // same state of normalization.
260
+ const found = relationshipData.find(v => {
261
+ return v.type === parentRelationshipData.type && v.id === parentRelationshipData.id;
262
+ });
263
+ if (!found) {
264
+ relData.push(parentRelationshipData);
265
+ }
266
+ } else {
267
+ relData.push(parentRelationshipData);
268
+ }
269
+ payload = relData;
270
+ } else {
271
+ const relData = relationshipData || {};
272
+ Object.assign(relData, parentRelationshipData);
273
+ payload = relData;
274
+ }
275
+ return payload;
276
+ }
277
+ function validateRelationshipEntry({
278
+ id
279
+ }, {
280
+ id: parentModelID
281
+ }) {
282
+ return !!id && id.toString() === parentModelID;
283
+ }
284
+
285
+ /*
286
+ These are the runtime implementations for the javascript macros that have
287
+ runtime implementations.
288
+
289
+ Not every macro has a runtime implementation, some only make sense in the
290
+ build and always run there.
291
+
292
+ Even when we have runtime implementations, we are still careful to emit static
293
+ errors during the build wherever possible, and runtime errors when necessary,
294
+ so that you're not surprised when you switch from runtime-mode to compile-time
295
+ mode.
296
+ */
297
+
298
+ // This is here as a compile target for `getConfig` and `getOwnConfig` when
299
+ // we're in runtime mode. This is not public API to call from your own code.
300
+ function config(packageRoot) {
301
+ return runtimeConfig.packages[packageRoot];
302
+ }
303
+ function getGlobalConfig() {
304
+ return runtimeConfig.global;
305
+ }
306
+ const runtimeConfig = initializeRuntimeMacrosConfig();
307
+
308
+ // this exists to be targeted by our babel plugin
309
+ function initializeRuntimeMacrosConfig() {
310
+ return {
311
+ "packages": {},
312
+ "global": {
313
+ "@embroider/macros": {
314
+ "isTesting": false
315
+ },
316
+ "WarpDrive": {
317
+ "debug": {
318
+ "DEBUG_RELATIONSHIP_NOTIFICATIONS": false,
319
+ "LOG_CACHE": false,
320
+ "LOG_CACHE_POLICY": false,
321
+ "LOG_GRAPH": false,
322
+ "LOG_IDENTIFIERS": false,
323
+ "LOG_INSTANCE_CACHE": false,
324
+ "LOG_METRIC_COUNTS": false,
325
+ "LOG_MUTATIONS": false,
326
+ "LOG_NOTIFICATIONS": false,
327
+ "LOG_OPERATIONS": false,
328
+ "LOG_PAYLOADS": false,
329
+ "LOG_REACT_SIGNAL_INTEGRATION": false,
330
+ "LOG_REQUESTS": false,
331
+ "LOG_REQUEST_STATUS": false,
332
+ "__INTERNAL_LOG_NATIVE_MAP_SET_COUNTS": false
333
+ },
334
+ "polyfillUUID": false,
335
+ "includeDataAdapter": true,
336
+ "compatWith": null,
337
+ "deprecations": {
338
+ "DEPRECATE_CATCH_ALL": true,
339
+ "DEPRECATE_COMPUTED_CHAINS": true,
340
+ "DEPRECATE_EMBER_INFLECTOR": true,
341
+ "DEPRECATE_LEGACY_IMPORTS": true,
342
+ "DEPRECATE_MANY_ARRAY_DUPLICATES": true,
343
+ "DEPRECATE_NON_STRICT_ID": true,
344
+ "DEPRECATE_NON_STRICT_TYPES": true,
345
+ "DEPRECATE_NON_UNIQUE_PAYLOADS": true,
346
+ "DEPRECATE_RELATIONSHIP_REMOTE_UPDATE_CLEARING_LOCAL_STATE": true,
347
+ "DEPRECATE_STORE_EXTENDS_EMBER_OBJECT": true,
348
+ "DEPRECATE_TRACKING_PACKAGE": true,
349
+ "DISABLE_7X_DEPRECATIONS": true,
350
+ "ENABLE_LEGACY_REQUEST_METHODS": true,
351
+ "ENABLE_LEGACY_SCHEMA_SERVICE": true
352
+ },
353
+ "features": {
354
+ "ENFORCE_STRICT_RESOURCE_FINALIZATION": false,
355
+ "JSON_API_CACHE_VALIDATION_ERRORS": false,
356
+ "SAMPLE_FEATURE_FLAG": false
357
+ },
358
+ "activeLogging": {
359
+ "DEBUG_RELATIONSHIP_NOTIFICATIONS": true,
360
+ "LOG_CACHE": true,
361
+ "LOG_CACHE_POLICY": true,
362
+ "LOG_GRAPH": true,
363
+ "LOG_IDENTIFIERS": true,
364
+ "LOG_INSTANCE_CACHE": true,
365
+ "LOG_METRIC_COUNTS": true,
366
+ "LOG_MUTATIONS": true,
367
+ "LOG_NOTIFICATIONS": true,
368
+ "LOG_OPERATIONS": true,
369
+ "LOG_PAYLOADS": true,
370
+ "LOG_REACT_SIGNAL_INTEGRATION": true,
371
+ "LOG_REQUESTS": true,
372
+ "LOG_REQUEST_STATUS": true,
373
+ "__INTERNAL_LOG_NATIVE_MAP_SET_COUNTS": true
374
+ },
375
+ "env": {
376
+ "TESTING": true,
377
+ "PRODUCTION": false,
378
+ "DEBUG": true,
379
+ "IS_RECORDING": false,
380
+ "IS_CI": true,
381
+ "SHOULD_RECORD": false
382
+ }
383
+ }
384
+ }
385
+ };
386
+ }
387
+ function updaterMethods() {
388
+ return {
389
+ config,
390
+ getGlobalConfig,
391
+ setConfig(packageRoot, value) {
392
+ runtimeConfig.packages[packageRoot] = value;
393
+ },
394
+ setGlobalConfig(key, value) {
395
+ runtimeConfig.global[key] = value;
396
+ }
397
+ };
398
+ }
399
+
400
+ // this is how runtime config can get injected at boot. I'm not sure yet if this
401
+ // should be public API, but we certainly need it internally to set things like
402
+ // the global fastboot.isRunning.
403
+ //
404
+ // consumers of this API push a function onto
405
+ // window._embroider_macros_runtime_config. The function is given four methods
406
+ // which allow it to read and write the per-package and global configs. The
407
+ // reason for allowing both read & write is that merging strategies are up to
408
+ // each consumers -- read first, then merge, then write.
409
+ //
410
+ // For an example user of this API, see where we generate
411
+ // embroider_macros_fastboot_init.js' in @embroider/core.
412
+ let updaters = typeof window !== 'undefined' ? window._embroider_macros_runtime_config : undefined;
413
+ if (updaters) {
414
+ let methods = updaterMethods();
415
+ for (let updater of updaters) {
416
+ updater(methods);
417
+ }
418
+ }
419
+ const PotentialLegacyOperations = new Set(['findRecord', 'findAll', 'query', 'queryRecord', 'findBelongsTo', 'findHasMany', 'updateRecord', 'createRecord', 'deleteRecord']);
420
+ const LegacyNetworkHandler = {
421
+ request(context, next) {
422
+ // if we are not a legacy request, move on
423
+ if (context.request.url || !context.request.op || !PotentialLegacyOperations.has(context.request.op)) {
424
+ return next(context.request);
425
+ }
426
+ const {
427
+ store
428
+ } = context.request;
429
+ if (!store._fetchManager) {
430
+ store._fetchManager = new FetchManager(store);
431
+ }
432
+ switch (context.request.op) {
433
+ case 'findRecord':
434
+ return findRecord(context);
435
+ case 'findAll':
436
+ return findAll(context);
437
+ case 'query':
438
+ return query(context);
439
+ case 'queryRecord':
440
+ return queryRecord(context);
441
+ case 'findBelongsTo':
442
+ return findBelongsTo(context);
443
+ case 'findHasMany':
444
+ return findHasMany(context);
445
+ case 'updateRecord':
446
+ return saveRecord(context);
447
+ case 'createRecord':
448
+ return saveRecord(context);
449
+ case 'deleteRecord':
450
+ return saveRecord(context);
451
+ default:
452
+ return next(context.request);
453
+ }
454
+ }
455
+ };
456
+ function findBelongsTo(context) {
457
+ const {
458
+ store,
459
+ data,
460
+ records: identifiers
461
+ } = context.request;
462
+ const {
463
+ options,
464
+ record,
465
+ links,
466
+ useLink,
467
+ field
468
+ } = data;
469
+ const identifier = identifiers?.[0];
470
+
471
+ // short circuit if we are already loading
472
+ const pendingRequest = identifier && store._fetchManager.getPendingFetch(identifier, options);
473
+ if (pendingRequest) {
474
+ return pendingRequest;
475
+ }
476
+ if (useLink) {
477
+ (test => {
478
+ if (!test) {
479
+ throw new Error(`Expected a related link when calling store.findBelongsTo, found ${String(links)}`);
480
+ }
481
+ })(links && links.related);
482
+ return _findBelongsTo(store, record, links.related, field, options);
483
+ }
484
+ (test => {
485
+ if (!test) {
486
+ throw new Error(`Expected an identifier`);
487
+ }
488
+ })(Array.isArray(identifiers) && identifiers.length === 1);
489
+ const manager = store._fetchManager;
490
+ assertIdentifierHasId(identifier);
491
+ return options.reload ? manager.scheduleFetch(identifier, options, context.request) : manager.fetchDataIfNeededForIdentifier(identifier, options, context.request);
492
+ }
493
+ function findHasMany(context) {
494
+ const {
495
+ store,
496
+ data,
497
+ records: identifiers
498
+ } = context.request;
499
+ const {
500
+ options,
501
+ record,
502
+ links,
503
+ useLink,
504
+ field
505
+ } = data;
506
+
507
+ // link case
508
+ if (useLink) {
509
+ const adapter = store.adapterFor(record.type);
510
+ /*
511
+ If a relationship was originally populated by the adapter as a link
512
+ (as opposed to a list of IDs), this method is called when the
513
+ relationship is fetched.
514
+ The link (which is usually a URL) is passed through unchanged, so the
515
+ adapter can make whatever request it wants.
516
+ The usual use-case is for the server to register a URL as a link, and
517
+ then use that URL in the future to make a request for the relationship.
518
+ */
519
+ (test => {
520
+ if (!test) {
521
+ throw new Error(`You tried to load a hasMany relationship but you have no adapter (for ${record.type})`);
522
+ }
523
+ })(adapter);
524
+ (test => {
525
+ if (!test) {
526
+ throw new Error(`You tried to load a hasMany relationship from a specified 'link' in the original payload but your adapter does not implement 'findHasMany'`);
527
+ }
528
+ })(typeof adapter.findHasMany === 'function');
529
+ (test => {
530
+ if (!test) {
531
+ throw new Error(`Expected a related link when calling store.findHasMany, found ${String(links)}`);
532
+ }
533
+ })(links && links.related);
534
+ return _findHasMany(adapter, store, record, links.related, field, options);
535
+ }
536
+
537
+ // identifiers case
538
+ (test => {
539
+ if (!test) {
540
+ throw new Error(`Expected an array of identifiers to fetch`);
541
+ }
542
+ })(Array.isArray(identifiers));
543
+ const fetches = new Array(identifiers.length);
544
+ const manager = store._fetchManager;
545
+ for (let i = 0; i < identifiers.length; i++) {
546
+ const identifier = identifiers[i];
547
+ // TODO we probably can be lenient here and return from cache for the isNew case
548
+ assertIdentifierHasId(identifier);
549
+ fetches[i] = options.reload ? manager.scheduleFetch(identifier, options, context.request) : manager.fetchDataIfNeededForIdentifier(identifier, options, context.request);
550
+ }
551
+ return Promise.all(fetches);
552
+ }
553
+ function saveRecord(context) {
554
+ const {
555
+ store,
556
+ data,
557
+ op: operation
558
+ } = context.request;
559
+ const {
560
+ options,
561
+ record: identifier
562
+ } = data;
563
+ assertPrivateStore(store);
564
+ store.cache.willCommit(identifier, context);
565
+ const saveOptions = Object.assign({
566
+ [SaveOp]: operation
567
+ }, options);
568
+ const fetchManagerPromise = store._fetchManager.scheduleSave(identifier, saveOptions);
569
+ return fetchManagerPromise.then(payload => {
570
+ let result;
571
+ store._join(() => {
572
+ // @ts-expect-error we don't have access to a response in legacy
573
+ result = store.cache.didCommit(identifier, {
574
+ request: context.request,
575
+ content: payload
576
+ });
577
+ });
578
+
579
+ // blatantly lie if we were a createRecord request
580
+ // to give some semblance of cache-control to the
581
+ // CachePolicy while legacy is still around
582
+ if (store.lifetimes?.didRequest && operation === 'createRecord') {
583
+ store.lifetimes.didRequest(context.request, {
584
+ status: 201
585
+ }, null, store);
586
+ }
587
+ return store.peekRecord(result.data);
588
+ }).catch(e => {
589
+ let err = e;
590
+ if (!e) {
591
+ err = new Error(`Unknown Error Occurred During Request`);
592
+ } else if (typeof e === 'string') {
593
+ err = new Error(e);
594
+ }
595
+ adapterDidInvalidate(store, identifier, err);
596
+ throw err;
597
+ });
598
+ }
599
+ function adapterDidInvalidate(store, identifier, error) {
600
+ if (error && error.isAdapterError === true && error.code === 'InvalidError') {
601
+ const serializer = store.serializerFor(identifier.type);
602
+
603
+ // TODO @deprecate extractErrors being called
604
+ // TODO remove extractErrors from the default serializers.
605
+ if (serializer && typeof serializer.extractErrors === 'function') {
606
+ const errorsHash = serializer.extractErrors(store, store.modelFor(identifier.type), error, identifier.id);
607
+ error.errors = errorsHashToArray(errorsHash);
608
+ }
609
+ }
610
+ const cache = store.cache;
611
+ if (error.errors) {
612
+ (test => {
613
+ if (!test) {
614
+ throw new Error(`Expected the cache in use by resource ${String(identifier)} to have a getErrors(identifier) method for retrieving errors.`);
615
+ }
616
+ })(typeof cache.getErrors === 'function');
617
+ let jsonApiErrors = error.errors;
618
+ if (jsonApiErrors.length === 0) {
619
+ jsonApiErrors = [{
620
+ title: 'Invalid Error',
621
+ detail: '',
622
+ source: {
623
+ pointer: '/data'
624
+ }
625
+ }];
626
+ }
627
+ cache.commitWasRejected(identifier, jsonApiErrors);
628
+ } else {
629
+ cache.commitWasRejected(identifier);
630
+ }
631
+ }
632
+ function makeArray(value) {
633
+ return Array.isArray(value) ? value : [value];
634
+ }
635
+ const PRIMARY_ATTRIBUTE_KEY = 'base';
636
+ function errorsHashToArray(errors) {
637
+ const out = [];
638
+ if (errors) {
639
+ Object.keys(errors).forEach(key => {
640
+ const messages = makeArray(errors[key]);
641
+ for (let i = 0; i < messages.length; i++) {
642
+ let title = 'Invalid Attribute';
643
+ let pointer = `/data/attributes/${key}`;
644
+ if (key === PRIMARY_ATTRIBUTE_KEY) {
645
+ title = 'Invalid Document';
646
+ pointer = `/data`;
647
+ }
648
+ out.push({
649
+ title: title,
650
+ detail: messages[i],
651
+ source: {
652
+ pointer: pointer
653
+ }
654
+ });
655
+ }
656
+ });
657
+ }
658
+ return out;
659
+ }
660
+ function findRecord(context) {
661
+ const {
662
+ store,
663
+ data
664
+ } = context.request;
665
+ const {
666
+ record: identifier,
667
+ options
668
+ } = data;
669
+ assertPrivateStore(store);
670
+ let promise;
671
+
672
+ // if not loaded start loading
673
+ if (!store._instanceCache.recordIsLoaded(identifier)) {
674
+ promise = store._fetchManager.fetchDataIfNeededForIdentifier(identifier, options, context.request);
675
+
676
+ // Refetch if the reload option is passed
677
+ } else if (options.reload) {
678
+ assertIdentifierHasId(identifier);
679
+ promise = store._fetchManager.scheduleFetch(identifier, options, context.request);
680
+ } else {
681
+ let snapshot = null;
682
+ const adapter = store.adapterFor(identifier.type);
683
+
684
+ // Refetch the record if the adapter thinks the record is stale
685
+ if (typeof options.reload === 'undefined' && adapter.shouldReloadRecord && adapter.shouldReloadRecord(store, snapshot = store._fetchManager.createSnapshot(identifier, options))) {
686
+ assertIdentifierHasId(identifier);
687
+ {
688
+ promise = store._fetchManager.scheduleFetch(identifier, Object.assign({}, options, {
689
+ reload: true
690
+ }), context.request);
691
+ }
692
+ } else {
693
+ // Trigger the background refetch if backgroundReload option is passed
694
+ if (options.backgroundReload !== false && (options.backgroundReload || !adapter.shouldBackgroundReloadRecord || adapter.shouldBackgroundReloadRecord(store, snapshot = snapshot || store._fetchManager.createSnapshot(identifier, options)))) {
695
+ assertIdentifierHasId(identifier);
696
+ {
697
+ void store._fetchManager.scheduleFetch(identifier, Object.assign({}, options, {
698
+ backgroundReload: true
699
+ }), context.request);
700
+ }
701
+ }
702
+
703
+ // Return the cached record
704
+ promise = Promise.resolve(identifier);
705
+ }
706
+ }
707
+ return promise.then(i => store.peekRecord(i));
708
+ }
709
+ function findAll(context) {
710
+ const {
711
+ store,
712
+ data
713
+ } = context.request;
714
+ const {
715
+ type,
716
+ options
717
+ } = data;
718
+ assertPrivateStore(store);
719
+ const adapter = store.adapterFor(type);
720
+ (test => {
721
+ if (!test) {
722
+ throw new Error(`You tried to load all records but you have no adapter (for ${type})`);
723
+ }
724
+ })(adapter);
725
+ (test => {
726
+ if (!test) {
727
+ throw new Error(`You tried to load all records but your adapter does not implement 'findAll'`);
728
+ }
729
+ })(typeof adapter.findAll === 'function');
730
+
731
+ // avoid initializing the liveArray just to set `isUpdating`
732
+ const maybeRecordArray = store.recordArrayManager._live.get(type);
733
+ const snapshotArray = new SnapshotRecordArray(store, type, options);
734
+ const shouldReload = options.reload || options.reload !== false && (adapter.shouldReloadAll && adapter.shouldReloadAll(store, snapshotArray) || !adapter.shouldReloadAll && snapshotArray.length === 0);
735
+ let fetch;
736
+ if (shouldReload) {
737
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
738
+ maybeRecordArray && (maybeRecordArray.isUpdating = true);
739
+ fetch = _findAll(adapter, store, type, snapshotArray, context.request, true);
740
+ } else {
741
+ fetch = Promise.resolve(store.peekAll(type));
742
+ if (options.backgroundReload || options.backgroundReload !== false && (!adapter.shouldBackgroundReloadAll || adapter.shouldBackgroundReloadAll(store, snapshotArray))) {
743
+ // eslint-disable-next-line @typescript-eslint/no-unused-expressions
744
+ maybeRecordArray && (maybeRecordArray.isUpdating = true);
745
+ void _findAll(adapter, store, type, snapshotArray, context.request, false);
746
+ }
747
+ }
748
+ return fetch;
749
+ }
750
+ function _findAll(adapter, store, type, snapshotArray, request, isAsyncFlush) {
751
+ const schema = store.modelFor(type);
752
+ let promise = Promise.resolve().then(() => adapter.findAll(store, schema, null, snapshotArray));
753
+ promise = promise.then(adapterPayload => {
754
+ (test => {
755
+ if (!test) {
756
+ throw new Error(`You made a 'findAll' request for '${type}' records, but the adapter's response did not have any data`);
757
+ }
758
+ })(payloadIsNotBlank(adapterPayload));
759
+ const serializer = store.serializerFor(type);
760
+ const payload = normalizeResponseHelper(serializer, store, schema, adapterPayload, null, 'findAll');
761
+ store._push(payload, isAsyncFlush);
762
+ snapshotArray._recordArray.isUpdating = false;
763
+ {
764
+ if (getGlobalConfig().WarpDrive.debug.LOG_REQUESTS || globalThis.getWarpDriveRuntimeConfig().debug.LOG_REQUESTS) {
765
+ // eslint-disable-next-line no-console
766
+ console.log(`request: findAll<${type}> background reload complete`);
767
+ }
768
+ }
769
+ return snapshotArray._recordArray;
770
+ });
771
+ {
772
+ if (!request.disableTestWaiter) {
773
+ promise = waitFor(promise);
774
+ }
775
+ }
776
+ return promise;
777
+ }
778
+ function query(context) {
779
+ const {
780
+ store,
781
+ data
782
+ } = context.request;
783
+ assertPrivateStore(store);
784
+ let {
785
+ options
786
+ } = data;
787
+ // eslint-disable-next-line @typescript-eslint/no-shadow
788
+ const {
789
+ type,
790
+ query
791
+ } = data;
792
+ const adapter = store.adapterFor(type);
793
+ (test => {
794
+ if (!test) {
795
+ throw new Error(`You tried to make a query but you have no adapter (for ${type})`);
796
+ }
797
+ })(adapter);
798
+ (test => {
799
+ if (!test) {
800
+ throw new Error(`You tried to make a query but your adapter does not implement 'query'`);
801
+ }
802
+ })(typeof adapter.query === 'function');
803
+ const recordArray = options._recordArray || store.recordArrayManager.getCollection({
804
+ type,
805
+ query
806
+ });
807
+ {
808
+ options = Object.assign({}, options);
809
+ delete options._recordArray;
810
+ }
811
+ const schema = store.modelFor(type);
812
+ const promise = Promise.resolve().then(() => adapter.query(store, schema, query, recordArray, options));
813
+ return promise.then(adapterPayload => {
814
+ const serializer = store.serializerFor(type);
815
+ const payload = normalizeResponseHelper(serializer, store, schema, adapterPayload, null, 'query');
816
+ const identifiers = store._push(payload, true);
817
+ (test => {
818
+ if (!test) {
819
+ throw new Error('The response to store.query is expected to be an array but it was a single record. Please wrap your response in an array or use `store.queryRecord` to query for a single record.');
820
+ }
821
+ })(Array.isArray(identifiers));
822
+ store.recordArrayManager.populateManagedArray(recordArray, identifiers, payload);
823
+ return recordArray;
824
+ });
825
+ }
826
+ function assertSingleResourceDocument(payload) {
827
+ (test => {
828
+ if (!test) {
829
+ throw new Error(`Expected the primary data returned by the serializer for a 'queryRecord' response to be a single object or null but instead it was an array.`);
830
+ }
831
+ })(!Array.isArray(payload.data));
832
+ }
833
+ function queryRecord(context) {
834
+ const {
835
+ store,
836
+ data
837
+ } = context.request;
838
+ // eslint-disable-next-line @typescript-eslint/no-shadow
839
+ const {
840
+ type,
841
+ query,
842
+ options
843
+ } = data;
844
+ const adapter = store.adapterFor(type);
845
+ (test => {
846
+ if (!test) {
847
+ throw new Error(`You tried to make a query but you have no adapter (for ${type})`);
848
+ }
849
+ })(adapter);
850
+ (test => {
851
+ if (!test) {
852
+ throw new Error(`You tried to make a query but your adapter does not implement 'queryRecord'`);
853
+ }
854
+ })(typeof adapter.queryRecord === 'function');
855
+ const schema = store.modelFor(type);
856
+ const promise = Promise.resolve().then(() => adapter.queryRecord(store, schema, query, options));
857
+ return promise.then(adapterPayload => {
858
+ const serializer = store.serializerFor(type);
859
+ const payload = normalizeResponseHelper(serializer, store, schema, adapterPayload, null, 'queryRecord');
860
+ assertSingleResourceDocument(payload);
861
+ const identifier = store._push(payload, true);
862
+ return identifier ? store.peekRecord(identifier) : null;
863
+ });
864
+ }
865
+
866
+ /**
867
+ * Extends the signature of {@link Store} with additional
868
+ * methods available when using the legacy network layer.
869
+ *
870
+ * @public
871
+ * @noInheritDoc
872
+ * @legacy
873
+ */
874
+
875
+ /**
876
+ * @deprecated - use {@link LegacyStoreCompat} instead
877
+ */
878
+
879
+ /**
880
+ Returns an instance of the adapter for a given type. For
881
+ example, `adapterFor('person')` will return an instance of
882
+ the adapter located at `app/adapters/person.js`
883
+
884
+ If no `person` adapter is found, this method will look
885
+ for an `application` adapter (the default adapter for
886
+ your entire application).
887
+
888
+ @public
889
+ @param modelName
890
+ */
891
+
892
+ function adapterFor(modelName, _allowMissing) {
893
+ (test => {
894
+ if (!test) {
895
+ throw new Error(`Attempted to call store.adapterFor(), but the store instance has already been destroyed.`);
896
+ }
897
+ })(!(this.isDestroying || this.isDestroyed));
898
+ (test => {
899
+ if (!test) {
900
+ throw new Error(`You need to pass a model name to the store's adapterFor method`);
901
+ }
902
+ })(modelName);
903
+ (test => {
904
+ if (!test) {
905
+ throw new Error(`Passing classes to store.adapterFor has been removed. Please pass a dasherized string instead of ${modelName}`);
906
+ }
907
+ })(typeof modelName === 'string');
908
+ this._adapterCache = this._adapterCache || Object.create(null);
909
+ const normalizedModelName = _deprecatingNormalize(modelName);
910
+ const {
911
+ _adapterCache
912
+ } = this;
913
+ let adapter = _adapterCache[normalizedModelName];
914
+ if (adapter) {
915
+ return adapter;
916
+ }
917
+ const owner = getOwner(this);
918
+
919
+ // name specific adapter
920
+ adapter = owner.lookup(`adapter:${normalizedModelName}`);
921
+ if (adapter !== undefined) {
922
+ _adapterCache[normalizedModelName] = adapter;
923
+ return adapter;
924
+ }
925
+
926
+ // no adapter found for the specific name, fallback and check for application adapter
927
+ adapter = _adapterCache.application || owner.lookup('adapter:application');
928
+ if (adapter !== undefined) {
929
+ _adapterCache[normalizedModelName] = adapter;
930
+ _adapterCache.application = adapter;
931
+ return adapter;
932
+ }
933
+ (test => {
934
+ if (!test) {
935
+ throw new Error(`No adapter was found for '${modelName}' and no 'application' adapter was found as a fallback.`);
936
+ }
937
+ })(_allowMissing);
938
+ }
939
+
940
+ /**
941
+ Returns an instance of the serializer for a given type. For
942
+ example, `serializerFor('person')` will return an instance of
943
+ `App.PersonSerializer`.
944
+
945
+ If no `App.PersonSerializer` is found, this method will look
946
+ for an `App.ApplicationSerializer` (the default serializer for
947
+ your entire application).
948
+
949
+ If a serializer cannot be found on the adapter, it will fall back
950
+ to an instance of `JSONSerializer`.
951
+
952
+ @public
953
+ @param modelName the record to serialize
954
+ */
955
+ function serializerFor(modelName) {
956
+ (test => {
957
+ if (!test) {
958
+ throw new Error(`Attempted to call store.serializerFor(), but the store instance has already been destroyed.`);
959
+ }
960
+ })(!(this.isDestroying || this.isDestroyed));
961
+ (test => {
962
+ if (!test) {
963
+ throw new Error(`You need to pass a model name to the store's serializerFor method`);
964
+ }
965
+ })(modelName);
966
+ (test => {
967
+ if (!test) {
968
+ throw new Error(`Passing classes to store.serializerFor has been removed. Please pass a dasherized string instead of ${modelName}`);
969
+ }
970
+ })(typeof modelName === 'string');
971
+ this._serializerCache = this._serializerCache || Object.create(null);
972
+ const normalizedModelName = _deprecatingNormalize(modelName);
973
+ const {
974
+ _serializerCache
975
+ } = this;
976
+ let serializer = _serializerCache[normalizedModelName];
977
+ if (serializer) {
978
+ return serializer;
979
+ }
980
+
981
+ // by name
982
+ const owner = getOwner(this);
983
+ serializer = owner.lookup(`serializer:${normalizedModelName}`);
984
+ if (serializer !== undefined) {
985
+ _serializerCache[normalizedModelName] = serializer;
986
+ return serializer;
987
+ }
988
+
989
+ // no serializer found for the specific model, fallback and check for application serializer
990
+ serializer = _serializerCache.application || owner.lookup('serializer:application');
991
+ if (serializer !== undefined) {
992
+ _serializerCache[normalizedModelName] = serializer;
993
+ _serializerCache.application = serializer;
994
+ return serializer;
995
+ }
996
+ return null;
997
+ }
998
+
999
+ /**
1000
+ `normalize` converts a json payload into the normalized form expected by
1001
+ {@link Store.push | push} using the serializer specified by `modelName`
1002
+
1003
+ :::warning
1004
+ Generally it would be better to invoke the serializer yourself directly,
1005
+ or write a more specialized normalization utility.
1006
+ :::
1007
+
1008
+ Example
1009
+
1010
+ ```js
1011
+ socket.on('message', function(message) {
1012
+ let modelName = message.model;
1013
+ let data = message.data;
1014
+ store.push(store.normalize(modelName, data));
1015
+ });
1016
+ ```
1017
+
1018
+ @legacy
1019
+ @public
1020
+ @param modelName The name of the model type for this payload
1021
+ @return The normalized payload
1022
+ */
1023
+ // TODO @runspired @deprecate users should call normalize on the associated serializer directly
1024
+ function normalize(modelName, payload) {
1025
+ (test => {
1026
+ if (!test) {
1027
+ throw new Error(`Attempted to call store.normalize(), but the store instance has already been destroyed.`);
1028
+ }
1029
+ })(!(this.isDestroying || this.isDestroyed));
1030
+ (test => {
1031
+ if (!test) {
1032
+ throw new Error(`You need to pass a model name to the store's normalize method`);
1033
+ }
1034
+ })(modelName);
1035
+ (test => {
1036
+ if (!test) {
1037
+ throw new Error(`Passing classes to store methods has been removed. Please pass a dasherized string instead of ${typeof modelName}`);
1038
+ }
1039
+ })(typeof modelName === 'string');
1040
+ const normalizedModelName = _deprecatingNormalize(modelName);
1041
+ const serializer = this.serializerFor(normalizedModelName);
1042
+ const schema = this.modelFor(normalizedModelName);
1043
+ (test => {
1044
+ if (!test) {
1045
+ throw new Error(`You must define a normalize method in your serializer in order to call store.normalize`);
1046
+ }
1047
+ })(typeof serializer?.normalize === 'function');
1048
+ return serializer.normalize(schema, payload);
1049
+ }
1050
+
1051
+ /**
1052
+ Push some raw data into the store.
1053
+
1054
+ This method can be used both to push in brand new
1055
+ records, as well as to update existing records. You
1056
+ can push in more than one type of object at once.
1057
+ All objects should be in the format expected by the
1058
+ serializer.
1059
+
1060
+ ```js [app/serializers/application.js]
1061
+ import RESTSerializer from '@warp-drive/legacy/serializer/rest';
1062
+
1063
+ export default class ApplicationSerializer extends RESTSerializer;
1064
+ ```
1065
+
1066
+ ```js
1067
+ let pushData = {
1068
+ posts: [
1069
+ { id: 1, postTitle: "Great post", commentIds: [2] }
1070
+ ],
1071
+ comments: [
1072
+ { id: 2, commentBody: "Insightful comment" }
1073
+ ]
1074
+ }
1075
+
1076
+ store.pushPayload(pushData);
1077
+ ```
1078
+
1079
+ By default, the data will be deserialized using a default
1080
+ serializer (the application serializer if it exists).
1081
+
1082
+ Alternatively, `pushPayload` will accept a model type which
1083
+ will determine which serializer will process the payload.
1084
+
1085
+ ```js [app/serializers/application.js]
1086
+ import RESTSerializer from '@warp-drive/legacy/serializer/rest';
1087
+
1088
+ export default class ApplicationSerializer extends RESTSerializer;
1089
+ ```
1090
+
1091
+ ```js [app/serializers/post.js]
1092
+ import JSONSerializer from '@warp-drive/legacy/serializer/json';
1093
+
1094
+ export default JSONSerializer;
1095
+ ```
1096
+
1097
+ ```js
1098
+ store.pushPayload(pushData); // Will use the application serializer
1099
+ store.pushPayload('post', pushData); // Will use the post serializer
1100
+ ```
1101
+
1102
+ @public
1103
+ @param modelName Optionally, a model type used to determine which serializer will be used
1104
+ @param inputPayload
1105
+ */
1106
+ // TODO @runspired @deprecate pushPayload in favor of looking up the serializer
1107
+ function pushPayload(modelName, inputPayload) {
1108
+ (test => {
1109
+ if (!test) {
1110
+ throw new Error(`Attempted to call store.pushPayload(), but the store instance has already been destroyed.`);
1111
+ }
1112
+ })(!(this.isDestroying || this.isDestroyed));
1113
+ const payload = inputPayload || modelName;
1114
+ const normalizedModelName = inputPayload ? _deprecatingNormalize(modelName) : 'application';
1115
+ const serializer = this.serializerFor(normalizedModelName);
1116
+ (test => {
1117
+ if (!test) {
1118
+ throw new Error(`You cannot use 'store.pushPayload(<type>, <payload>)' unless the serializer for '${normalizedModelName}' defines 'pushPayload'`);
1119
+ }
1120
+ })(serializer && typeof serializer.pushPayload === 'function');
1121
+ serializer.pushPayload(this, payload);
1122
+ }
1123
+
1124
+ // TODO @runspired @deprecate records should implement their own serialization if desired
1125
+ function serializeRecord(record, options) {
1126
+ // TODO we used to check if the record was destroyed here
1127
+ if (!this._fetchManager) {
1128
+ this._fetchManager = new FetchManager(this);
1129
+ }
1130
+ return this._fetchManager.createSnapshot(recordIdentifierFor(record)).serialize(options);
1131
+ }
1132
+ function cleanup() {
1133
+ // enqueue destruction of any adapters/serializers we have created
1134
+ for (const adapterName in this._adapterCache) {
1135
+ const adapter = this._adapterCache[adapterName];
1136
+ if (typeof adapter.destroy === 'function') {
1137
+ adapter.destroy();
1138
+ }
1139
+ }
1140
+ for (const serializerName in this._serializerCache) {
1141
+ const serializer = this._serializerCache[serializerName];
1142
+ if (typeof serializer.destroy === 'function') {
1143
+ serializer.destroy();
1144
+ }
1145
+ }
1146
+ }
1147
+ export { LegacyNetworkHandler, adapterFor, cleanup, normalize, pushPayload, serializeRecord, serializerFor };