@warp-drive/legacy 5.9.0-alpha.21 → 5.9.0-alpha.23

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 (467) hide show
  1. package/dist/-private-DQ6Iap2t.js +193 -0
  2. package/dist/-private-DQ6Iap2t.js.map +1 -0
  3. package/dist/-private-DccRZQsm.js +904 -0
  4. package/dist/-private-DccRZQsm.js.map +1 -0
  5. package/dist/adapter/-private.d.ts +33 -0
  6. package/dist/adapter/-private.d.ts.map +1 -0
  7. package/dist/adapter/-private.js +3 -1
  8. package/dist/adapter/error.d.ts +2 -0
  9. package/dist/adapter/error.js +422 -453
  10. package/dist/adapter/error.js.map +1 -0
  11. package/{declarations → dist}/adapter/json-api.d.ts +30 -29
  12. package/dist/adapter/json-api.d.ts.map +1 -0
  13. package/dist/adapter/json-api.js +265 -143
  14. package/dist/adapter/json-api.js.map +1 -0
  15. package/dist/adapter/rest.d.ts +2 -0
  16. package/dist/adapter/rest.js +937 -1136
  17. package/dist/adapter/rest.js.map +1 -0
  18. package/dist/adapter-CARrK3-8.d.ts +784 -0
  19. package/dist/adapter-CARrK3-8.d.ts.map +1 -0
  20. package/dist/adapter-DFVAuivO.js +1130 -0
  21. package/dist/adapter-DFVAuivO.js.map +1 -0
  22. package/dist/adapter.d.ts +2 -0
  23. package/dist/adapter.js +1 -1248
  24. package/dist/compat/-private.d.ts +2 -0
  25. package/dist/compat/-private.js +3 -1
  26. package/dist/compat/builders.d.ts +169 -0
  27. package/dist/compat/builders.d.ts.map +1 -0
  28. package/dist/compat/builders.js +205 -245
  29. package/dist/compat/builders.js.map +1 -0
  30. package/dist/compat/extensions.d.ts +2 -0
  31. package/dist/compat/extensions.js +265 -304
  32. package/dist/compat/extensions.js.map +1 -0
  33. package/dist/compat/utils.d.ts +151 -0
  34. package/dist/compat/utils.d.ts.map +1 -0
  35. package/dist/compat/utils.js +187 -206
  36. package/dist/compat/utils.js.map +1 -0
  37. package/dist/compat-BbNNmTM-.d.ts +1386 -0
  38. package/dist/compat-BbNNmTM-.d.ts.map +1 -0
  39. package/dist/compat-g__Pfy2S.js +734 -0
  40. package/dist/compat-g__Pfy2S.js.map +1 -0
  41. package/dist/compat.d.ts +2 -0
  42. package/dist/compat.js +1 -1036
  43. package/dist/error-CzkyXr1Q.d.ts +435 -0
  44. package/dist/error-CzkyXr1Q.d.ts.map +1 -0
  45. package/dist/errors-BbCo7Tut.js +2134 -0
  46. package/dist/errors-BbCo7Tut.js.map +1 -0
  47. package/dist/extensions-DumNm4dA.d.ts +102 -0
  48. package/dist/extensions-DumNm4dA.d.ts.map +1 -0
  49. package/dist/index.d.ts +243 -0
  50. package/dist/index.d.ts.map +1 -0
  51. package/dist/index.js +267 -339
  52. package/dist/index.js.map +1 -0
  53. package/dist/json-BXNPTlcH.js +1106 -0
  54. package/dist/json-BXNPTlcH.js.map +1 -0
  55. package/dist/model/-private.d.ts +3 -0
  56. package/dist/model/-private.js +3 -1
  57. package/dist/model/migration-support.d.ts +335 -0
  58. package/dist/model/migration-support.d.ts.map +1 -0
  59. package/dist/model/migration-support.js +504 -575
  60. package/dist/model/migration-support.js.map +1 -0
  61. package/dist/model-62hn5_PD.d.ts +3107 -0
  62. package/dist/model-62hn5_PD.d.ts.map +1 -0
  63. package/dist/model-C7-ZIbd5.js +683 -0
  64. package/dist/model-C7-ZIbd5.js.map +1 -0
  65. package/dist/model-fragments-tlMYPSd_.js +334 -0
  66. package/dist/model-fragments-tlMYPSd_.js.map +1 -0
  67. package/dist/model-fragments.d.ts +294 -0
  68. package/dist/model-fragments.d.ts.map +1 -0
  69. package/dist/model-fragments.js +2 -94
  70. package/dist/model.d.ts +404 -0
  71. package/dist/model.d.ts.map +1 -0
  72. package/dist/model.js +3 -670
  73. package/{declarations/adapter/rest.d.ts → dist/rest-0NhtexIq.d.ts} +183 -173
  74. package/dist/rest-0NhtexIq.d.ts.map +1 -0
  75. package/dist/runtime-BPCpkOf1-CVHWB0MF.js +53 -0
  76. package/dist/runtime-BPCpkOf1-CVHWB0MF.js.map +1 -0
  77. package/dist/schema-provider-7RHVFTdb.js +1929 -0
  78. package/dist/schema-provider-7RHVFTdb.js.map +1 -0
  79. package/{declarations → dist}/serializer/json-api.d.ts +3 -0
  80. package/dist/serializer/json-api.d.ts.map +1 -0
  81. package/dist/serializer/json-api.js +527 -519
  82. package/dist/serializer/json-api.js.map +1 -0
  83. package/{declarations → dist}/serializer/json.d.ts +3 -0
  84. package/dist/serializer/json.d.ts.map +1 -0
  85. package/dist/serializer/json.js +3 -6
  86. package/{declarations/serializer/-private/embedded-records-mixin.d.ts → dist/serializer/rest.d.ts} +56 -1
  87. package/dist/serializer/rest.d.ts.map +1 -0
  88. package/dist/serializer/rest.js +958 -1199
  89. package/dist/serializer/rest.js.map +1 -0
  90. package/dist/serializer/transform.d.ts +313 -0
  91. package/dist/serializer/transform.d.ts.map +1 -0
  92. package/dist/serializer/transform.js +283 -308
  93. package/dist/serializer/transform.js.map +1 -0
  94. package/dist/serializer.d.ts +151 -0
  95. package/dist/serializer.d.ts.map +1 -0
  96. package/dist/serializer.js +205 -205
  97. package/dist/serializer.js.map +1 -0
  98. package/dist/store-C4qYzXei.js +473 -0
  99. package/dist/store-C4qYzXei.js.map +1 -0
  100. package/dist/store.d.ts +13 -0
  101. package/dist/store.d.ts.map +1 -0
  102. package/dist/store.js +1 -641
  103. package/dist/unpkg/dev/-private-CrEyBE-B.js +189 -0
  104. package/dist/unpkg/dev/-private-CrEyBE-B.js.map +1 -0
  105. package/dist/unpkg/dev/-private-vOhxVExH.js +896 -0
  106. package/dist/unpkg/dev/-private-vOhxVExH.js.map +1 -0
  107. package/dist/unpkg/dev/adapter/-private.js +3 -1
  108. package/dist/unpkg/dev/adapter/error.js +421 -452
  109. package/dist/unpkg/dev/adapter/error.js.map +1 -0
  110. package/dist/unpkg/dev/adapter/json-api.js +239 -257
  111. package/dist/unpkg/dev/adapter/json-api.js.map +1 -0
  112. package/dist/unpkg/dev/adapter/rest.js +935 -1134
  113. package/dist/unpkg/dev/adapter/rest.js.map +1 -0
  114. package/dist/unpkg/dev/adapter-RlAbN461.js +1129 -0
  115. package/dist/unpkg/dev/adapter-RlAbN461.js.map +1 -0
  116. package/dist/unpkg/dev/adapter.js +1 -1247
  117. package/dist/unpkg/dev/compat/-private.js +3 -1
  118. package/dist/unpkg/dev/compat/builders.js +204 -247
  119. package/dist/unpkg/dev/compat/builders.js.map +1 -0
  120. package/dist/unpkg/dev/compat/extensions.js +264 -303
  121. package/dist/unpkg/dev/compat/extensions.js.map +1 -0
  122. package/dist/unpkg/dev/compat/utils.js +186 -205
  123. package/dist/unpkg/dev/compat/utils.js.map +1 -0
  124. package/dist/unpkg/dev/compat-B2O7jZLN.js +825 -0
  125. package/dist/unpkg/dev/compat-B2O7jZLN.js.map +1 -0
  126. package/dist/unpkg/dev/compat.js +1 -1178
  127. package/dist/unpkg/dev/errors-DiBJX1gq.js +2127 -0
  128. package/dist/unpkg/dev/errors-DiBJX1gq.js.map +1 -0
  129. package/dist/unpkg/dev/index.js +266 -344
  130. package/dist/unpkg/dev/index.js.map +1 -0
  131. package/dist/unpkg/dev/json-DDJji3V3.js +1104 -0
  132. package/dist/unpkg/dev/json-DDJji3V3.js.map +1 -0
  133. package/dist/unpkg/dev/model/-private.js +3 -1
  134. package/dist/unpkg/dev/model/migration-support.js +481 -549
  135. package/dist/unpkg/dev/model/migration-support.js.map +1 -0
  136. package/dist/unpkg/dev/model-BrJ3pFM-.js +655 -0
  137. package/dist/unpkg/dev/model-BrJ3pFM-.js.map +1 -0
  138. package/dist/unpkg/dev/model-fragments-tlMYPSd_.js +334 -0
  139. package/dist/unpkg/dev/model-fragments-tlMYPSd_.js.map +1 -0
  140. package/dist/unpkg/dev/model-fragments.js +2 -94
  141. package/dist/unpkg/dev/model.js +3 -681
  142. package/dist/unpkg/dev/runtime-BPCpkOf1-CVHWB0MF.js +53 -0
  143. package/dist/unpkg/dev/runtime-BPCpkOf1-CVHWB0MF.js.map +1 -0
  144. package/dist/unpkg/dev/schema-provider-Z5GzOhu-.js +1834 -0
  145. package/dist/unpkg/dev/schema-provider-Z5GzOhu-.js.map +1 -0
  146. package/dist/unpkg/dev/serializer/json-api.js +514 -634
  147. package/dist/unpkg/dev/serializer/json-api.js.map +1 -0
  148. package/dist/unpkg/dev/serializer/json.js +3 -4
  149. package/dist/unpkg/dev/serializer/rest.js +954 -1196
  150. package/dist/unpkg/dev/serializer/rest.js.map +1 -0
  151. package/dist/unpkg/dev/serializer/transform.js +283 -308
  152. package/dist/unpkg/dev/serializer/transform.js.map +1 -0
  153. package/dist/unpkg/dev/serializer.js +205 -205
  154. package/dist/unpkg/dev/serializer.js.map +1 -0
  155. package/dist/unpkg/dev/store-D_V8-HQl.js +472 -0
  156. package/dist/unpkg/dev/store-D_V8-HQl.js.map +1 -0
  157. package/dist/unpkg/dev/store.js +1 -649
  158. package/dist/unpkg/dev/util-dSKEgQDp.js +12 -0
  159. package/dist/unpkg/dev/util-dSKEgQDp.js.map +1 -0
  160. package/dist/unpkg/dev/utils-DYC9utVJ.js +11 -0
  161. package/dist/unpkg/dev/utils-DYC9utVJ.js.map +1 -0
  162. package/dist/unpkg/dev-deprecated/-private-CrEyBE-B.js +189 -0
  163. package/dist/unpkg/dev-deprecated/-private-CrEyBE-B.js.map +1 -0
  164. package/dist/unpkg/dev-deprecated/-private-vOhxVExH.js +896 -0
  165. package/dist/unpkg/dev-deprecated/-private-vOhxVExH.js.map +1 -0
  166. package/dist/unpkg/dev-deprecated/adapter/-private.js +3 -1
  167. package/dist/unpkg/dev-deprecated/adapter/error.js +421 -452
  168. package/dist/unpkg/dev-deprecated/adapter/error.js.map +1 -0
  169. package/dist/unpkg/dev-deprecated/adapter/json-api.js +239 -257
  170. package/dist/unpkg/dev-deprecated/adapter/json-api.js.map +1 -0
  171. package/dist/unpkg/dev-deprecated/adapter/rest.js +935 -1134
  172. package/dist/unpkg/dev-deprecated/adapter/rest.js.map +1 -0
  173. package/dist/unpkg/dev-deprecated/adapter-RlAbN461.js +1129 -0
  174. package/dist/unpkg/dev-deprecated/adapter-RlAbN461.js.map +1 -0
  175. package/dist/unpkg/dev-deprecated/adapter.js +1 -1247
  176. package/dist/unpkg/dev-deprecated/compat/-private.js +3 -1
  177. package/dist/unpkg/dev-deprecated/compat/builders.js +204 -247
  178. package/dist/unpkg/dev-deprecated/compat/builders.js.map +1 -0
  179. package/dist/unpkg/dev-deprecated/compat/extensions.js +264 -303
  180. package/dist/unpkg/dev-deprecated/compat/extensions.js.map +1 -0
  181. package/dist/unpkg/dev-deprecated/compat/utils.js +186 -205
  182. package/dist/unpkg/dev-deprecated/compat/utils.js.map +1 -0
  183. package/dist/unpkg/dev-deprecated/compat-CgUa-VIF.js +825 -0
  184. package/dist/unpkg/dev-deprecated/compat-CgUa-VIF.js.map +1 -0
  185. package/dist/unpkg/dev-deprecated/compat.js +1 -1178
  186. package/dist/unpkg/dev-deprecated/errors-Cba2PDIB.js +2128 -0
  187. package/dist/unpkg/dev-deprecated/errors-Cba2PDIB.js.map +1 -0
  188. package/dist/unpkg/dev-deprecated/index.js +266 -343
  189. package/dist/unpkg/dev-deprecated/index.js.map +1 -0
  190. package/dist/unpkg/dev-deprecated/json-DDJji3V3.js +1104 -0
  191. package/dist/unpkg/dev-deprecated/json-DDJji3V3.js.map +1 -0
  192. package/dist/unpkg/dev-deprecated/model/-private.js +3 -1
  193. package/dist/unpkg/dev-deprecated/model/migration-support.js +492 -566
  194. package/dist/unpkg/dev-deprecated/model/migration-support.js.map +1 -0
  195. package/dist/unpkg/dev-deprecated/model-D-JSD2Bx.js +660 -0
  196. package/dist/unpkg/dev-deprecated/model-D-JSD2Bx.js.map +1 -0
  197. package/dist/unpkg/dev-deprecated/model-fragments-tlMYPSd_.js +334 -0
  198. package/dist/unpkg/dev-deprecated/model-fragments-tlMYPSd_.js.map +1 -0
  199. package/dist/unpkg/dev-deprecated/model-fragments.js +2 -94
  200. package/dist/unpkg/dev-deprecated/model.js +3 -685
  201. package/dist/unpkg/dev-deprecated/runtime-BPCpkOf1-CVHWB0MF.js +53 -0
  202. package/dist/unpkg/dev-deprecated/runtime-BPCpkOf1-CVHWB0MF.js.map +1 -0
  203. package/dist/unpkg/dev-deprecated/schema-provider-n96uVRhl.js +1835 -0
  204. package/dist/unpkg/dev-deprecated/schema-provider-n96uVRhl.js.map +1 -0
  205. package/dist/unpkg/dev-deprecated/serializer/json-api.js +514 -634
  206. package/dist/unpkg/dev-deprecated/serializer/json-api.js.map +1 -0
  207. package/dist/unpkg/dev-deprecated/serializer/json.js +3 -4
  208. package/dist/unpkg/dev-deprecated/serializer/rest.js +954 -1196
  209. package/dist/unpkg/dev-deprecated/serializer/rest.js.map +1 -0
  210. package/dist/unpkg/dev-deprecated/serializer/transform.js +283 -308
  211. package/dist/unpkg/dev-deprecated/serializer/transform.js.map +1 -0
  212. package/dist/unpkg/dev-deprecated/serializer.js +205 -205
  213. package/dist/unpkg/dev-deprecated/serializer.js.map +1 -0
  214. package/dist/unpkg/dev-deprecated/store-BX7GvXC_.js +472 -0
  215. package/dist/unpkg/dev-deprecated/store-BX7GvXC_.js.map +1 -0
  216. package/dist/unpkg/dev-deprecated/store.js +1 -649
  217. package/dist/unpkg/dev-deprecated/util-9mgEBJ3W.js +18 -0
  218. package/dist/unpkg/dev-deprecated/util-9mgEBJ3W.js.map +1 -0
  219. package/dist/unpkg/dev-deprecated/utils-Cl86-e7F.js +17 -0
  220. package/dist/unpkg/dev-deprecated/utils-Cl86-e7F.js.map +1 -0
  221. package/dist/unpkg/prod/-private-B4bxoTuK.js +160 -0
  222. package/dist/unpkg/prod/-private-B4bxoTuK.js.map +1 -0
  223. package/dist/unpkg/prod/-private-SfXkkHKd.js +745 -0
  224. package/dist/unpkg/prod/-private-SfXkkHKd.js.map +1 -0
  225. package/dist/unpkg/prod/adapter/-private.js +3 -1
  226. package/dist/unpkg/prod/adapter/error.js +418 -447
  227. package/dist/unpkg/prod/adapter/error.js.map +1 -0
  228. package/dist/unpkg/prod/adapter/json-api.js +236 -252
  229. package/dist/unpkg/prod/adapter/json-api.js.map +1 -0
  230. package/dist/unpkg/prod/adapter/rest.js +911 -1097
  231. package/dist/unpkg/prod/adapter/rest.js.map +1 -0
  232. package/dist/unpkg/prod/adapter-DAx_reer.js +1111 -0
  233. package/dist/unpkg/prod/adapter-DAx_reer.js.map +1 -0
  234. package/dist/unpkg/prod/adapter.js +1 -1214
  235. package/dist/unpkg/prod/compat/-private.js +3 -1
  236. package/dist/unpkg/prod/compat/builders.js +165 -182
  237. package/dist/unpkg/prod/compat/builders.js.map +1 -0
  238. package/dist/unpkg/prod/compat/extensions.js +252 -283
  239. package/dist/unpkg/prod/compat/extensions.js.map +1 -0
  240. package/dist/unpkg/prod/compat/utils.js +183 -200
  241. package/dist/unpkg/prod/compat/utils.js.map +1 -0
  242. package/dist/unpkg/prod/compat-CJ87MzDn.js +547 -0
  243. package/dist/unpkg/prod/compat-CJ87MzDn.js.map +1 -0
  244. package/dist/unpkg/prod/compat.js +1 -742
  245. package/dist/unpkg/prod/errors-CoKAYQJi.js +1977 -0
  246. package/dist/unpkg/prod/errors-CoKAYQJi.js.map +1 -0
  247. package/dist/unpkg/prod/index.js +245 -308
  248. package/dist/unpkg/prod/index.js.map +1 -0
  249. package/dist/unpkg/prod/json-BD1kRTfB.js +1080 -0
  250. package/dist/unpkg/prod/json-BD1kRTfB.js.map +1 -0
  251. package/dist/unpkg/prod/model/-private.js +3 -1
  252. package/dist/unpkg/prod/model/migration-support.js +477 -542
  253. package/dist/unpkg/prod/model/migration-support.js.map +1 -0
  254. package/dist/unpkg/prod/model-D2zzdmP8.js +603 -0
  255. package/dist/unpkg/prod/model-D2zzdmP8.js.map +1 -0
  256. package/dist/unpkg/prod/model-fragments-tlMYPSd_.js +334 -0
  257. package/dist/unpkg/prod/model-fragments-tlMYPSd_.js.map +1 -0
  258. package/dist/unpkg/prod/model-fragments.js +2 -94
  259. package/dist/unpkg/prod/model.js +3 -596
  260. package/dist/unpkg/prod/runtime-BPCpkOf1-CVHWB0MF.js +53 -0
  261. package/dist/unpkg/prod/runtime-BPCpkOf1-CVHWB0MF.js.map +1 -0
  262. package/dist/unpkg/prod/schema-provider-BUXipKQV.js +1622 -0
  263. package/dist/unpkg/prod/schema-provider-BUXipKQV.js.map +1 -0
  264. package/dist/unpkg/prod/serializer/json-api.js +497 -592
  265. package/dist/unpkg/prod/serializer/json-api.js.map +1 -0
  266. package/dist/unpkg/prod/serializer/json.js +3 -4
  267. package/dist/unpkg/prod/serializer/rest.js +938 -1166
  268. package/dist/unpkg/prod/serializer/rest.js.map +1 -0
  269. package/dist/unpkg/prod/serializer/transform.js +283 -308
  270. package/dist/unpkg/prod/serializer/transform.js.map +1 -0
  271. package/dist/unpkg/prod/serializer.js +205 -205
  272. package/dist/unpkg/prod/serializer.js.map +1 -0
  273. package/dist/unpkg/prod/store-RGGc1ilN.js +392 -0
  274. package/dist/unpkg/prod/store-RGGc1ilN.js.map +1 -0
  275. package/dist/unpkg/prod/store.js +1 -517
  276. package/dist/unpkg/prod/util-dSKEgQDp.js +12 -0
  277. package/dist/unpkg/prod/util-dSKEgQDp.js.map +1 -0
  278. package/dist/unpkg/prod/utils-DYC9utVJ.js +11 -0
  279. package/dist/unpkg/prod/utils-DYC9utVJ.js.map +1 -0
  280. package/dist/unpkg/prod-deprecated/-private-B4bxoTuK.js +160 -0
  281. package/dist/unpkg/prod-deprecated/-private-B4bxoTuK.js.map +1 -0
  282. package/dist/unpkg/prod-deprecated/-private-SfXkkHKd.js +745 -0
  283. package/dist/unpkg/prod-deprecated/-private-SfXkkHKd.js.map +1 -0
  284. package/dist/unpkg/prod-deprecated/adapter/-private.js +3 -1
  285. package/dist/unpkg/prod-deprecated/adapter/error.js +418 -447
  286. package/dist/unpkg/prod-deprecated/adapter/error.js.map +1 -0
  287. package/dist/unpkg/prod-deprecated/adapter/json-api.js +236 -252
  288. package/dist/unpkg/prod-deprecated/adapter/json-api.js.map +1 -0
  289. package/dist/unpkg/prod-deprecated/adapter/rest.js +911 -1097
  290. package/dist/unpkg/prod-deprecated/adapter/rest.js.map +1 -0
  291. package/{declarations/adapter.d.ts → dist/unpkg/prod-deprecated/adapter-DAx_reer.js} +445 -110
  292. package/dist/unpkg/prod-deprecated/adapter-DAx_reer.js.map +1 -0
  293. package/dist/unpkg/prod-deprecated/adapter.js +1 -1214
  294. package/dist/unpkg/prod-deprecated/compat/-private.js +3 -1
  295. package/dist/unpkg/prod-deprecated/compat/builders.js +165 -182
  296. package/dist/unpkg/prod-deprecated/compat/builders.js.map +1 -0
  297. package/dist/unpkg/prod-deprecated/compat/extensions.js +252 -283
  298. package/dist/unpkg/prod-deprecated/compat/extensions.js.map +1 -0
  299. package/dist/unpkg/prod-deprecated/compat/utils.js +183 -200
  300. package/dist/unpkg/prod-deprecated/compat/utils.js.map +1 -0
  301. package/dist/unpkg/prod-deprecated/compat-CJ87MzDn.js +547 -0
  302. package/dist/unpkg/prod-deprecated/compat-CJ87MzDn.js.map +1 -0
  303. package/dist/unpkg/prod-deprecated/compat.js +1 -742
  304. package/dist/unpkg/prod-deprecated/errors-BUVJ3jcW.js +1978 -0
  305. package/dist/unpkg/prod-deprecated/errors-BUVJ3jcW.js.map +1 -0
  306. package/dist/unpkg/prod-deprecated/index.js +245 -307
  307. package/dist/unpkg/prod-deprecated/index.js.map +1 -0
  308. package/dist/unpkg/prod-deprecated/json-BD1kRTfB.js +1080 -0
  309. package/dist/unpkg/prod-deprecated/json-BD1kRTfB.js.map +1 -0
  310. package/dist/unpkg/prod-deprecated/model/-private.js +3 -1
  311. package/dist/unpkg/prod-deprecated/model/migration-support.js +488 -559
  312. package/dist/unpkg/prod-deprecated/model/migration-support.js.map +1 -0
  313. package/dist/unpkg/prod-deprecated/model-66Pl5niO.js +604 -0
  314. package/dist/unpkg/prod-deprecated/model-66Pl5niO.js.map +1 -0
  315. package/dist/unpkg/prod-deprecated/model-fragments-tlMYPSd_.js +334 -0
  316. package/dist/unpkg/prod-deprecated/model-fragments-tlMYPSd_.js.map +1 -0
  317. package/dist/unpkg/prod-deprecated/model-fragments.js +2 -94
  318. package/dist/unpkg/prod-deprecated/model.js +3 -599
  319. package/dist/unpkg/prod-deprecated/runtime-BPCpkOf1-CVHWB0MF.js +53 -0
  320. package/dist/unpkg/prod-deprecated/runtime-BPCpkOf1-CVHWB0MF.js.map +1 -0
  321. package/dist/unpkg/prod-deprecated/schema-provider-DtyBaLoG.js +1635 -0
  322. package/dist/unpkg/prod-deprecated/schema-provider-DtyBaLoG.js.map +1 -0
  323. package/dist/unpkg/prod-deprecated/serializer/json-api.js +497 -592
  324. package/dist/unpkg/prod-deprecated/serializer/json-api.js.map +1 -0
  325. package/dist/unpkg/prod-deprecated/serializer/json.js +3 -4
  326. package/dist/unpkg/prod-deprecated/serializer/rest.js +938 -1166
  327. package/dist/unpkg/prod-deprecated/serializer/rest.js.map +1 -0
  328. package/dist/unpkg/prod-deprecated/serializer/transform.js +283 -308
  329. package/dist/unpkg/prod-deprecated/serializer/transform.js.map +1 -0
  330. package/dist/unpkg/prod-deprecated/serializer.js +205 -205
  331. package/dist/unpkg/prod-deprecated/serializer.js.map +1 -0
  332. package/dist/unpkg/prod-deprecated/store-0L2CkYzh.js +392 -0
  333. package/dist/unpkg/prod-deprecated/store-0L2CkYzh.js.map +1 -0
  334. package/dist/unpkg/prod-deprecated/store.js +1 -517
  335. package/dist/unpkg/prod-deprecated/util-CQQdHX3n.js +14 -0
  336. package/dist/unpkg/prod-deprecated/util-CQQdHX3n.js.map +1 -0
  337. package/dist/unpkg/prod-deprecated/utils-BMI_JwFP.js +13 -0
  338. package/dist/unpkg/prod-deprecated/utils-BMI_JwFP.js.map +1 -0
  339. package/dist/util-Bnx8nm-D.js +29 -0
  340. package/dist/util-Bnx8nm-D.js.map +1 -0
  341. package/dist/utils-ZAytYep8.js +28 -0
  342. package/dist/utils-ZAytYep8.js.map +1 -0
  343. package/package.json +16 -16
  344. package/declarations/adapter/-private/build-url-mixin.d.ts +0 -189
  345. package/declarations/adapter/-private/fastboot-interface.d.ts +0 -8
  346. package/declarations/adapter/-private/utils/continue-on-reject.d.ts +0 -1
  347. package/declarations/adapter/-private/utils/determine-body-promise.d.ts +0 -4
  348. package/declarations/adapter/-private/utils/fetch.d.ts +0 -8
  349. package/declarations/adapter/-private/utils/parse-response-headers.d.ts +0 -1
  350. package/declarations/adapter/-private/utils/serialize-into-hash.d.ts +0 -6
  351. package/declarations/adapter/-private/utils/serialize-query-params.d.ts +0 -5
  352. package/declarations/adapter/-private.d.ts +0 -5
  353. package/declarations/adapter/error.d.ts +0 -431
  354. package/declarations/compat/-private.d.ts +0 -13
  355. package/declarations/compat/builders/find-all.d.ts +0 -34
  356. package/declarations/compat/builders/find-record.d.ts +0 -56
  357. package/declarations/compat/builders/query.d.ts +0 -66
  358. package/declarations/compat/builders/save-record.d.ts +0 -34
  359. package/declarations/compat/builders/utils.d.ts +0 -3
  360. package/declarations/compat/builders.d.ts +0 -14
  361. package/declarations/compat/extensions.d.ts +0 -99
  362. package/declarations/compat/legacy-network-handler/fetch-manager.d.ts +0 -55
  363. package/declarations/compat/legacy-network-handler/identifier-has-id.d.ts +0 -2
  364. package/declarations/compat/legacy-network-handler/legacy-data-fetch.d.ts +0 -11
  365. package/declarations/compat/legacy-network-handler/legacy-data-utils.d.ts +0 -5
  366. package/declarations/compat/legacy-network-handler/legacy-network-handler.d.ts +0 -9
  367. package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts +0 -528
  368. package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +0 -220
  369. package/declarations/compat/legacy-network-handler/serializer-response.d.ts +0 -6
  370. package/declarations/compat/legacy-network-handler/snapshot-record-array.d.ts +0 -110
  371. package/declarations/compat/legacy-network-handler/snapshot.d.ts +0 -255
  372. package/declarations/compat/utils.d.ts +0 -148
  373. package/declarations/compat.d.ts +0 -189
  374. package/declarations/index.d.ts +0 -244
  375. package/declarations/model/-private/attr.d.ts +0 -191
  376. package/declarations/model/-private/belongs-to.d.ts +0 -195
  377. package/declarations/model/-private/debug/assert-polymorphic-type.d.ts +0 -5
  378. package/declarations/model/-private/errors.d.ts +0 -289
  379. package/declarations/model/-private/has-many.d.ts +0 -162
  380. package/declarations/model/-private/hooks.d.ts +0 -36
  381. package/declarations/model/-private/legacy-relationships-support.d.ts +0 -47
  382. package/declarations/model/-private/model-for-mixin.d.ts +0 -3
  383. package/declarations/model/-private/model-methods.d.ts +0 -39
  384. package/declarations/model/-private/model.d.ts +0 -1279
  385. package/declarations/model/-private/notify-changes.d.ts +0 -4
  386. package/declarations/model/-private/promise-belongs-to.d.ts +0 -40
  387. package/declarations/model/-private/promise-many-array.d.ts +0 -130
  388. package/declarations/model/-private/record-state.d.ts +0 -58
  389. package/declarations/model/-private/references/belongs-to.d.ts +0 -488
  390. package/declarations/model/-private/references/has-many.d.ts +0 -498
  391. package/declarations/model/-private/schema-provider.d.ts +0 -66
  392. package/declarations/model/-private/type-utils.d.ts +0 -57
  393. package/declarations/model/-private/util.d.ts +0 -5
  394. package/declarations/model/-private.d.ts +0 -8
  395. package/declarations/model/migration-support.d.ts +0 -306
  396. package/declarations/model-fragments/extensions/fragment-array.d.ts +0 -54
  397. package/declarations/model-fragments/extensions/fragment.d.ts +0 -50
  398. package/declarations/model-fragments/hooks/model-for.d.ts +0 -27
  399. package/declarations/model-fragments/index.d.ts +0 -13
  400. package/declarations/model-fragments/instance-initializers/fragment-extensions.d.ts +0 -15
  401. package/declarations/model-fragments/utilities/with-array-defaults.d.ts +0 -56
  402. package/declarations/model-fragments/utilities/with-fragment-array-defaults.d.ts +0 -38
  403. package/declarations/model-fragments/utilities/with-fragment-defaults.d.ts +0 -34
  404. package/declarations/model-fragments/utilities/with-legacy.d.ts +0 -11
  405. package/declarations/model-fragments.d.ts +0 -9
  406. package/declarations/model.d.ts +0 -49
  407. package/declarations/serializer/-private/transforms/boolean.d.ts +0 -60
  408. package/declarations/serializer/-private/transforms/date.d.ts +0 -42
  409. package/declarations/serializer/-private/transforms/number.d.ts +0 -42
  410. package/declarations/serializer/-private/transforms/string.d.ts +0 -42
  411. package/declarations/serializer/-private/transforms/transform.d.ts +0 -118
  412. package/declarations/serializer/-private/utils.d.ts +0 -27
  413. package/declarations/serializer/rest.d.ts +0 -51
  414. package/declarations/serializer/transform.d.ts +0 -5
  415. package/declarations/serializer.d.ts +0 -252
  416. package/declarations/store/-private.d.ts +0 -223
  417. package/declarations/store.d.ts +0 -10
  418. package/dist/-private-BG3bMiKp.js +0 -1211
  419. package/dist/errors-DZKA206g.js +0 -2589
  420. package/dist/hooks-BvOqJBei.js +0 -105
  421. package/dist/json-DU8m1pGf.js +0 -1336
  422. package/dist/model-for-CXkJw59V.js +0 -297
  423. package/dist/runtime-BPCpkOf1-BKOwiRJp.js +0 -65
  424. package/dist/schema-provider-DtPOp0d7.js +0 -2382
  425. package/dist/serialize-into-hash-DPZYoF-i.js +0 -261
  426. package/dist/unpkg/dev/-private-DtjBbEgy.js +0 -1206
  427. package/dist/unpkg/dev/errors-DngRU-If.js +0 -2585
  428. package/dist/unpkg/dev/hooks-UuiLxWxS.js +0 -104
  429. package/dist/unpkg/dev/json-K2Y9z6MN.js +0 -1333
  430. package/dist/unpkg/dev/model-for-CXkJw59V.js +0 -297
  431. package/dist/unpkg/dev/runtime-BPCpkOf1-BKOwiRJp.js +0 -65
  432. package/dist/unpkg/dev/schema-provider-0D7f-QOn.js +0 -2272
  433. package/dist/unpkg/dev/serialize-into-hash-B2xDbuo5.js +0 -259
  434. package/dist/unpkg/dev/util-DvanW33H.js +0 -20
  435. package/dist/unpkg/dev/utils-BhvS1iTS.js +0 -8
  436. package/dist/unpkg/dev-deprecated/-private-DtjBbEgy.js +0 -1206
  437. package/dist/unpkg/dev-deprecated/errors-Bzidntpi.js +0 -2588
  438. package/dist/unpkg/dev-deprecated/hooks-DVXhMW14.js +0 -104
  439. package/dist/unpkg/dev-deprecated/json-K2Y9z6MN.js +0 -1333
  440. package/dist/unpkg/dev-deprecated/model-for-CXkJw59V.js +0 -297
  441. package/dist/unpkg/dev-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +0 -65
  442. package/dist/unpkg/dev-deprecated/schema-provider-ZwO5_fZw.js +0 -2292
  443. package/dist/unpkg/dev-deprecated/serialize-into-hash-B2xDbuo5.js +0 -259
  444. package/dist/unpkg/dev-deprecated/util-CWr5WQOT.js +0 -24
  445. package/dist/unpkg/dev-deprecated/utils-C9PJehtL.js +0 -12
  446. package/dist/unpkg/prod/-private-BdyZaGEh.js +0 -971
  447. package/dist/unpkg/prod/errors-B-PWeYlW.js +0 -2337
  448. package/dist/unpkg/prod/hooks-Bx-zBziU.js +0 -72
  449. package/dist/unpkg/prod/json-Qewu-UN9.js +0 -1300
  450. package/dist/unpkg/prod/model-for-CXkJw59V.js +0 -297
  451. package/dist/unpkg/prod/runtime-BPCpkOf1-BKOwiRJp.js +0 -65
  452. package/dist/unpkg/prod/schema-provider-BgRqk8i9.js +0 -1951
  453. package/dist/unpkg/prod/serialize-into-hash-DGlzQteF.js +0 -215
  454. package/dist/unpkg/prod/util-DvanW33H.js +0 -20
  455. package/dist/unpkg/prod/utils-BhvS1iTS.js +0 -8
  456. package/dist/unpkg/prod-deprecated/-private-BdyZaGEh.js +0 -971
  457. package/dist/unpkg/prod-deprecated/errors-Dl2J9d1D.js +0 -2340
  458. package/dist/unpkg/prod-deprecated/hooks-Cm60wWTf.js +0 -72
  459. package/dist/unpkg/prod-deprecated/json-Qewu-UN9.js +0 -1300
  460. package/dist/unpkg/prod-deprecated/model-for-CXkJw59V.js +0 -297
  461. package/dist/unpkg/prod-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +0 -65
  462. package/dist/unpkg/prod-deprecated/schema-provider-Dh6tRqVI.js +0 -1980
  463. package/dist/unpkg/prod-deprecated/serialize-into-hash-DGlzQteF.js +0 -215
  464. package/dist/unpkg/prod-deprecated/util-B6cn-i93.js +0 -23
  465. package/dist/unpkg/prod-deprecated/utils-BUWwQwCh.js +0 -11
  466. package/dist/util-Dul6TZts.js +0 -35
  467. package/dist/utils-Cqw9eRj5.js +0 -23
@@ -1,2588 +0,0 @@
1
- import { Context } from '@warp-drive/core/reactive/-private';
2
- import { memoized, defineSignal, defineNonEnumerableSignal, notifyInternalSignal } from '@warp-drive/core/signals/-leaked';
3
- import { assertPrivateStore, isResourceKey, recordIdentifierFor, isPrivateStore, storeFor, fastPush, createLegacyManyArray } from '@warp-drive/core/store/-private';
4
- import { getOrSetGlobal } from '@warp-drive/core/types/-private';
5
- import { EnableHydration } from '@warp-drive/core/types/request';
6
- import { u as upgradeStore } from "./-private-DtjBbEgy.js";
7
- import { computed, get } from '@ember/object';
8
- import PromiseProxyMixin from '@ember/object/promise-proxy-mixin';
9
- import ObjectProxy from '@ember/object/proxy';
10
- import { d as decorateMethodV2, a as decorateFieldV2, i as initializeDeferredDecorator } from "./runtime-BPCpkOf1-BKOwiRJp.js";
11
- import { A } from '@ember/array';
12
- import ArrayProxy from '@ember/array/proxy';
13
- import { mapBy, not } from '@ember/object/computed';
14
- const PromiseObject = ObjectProxy.extend(PromiseProxyMixin);
15
- const LegacyPromiseProxy = Symbol.for('LegacyPromiseProxy');
16
-
17
- // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-extraneous-class
18
-
19
- const Extended = PromiseObject;
20
-
21
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
22
-
23
- /**
24
- A PromiseBelongsTo is a PromiseObject that also proxies certain method calls
25
- to the underlying belongsTo model.
26
- Right now we proxy:
27
- * `reload()`
28
- @class PromiseBelongsTo
29
- @private
30
- */
31
- class PromiseBelongsTo extends Extended {
32
- get id() {
33
- const {
34
- key,
35
- legacySupport
36
- } = this._belongsToState;
37
- const ref = legacySupport.referenceFor('belongsTo', key);
38
- return ref.id();
39
- }
40
-
41
- // we don't proxy meta because we would need to proxy it to the relationship state container
42
- // however, meta on relationships does not trigger change notifications.
43
- // if you need relationship meta, you should do `record.belongsTo(relationshipName).meta()`
44
- static {
45
- decorateMethodV2(this.prototype, "id", [memoized]);
46
- }
47
- get meta() {
48
- // eslint-disable-next-line no-constant-condition
49
- {
50
- (test => {
51
- {
52
- throw new Error('You attempted to access meta on the promise for the async belongsTo relationship ' + `${this._belongsToState.modelName}:${this._belongsToState.key}'.` + '\nUse `record.belongsTo(relationshipName).meta()` instead.');
53
- }
54
- })();
55
- }
56
- return;
57
- }
58
- static {
59
- decorateMethodV2(this.prototype, "meta", [computed()]);
60
- }
61
- async reload(options) {
62
- (test => {
63
- if (!test) {
64
- throw new Error('You are trying to reload an async belongsTo before it has been created');
65
- }
66
- })(this.content !== undefined);
67
- const {
68
- key,
69
- legacySupport
70
- } = this._belongsToState;
71
- await legacySupport.reloadBelongsTo(key, options);
72
- return this;
73
- }
74
- }
75
- PromiseBelongsTo.prototype[LegacyPromiseProxy] = true;
76
-
77
- /**
78
- This class is returned as the result of accessing an async hasMany relationship
79
- on an instance of a Model extending from `@warp-drive/legacy/model`.
80
-
81
- A PromiseManyArray is an iterable proxy that allows templates to consume related
82
- ManyArrays and update once their contents are no longer pending.
83
-
84
- In your JS code you should resolve the promise first.
85
-
86
- ```js
87
- const comments = await post.comments;
88
- ```
89
-
90
- @public
91
- */
92
- class PromiseManyArray {
93
- /**
94
- * The promise for the relationship's content, or `null` once
95
- * {@link PromiseManyArray.destroy | destroy} has been called.
96
- */
97
-
98
- /**
99
- * Whether {@link PromiseManyArray.destroy | destroy} has been called.
100
- */
101
-
102
- /**
103
- * The resolved `ManyArray` for the relationship, if the promise has
104
- * resolved, else `null`.
105
- */
106
-
107
- constructor(promise, content) {
108
- this._update(promise, content);
109
- this.isDestroyed = false;
110
- this[LegacyPromiseProxy] = true;
111
- }
112
-
113
- /**
114
- * Retrieve the length of the content
115
- * @public
116
- */
117
- get length() {
118
- // shouldn't be needed, but ends up being needed
119
- // for computed chains even in 4.x
120
- {
121
- // eslint-disable-next-line @typescript-eslint/no-unused-expressions
122
- this['[]'];
123
- }
124
- return this.content ? this.content.length : 0;
125
- }
126
-
127
- /**
128
- * @private
129
- */
130
- // this will error if someone tries to call
131
- // A(identifierArray) since it is not configurable
132
- // which is preferrable to the `meta` override we used
133
- // before which required importing all of Ember
134
- static {
135
- decorateMethodV2(this.prototype, "length", [memoized]);
136
- }
137
- get '[]'() {
138
- // ember-source < 3.23 (e.g. 3.20 lts)
139
- // requires that the tag `'[]'` be notified
140
- // on the ArrayProxy in order for `{{#each}}`
141
- // to recompute. We entangle content.
142
- return this.content?.length && this.content;
143
- }
144
-
145
- /**
146
- * Iterate the proxied content. Called by the glimmer iterator in #each
147
- * We do not guarantee that forEach will always be available. This
148
- * may eventually be made to use Symbol.Iterator once glimmer supports it.
149
- *
150
- * @param cb
151
- * @return
152
- * @private
153
- */
154
- static {
155
- decorateMethodV2(this.prototype, '[]', [memoized]);
156
- }
157
- forEach(cb) {
158
- if (this.content && this.length) {
159
- this.content.forEach(cb);
160
- }
161
- }
162
-
163
- /**
164
- * Reload the relationship
165
- * @public
166
- */
167
- reload(options) {
168
- (test => {
169
- if (!test) {
170
- throw new Error('You are trying to reload an async manyArray before it has been created');
171
- }
172
- })(this.content);
173
- void this.content.reload(options);
174
- return this;
175
- }
176
-
177
- //---- Properties/Methods from the PromiseProxyMixin that we will keep as our API
178
-
179
- /**
180
- * Whether the loading promise is still pending
181
- *
182
- * @public
183
- */
184
-
185
- /**
186
- * Whether the loading promise rejected
187
- *
188
- * @public
189
- */
190
-
191
- /**
192
- * Whether the loading promise succeeded
193
- *
194
- * @public
195
- */
196
-
197
- /**
198
- * Whether the loading promise completed (resolved or rejected)
199
- *
200
- * @public
201
- */
202
-
203
- /**
204
- * chain this promise
205
- *
206
- * @public
207
- */
208
- then(success, rejected) {
209
- return this.promise.then(success, rejected);
210
- }
211
-
212
- /**
213
- * catch errors thrown by this promise
214
- * @public
215
- */
216
- catch(cb) {
217
- return this.promise.catch(cb);
218
- }
219
-
220
- /**
221
- * run cleanup after this promise completes
222
- *
223
- * @public
224
- */
225
- finally(cb) {
226
- return this.promise.finally(cb);
227
- }
228
-
229
- //---- Methods on EmberObject that we should keep
230
-
231
- /**
232
- * Tears down this proxy, releasing its {@link PromiseManyArray.content | content} and
233
- * {@link PromiseManyArray.promise | promise} and marking it as {@link PromiseManyArray.isDestroyed | destroyed}.
234
- *
235
- * @public
236
- */
237
- destroy() {
238
- this.isDestroyed = true;
239
- this.content = null;
240
- this.promise = null;
241
- }
242
-
243
- //---- Methods/Properties on ManyArray that we own and proxy to
244
-
245
- /**
246
- * Retrieve the links for this relationship
247
- * @public
248
- */
249
- get links() {
250
- return this.content ? this.content.links : undefined;
251
- }
252
-
253
- /**
254
- * Retrieve the meta for this relationship
255
- * @public
256
- */
257
- static {
258
- decorateMethodV2(this.prototype, "links", [memoized]);
259
- }
260
- get meta() {
261
- return this.content ? this.content.meta : undefined;
262
- }
263
-
264
- //---- Our own stuff
265
-
266
- /** @internal */
267
- static {
268
- decorateMethodV2(this.prototype, "meta", [memoized]);
269
- }
270
- _update(promise, content) {
271
- if (content !== undefined) {
272
- this.content = content;
273
- }
274
- this.promise = tapPromise(this, promise);
275
- }
276
- static create({
277
- promise,
278
- content
279
- }) {
280
- return new this(promise, content);
281
- }
282
- }
283
- defineSignal(PromiseManyArray.prototype, 'content', null);
284
- defineSignal(PromiseManyArray.prototype, 'isPending', false);
285
- defineSignal(PromiseManyArray.prototype, 'isRejected', false);
286
- defineSignal(PromiseManyArray.prototype, 'isFulfilled', false);
287
- defineSignal(PromiseManyArray.prototype, 'isSettled', false);
288
- function tapPromise(proxy, promise) {
289
- proxy.isPending = true;
290
- proxy.isSettled = false;
291
- proxy.isFulfilled = false;
292
- proxy.isRejected = false;
293
- return Promise.resolve(promise).then(content => {
294
- proxy.isPending = false;
295
- proxy.isFulfilled = true;
296
- proxy.isSettled = true;
297
- proxy.content = content;
298
- return content;
299
- }, error => {
300
- proxy.isPending = false;
301
- proxy.isFulfilled = false;
302
- proxy.isRejected = true;
303
- proxy.isSettled = true;
304
- throw error;
305
- });
306
- }
307
-
308
- /*
309
- Assert that `addedRecord` has a valid type so it can be added to the
310
- relationship of the `record`.
311
-
312
- The assert basically checks if the `addedRecord` can be added to the
313
- relationship (specified via `relationshipMeta`) of the `record`.
314
-
315
- This utility should only be used internally, as both record parameters must
316
- be stable record identifiers and the `relationshipMeta` needs to be the meta
317
- information about the relationship, retrieved via
318
- `record.relationshipFor(key)`.
319
- */
320
- let assertPolymorphicType;
321
- {
322
- // eslint-disable-next-line @typescript-eslint/no-shadow
323
- assertPolymorphicType = function assertPolymorphicType(parentIdentifier, parentDefinition, addedIdentifier, store) {
324
- if (parentDefinition.inverseIsImplicit) {
325
- return;
326
- }
327
- if (parentDefinition.isPolymorphic) {
328
- const meta = store.schema.fields(addedIdentifier)?.get(parentDefinition.inverseKey);
329
- if (meta) {
330
- (test => {
331
- if (!test) {
332
- throw new Error(`Expected the schema for the field ${parentDefinition.inverseKey} on ${addedIdentifier.type} to be for a legacy relationship`);
333
- }
334
- })(meta.kind === 'belongsTo' || meta.kind === 'hasMany');
335
- (test => {
336
- if (!test) {
337
- throw new Error(`The schema for the relationship '${parentDefinition.inverseKey}' on '${addedIdentifier.type}' type does not implement '${parentDefinition.type}' and thus cannot be assigned to the '${parentDefinition.key}' relationship in '${parentIdentifier.type}'. The definition should specify 'as: "${parentDefinition.type}"' in options.`);
338
- }
339
- })(meta?.options?.as === parentDefinition.type);
340
- }
341
- }
342
- };
343
- }
344
- function isResourceIdentiferWithRelatedLinks$1(value) {
345
- return Boolean(value && value.links && value.links.related);
346
- }
347
- /**
348
- A `HasManyReference` is a low-level API that allows access
349
- and manipulation of a hasMany relationship.
350
-
351
- It is especially useful when you're dealing with `async` relationships
352
- from `@warp-drive/legacy/model` as it allows synchronous access to
353
- the relationship data if loaded, as well as APIs for loading, reloading
354
- the data or accessing available information without triggering a load.
355
-
356
- It may also be useful when using `sync` relationships with `@warp-drive/legacy/model`
357
- that need to be loaded/reloaded with more precise timing than marking the
358
- relationship as `async` and relying on autofetch would have allowed.
359
-
360
- However,keep in mind that marking a relationship as `async: false` will introduce
361
- bugs into your application if the data is not always guaranteed to be available
362
- by the time the relationship is accessed. Ergo, it is recommended when using this
363
- approach to utilize `links` for unloaded relationship state instead of identifiers.
364
-
365
- Reference APIs are entangled with the relationship's underlying state,
366
- thus any getters or cached properties that utilize these will properly
367
- invalidate if the relationship state changes.
368
-
369
- References are "stable", meaning that multiple calls to retrieve the reference
370
- for a given relationship will always return the same HasManyReference.
371
-
372
- @class HasManyReference
373
- @public
374
- */
375
- class HasManyReference {
376
- /**
377
- * The field name on the parent record for this has-many relationship.
378
- *
379
- * @property key
380
- * @type {String}
381
- * @public
382
- */
383
-
384
- /**
385
- * The type of resource this relationship will contain.
386
- *
387
- * @property type
388
- * @type {String}
389
- * @public
390
- */
391
-
392
- // unsubscribe tokens given to us by the notification manager
393
- ___token;
394
- ___identifier;
395
- ___relatedTokenMap;
396
- constructor(store, graph, parentIdentifier, hasManyRelationship, key) {
397
- this.graph = graph;
398
- this.key = key;
399
- this.hasManyRelationship = hasManyRelationship;
400
- this.type = hasManyRelationship.definition.type;
401
- this.store = store;
402
- this.___identifier = parentIdentifier;
403
- this.___token = store.notifications.subscribe(parentIdentifier, (_, bucket, notifiedKey) => {
404
- if (bucket === 'relationships' && notifiedKey === key) {
405
- this._ref++;
406
- }
407
- });
408
- this.___relatedTokenMap = new Map();
409
- // TODO inverse
410
- }
411
-
412
- /**
413
- * This method should never be called by user code.
414
- *
415
- * @internal
416
- */
417
- destroy() {
418
- this.store.notifications.unsubscribe(this.___token);
419
- this.___relatedTokenMap.forEach(token => {
420
- this.store.notifications.unsubscribe(token);
421
- });
422
- this.___relatedTokenMap.clear();
423
- }
424
-
425
- /**
426
- * An array of identifiers for the records that this reference refers to.
427
- *
428
- * @property identifiers
429
- * @type {ResourceKey[]}
430
- * @public
431
- */
432
- get identifiers() {
433
- ensureRefCanSubscribe(this);
434
- // eslint-disable-next-line @typescript-eslint/no-unused-expressions
435
- this._ref;
436
- const resource = this._resource();
437
- const map = this.___relatedTokenMap;
438
- this.___relatedTokenMap = new Map();
439
- if (resource && resource.data) {
440
- return resource.data.map(resourceIdentifier => {
441
- const identifier = this.store.cacheKeyManager.getOrCreateRecordIdentifier(resourceIdentifier);
442
- let token = map.get(identifier);
443
- if (token) {
444
- map.delete(identifier);
445
- } else {
446
- token = this.store.notifications.subscribe(identifier, (_, bucket, notifiedKey) => {
447
- if (bucket === 'identity' || bucket === 'attributes' && notifiedKey === 'id') {
448
- this._ref++;
449
- }
450
- });
451
- }
452
- this.___relatedTokenMap.set(identifier, token);
453
- return identifier;
454
- });
455
- }
456
- map.forEach(token => {
457
- this.store.notifications.unsubscribe(token);
458
- });
459
- map.clear();
460
- return [];
461
- }
462
- static {
463
- decorateMethodV2(this.prototype, "identifiers", [memoized]);
464
- }
465
- _resource() {
466
- const cache = this.store.cache;
467
- return cache.getRelationship(this.___identifier, this.key);
468
- }
469
-
470
- /**
471
- This returns a string that represents how the reference will be
472
- looked up when it is loaded. If the relationship has a link it will
473
- use the "link" otherwise it defaults to "id".
474
- Example
475
- ```js [app/models/post.js]
476
- import { Model, hasMany } from '@warp-drive/legacy/model';
477
- export default class PostModel extends Model {
478
- @hasMany('comment', { async: true, inverse: null }) comments;
479
- }
480
- ```
481
- ```javascript
482
- let post = store.push({
483
- data: {
484
- type: 'post',
485
- id: 1,
486
- relationships: {
487
- comments: {
488
- data: [{ type: 'comment', id: 1 }]
489
- }
490
- }
491
- }
492
- });
493
- let commentsRef = post.hasMany('comments');
494
- // get the identifier of the reference
495
- if (commentsRef.remoteType() === "ids") {
496
- let ids = commentsRef.ids();
497
- } else if (commentsRef.remoteType() === "link") {
498
- let link = commentsRef.link();
499
- }
500
- ```
501
- @public
502
- @return The name of the remote type. This should either be `link` or `ids`
503
- */
504
- remoteType() {
505
- const value = this._resource();
506
- if (value && value.links && value.links.related) {
507
- return 'link';
508
- }
509
- return 'ids';
510
- }
511
-
512
- /**
513
- `ids()` returns an array of the record IDs in this relationship.
514
- Example
515
- ```js [app/models/post.js]
516
- import { Model, hasMany } from '@warp-drive/legacy/model';
517
- export default class PostModel extends Model {
518
- @hasMany('comment', { async: true, inverse: null }) comments;
519
- }
520
- ```
521
- ```javascript
522
- let post = store.push({
523
- data: {
524
- type: 'post',
525
- id: 1,
526
- relationships: {
527
- comments: {
528
- data: [{ type: 'comment', id: 1 }]
529
- }
530
- }
531
- }
532
- });
533
- let commentsRef = post.hasMany('comments');
534
- commentsRef.ids(); // ['1']
535
- ```
536
- @public
537
- @return The ids in this has-many relationship
538
- */
539
- ids() {
540
- return this.identifiers.map(identifier => identifier.id);
541
- }
542
-
543
- /**
544
- The link WarpDrive will use to fetch or reload this belongs-to
545
- relationship. By default it uses only the "related" resource linkage.
546
- Example
547
- ```javascript
548
- // models/blog.js
549
- import { Model, belongsTo } from '@warp-drive/legacy/model';
550
- export default Model.extend({
551
- user: belongsTo('user', { async: true, inverse: null })
552
- });
553
- let blog = store.push({
554
- data: {
555
- type: 'blog',
556
- id: 1,
557
- relationships: {
558
- user: {
559
- links: {
560
- related: '/articles/1/author'
561
- }
562
- }
563
- }
564
- }
565
- });
566
- let userRef = blog.belongsTo('user');
567
- // get the identifier of the reference
568
- if (userRef.remoteType() === "link") {
569
- let link = userRef.link();
570
- }
571
- ```
572
- @public
573
- @return The link WarpDrive will use to fetch or reload this belongs-to relationship.
574
- */
575
- link() {
576
- const resource = this._resource();
577
- if (isResourceIdentiferWithRelatedLinks$1(resource)) {
578
- if (resource.links) {
579
- const related = resource.links.related;
580
- return !related || typeof related === 'string' ? related : related.href;
581
- }
582
- }
583
- return null;
584
- }
585
-
586
- /**
587
- * any links that have been received for this relationship
588
- *
589
- * @public
590
- * @return
591
- */
592
- links() {
593
- const resource = this._resource();
594
- return resource && resource.links ? resource.links : null;
595
- }
596
-
597
- /**
598
- The meta data for the has-many relationship.
599
- Example
600
- ```javascript
601
- // models/blog.js
602
- import { Model, hasMany } from '@warp-drive/legacy/model';
603
- export default Model.extend({
604
- users: hasMany('user', { async: true, inverse: null })
605
- });
606
- let blog = store.push({
607
- data: {
608
- type: 'blog',
609
- id: 1,
610
- relationships: {
611
- users: {
612
- links: {
613
- related: {
614
- href: '/articles/1/authors'
615
- },
616
- },
617
- meta: {
618
- lastUpdated: 1458014400000
619
- }
620
- }
621
- }
622
- }
623
- });
624
- let usersRef = blog.hasMany('user');
625
- usersRef.meta() // { lastUpdated: 1458014400000 }
626
- ```
627
- @public
628
- @return The meta information for the belongs-to relationship.
629
- */
630
- meta() {
631
- let meta = null;
632
- const resource = this._resource();
633
- if (resource && resource.meta && typeof resource.meta === 'object') {
634
- meta = resource.meta;
635
- }
636
- return meta;
637
- }
638
-
639
- /**
640
- `push` can be used to update the data in the relationship and WarpDrive
641
- will treat the new data as the canonical value of this relationship on
642
- the backend. An empty array will signify the canonical value should be
643
- empty.
644
- Example model
645
- ```js [app/models/post.js]
646
- import { Model, hasMany } from '@warp-drive/legacy/model';
647
- export default class PostModel extends Model {
648
- @hasMany('comment', { async: true, inverse: null }) comments;
649
- }
650
- ```
651
- Setup some initial state, note we haven't loaded the comments yet:
652
- ```js
653
- const post = store.push({
654
- data: {
655
- type: 'post',
656
- id: '1',
657
- relationships: {
658
- comments: {
659
- data: [{ type: 'comment', id: '1' }]
660
- }
661
- }
662
- }
663
- });
664
- const commentsRef = post.hasMany('comments');
665
- commentsRef.ids(); // ['1']
666
- ```
667
- Update the state using `push`, note we can do this even without
668
- having loaded these comments yet by providing resource identifiers.
669
- Both full resources and resource identifiers are supported.
670
- ```js
671
- await commentsRef.push({
672
- data: [
673
- { type: 'comment', id: '2' },
674
- { type: 'comment', id: '3' },
675
- ]
676
- });
677
- commentsRef.ids(); // ['2', '3']
678
- ```
679
- For convenience, you can also pass in an array of resources or resource identifiers
680
- without wrapping them in the `data` property:
681
- ```js
682
- await commentsRef.push([
683
- { type: 'comment', id: '4' },
684
- { type: 'comment', id: '5' },
685
- ]);
686
- commentsRef.ids(); // ['4', '5']
687
- ```
688
- When using the `data` property, you may also include other resource data via included,
689
- as well as provide new links and meta to the relationship.
690
- ```js
691
- await commentsRef.push({
692
- links: {
693
- related: '/posts/1/comments'
694
- },
695
- meta: {
696
- total: 2
697
- },
698
- data: [
699
- { type: 'comment', id: '4' },
700
- { type: 'comment', id: '5' },
701
- ],
702
- included: [
703
- { type: 'other-thing', id: '1', attributes: { foo: 'bar' },
704
- ]
705
- });
706
- ```
707
- By default, the store will attempt to fetch any unloaded records before resolving
708
- the returned promise with the ManyArray.
709
- Alternatively, pass `true` as the second argument to avoid fetching unloaded records
710
- and instead the promise will resolve with void without attempting to fetch. This is
711
- particularly useful if you want to update the state of the relationship without
712
- forcing the load of all of the associated records.
713
- @public
714
- @param doc a JSONAPI document object describing the new value of this relationship.
715
- @param skipFetch [optional] if `true`, do not attempt to fetch unloaded records
716
- */
717
- async push(doc, skipFetch) {
718
- const {
719
- store
720
- } = this;
721
- const dataDoc = Array.isArray(doc) ? {
722
- data: doc
723
- } : doc;
724
- const isResourceData = Array.isArray(dataDoc.data) && dataDoc.data.length > 0 && isMaybeResource(dataDoc.data[0]);
725
-
726
- // enforce that one of links, meta or data is present
727
- (test => {
728
- if (!test) {
729
- throw new Error(`You must provide at least one of 'links', 'meta' or 'data' when calling hasManyReference.push`);
730
- }
731
- })('links' in dataDoc || 'meta' in dataDoc || 'data' in dataDoc);
732
- const identifiers = !Array.isArray(dataDoc.data) ? [] : isResourceData ? store._push(dataDoc, true) : dataDoc.data.map(i => store.cacheKeyManager.getOrCreateRecordIdentifier(i));
733
- const {
734
- identifier
735
- } = this.hasManyRelationship;
736
- {
737
- const relationshipMeta = this.hasManyRelationship.definition;
738
- identifiers.forEach(added => {
739
- assertPolymorphicType(identifier, relationshipMeta, added, store);
740
- });
741
- }
742
- const newData = {};
743
- // only set data if it was passed in
744
- if (Array.isArray(dataDoc.data)) {
745
- newData.data = identifiers;
746
- }
747
- if ('links' in dataDoc) {
748
- newData.links = dataDoc.links;
749
- }
750
- if ('meta' in dataDoc) {
751
- newData.meta = dataDoc.meta;
752
- }
753
- assertPrivateStore(store);
754
- store._join(() => {
755
- this.graph.push({
756
- op: 'updateRelationship',
757
- record: identifier,
758
- field: this.key,
759
- value: newData
760
- });
761
- });
762
- if (!skipFetch) return this.load();
763
- }
764
- _isLoaded() {
765
- const hasRelationshipDataProperty = this.hasManyRelationship.state.hasReceivedData;
766
- if (!hasRelationshipDataProperty) {
767
- return false;
768
- }
769
- const relationship = this.graph.getData(this.hasManyRelationship.identifier, this.key);
770
- return relationship.data ? relationship.data.every(identifier => {
771
- assertPrivateStore(this.store);
772
- return this.store._instanceCache.recordIsLoaded(identifier, true) === true;
773
- }) : false;
774
- }
775
-
776
- /**
777
- `value()` synchronously returns the current value of the has-many
778
- relationship. Unlike `record.relationshipName`, calling
779
- `value()` on a reference does not trigger a fetch if the async
780
- relationship is not yet loaded. If the relationship is not loaded
781
- it will always return `null`.
782
- Example
783
- ```js [app/models/post.js]
784
- import { Model, hasMany } from '@warp-drive/legacy/model';
785
- export default class PostModel extends Model {
786
- @hasMany('comment', { async: true, inverse: null }) comments;
787
- }
788
- ```
789
- ```javascript
790
- let post = store.push({
791
- data: {
792
- type: 'post',
793
- id: 1,
794
- relationships: {
795
- comments: {
796
- data: [{ type: 'comment', id: 1 }]
797
- }
798
- }
799
- }
800
- });
801
- let commentsRef = post.hasMany('comments');
802
- post.comments.then(function(comments) {
803
- commentsRef.value() === comments
804
- })
805
- ```
806
- @public
807
- */
808
- value() {
809
- const support = LEGACY_SUPPORT.get(this.___identifier);
810
- if (!ensureRefCanSubscribe(this)) {
811
- // eslint-disable-next-line @typescript-eslint/no-unused-expressions
812
- this._ref;
813
- return null;
814
- }
815
- return support.getManyArray(this.key);
816
- }
817
-
818
- /**
819
- Loads the relationship if it is not already loaded. If the
820
- relationship is already loaded this method does not trigger a new
821
- load. This causes a request to the specified
822
- relationship link or reloads all items currently in the relationship.
823
- Example
824
- ```js [app/models/post.js]
825
- import { Model, hasMany } from '@warp-drive/legacy/model';
826
- export default class PostModel extends Model {
827
- @hasMany('comment', { async: true, inverse: null }) comments;
828
- }
829
- ```
830
- ```javascript
831
- let post = store.push({
832
- data: {
833
- type: 'post',
834
- id: 1,
835
- relationships: {
836
- comments: {
837
- data: [{ type: 'comment', id: 1 }]
838
- }
839
- }
840
- }
841
- });
842
- let commentsRef = post.hasMany('comments');
843
- commentsRef.load().then(function(comments) {
844
- //...
845
- });
846
- ```
847
- You may also pass in an options object whose properties will be
848
- fed forward. This enables you to pass `adapterOptions` into the
849
- request given to the adapter via the reference.
850
- Example
851
- ```javascript
852
- commentsRef.load({ adapterOptions: { isPrivate: true } })
853
- .then(function(comments) {
854
- //...
855
- });
856
- ```
857
- ```js [app/adapters/comment.js]
858
- export default ApplicationAdapter.extend({
859
- findMany(store, type, id, snapshots) {
860
- // In the adapter you will have access to adapterOptions.
861
- let adapterOptions = snapshots[0].adapterOptions;
862
- }
863
- });
864
- ```
865
- @public
866
- @param options the options to pass in.
867
- @return a promise that resolves with the ManyArray in
868
- this has-many relationship.
869
- */
870
- async load(options) {
871
- const support = LEGACY_SUPPORT.get(this.___identifier);
872
- const fetchSyncRel = !this.hasManyRelationship.definition.isAsync && !areAllInverseRecordsLoaded(this.store, this._resource());
873
- return fetchSyncRel ? support.reloadHasMany(this.key, options) :
874
- // we cast to fix the return type since typescript and eslint don't understand async functions
875
- // properly
876
- support.getHasMany(this.key, options);
877
- }
878
-
879
- /**
880
- Reloads this has-many relationship. This causes a request to the specified
881
- relationship link or reloads all items currently in the relationship.
882
- Example
883
- ```js [app/models/post.js]
884
- import { Model, hasMany } from '@warp-drive/legacy/model';
885
- export default class PostModel extends Model {
886
- @hasMany('comment', { async: true, inverse: null }) comments;
887
- }
888
- ```
889
- ```javascript
890
- let post = store.push({
891
- data: {
892
- type: 'post',
893
- id: 1,
894
- relationships: {
895
- comments: {
896
- data: [{ type: 'comment', id: 1 }]
897
- }
898
- }
899
- }
900
- });
901
- let commentsRef = post.hasMany('comments');
902
- commentsRef.reload().then(function(comments) {
903
- //...
904
- });
905
- ```
906
- You may also pass in an options object whose properties will be
907
- fed forward. This enables you to pass `adapterOptions` into the
908
- request given to the adapter via the reference. A full example
909
- can be found in the `load` method.
910
- Example
911
- ```javascript
912
- commentsRef.reload({ adapterOptions: { isPrivate: true } })
913
- ```
914
- @public
915
- @param options the options to pass in.
916
- @return a promise that resolves with the ManyArray in this has-many relationship.
917
- */
918
- reload(options) {
919
- const support = LEGACY_SUPPORT.get(this.___identifier);
920
- return support.reloadHasMany(this.key, options);
921
- }
922
- }
923
- defineNonEnumerableSignal(HasManyReference.prototype, '_ref', 0);
924
- function isMaybeResource(object) {
925
- const keys = Object.keys(object).filter(k => k !== 'id' && k !== 'type' && k !== 'lid');
926
- return keys.length > 0;
927
- }
928
- function ensureRefCanSubscribe(rel) {
929
- const loaded = rel._isLoaded();
930
- if (!loaded) {
931
- // subscribe to changes
932
- // for when we are not loaded yet
933
- //
934
- // because the graph optimizes the case where a relationship has never been subscribed,
935
- // we force accessed to be true here. When we make the graph public we should create a
936
- // subscribe/unsubscribe API
937
- const edge = rel.graph.get(rel.___identifier, rel.key);
938
- (test => {
939
- if (!test) {
940
- throw new Error(`Expected a hasMany relationship for ${rel.___identifier.type}:${rel.key}`);
941
- }
942
- })('accessed' in edge);
943
- edge.accessed = true;
944
- return false;
945
- }
946
- return true;
947
- }
948
- function isResourceIdentiferWithRelatedLinks(value) {
949
- return Boolean(value && value.links && value.links.related);
950
- }
951
-
952
- /**
953
- A `BelongsToReference` is a low-level API that allows access
954
- and manipulation of a belongsTo relationship.
955
-
956
- It is especially useful when you're dealing with `async` relationships
957
- from `@warp-drive/legacy/model` as it allows synchronous access to
958
- the relationship data if loaded, as well as APIs for loading, reloading
959
- the data or accessing available information without triggering a load.
960
-
961
- It may also be useful when using `sync` relationships with `@warp-drive/legacy/model`
962
- that need to be loaded/reloaded with more precise timing than marking the
963
- relationship as `async` and relying on autofetch would have allowed.
964
-
965
- However,keep in mind that marking a relationship as `async: false` will introduce
966
- bugs into your application if the data is not always guaranteed to be available
967
- by the time the relationship is accessed. Ergo, it is recommended when using this
968
- approach to utilize `links` for unloaded relationship state instead of identifiers.
969
-
970
- Reference APIs are entangled with the relationship's underlying state,
971
- thus any getters or cached properties that utilize these will properly
972
- invalidate if the relationship state changes.
973
-
974
- References are "stable", meaning that multiple calls to retrieve the reference
975
- for a given relationship will always return the same HasManyReference.
976
-
977
- @public
978
- */
979
- class BelongsToReference {
980
- /**
981
- * The field name on the parent record for this has-many relationship.
982
- *
983
- */
984
-
985
- /**
986
- * The type of resource this relationship will contain.
987
- *
988
- */
989
-
990
- // unsubscribe tokens given to us by the notification manager
991
-
992
- constructor(store, graph, parentIdentifier, belongsToRelationship, key) {
993
- this.graph = graph;
994
- this.key = key;
995
- this.belongsToRelationship = belongsToRelationship;
996
- this.type = belongsToRelationship.definition.type;
997
- this.store = store;
998
- this.___identifier = parentIdentifier;
999
- this.___relatedToken = null;
1000
- this.___token = store.notifications.subscribe(parentIdentifier, (_, bucket, notifiedKey) => {
1001
- if (bucket === 'relationships' && notifiedKey === key) {
1002
- this._ref++;
1003
- }
1004
- });
1005
-
1006
- // TODO inverse
1007
- }
1008
- destroy() {
1009
- // TODO @feature we need the notification manager often enough
1010
- // we should potentially just expose it fully public
1011
- this.store.notifications.unsubscribe(this.___token);
1012
- this.___token = null;
1013
- if (this.___relatedToken) {
1014
- this.store.notifications.unsubscribe(this.___relatedToken);
1015
- this.___relatedToken = null;
1016
- }
1017
- }
1018
-
1019
- /**
1020
- * The identifier of the record that this reference refers to.
1021
- * `null` if no related record is known.
1022
- *
1023
- */
1024
- get identifier() {
1025
- if (this.___relatedToken) {
1026
- this.store.notifications.unsubscribe(this.___relatedToken);
1027
- this.___relatedToken = null;
1028
- }
1029
- const resource = this._resource();
1030
- if (resource && resource.data) {
1031
- const identifier = this.store.cacheKeyManager.getOrCreateRecordIdentifier(resource.data);
1032
- this.___relatedToken = this.store.notifications.subscribe(identifier, (_, bucket, notifiedKey) => {
1033
- if (bucket === 'identity' || bucket === 'attributes' && notifiedKey === 'id') {
1034
- this._ref++;
1035
- }
1036
- });
1037
- return identifier;
1038
- }
1039
- return null;
1040
- }
1041
-
1042
- /**
1043
- The `id` of the record that this reference refers to. Together, the
1044
- `type()` and `id()` methods form a composite key for the identity
1045
- map. This can be used to access the id of an async relationship
1046
- without triggering a fetch that would normally happen if you
1047
- attempted to use `record.relationship.id`.
1048
- Example
1049
- ```javascript
1050
- // models/blog.js
1051
- import { Model, belongsTo } from '@warp-drive/legacy/model';
1052
- export default class BlogModel extends Model {
1053
- @belongsTo('user', { async: true, inverse: null }) user;
1054
- }
1055
- let blog = store.push({
1056
- data: {
1057
- type: 'blog',
1058
- id: 1,
1059
- relationships: {
1060
- user: {
1061
- data: { type: 'user', id: 1 }
1062
- }
1063
- }
1064
- }
1065
- });
1066
- let userRef = blog.belongsTo('user');
1067
- // get the identifier of the reference
1068
- if (userRef.remoteType() === "id") {
1069
- let id = userRef.id();
1070
- }
1071
- ```
1072
- @public
1073
- @return The id of the record in this belongsTo relationship.
1074
- */
1075
- static {
1076
- decorateMethodV2(this.prototype, "identifier", [memoized]);
1077
- }
1078
- id() {
1079
- return this.identifier?.id || null;
1080
- }
1081
-
1082
- /**
1083
- The link WarpDrive will use to fetch or reload this belongs-to
1084
- relationship. By default it uses only the "related" resource linkage.
1085
- Example
1086
- ```javascript
1087
- // models/blog.js
1088
- import { Model, belongsTo } from '@warp-drive/legacy/model';
1089
- export default Model.extend({
1090
- user: belongsTo('user', { async: true, inverse: null })
1091
- });
1092
- let blog = store.push({
1093
- data: {
1094
- type: 'blog',
1095
- id: 1,
1096
- relationships: {
1097
- user: {
1098
- links: {
1099
- related: '/articles/1/author'
1100
- }
1101
- }
1102
- }
1103
- }
1104
- });
1105
- let userRef = blog.belongsTo('user');
1106
- // get the identifier of the reference
1107
- if (userRef.remoteType() === "link") {
1108
- let link = userRef.link();
1109
- }
1110
- ```
1111
- @public
1112
- @return The link WarpDrive will use to fetch or reload this belongs-to relationship.
1113
- */
1114
- link() {
1115
- const resource = this._resource();
1116
- if (isResourceIdentiferWithRelatedLinks(resource)) {
1117
- if (resource.links) {
1118
- const related = resource.links.related;
1119
- return !related || typeof related === 'string' ? related : related.href;
1120
- }
1121
- }
1122
- return null;
1123
- }
1124
-
1125
- /**
1126
- * any links that have been received for this relationship
1127
- *
1128
- * @public
1129
- * @return
1130
- */
1131
- links() {
1132
- const resource = this._resource();
1133
- return resource && resource.links ? resource.links : null;
1134
- }
1135
-
1136
- /**
1137
- The meta data for the belongs-to relationship.
1138
- Example
1139
- ```javascript
1140
- // models/blog.js
1141
- import { Model, belongsTo } from '@warp-drive/legacy/model';
1142
- export default Model.extend({
1143
- user: belongsTo('user', { async: true, inverse: null })
1144
- });
1145
- let blog = store.push({
1146
- data: {
1147
- type: 'blog',
1148
- id: 1,
1149
- relationships: {
1150
- user: {
1151
- links: {
1152
- related: {
1153
- href: '/articles/1/author'
1154
- },
1155
- },
1156
- meta: {
1157
- lastUpdated: 1458014400000
1158
- }
1159
- }
1160
- }
1161
- }
1162
- });
1163
- let userRef = blog.belongsTo('user');
1164
- userRef.meta() // { lastUpdated: 1458014400000 }
1165
- ```
1166
- @public
1167
- @return The meta information for the belongs-to relationship.
1168
- */
1169
- meta() {
1170
- let meta = null;
1171
- const resource = this._resource();
1172
- if (resource && resource.meta && typeof resource.meta === 'object') {
1173
- meta = resource.meta;
1174
- }
1175
- return meta;
1176
- }
1177
- _resource() {
1178
- // eslint-disable-next-line @typescript-eslint/no-unused-expressions
1179
- this._ref; // subscribe
1180
- const cache = this.store.cache;
1181
- return cache.getRelationship(this.___identifier, this.key);
1182
- }
1183
-
1184
- /**
1185
- This returns a string that represents how the reference will be
1186
- looked up when it is loaded. If the relationship has a link it will
1187
- use the "link" otherwise it defaults to "id".
1188
- Example
1189
- ```js [app/models/post.js]
1190
- import Model, { hasMany } from '@warp-drive/legacy/model';
1191
- export default class PostModel extends Model {
1192
- @hasMany('comment', { async: true, inverse: null }) comments;
1193
- }
1194
- ```
1195
- ```javascript
1196
- let post = store.push({
1197
- data: {
1198
- type: 'post',
1199
- id: 1,
1200
- relationships: {
1201
- comments: {
1202
- data: [{ type: 'comment', id: 1 }]
1203
- }
1204
- }
1205
- }
1206
- });
1207
- let commentsRef = post.hasMany('comments');
1208
- // get the identifier of the reference
1209
- if (commentsRef.remoteType() === "ids") {
1210
- let ids = commentsRef.ids();
1211
- } else if (commentsRef.remoteType() === "link") {
1212
- let link = commentsRef.link();
1213
- }
1214
- ```
1215
- @public
1216
- @return The name of the remote type. This should either be `link` or `id`
1217
- */
1218
- remoteType() {
1219
- const value = this._resource();
1220
- if (isResourceIdentiferWithRelatedLinks(value)) {
1221
- return 'link';
1222
- }
1223
- return 'id';
1224
- }
1225
-
1226
- /**
1227
- `push` can be used to update the data in the relationship and WarpDrive
1228
- will treat the new data as the canonical value of this relationship on
1229
- the backend. A value of `null` (e.g. `{ data: null }`) can be passed to
1230
- clear the relationship.
1231
- Example model
1232
- ```js [app/models/blog.js]
1233
- import { Model, belongsTo } from '@warp-drive/legacy/model';
1234
- export default class BlogModel extends Model {
1235
- @belongsTo('user', { async: true, inverse: null }) user;
1236
- }
1237
- ```
1238
- Setup some initial state, note we haven't loaded the user yet:
1239
- ```js
1240
- const blog = store.push({
1241
- data: {
1242
- type: 'blog',
1243
- id: '1',
1244
- relationships: {
1245
- user: {
1246
- data: { type: 'user', id: '1' }
1247
- }
1248
- }
1249
- }
1250
- });
1251
- const userRef = blog.belongsTo('user');
1252
- userRef.id(); // '1'
1253
- ```
1254
- Update the state using `push`, note we can do this even without
1255
- having loaded the user yet by providing a resource-identifier.
1256
- Both full a resource and a resource-identifier are supported.
1257
- ```js
1258
- await userRef.push({
1259
- data: {
1260
- type: 'user',
1261
- id: '2',
1262
- }
1263
- });
1264
- userRef.id(); // '2'
1265
- ```
1266
- You may also pass in links and meta fore the relationship, and sideload
1267
- additional resources that might be required.
1268
- ```js
1269
- await userRef.push({
1270
- data: {
1271
- type: 'user',
1272
- id: '2',
1273
- },
1274
- links: {
1275
- related: '/articles/1/author'
1276
- },
1277
- meta: {
1278
- lastUpdated: Date.now()
1279
- },
1280
- included: [
1281
- {
1282
- type: 'user-preview',
1283
- id: '2',
1284
- attributes: {
1285
- username: '@runspired'
1286
- }
1287
- }
1288
- ]
1289
- });
1290
- ```
1291
- By default, the store will attempt to fetch the record if it is not loaded or its
1292
- resource data is not included in the call to `push` before resolving the returned
1293
- promise with the new state..
1294
- Alternatively, pass `true` as the second argument to avoid fetching unloaded records
1295
- and instead the promise will resolve with void without attempting to fetch. This is
1296
- particularly useful if you want to update the state of the relationship without
1297
- forcing the load of all of the associated record.
1298
- @public
1299
- @param doc a JSONAPI document object describing the new value of this relationship.
1300
- @param skipFetch [optional] if `true`, do not attempt to fetch unloaded records
1301
- @return a promise that resolves with the record in this belongs-to relationship after the push has completed.
1302
- */
1303
- async push(doc, skipFetch) {
1304
- const {
1305
- store
1306
- } = this;
1307
- const isResourceData = doc.data && isMaybeResource(doc.data);
1308
- const added = isResourceData ? store._push(doc, true) : doc.data ? store.cacheKeyManager.getOrCreateRecordIdentifier(doc.data) : null;
1309
- const {
1310
- identifier
1311
- } = this.belongsToRelationship;
1312
- {
1313
- if (added) {
1314
- assertPolymorphicType(identifier, this.belongsToRelationship.definition, added, store);
1315
- }
1316
- }
1317
- const newData = {};
1318
-
1319
- // only set data if it was passed in
1320
- if (doc.data || doc.data === null) {
1321
- newData.data = added;
1322
- }
1323
- if ('links' in doc) {
1324
- newData.links = doc.links;
1325
- }
1326
- if ('meta' in doc) {
1327
- newData.meta = doc.meta;
1328
- }
1329
- assertPrivateStore(store);
1330
- store._join(() => {
1331
- this.graph.push({
1332
- op: 'updateRelationship',
1333
- record: identifier,
1334
- field: this.key,
1335
- value: newData
1336
- });
1337
- });
1338
- if (!skipFetch) return this.load();
1339
- }
1340
-
1341
- /**
1342
- `value()` synchronously returns the current value of the belongs-to
1343
- relationship. Unlike `record.relationshipName`, calling
1344
- `value()` on a reference does not trigger a fetch if the async
1345
- relationship is not yet loaded. If the relationship is not loaded
1346
- it will always return `null`.
1347
- Example
1348
- ```javascript
1349
- // models/blog.js
1350
- import { Model, belongsTo } from '@warp-drive/legacy/model';
1351
- export default class BlogModel extends Model {
1352
- @belongsTo('user', { async: true, inverse: null }) user;
1353
- }
1354
- let blog = store.push({
1355
- data: {
1356
- type: 'blog',
1357
- id: 1,
1358
- relationships: {
1359
- user: {
1360
- data: { type: 'user', id: 1 }
1361
- }
1362
- }
1363
- }
1364
- });
1365
- let userRef = blog.belongsTo('user');
1366
- userRef.value(); // null
1367
- // provide data for reference
1368
- userRef.push({
1369
- data: {
1370
- type: 'user',
1371
- id: 1,
1372
- attributes: {
1373
- username: "@user"
1374
- }
1375
- }
1376
- }).then(function(user) {
1377
- userRef.value(); // user
1378
- });
1379
- ```
1380
- @public
1381
- @return the record in this relationship
1382
- */
1383
- value() {
1384
- const resource = this._resource();
1385
- return resource && resource.data ? this.store.peekRecord(resource.data) : null;
1386
- }
1387
-
1388
- /**
1389
- Loads a record in a belongs-to relationship if it is not already
1390
- loaded. If the relationship is already loaded this method does not
1391
- trigger a new load.
1392
- Example
1393
- ```javascript
1394
- // models/blog.js
1395
- import { Model, belongsTo } from '@warp-drive/legacy/model';
1396
- export default class BlogModel extends Model {
1397
- @belongsTo('user', { async: true, inverse: null }) user;
1398
- }
1399
- let blog = store.push({
1400
- data: {
1401
- type: 'blog',
1402
- id: 1,
1403
- relationships: {
1404
- user: {
1405
- data: { type: 'user', id: 1 }
1406
- }
1407
- }
1408
- }
1409
- });
1410
- let userRef = blog.belongsTo('user');
1411
- userRef.value(); // null
1412
- userRef.load().then(function(user) {
1413
- userRef.value() === user
1414
- });
1415
- ```
1416
- You may also pass in an options object whose properties will be
1417
- fed forward. This enables you to pass `adapterOptions` into the
1418
- request given to the adapter via the reference.
1419
- Example
1420
- ```javascript
1421
- userRef.load({ adapterOptions: { isPrivate: true } }).then(function(user) {
1422
- userRef.value() === user;
1423
- });
1424
- ```
1425
- ```js [app/adapters/user.js]
1426
- import Adapter from '@warp-drive/legacy/adapter';
1427
- export default class UserAdapter extends Adapter {
1428
- findRecord(store, type, id, snapshot) {
1429
- // In the adapter you will have access to adapterOptions.
1430
- let adapterOptions = snapshot.adapterOptions;
1431
- }
1432
- });
1433
- ```
1434
- @public
1435
- @param options the options to pass in.
1436
- @return a promise that resolves with the record in this belongs-to relationship.
1437
- */
1438
- async load(options) {
1439
- const support = LEGACY_SUPPORT.get(this.___identifier);
1440
- const fetchSyncRel = !this.belongsToRelationship.definition.isAsync && !areAllInverseRecordsLoaded(this.store, this._resource());
1441
- return fetchSyncRel ? support.reloadBelongsTo(this.key, options).then(() => this.value()) :
1442
- // we cast to fix the return type since typescript and eslint don't understand async functions
1443
- // properly
1444
- support.getBelongsTo(this.key, options);
1445
- }
1446
-
1447
- /**
1448
- Triggers a reload of the value in this relationship. If the
1449
- remoteType is `"link"` WarpDrive will use the relationship link to
1450
- reload the relationship. Otherwise it will reload the record by its
1451
- id.
1452
- Example
1453
- ```javascript
1454
- // models/blog.js
1455
- import { Model, belongsTo } from '@warp-drive/legacy/model';
1456
- export default class BlogModel extends Model {
1457
- @belongsTo('user', { async: true, inverse: null }) user;
1458
- }
1459
- let blog = store.push({
1460
- data: {
1461
- type: 'blog',
1462
- id: 1,
1463
- relationships: {
1464
- user: {
1465
- data: { type: 'user', id: 1 }
1466
- }
1467
- }
1468
- }
1469
- });
1470
- let userRef = blog.belongsTo('user');
1471
- userRef.reload().then(function(user) {
1472
- userRef.value() === user
1473
- });
1474
- ```
1475
- You may also pass in an options object whose properties will be
1476
- fed forward. This enables you to pass `adapterOptions` into the
1477
- request given to the adapter via the reference. A full example
1478
- can be found in the `load` method.
1479
- Example
1480
- ```javascript
1481
- userRef.reload({ adapterOptions: { isPrivate: true } })
1482
- ```
1483
- @public
1484
- @param options the options to pass in.
1485
- @return a promise that resolves with the record in this belongs-to relationship after the reload has completed.
1486
- */
1487
- reload(options) {
1488
- const support = LEGACY_SUPPORT.get(this.___identifier);
1489
- return support.reloadBelongsTo(this.key, options).then(() => this.value());
1490
- }
1491
- }
1492
- defineNonEnumerableSignal(BelongsToReference.prototype, '_ref', 0);
1493
- const LEGACY_SUPPORT = getOrSetGlobal('LEGACY_SUPPORT', new Map());
1494
- function lookupLegacySupport(record) {
1495
- const identifier = recordIdentifierFor(record);
1496
- (test => {
1497
- if (!test) {
1498
- throw new Error(`Expected a record`);
1499
- }
1500
- })(identifier);
1501
- let support = LEGACY_SUPPORT.get(identifier);
1502
- if (!support) {
1503
- (test => {
1504
- if (!test) {
1505
- throw new Error(`Memory Leak Detected`);
1506
- }
1507
- })(!record.isDestroyed && !record.isDestroying);
1508
- support = new LegacySupport(record, identifier);
1509
- LEGACY_SUPPORT.set(identifier, support);
1510
- }
1511
- return support;
1512
- }
1513
-
1514
- /**
1515
- * @hideconstructor
1516
- */
1517
- class LegacySupport {
1518
- /** @internal */
1519
-
1520
- /** @internal */
1521
-
1522
- /** @internal */
1523
-
1524
- /** @internal */
1525
-
1526
- /** @internal */
1527
-
1528
- /** @internal */
1529
-
1530
- /** @internal */
1531
-
1532
- /** @internal */
1533
-
1534
- /** @internal */
1535
-
1536
- /** @internal */
1537
-
1538
- /** @internal */
1539
-
1540
- constructor(record, identifier) {
1541
- this.record = record;
1542
- this.store = isPrivateStore(storeFor(record, false));
1543
- this.identifier = identifier;
1544
- this.cache = this.store.cache;
1545
- if (this.store._graph) {
1546
- this.graph = this.store._graph;
1547
- }
1548
- this._manyArrayCache = Object.create(null);
1549
- this._relationshipPromisesCache = Object.create(null);
1550
- this._relationshipProxyCache = Object.create(null);
1551
- this._pending = Object.create(null);
1552
- this.references = Object.create(null);
1553
- }
1554
-
1555
- /** @private */
1556
- _syncArray(array) {
1557
- // It’s possible the parent side of the relationship may have been destroyed by this point
1558
- if (this.isDestroyed || this.isDestroying) {
1559
- return;
1560
- }
1561
- const currentState = array[Context].source;
1562
- const identifier = this.identifier;
1563
- const [identifiers, jsonApi] = this._getCurrentState(identifier, array.key);
1564
- if (jsonApi.meta) {
1565
- array.meta = jsonApi.meta;
1566
- }
1567
- if (jsonApi.links) {
1568
- array.links = jsonApi.links;
1569
- }
1570
- currentState.length = 0;
1571
- fastPush(currentState, identifiers);
1572
- }
1573
- mutate(mutation) {
1574
- this.cache.mutate(mutation);
1575
- }
1576
- _findBelongsTo(key, resource, relationship, options) {
1577
- const name = getRealFieldName(this, key);
1578
-
1579
- // TODO @runspired follow up if parent isNew then we should not be attempting load here
1580
- // TODO @runspired follow up on whether this should be in the relationship requests cache
1581
- return this._findBelongsToByJsonApiResource(resource, this.identifier, relationship, options).then(identifier => handleCompletedRelationshipRequest(this, name, relationship, identifier), e => handleCompletedRelationshipRequest(this, name, relationship, null, e));
1582
- }
1583
- reloadBelongsTo(key, options) {
1584
- const loadingPromise = this._relationshipPromisesCache[key];
1585
- if (loadingPromise) {
1586
- return loadingPromise;
1587
- }
1588
- const name = getRealFieldName(this, key);
1589
- const resource = this.store.cache.getRelationship(this.identifier, name);
1590
- const relationship = this.graph.get(this.identifier, name);
1591
- (test => {
1592
- if (!test) {
1593
- throw new Error(`Expected ${key === name ? name : `${key} (source: ${name})`} to be a belongs-to relationship`);
1594
- }
1595
- })(isBelongsTo(relationship));
1596
- relationship.state.hasFailedLoadAttempt = false;
1597
- relationship.state.shouldForceReload = true;
1598
- const promise = this._findBelongsTo(name, resource, relationship, options);
1599
- if (this._relationshipProxyCache[name]) {
1600
- // @ts-expect-error
1601
- return this._updatePromiseProxyFor('belongsTo', name, {
1602
- promise
1603
- });
1604
- }
1605
- return promise;
1606
- }
1607
- getBelongsTo(key, options) {
1608
- const {
1609
- identifier
1610
- } = this;
1611
- const name = getRealFieldName(this, key);
1612
- const resource = this.store.cache.getRelationship(this.identifier, name);
1613
- const relatedIdentifier = resource && resource.data ? resource.data : null;
1614
- (test => {
1615
- if (!test) {
1616
- throw new Error(`Expected a stable identifier`);
1617
- }
1618
- })(!relatedIdentifier || isResourceKey(relatedIdentifier));
1619
- const store = this.store;
1620
- const relationship = this.graph.get(this.identifier, name);
1621
- (test => {
1622
- if (!test) {
1623
- throw new Error(`Expected ${key === name ? name : `${key} (source: ${name})`} to be a belongs-to relationship`);
1624
- }
1625
- })(isBelongsTo(relationship));
1626
- const isAsync = relationship.definition.isAsync;
1627
- const _belongsToState = {
1628
- key,
1629
- store,
1630
- legacySupport: this,
1631
- modelName: relationship.definition.type
1632
- };
1633
- if (isAsync) {
1634
- if (relationship.state.hasFailedLoadAttempt) {
1635
- return this._relationshipProxyCache[name];
1636
- }
1637
- const promise = this._findBelongsTo(name, resource, relationship, options);
1638
- const isLoaded = relatedIdentifier && store._instanceCache.recordIsLoaded(relatedIdentifier);
1639
- return this._updatePromiseProxyFor('belongsTo', name, {
1640
- promise,
1641
- content: isLoaded ? store._instanceCache.getRecord(relatedIdentifier) : null,
1642
- _belongsToState
1643
- });
1644
- } else {
1645
- if (relatedIdentifier === null) {
1646
- return null;
1647
- } else {
1648
- (test => {
1649
- if (!test) {
1650
- throw new Error(`You looked up the '${key === name ? name : `${key} (source: ${name})`}' relationship on a '${identifier.type}' with id ${identifier.id || 'null'} but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async (\`belongsTo(<type>, { async: true, inverse: <inverse> })\`)`);
1651
- }
1652
- })(store._instanceCache.recordIsLoaded(relatedIdentifier, true));
1653
- return store._instanceCache.getRecord(relatedIdentifier);
1654
- }
1655
- }
1656
- }
1657
- setDirtyBelongsTo(key, value) {
1658
- const name = getRealFieldName(this, key);
1659
- return this.cache.mutate({
1660
- op: 'replaceRelatedRecord',
1661
- record: this.identifier,
1662
- field: name,
1663
- value: extractIdentifierFromRecord(value)
1664
- },
1665
- // @ts-expect-error
1666
- true);
1667
- }
1668
- _getCurrentState(identifier, field) {
1669
- const jsonApi = this.cache.getRelationship(identifier, field);
1670
- const cache = this.store._instanceCache;
1671
- const identifiers = [];
1672
- if (jsonApi.data) {
1673
- for (let i = 0; i < jsonApi.data.length; i++) {
1674
- const relatedIdentifier = jsonApi.data[i];
1675
- (test => {
1676
- if (!test) {
1677
- throw new Error(`Expected a stable identifier`);
1678
- }
1679
- })(isResourceKey(relatedIdentifier));
1680
- if (cache.recordIsLoaded(relatedIdentifier, true)) {
1681
- identifiers.push(relatedIdentifier);
1682
- }
1683
- }
1684
- }
1685
- return [identifiers, jsonApi];
1686
- }
1687
- getManyArray(key, definition) {
1688
- const name = getRealFieldName(this, key);
1689
- if (this.graph) {
1690
- let manyArray = this._manyArrayCache[name];
1691
- if (!definition) {
1692
- definition = this.graph.get(this.identifier, name).definition;
1693
- }
1694
- if (!manyArray) {
1695
- const [identifiers, doc] = this._getCurrentState(this.identifier, name);
1696
- const field = getField(this, name);
1697
- manyArray = createLegacyManyArray({
1698
- store: this.store,
1699
- // @ts-expect-error Typescript doesn't have a way for us to thread the generic backwards so it infers unknown instead of T
1700
- manager: this,
1701
- source: identifiers,
1702
- type: definition.type,
1703
- isLoaded: !definition.isAsync,
1704
- editable: true,
1705
- isAsync: definition.isAsync,
1706
- isPolymorphic: definition.isPolymorphic,
1707
- field: field,
1708
- identifier: this.identifier,
1709
- links: doc.links || null,
1710
- meta: doc.meta || null
1711
- });
1712
- this._manyArrayCache[name] = manyArray;
1713
- }
1714
- return manyArray;
1715
- }
1716
- (test => {
1717
- {
1718
- throw new Error('hasMany only works with the JSONAPICache');
1719
- }
1720
- })();
1721
- }
1722
- fetchAsyncHasMany(key, relationship, manyArray, options) {
1723
- const name = getRealFieldName(this, key);
1724
- if (this.graph) {
1725
- let loadingPromise = this._relationshipPromisesCache[name];
1726
- if (loadingPromise) {
1727
- return loadingPromise;
1728
- }
1729
- const jsonApi = this.cache.getRelationship(this.identifier, name);
1730
- const promise = this._findHasManyByJsonApiResource(jsonApi, this.identifier, relationship, options);
1731
- if (!promise) {
1732
- manyArray.isLoaded = true;
1733
- return Promise.resolve(manyArray);
1734
- }
1735
- loadingPromise = promise.then(() => handleCompletedRelationshipRequest(this, name, relationship, manyArray), e => handleCompletedRelationshipRequest(this, name, relationship, manyArray, e));
1736
- this._relationshipPromisesCache[name] = loadingPromise;
1737
- return loadingPromise;
1738
- }
1739
- (test => {
1740
- {
1741
- throw new Error('hasMany only works with the JSONAPICache');
1742
- }
1743
- })();
1744
- }
1745
- reloadHasMany(key, options) {
1746
- const name = getRealFieldName(this, key);
1747
- if (this.graph) {
1748
- const loadingPromise = this._relationshipPromisesCache[name];
1749
- if (loadingPromise) {
1750
- return loadingPromise;
1751
- }
1752
- const relationship = this.graph.get(this.identifier, name);
1753
- const {
1754
- definition,
1755
- state
1756
- } = relationship;
1757
- state.hasFailedLoadAttempt = false;
1758
- state.shouldForceReload = true;
1759
- const manyArray = this.getManyArray(name, definition);
1760
- const promise = this.fetchAsyncHasMany(name, relationship, manyArray, options);
1761
- if (this._relationshipProxyCache[name]) {
1762
- return this._updatePromiseProxyFor('hasMany', name, {
1763
- promise
1764
- });
1765
- }
1766
- return promise;
1767
- }
1768
- (test => {
1769
- {
1770
- throw new Error(`hasMany only works with the JSONAPICache`);
1771
- }
1772
- })();
1773
- }
1774
- getHasMany(key, options) {
1775
- const name = getRealFieldName(this, key);
1776
- if (this.graph) {
1777
- const relationship = this.graph.get(this.identifier, name);
1778
- const {
1779
- definition,
1780
- state
1781
- } = relationship;
1782
- const manyArray = this.getManyArray(name, definition);
1783
- if (definition.isAsync) {
1784
- if (state.hasFailedLoadAttempt) {
1785
- return this._relationshipProxyCache[name];
1786
- }
1787
- const promise = this.fetchAsyncHasMany(name, relationship, manyArray, options);
1788
- return this._updatePromiseProxyFor('hasMany', name, {
1789
- promise,
1790
- content: manyArray
1791
- });
1792
- } else {
1793
- (test => {
1794
- if (!test) {
1795
- throw new Error(`You looked up the '${key === name ? name : `${key} (source: ${name})`}' relationship on a '${this.identifier.type}' with id ${this.identifier.id || 'null'} but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async ('hasMany(<type>, { async: true, inverse: <inverse> })')`);
1796
- }
1797
- })(!anyUnloaded(this.store, relationship));
1798
- return manyArray;
1799
- }
1800
- }
1801
- (test => {
1802
- {
1803
- throw new Error(`hasMany only works with the JSONAPICache`);
1804
- }
1805
- })();
1806
- }
1807
- _updatePromiseProxyFor(kind, key, args) {
1808
- let promiseProxy = this._relationshipProxyCache[key];
1809
- if (kind === 'hasMany') {
1810
- const {
1811
- promise,
1812
- content
1813
- } = args;
1814
- if (promiseProxy) {
1815
- (test => {
1816
- if (!test) {
1817
- throw new Error(`Expected a PromiseManyArray`);
1818
- }
1819
- })('_update' in promiseProxy);
1820
- promiseProxy._update(promise, content);
1821
- } else {
1822
- promiseProxy = this._relationshipProxyCache[key] = new PromiseManyArray(promise, content);
1823
- }
1824
- return promiseProxy;
1825
- }
1826
- if (promiseProxy) {
1827
- const {
1828
- promise,
1829
- content
1830
- } = args;
1831
- (test => {
1832
- if (!test) {
1833
- throw new Error(`Expected a PromiseBelongsTo`);
1834
- }
1835
- })('_belongsToState' in promiseProxy);
1836
- if (content !== undefined) {
1837
- promiseProxy.set('content', content);
1838
- }
1839
- void promiseProxy.set('promise', promise);
1840
- } else {
1841
- promiseProxy = PromiseBelongsTo.create(args);
1842
- this._relationshipProxyCache[key] = promiseProxy;
1843
- }
1844
- return promiseProxy;
1845
- }
1846
- referenceFor(kind, key) {
1847
- const name = getRealFieldName(this, key);
1848
- let reference = this.references[name];
1849
- if (!reference) {
1850
- if (!this.graph) {
1851
- // TODO @runspired while this feels odd, it is not a regression in capability because we do
1852
- // not today support references pulling from RecordDatas other than our own
1853
- // because of the intimate API access involved. This is something we will need to redesign.
1854
- (test => {
1855
- {
1856
- throw new Error(`snapshot.belongsTo only supported when using the JSONAPICache`);
1857
- }
1858
- })();
1859
- }
1860
- const {
1861
- graph,
1862
- identifier
1863
- } = this;
1864
- const relationship = graph.get(identifier, name);
1865
- {
1866
- if (kind) {
1867
- const modelName = identifier.type;
1868
- const actualRelationshipKind = relationship.definition.kind;
1869
- (test => {
1870
- if (!test) {
1871
- throw new Error(`You tried to get the '${key === name ? name : `${key} (source: ${name})`}' relationship on a '${modelName}' via record.${kind}('${key}'), but the relationship is of kind '${actualRelationshipKind}'. Use record.${actualRelationshipKind}('${key}') instead.`);
1872
- }
1873
- })(actualRelationshipKind === kind);
1874
- }
1875
- }
1876
- const relationshipKind = relationship.definition.kind;
1877
- if (relationshipKind === 'belongsTo') {
1878
- reference = new BelongsToReference(this.store, graph, identifier, relationship, key);
1879
- } else if (relationshipKind === 'hasMany') {
1880
- reference = new HasManyReference(this.store, graph, identifier, relationship, key);
1881
- }
1882
- this.references[name] = reference;
1883
- }
1884
- return reference;
1885
- }
1886
- _findHasManyByJsonApiResource(resource, parentIdentifier, relationship, options = {}) {
1887
- if (this.graph) {
1888
- if (!resource) {
1889
- return;
1890
- }
1891
- const {
1892
- definition,
1893
- state
1894
- } = relationship;
1895
- upgradeStore(this.store);
1896
- const adapter = this.store.adapterFor?.(definition.type);
1897
- const {
1898
- isStale,
1899
- hasDematerializedInverse,
1900
- hasReceivedData,
1901
- isEmpty,
1902
- shouldForceReload
1903
- } = state;
1904
- const allInverseRecordsAreLoaded = areAllInverseRecordsLoaded(this.store, resource);
1905
- const identifiers = resource.data;
1906
- const shouldFindViaLink = resource.links && resource.links.related && (typeof adapter?.findHasMany === 'function' || typeof identifiers === 'undefined') && (shouldForceReload || hasDematerializedInverse || isStale || !allInverseRecordsAreLoaded && !isEmpty);
1907
- const field = this.store.schema.fields({
1908
- type: definition.inverseType
1909
- }).get(definition.key);
1910
- (test => {
1911
- if (!test) {
1912
- throw new Error(`Expected a hasMany field definition for ${definition.inverseType}.${definition.key}`);
1913
- }
1914
- })(field && field.kind === 'hasMany');
1915
- const request = {
1916
- useLink: shouldFindViaLink,
1917
- field,
1918
- links: resource.links,
1919
- meta: resource.meta,
1920
- options,
1921
- record: parentIdentifier
1922
- };
1923
-
1924
- // fetch via link
1925
- if (shouldFindViaLink) {
1926
- (test => {
1927
- if (!test) {
1928
- throw new Error(`Expected collection to be an array`);
1929
- }
1930
- })(!identifiers || Array.isArray(identifiers));
1931
- (test => {
1932
- if (!test) {
1933
- throw new Error(`Expected stable identifiers`);
1934
- }
1935
- })(!identifiers || identifiers.every(isResourceKey));
1936
- const req = field.options.linksMode ? {
1937
- url: getRelatedLink(resource),
1938
- op: 'findHasMany',
1939
- method: 'GET',
1940
- records: identifiers || [],
1941
- data: request,
1942
- [EnableHydration]: false
1943
- } : {
1944
- op: 'findHasMany',
1945
- records: identifiers || [],
1946
- data: request,
1947
- cacheOptions: {
1948
- [Symbol.for('wd:skip-cache')]: true
1949
- }
1950
- };
1951
- return this.store.request(req);
1952
- }
1953
- const preferLocalCache = hasReceivedData && !isEmpty;
1954
- const hasLocalPartialData = hasDematerializedInverse || isEmpty && Array.isArray(identifiers) && identifiers.length > 0;
1955
- const attemptLocalCache = !shouldForceReload && !isStale && (preferLocalCache || hasLocalPartialData);
1956
- if (attemptLocalCache && allInverseRecordsAreLoaded) {
1957
- return;
1958
- }
1959
- const hasData = hasReceivedData && !isEmpty;
1960
- if (attemptLocalCache || hasData || hasLocalPartialData) {
1961
- (test => {
1962
- if (!test) {
1963
- throw new Error(`Expected collection to be an array`);
1964
- }
1965
- })(Array.isArray(identifiers));
1966
- (test => {
1967
- if (!test) {
1968
- throw new Error(`Expected stable identifiers`);
1969
- }
1970
- })(identifiers.every(isResourceKey));
1971
- options.reload = options.reload || !attemptLocalCache || undefined;
1972
- return this.store.request({
1973
- op: 'findHasMany',
1974
- records: identifiers,
1975
- data: request,
1976
- cacheOptions: {
1977
- [Symbol.for('wd:skip-cache')]: true
1978
- }
1979
- });
1980
- }
1981
-
1982
- // we were explicitly told we have no data and no links.
1983
- // TODO if the relationshipIsStale, should we hit the adapter anyway?
1984
- return;
1985
- }
1986
- (test => {
1987
- {
1988
- throw new Error(`hasMany only works with the JSONAPICache`);
1989
- }
1990
- })();
1991
- }
1992
- _findBelongsToByJsonApiResource(resource, parentIdentifier, relationship, options = {}) {
1993
- if (!resource) {
1994
- return Promise.resolve(null);
1995
- }
1996
- const key = relationship.definition.key;
1997
- const name = getRealFieldName(this, key);
1998
-
1999
- // interleaved promises mean that we MUST cache this here
2000
- // in order to prevent infinite re-render if the request
2001
- // fails.
2002
- if (this._pending[name]) {
2003
- return this._pending[name];
2004
- }
2005
- const identifier = resource.data ? resource.data : null;
2006
- (test => {
2007
- if (!test) {
2008
- throw new Error(`Expected a stable identifier`);
2009
- }
2010
- })(!identifier || isResourceKey(identifier));
2011
- const {
2012
- isStale,
2013
- hasDematerializedInverse,
2014
- hasReceivedData,
2015
- isEmpty,
2016
- shouldForceReload
2017
- } = relationship.state;
2018
- const allInverseRecordsAreLoaded = areAllInverseRecordsLoaded(this.store, resource);
2019
- const shouldFindViaLink = resource.links?.related && (shouldForceReload || hasDematerializedInverse || isStale || !allInverseRecordsAreLoaded && !isEmpty);
2020
- const field = this.store.schema.fields(this.identifier).get(relationship.definition.key);
2021
- (test => {
2022
- if (!test) {
2023
- throw new Error(`Attempted to access a belongsTo relationship but no definition exists for it`);
2024
- }
2025
- })(field && field.kind === 'belongsTo');
2026
- const request = {
2027
- useLink: shouldFindViaLink,
2028
- field,
2029
- links: resource.links,
2030
- meta: resource.meta,
2031
- options,
2032
- record: parentIdentifier
2033
- };
2034
-
2035
- // fetch via link
2036
- if (shouldFindViaLink) {
2037
- const req = field.options.linksMode ? {
2038
- url: getRelatedLink(resource),
2039
- op: 'findBelongsTo',
2040
- method: 'GET',
2041
- records: identifier ? [identifier] : [],
2042
- data: request,
2043
- [EnableHydration]: false
2044
- } : {
2045
- op: 'findBelongsTo',
2046
- records: identifier ? [identifier] : [],
2047
- data: request,
2048
- cacheOptions: {
2049
- [Symbol.for('wd:skip-cache')]: true
2050
- }
2051
- };
2052
- const future = this.store.request(req);
2053
- this._pending[name] = future.then(doc => field.options.linksMode ? doc.content.data : doc.content).finally(() => {
2054
- this._pending[name] = undefined;
2055
- });
2056
- return this._pending[name];
2057
- }
2058
- const preferLocalCache = hasReceivedData && allInverseRecordsAreLoaded && !isEmpty;
2059
- const hasLocalPartialData = hasDematerializedInverse || isEmpty && resource.data;
2060
- // null is explicit empty, undefined is "we don't know anything"
2061
- const localDataIsEmpty = !identifier;
2062
- const attemptLocalCache = !shouldForceReload && !isStale && (preferLocalCache || hasLocalPartialData);
2063
-
2064
- // we dont need to fetch and are empty
2065
- if (attemptLocalCache && localDataIsEmpty) {
2066
- return Promise.resolve(null);
2067
- }
2068
-
2069
- // we dont need to fetch because we are local state
2070
- const resourceIsLocal = identifier?.id === null;
2071
- if (attemptLocalCache && allInverseRecordsAreLoaded || resourceIsLocal) {
2072
- return Promise.resolve(identifier);
2073
- }
2074
-
2075
- // we may need to fetch
2076
- if (identifier) {
2077
- (test => {
2078
- if (!test) {
2079
- throw new Error(`Cannot fetch belongs-to relationship with no information`);
2080
- }
2081
- })(identifier);
2082
- options.reload = options.reload || !attemptLocalCache || undefined;
2083
- this._pending[name] = this.store.request({
2084
- op: 'findBelongsTo',
2085
- records: [identifier],
2086
- data: request,
2087
- cacheOptions: {
2088
- [Symbol.for('wd:skip-cache')]: true
2089
- }
2090
- }).then(doc => doc.content).finally(() => {
2091
- this._pending[name] = undefined;
2092
- });
2093
- return this._pending[name];
2094
- }
2095
-
2096
- // we were explicitly told we have no data and no links.
2097
- // TODO if the relationshipIsStale, should we hit the adapter anyway?
2098
- return Promise.resolve(null);
2099
- }
2100
- destroy() {
2101
- this.isDestroying = true;
2102
- let cache = this._manyArrayCache;
2103
- this._manyArrayCache = Object.create(null);
2104
- Object.keys(cache).forEach(key => {
2105
- cache[key].destroy();
2106
- });
2107
- cache = this._relationshipProxyCache;
2108
- this._relationshipProxyCache = Object.create(null);
2109
- Object.keys(cache).forEach(key => {
2110
- const proxy = cache[key];
2111
- if (proxy.destroy) {
2112
- proxy.destroy();
2113
- }
2114
- });
2115
- cache = this.references;
2116
- this.references = Object.create(null);
2117
- Object.keys(cache).forEach(key => {
2118
- cache[key].destroy();
2119
- });
2120
- this.isDestroyed = true;
2121
- }
2122
- }
2123
- function getRelatedLink(resource) {
2124
- const related = resource.links?.related;
2125
- (test => {
2126
- if (!test) {
2127
- throw new Error(`Expected a related link`);
2128
- }
2129
- })(related);
2130
- return typeof related === 'object' ? related.href : related;
2131
- }
2132
- function handleCompletedRelationshipRequest(recordExt, key, relationship, value, error) {
2133
- delete recordExt._relationshipPromisesCache[key];
2134
- relationship.state.shouldForceReload = false;
2135
- const isHasMany = relationship.definition.kind === 'hasMany';
2136
- if (isHasMany) {
2137
- // we don't notify the record property here to avoid refetch
2138
- // only the many array
2139
- notifyInternalSignal(value[Context].signal);
2140
- }
2141
- if (error) {
2142
- relationship.state.hasFailedLoadAttempt = true;
2143
- const proxy = recordExt._relationshipProxyCache[key];
2144
- // belongsTo relationships are sometimes unloaded
2145
- // when a load fails, in this case we need
2146
- // to make sure that we aren't proxying
2147
- // to destroyed content
2148
- // for the sync belongsTo reload case there will be no proxy
2149
- // for the async reload case there will be no proxy if the ui
2150
- // has never been accessed
2151
- if (proxy && !isHasMany) {
2152
- // @ts-expect-error unsure why this is not resolving the boolean but async belongsTo is weird
2153
- if (proxy.content && proxy.content.isDestroying) {
2154
- proxy.set('content', null);
2155
- }
2156
- recordExt.store.notifications._flush();
2157
- }
2158
- throw error;
2159
- }
2160
- if (isHasMany) {
2161
- value.isLoaded = true;
2162
- } else {
2163
- recordExt.store.notifications._flush();
2164
- }
2165
- relationship.state.hasFailedLoadAttempt = false;
2166
- // only set to not stale if no error is thrown
2167
- relationship.state.isStale = false;
2168
- return isHasMany || !value ? value : recordExt.store.peekRecord(value);
2169
- }
2170
- function extractIdentifierFromRecord(record) {
2171
- if (!record) {
2172
- return null;
2173
- }
2174
- return recordIdentifierFor(record);
2175
- }
2176
- function anyUnloaded(store, relationship) {
2177
- assertPrivateStore(store);
2178
- const graph = store._graph;
2179
- (test => {
2180
- if (!test) {
2181
- throw new Error(`Expected a Graph instance to be available`);
2182
- }
2183
- })(graph);
2184
- const relationshipData = graph.getData(relationship.identifier, relationship.definition.key);
2185
- const state = relationshipData.data;
2186
- const cache = store._instanceCache;
2187
- const unloaded = state?.find(s => {
2188
- const isLoaded = cache.recordIsLoaded(s, true);
2189
- return !isLoaded;
2190
- });
2191
- return unloaded || false;
2192
- }
2193
- function areAllInverseRecordsLoaded(store, resource) {
2194
- assertPrivateStore(store);
2195
- const instanceCache = store._instanceCache;
2196
- const identifiers = resource.data;
2197
- if (Array.isArray(identifiers)) {
2198
- (test => {
2199
- if (!test) {
2200
- throw new Error(`Expected stable identifiers`);
2201
- }
2202
- })(identifiers.every(isResourceKey));
2203
- // treat as collection
2204
- // check for unloaded records
2205
- return identifiers.every(identifier => instanceCache.recordIsLoaded(identifier));
2206
- }
2207
-
2208
- // treat as single resource
2209
- if (!identifiers) return true;
2210
- (test => {
2211
- if (!test) {
2212
- throw new Error(`Expected stable identifiers`);
2213
- }
2214
- })(isResourceKey(identifiers));
2215
- return instanceCache.recordIsLoaded(identifiers);
2216
- }
2217
- function isBelongsTo(relationship) {
2218
- return relationship.definition.kind === 'belongsTo';
2219
- }
2220
- function getField(context, key) {
2221
- const {
2222
- identifier,
2223
- store
2224
- } = context;
2225
- return store.schema.fields(identifier).get(key) ?? store.schema.cacheFields?.(identifier).get(key);
2226
- }
2227
- function getRealFieldName(context, key) {
2228
- const field = getField(context, key);
2229
- return field ? field.sourceKey ?? field.name : key;
2230
- }
2231
-
2232
- // we force the type here to our own construct because mixin and extend patterns
2233
- // lose generic signatures. We also do this because we need to Omit `clear` from
2234
- // the type of ArrayProxy as we override it's signature.
2235
- const ArrayProxyWithCustomOverrides = ArrayProxy;
2236
-
2237
- /**
2238
- Holds validation errors for a given record, organized by attribute names.
2239
-
2240
- This class is not directly instantiable.
2241
-
2242
- Every `Model` has an `errors` property that is an instance of
2243
- `Errors`. This can be used to display validation error
2244
- messages returned from the server when a `record.save()` rejects.
2245
-
2246
- For Example, if you had a `User` model that looked like this:
2247
-
2248
- ```js [app/models/user.js]
2249
- import { Model, attr } from '@warp-drive/legacy/model';
2250
-
2251
- export default class UserModel extends Model {
2252
- @attr('string') username;
2253
- @attr('string') email;
2254
- }
2255
- ```
2256
- And you attempted to save a record that did not validate on the backend:
2257
-
2258
- ```javascript
2259
- let user = store.createRecord('user', {
2260
- username: 'tomster',
2261
- email: 'invalidEmail'
2262
- });
2263
- user.save();
2264
- ```
2265
-
2266
- Your backend would be expected to return an error response that described
2267
- the problem, so that error messages can be generated on the app.
2268
-
2269
- API responses will be translated into instances of `Errors` differently,
2270
- depending on the specific combination of adapter and serializer used. You
2271
- may want to check the documentation or the source code of the libraries
2272
- that you are using, to know how they expect errors to be communicated.
2273
-
2274
- Errors can be displayed to the user by accessing their property name
2275
- to get an array of all the error objects for that property. Each
2276
- error object is a JavaScript object with two keys:
2277
-
2278
- - `message` A string containing the error message from the backend
2279
- - `attribute` The name of the property associated with this error message
2280
-
2281
- ```handlebars
2282
- <label>Username: <Input @value={{@model.username}} /> </label>
2283
- {{#each @model.errors.username as |error|}}
2284
- <div class="error">
2285
- {{error.message}}
2286
- </div>
2287
- {{/each}}
2288
-
2289
- <label>Email: <Input @value={{@model.email}} /> </label>
2290
- {{#each @model.errors.email as |error|}}
2291
- <div class="error">
2292
- {{error.message}}
2293
- </div>
2294
- {{/each}}
2295
- ```
2296
-
2297
- You can also access the special `messages` property on the error
2298
- object to get an array of all the error strings.
2299
-
2300
- ```handlebars
2301
- {{#each @model.errors.messages as |message|}}
2302
- <div class="error">
2303
- {{message}}
2304
- </div>
2305
- {{/each}}
2306
- ```
2307
-
2308
- @class Errors
2309
- @public
2310
- */
2311
- class Errors extends ArrayProxyWithCustomOverrides {
2312
- /**
2313
- @property errorsByAttributeName
2314
- @type {MapWithDefault}
2315
- @private
2316
- */
2317
- get errorsByAttributeName() {
2318
- return new Map();
2319
- }
2320
-
2321
- /**
2322
- Returns errors for a given attribute
2323
- ```javascript
2324
- let user = store.createRecord('user', {
2325
- username: 'tomster',
2326
- email: 'invalidEmail'
2327
- });
2328
- user.save().catch(function(){
2329
- user.errors.errorsFor('email'); // returns:
2330
- // [{attribute: "email", message: "Doesn't look like a valid email."}]
2331
- });
2332
- ```
2333
- @public
2334
- @param {String} attribute
2335
- @return {Array}
2336
- */
2337
- static {
2338
- decorateMethodV2(this.prototype, "errorsByAttributeName", [computed()]);
2339
- }
2340
- errorsFor(attribute) {
2341
- const map = this.errorsByAttributeName;
2342
- let errors = map.get(attribute);
2343
- if (errors === undefined) {
2344
- errors = A();
2345
- map.set(attribute, errors);
2346
- }
2347
-
2348
- // Errors may be a native array with extensions turned on. Since we access
2349
- // the array via a method, and not a computed or using `Ember.get`, it does
2350
- // not entangle properly with autotracking, so we entangle manually by
2351
- // getting the `[]` property.
2352
- get(errors, '[]');
2353
- return errors;
2354
- }
2355
-
2356
- /**
2357
- An array containing all of the error messages for this
2358
- record. This is useful for displaying all errors to the user.
2359
- ```handlebars
2360
- {{#each @model.errors.messages as |message|}}
2361
- <div class="error">
2362
- {{message}}
2363
- </div>
2364
- {{/each}}
2365
- ```
2366
- @property messages
2367
- @public
2368
- @type {Array}
2369
- */
2370
- static {
2371
- decorateFieldV2(this.prototype, "messages", [mapBy('content', 'message')]);
2372
- }
2373
- #messages = (initializeDeferredDecorator(this, "messages"), void 0);
2374
- /**
2375
- @property content
2376
- @type {Array}
2377
- @private
2378
- */
2379
- get content() {
2380
- return A();
2381
- }
2382
-
2383
- /**
2384
- @private
2385
- */
2386
- static {
2387
- decorateMethodV2(this.prototype, "content", [computed()]);
2388
- }
2389
- unknownProperty(attribute) {
2390
- const errors = this.errorsFor(attribute);
2391
- if (errors.length === 0) {
2392
- return undefined;
2393
- }
2394
- return errors;
2395
- }
2396
-
2397
- /**
2398
- Total number of errors.
2399
- @property length
2400
- @type {Number}
2401
- @public
2402
- @readonly
2403
- */
2404
-
2405
- /**
2406
- `true` if we have no errors.
2407
- @property isEmpty
2408
- @type {Boolean}
2409
- @public
2410
- @readonly
2411
- */
2412
- static {
2413
- decorateFieldV2(this.prototype, "isEmpty", [not('length')]);
2414
- }
2415
- #isEmpty = (initializeDeferredDecorator(this, "isEmpty"), void 0);
2416
- /**
2417
- Manually adds errors to the record. This will trigger the `becameInvalid` event/ lifecycle method on
2418
- the record and transition the record into an `invalid` state.
2419
- Example
2420
- ```javascript
2421
- let errors = user.errors;
2422
- // add multiple errors
2423
- errors.add('password', [
2424
- 'Must be at least 12 characters',
2425
- 'Must contain at least one symbol',
2426
- 'Cannot contain your name'
2427
- ]);
2428
- errors.errorsFor('password');
2429
- // =>
2430
- // [
2431
- // { attribute: 'password', message: 'Must be at least 12 characters' },
2432
- // { attribute: 'password', message: 'Must contain at least one symbol' },
2433
- // { attribute: 'password', message: 'Cannot contain your name' },
2434
- // ]
2435
- // add a single error
2436
- errors.add('username', 'This field is required');
2437
- errors.errorsFor('username');
2438
- // =>
2439
- // [
2440
- // { attribute: 'username', message: 'This field is required' },
2441
- // ]
2442
- ```
2443
- @public
2444
- @param {String} attribute - the property name of an attribute or relationship
2445
- @param {string[]|string} messages - an error message or array of error messages for the attribute
2446
- */
2447
- add(attribute, messages) {
2448
- const errors = this._findOrCreateMessages(attribute, messages);
2449
- this.addObjects(errors);
2450
- this.errorsFor(attribute).addObjects(errors);
2451
- this.__record.currentState.notify('isValid');
2452
- this.notifyPropertyChange(attribute);
2453
- }
2454
-
2455
- /**
2456
- @private
2457
- */
2458
- _findOrCreateMessages(attribute, messages) {
2459
- const errors = this.errorsFor(attribute);
2460
- const messagesArray = Array.isArray(messages) ? messages : [messages];
2461
- const _messages = new Array(messagesArray.length);
2462
- for (let i = 0; i < messagesArray.length; i++) {
2463
- const message = messagesArray[i];
2464
- const err = errors.findBy('message', message);
2465
- if (err) {
2466
- _messages[i] = err;
2467
- } else {
2468
- _messages[i] = {
2469
- attribute: attribute,
2470
- message
2471
- };
2472
- }
2473
- }
2474
- return _messages;
2475
- }
2476
-
2477
- /**
2478
- Manually removes all errors for a given member from the record.
2479
- This will transition the record into a `valid` state, and
2480
- triggers the `becameValid` event and lifecycle method.
2481
- Example:
2482
- ```javascript
2483
- let errors = user.errors;
2484
- errors.add('phone', ['error-1', 'error-2']);
2485
- errors.errorsFor('phone');
2486
- // =>
2487
- // [
2488
- // { attribute: 'phone', message: 'error-1' },
2489
- // { attribute: 'phone', message: 'error-2' },
2490
- // ]
2491
- errors.remove('phone');
2492
- errors.errorsFor('phone');
2493
- // => undefined
2494
- ```
2495
- @public
2496
- @param {String} member - the property name of an attribute or relationship
2497
- */
2498
- remove(attribute) {
2499
- if (this.isEmpty) {
2500
- return;
2501
- }
2502
- const content = this.rejectBy('attribute', attribute);
2503
- this.content.setObjects(content);
2504
-
2505
- // Although errorsByAttributeName.delete is technically enough to sync errors state, we also
2506
- // must mutate the array as well for autotracking
2507
- const errors = this.errorsFor(attribute);
2508
- for (let i = 0; i < errors.length; i++) {
2509
- if (errors[i].attribute === attribute) {
2510
- // .replace from Ember.NativeArray is necessary. JS splice will not work.
2511
- errors.replace(i, 1);
2512
- }
2513
- }
2514
- this.errorsByAttributeName.delete(attribute);
2515
- this.__record.currentState.notify('isValid');
2516
- this.notifyPropertyChange(attribute);
2517
- this.notifyPropertyChange('length');
2518
- }
2519
-
2520
- /**
2521
- Manually clears all errors for the record.
2522
- This will transition the record into a `valid` state, and
2523
- will trigger the `becameValid` event and lifecycle method.
2524
- Example:
2525
- ```javascript
2526
- let errors = user.errors;
2527
- errors.add('username', ['error-a']);
2528
- errors.add('phone', ['error-1', 'error-2']);
2529
- errors.errorsFor('username');
2530
- // =>
2531
- // [
2532
- // { attribute: 'username', message: 'error-a' },
2533
- // ]
2534
- errors.errorsFor('phone');
2535
- // =>
2536
- // [
2537
- // { attribute: 'phone', message: 'error-1' },
2538
- // { attribute: 'phone', message: 'error-2' },
2539
- // ]
2540
- errors.clear();
2541
- errors.errorsFor('username');
2542
- // => undefined
2543
- errors.errorsFor('phone');
2544
- // => undefined
2545
- errors.messages
2546
- // => []
2547
- ```
2548
- @public
2549
- */
2550
- clear() {
2551
- if (this.isEmpty) {
2552
- return;
2553
- }
2554
- const errorsByAttributeName = this.errorsByAttributeName;
2555
- const attributes = [];
2556
- errorsByAttributeName.forEach(function (_, attribute) {
2557
- attributes.push(attribute);
2558
- });
2559
- errorsByAttributeName.clear();
2560
- attributes.forEach(attribute => {
2561
- this.notifyPropertyChange(attribute);
2562
- });
2563
- this.__record.currentState.notify('isValid');
2564
- super.clear();
2565
- }
2566
-
2567
- /**
2568
- Checks if there are error messages for the given attribute.
2569
- ```js [app/controllers/user/edit.js]
2570
- export default class UserEditController extends Controller {
2571
- @action
2572
- save(user) {
2573
- if (user.errors.has('email')) {
2574
- return alert('Please update your email before attempting to save.');
2575
- }
2576
- user.save();
2577
- }
2578
- }
2579
- ```
2580
- @public
2581
- @param {String} attribute
2582
- @return {Boolean} true if there some errors on given attribute
2583
- */
2584
- has(attribute) {
2585
- return this.errorsFor(attribute).length > 0;
2586
- }
2587
- }
2588
- export { Errors as E, LEGACY_SUPPORT as L, PromiseBelongsTo as P, PromiseManyArray as a, lookupLegacySupport as l };