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

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