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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (214) hide show
  1. package/README.md +16 -26
  2. package/declarations/adapter/error.d.ts +7 -7
  3. package/declarations/adapter/json-api.d.ts +6 -8
  4. package/declarations/adapter/rest.d.ts +26 -112
  5. package/declarations/adapter.d.ts +6 -8
  6. package/declarations/compat/-private.d.ts +1 -1
  7. package/declarations/compat/builders/find-all.d.ts +6 -6
  8. package/declarations/compat/builders/find-record.d.ts +8 -8
  9. package/declarations/compat/builders/query.d.ts +12 -12
  10. package/declarations/compat/extensions.d.ts +1 -1
  11. package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts +7 -9
  12. package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +20 -30
  13. package/declarations/compat/utils.d.ts +17 -17
  14. package/declarations/compat.d.ts +35 -11
  15. package/declarations/index.d.ts +102 -0
  16. package/declarations/model/-private/attr.d.ts +5 -6
  17. package/declarations/model/-private/belongs-to.d.ts +4 -5
  18. package/declarations/model/-private/has-many.d.ts +4 -5
  19. package/declarations/model/-private/hooks.d.ts +1 -1
  20. package/declarations/model/-private/legacy-relationships-support.d.ts +2 -2
  21. package/declarations/model/-private/model.d.ts +18 -59
  22. package/declarations/model/-private/promise-many-array.d.ts +0 -18
  23. package/declarations/model/-private/record-state.d.ts +1 -1
  24. package/declarations/model/-private/references/belongs-to.d.ts +19 -29
  25. package/declarations/model/-private/references/has-many.d.ts +14 -16
  26. package/declarations/model/migration-support.d.ts +46 -21
  27. package/declarations/model-fragments/extensions/fragment-array.d.ts +16 -0
  28. package/declarations/model-fragments/extensions/fragment.d.ts +15 -0
  29. package/declarations/model-fragments/hooks/model-for.d.ts +20 -0
  30. package/declarations/model-fragments/index.d.ts +5 -0
  31. package/declarations/model-fragments/instance-initializers/fragment-extensions.d.ts +9 -0
  32. package/declarations/model-fragments/utilities/with-array-defaults.d.ts +15 -0
  33. package/declarations/model-fragments/utilities/with-fragment-array-defaults.d.ts +20 -0
  34. package/declarations/model-fragments/utilities/with-fragment-defaults.d.ts +19 -0
  35. package/declarations/model-fragments/utilities/with-legacy.d.ts +3 -0
  36. package/declarations/model-fragments.d.ts +9 -0
  37. package/declarations/model.d.ts +2 -2
  38. package/declarations/serializer/-private/embedded-records-mixin.d.ts +1 -6
  39. package/declarations/serializer/-private/transforms/boolean.d.ts +2 -2
  40. package/declarations/serializer/-private/transforms/date.d.ts +2 -2
  41. package/declarations/serializer/-private/transforms/number.d.ts +1 -1
  42. package/declarations/serializer/-private/transforms/string.d.ts +1 -1
  43. package/declarations/serializer/json-api.d.ts +11 -12
  44. package/declarations/serializer/json.d.ts +9 -11
  45. package/declarations/serializer/rest.d.ts +4 -6
  46. package/declarations/serializer.d.ts +9 -12
  47. package/dist/{-private-8UmnAf9J.js → -private-BG3bMiKp.js} +3 -2
  48. package/dist/adapter/-private.js +1 -1
  49. package/dist/adapter/error.js +14 -15
  50. package/dist/adapter/json-api.js +4 -1
  51. package/dist/adapter/rest.js +38 -125
  52. package/dist/adapter.js +6 -8
  53. package/dist/compat/-private.js +1 -1
  54. package/dist/compat/builders.js +26 -26
  55. package/dist/compat/utils.js +17 -18
  56. package/dist/compat.js +61 -43
  57. package/dist/{errors-8kD2mSe_.js → errors-Cz5KrzBk.js} +115 -122
  58. package/dist/hooks-D6diaM34.js +74 -0
  59. package/dist/index.js +195 -0
  60. package/dist/{json-Et4mt_LM.js → json-ChdEfB0X.js} +18 -35
  61. package/dist/model/-private.js +1 -1
  62. package/dist/model/migration-support.js +59 -27
  63. package/dist/model-for-CqXsIKws.js +221 -0
  64. package/dist/model-fragments.js +76 -0
  65. package/dist/model.js +18 -90
  66. package/dist/{schema-provider-DQu4Rjco.js → schema-provider-DJCV_6AF.js} +50 -95
  67. package/dist/{serialize-into-hash-CS0MIv4F.js → serialize-into-hash-DPZYoF-i.js} +1 -1
  68. package/dist/serializer/json-api.js +18 -45
  69. package/dist/serializer/json.js +1 -1
  70. package/dist/serializer/rest.js +14 -21
  71. package/dist/serializer/transform.js +15 -6
  72. package/dist/serializer.js +9 -13
  73. package/dist/store.js +5 -1
  74. package/dist/unpkg/dev/-private-DtjBbEgy.js +1206 -0
  75. package/dist/unpkg/dev/adapter/-private.js +1 -0
  76. package/dist/unpkg/dev/adapter/error.js +335 -0
  77. package/dist/unpkg/dev/adapter/json-api.js +271 -0
  78. package/dist/unpkg/dev/adapter/rest.js +1171 -0
  79. package/dist/unpkg/dev/adapter.js +1252 -0
  80. package/dist/unpkg/dev/compat/-private.js +1 -0
  81. package/dist/unpkg/dev/compat/builders.js +275 -0
  82. package/dist/unpkg/dev/compat/extensions.js +242 -0
  83. package/dist/unpkg/dev/compat/utils.js +223 -0
  84. package/dist/unpkg/dev/compat.js +1147 -0
  85. package/dist/unpkg/dev/errors-DmGGJr3T.js +2562 -0
  86. package/dist/unpkg/dev/hooks-CkYiE6Ud.js +73 -0
  87. package/dist/unpkg/dev/index.js +197 -0
  88. package/dist/unpkg/dev/json-Cu1LNgmQ.js +1256 -0
  89. package/dist/unpkg/dev/model/-private.js +1 -0
  90. package/dist/unpkg/dev/model/migration-support.js +553 -0
  91. package/dist/unpkg/dev/model-for-CqXsIKws.js +221 -0
  92. package/dist/unpkg/dev/model-fragments.js +76 -0
  93. package/dist/unpkg/dev/model.js +678 -0
  94. package/dist/unpkg/dev/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  95. package/dist/unpkg/dev/schema-provider-DDVYxmUV.js +2186 -0
  96. package/dist/unpkg/dev/serialize-into-hash-B2xDbuo5.js +259 -0
  97. package/dist/unpkg/dev/serializer/json-api.js +649 -0
  98. package/dist/unpkg/dev/serializer/json.js +4 -0
  99. package/dist/unpkg/dev/serializer/rest.js +1242 -0
  100. package/dist/unpkg/dev/serializer/transform.js +278 -0
  101. package/dist/unpkg/dev/serializer.js +248 -0
  102. package/dist/unpkg/dev/store.js +637 -0
  103. package/dist/unpkg/dev/util-DvanW33H.js +20 -0
  104. package/dist/unpkg/dev/utils-BhvS1iTS.js +8 -0
  105. package/dist/unpkg/dev-deprecated/-private-DtjBbEgy.js +1206 -0
  106. package/dist/unpkg/dev-deprecated/adapter/-private.js +1 -0
  107. package/dist/unpkg/dev-deprecated/adapter/error.js +335 -0
  108. package/dist/unpkg/dev-deprecated/adapter/json-api.js +271 -0
  109. package/dist/unpkg/dev-deprecated/adapter/rest.js +1171 -0
  110. package/dist/unpkg/dev-deprecated/adapter.js +1252 -0
  111. package/dist/unpkg/dev-deprecated/compat/-private.js +1 -0
  112. package/dist/unpkg/dev-deprecated/compat/builders.js +275 -0
  113. package/dist/unpkg/dev-deprecated/compat/extensions.js +242 -0
  114. package/dist/unpkg/dev-deprecated/compat/utils.js +223 -0
  115. package/dist/unpkg/dev-deprecated/compat.js +1147 -0
  116. package/dist/unpkg/dev-deprecated/errors-Spt6ubMd.js +2565 -0
  117. package/dist/unpkg/dev-deprecated/hooks-DOXegvhL.js +73 -0
  118. package/dist/unpkg/dev-deprecated/index.js +196 -0
  119. package/dist/unpkg/dev-deprecated/json-Cu1LNgmQ.js +1256 -0
  120. package/dist/unpkg/dev-deprecated/model/-private.js +1 -0
  121. package/dist/unpkg/dev-deprecated/model/migration-support.js +570 -0
  122. package/dist/unpkg/dev-deprecated/model-for-CqXsIKws.js +221 -0
  123. package/dist/unpkg/dev-deprecated/model-fragments.js +76 -0
  124. package/dist/unpkg/dev-deprecated/model.js +682 -0
  125. package/dist/unpkg/dev-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  126. package/dist/unpkg/dev-deprecated/schema-provider-BP6_8N-V.js +2211 -0
  127. package/dist/unpkg/dev-deprecated/serialize-into-hash-B2xDbuo5.js +259 -0
  128. package/dist/unpkg/dev-deprecated/serializer/json-api.js +649 -0
  129. package/dist/unpkg/dev-deprecated/serializer/json.js +4 -0
  130. package/dist/unpkg/dev-deprecated/serializer/rest.js +1242 -0
  131. package/dist/unpkg/dev-deprecated/serializer/transform.js +278 -0
  132. package/dist/unpkg/dev-deprecated/serializer.js +248 -0
  133. package/dist/unpkg/dev-deprecated/store.js +637 -0
  134. package/dist/unpkg/dev-deprecated/util-CWr5WQOT.js +24 -0
  135. package/dist/unpkg/dev-deprecated/utils-C9PJehtL.js +12 -0
  136. package/dist/unpkg/prod/-private-BdyZaGEh.js +971 -0
  137. package/dist/unpkg/prod/adapter/-private.js +1 -0
  138. package/dist/unpkg/prod/adapter/error.js +330 -0
  139. package/dist/unpkg/prod/adapter/json-api.js +266 -0
  140. package/dist/unpkg/prod/adapter/rest.js +1134 -0
  141. package/dist/unpkg/prod/adapter.js +1219 -0
  142. package/dist/unpkg/prod/compat/-private.js +1 -0
  143. package/dist/unpkg/prod/compat/builders.js +210 -0
  144. package/dist/unpkg/prod/compat/extensions.js +232 -0
  145. package/dist/unpkg/prod/compat/utils.js +218 -0
  146. package/dist/unpkg/prod/compat.js +727 -0
  147. package/dist/unpkg/prod/errors-BGVFCBmi.js +2314 -0
  148. package/dist/unpkg/prod/hooks-BztVA_x0.js +41 -0
  149. package/dist/unpkg/prod/index.js +151 -0
  150. package/dist/unpkg/prod/json-BWrZ5546.js +1243 -0
  151. package/dist/unpkg/prod/model/-private.js +1 -0
  152. package/dist/unpkg/prod/model/migration-support.js +546 -0
  153. package/dist/unpkg/prod/model-for-CqXsIKws.js +221 -0
  154. package/dist/unpkg/prod/model-fragments.js +76 -0
  155. package/dist/unpkg/prod/model.js +593 -0
  156. package/dist/unpkg/prod/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  157. package/dist/unpkg/prod/schema-provider-DJtD_8jZ.js +1861 -0
  158. package/dist/unpkg/prod/serialize-into-hash-DGlzQteF.js +215 -0
  159. package/dist/unpkg/prod/serializer/json-api.js +592 -0
  160. package/dist/unpkg/prod/serializer/json.js +4 -0
  161. package/dist/unpkg/prod/serializer/rest.js +1210 -0
  162. package/dist/unpkg/prod/serializer/transform.js +278 -0
  163. package/dist/unpkg/prod/serializer.js +248 -0
  164. package/dist/unpkg/prod/store.js +505 -0
  165. package/dist/unpkg/prod/util-DvanW33H.js +20 -0
  166. package/dist/unpkg/prod/utils-BhvS1iTS.js +8 -0
  167. package/dist/unpkg/prod-deprecated/-private-BdyZaGEh.js +971 -0
  168. package/dist/unpkg/prod-deprecated/adapter/-private.js +1 -0
  169. package/dist/unpkg/prod-deprecated/adapter/error.js +330 -0
  170. package/dist/unpkg/prod-deprecated/adapter/json-api.js +266 -0
  171. package/dist/unpkg/prod-deprecated/adapter/rest.js +1134 -0
  172. package/dist/unpkg/prod-deprecated/adapter.js +1219 -0
  173. package/dist/unpkg/prod-deprecated/compat/-private.js +1 -0
  174. package/dist/unpkg/prod-deprecated/compat/builders.js +210 -0
  175. package/dist/unpkg/prod-deprecated/compat/extensions.js +232 -0
  176. package/dist/unpkg/prod-deprecated/compat/utils.js +218 -0
  177. package/dist/unpkg/prod-deprecated/compat.js +727 -0
  178. package/dist/unpkg/prod-deprecated/errors-CdDaK81x.js +2317 -0
  179. package/dist/unpkg/prod-deprecated/hooks-yId87yyG.js +41 -0
  180. package/dist/unpkg/prod-deprecated/index.js +150 -0
  181. package/dist/unpkg/prod-deprecated/json-BWrZ5546.js +1243 -0
  182. package/dist/unpkg/prod-deprecated/model/-private.js +1 -0
  183. package/dist/unpkg/prod-deprecated/model/migration-support.js +563 -0
  184. package/dist/unpkg/prod-deprecated/model-for-CqXsIKws.js +221 -0
  185. package/dist/unpkg/prod-deprecated/model-fragments.js +76 -0
  186. package/dist/unpkg/prod-deprecated/model.js +596 -0
  187. package/dist/unpkg/prod-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  188. package/dist/unpkg/prod-deprecated/schema-provider-CjX55uSY.js +1904 -0
  189. package/dist/unpkg/prod-deprecated/serialize-into-hash-DGlzQteF.js +215 -0
  190. package/dist/unpkg/prod-deprecated/serializer/json-api.js +592 -0
  191. package/dist/unpkg/prod-deprecated/serializer/json.js +4 -0
  192. package/dist/unpkg/prod-deprecated/serializer/rest.js +1210 -0
  193. package/dist/unpkg/prod-deprecated/serializer/transform.js +278 -0
  194. package/dist/unpkg/prod-deprecated/serializer.js +248 -0
  195. package/dist/unpkg/prod-deprecated/store.js +505 -0
  196. package/dist/unpkg/prod-deprecated/util-B6cn-i93.js +23 -0
  197. package/dist/unpkg/prod-deprecated/utils-BUWwQwCh.js +11 -0
  198. package/logos/README.md +2 -2
  199. package/logos/logo-yellow-slab.svg +1 -0
  200. package/logos/word-mark-black.svg +1 -0
  201. package/logos/word-mark-white.svg +1 -0
  202. package/package.json +15 -7
  203. package/logos/NCC-1701-a-blue.svg +0 -4
  204. package/logos/NCC-1701-a-gold.svg +0 -4
  205. package/logos/NCC-1701-a-gold_100.svg +0 -1
  206. package/logos/NCC-1701-a-gold_base-64.txt +0 -1
  207. package/logos/NCC-1701-a.svg +0 -4
  208. package/logos/docs-badge.svg +0 -2
  209. package/logos/ember-data-logo-dark.svg +0 -12
  210. package/logos/ember-data-logo-light.svg +0 -12
  211. package/logos/social1.png +0 -0
  212. package/logos/social2.png +0 -0
  213. package/logos/warp-drive-logo-dark.svg +0 -4
  214. package/logos/warp-drive-logo-gold.svg +0 -4
@@ -0,0 +1,1904 @@
1
+ import { getOwner } from '@ember/application';
2
+ import EmberObject from '@ember/object';
3
+ import { recordIdentifierFor, storeFor } from '@warp-drive/core';
4
+ import { notifyInternalSignal, peekInternalSignal, withSignalStore, gate, memoized, defineSignal, entangleSignal, defineGate } from '@warp-drive/core/signals/-leaked';
5
+ import { assertPrivateStore, recordIdentifierFor as recordIdentifierFor$1, coerceId } from '@warp-drive/core/store/-private';
6
+ import { RecordStore } from '@warp-drive/core/types/symbols';
7
+ import { l as lookupLegacySupport, L as LEGACY_SUPPORT, E as Errors } from "./errors-CdDaK81x.js";
8
+ import { u as upgradeStore, F as FetchManager } from "./-private-BdyZaGEh.js";
9
+ import { cacheFor } from '@ember/object/internals';
10
+ import { Context } from '@warp-drive/core/reactive/-private';
11
+ import { d as decorateMethodV2 } from "./runtime-BPCpkOf1-BKOwiRJp.js";
12
+ import { n as normalizeModelName } from "./util-B6cn-i93.js";
13
+ function rollbackAttributes() {
14
+ const {
15
+ currentState
16
+ } = this;
17
+ const {
18
+ isNew
19
+ } = currentState;
20
+ const store = this[RecordStore];
21
+ assertPrivateStore(store);
22
+ store._join(() => {
23
+ store.cache.rollbackAttrs(recordIdentifierFor(this));
24
+ this.errors.clear();
25
+ currentState.cleanErrorRequests();
26
+ if (isNew) {
27
+ this.unloadRecord();
28
+ }
29
+ });
30
+ }
31
+ function unloadRecord() {
32
+ if (this.currentState.isNew && (this.isDestroyed || this.isDestroying)) {
33
+ return;
34
+ }
35
+ this[RecordStore].unloadRecord(this);
36
+ }
37
+ function belongsTo(prop) {
38
+ return lookupLegacySupport(this).referenceFor('belongsTo', prop);
39
+ }
40
+ function hasMany(prop) {
41
+ return lookupLegacySupport(this).referenceFor('hasMany', prop);
42
+ }
43
+ function reload(options = {}) {
44
+ {
45
+ return _reload.call(this, options);
46
+ }
47
+ }
48
+ function _reload(options = {}) {
49
+ options.isReloading = true;
50
+ options.reload = true;
51
+ const identifier = recordIdentifierFor(this);
52
+ this.isReloading = true;
53
+ const promise = this[RecordStore].request({
54
+ op: 'findRecord',
55
+ data: {
56
+ options,
57
+ record: identifier
58
+ },
59
+ cacheOptions: {
60
+ [Symbol.for('wd:skip-cache')]: true
61
+ }
62
+ }).then(() => this).finally(() => {
63
+ this.isReloading = false;
64
+ });
65
+ return promise;
66
+ }
67
+ function changedAttributes() {
68
+ return this[RecordStore].cache.changedAttrs(recordIdentifierFor(this));
69
+ }
70
+ function serialize(options) {
71
+ upgradeStore(this[RecordStore]);
72
+ return this[RecordStore].serializeRecord(this, options);
73
+ }
74
+ function deleteRecord() {
75
+ // ensure we've populated currentState prior to deleting a new record
76
+ if (this.currentState) {
77
+ this[RecordStore].deleteRecord(this);
78
+ }
79
+ }
80
+ function save(options) {
81
+ {
82
+ return _save.call(this, options);
83
+ }
84
+ }
85
+ function _save(options) {
86
+ let promise;
87
+ if (this.currentState.isNew && this.currentState.isDeleted) {
88
+ promise = Promise.resolve(this);
89
+ } else {
90
+ this.errors.clear();
91
+ promise = this[RecordStore].saveRecord(this, options);
92
+ }
93
+ return promise;
94
+ }
95
+ function destroyRecord(options) {
96
+ {
97
+ return _destroyRecord.call(this, options);
98
+ }
99
+ }
100
+ function _destroyRecord(options) {
101
+ const {
102
+ isNew
103
+ } = this.currentState;
104
+ this.deleteRecord();
105
+ if (isNew) {
106
+ return Promise.resolve(this);
107
+ }
108
+ return this.save(options).then(_ => {
109
+ this.unloadRecord();
110
+ return this;
111
+ });
112
+ }
113
+ function createSnapshot() {
114
+ const store = this[RecordStore];
115
+ if (!store._fetchManager) {
116
+ store._fetchManager = new FetchManager(store);
117
+ }
118
+
119
+ // @ts-expect-error Typescript isn't able to curry narrowed args that are divorced from each other.
120
+ return store._fetchManager.createSnapshot(recordIdentifierFor(this));
121
+ }
122
+ function notifyChanges(identifier, value, key, record, store) {
123
+ switch (value) {
124
+ case 'added':
125
+ case 'attributes':
126
+ if (key) {
127
+ notifyAttribute(store, identifier, key, record);
128
+ } else {
129
+ record.eachAttribute(name => {
130
+ notifyAttribute(store, identifier, name, record);
131
+ });
132
+ }
133
+ break;
134
+ case 'relationships':
135
+ if (key) {
136
+ const meta = record.constructor.relationshipsByName.get(key);
137
+ notifyRelationship(identifier, key, record, meta);
138
+ } else {
139
+ record.eachRelationship((name, meta) => {
140
+ notifyRelationship(identifier, name, record, meta);
141
+ });
142
+ }
143
+ break;
144
+ case 'identity':
145
+ notifyInternalSignal(peekInternalSignal(withSignalStore(record), 'id'));
146
+ break;
147
+ }
148
+ }
149
+ function notifyRelationship(identifier, key, record, meta) {
150
+ if (meta.kind === 'belongsTo') {
151
+ record.notifyPropertyChange(key);
152
+ } else if (meta.kind === 'hasMany') {
153
+ const support = LEGACY_SUPPORT.get(identifier);
154
+ const manyArray = support && support._manyArrayCache[key];
155
+ const hasPromise = support && support._relationshipPromisesCache[key];
156
+ if (manyArray && hasPromise) {
157
+ // do nothing, we will notify the ManyArray directly
158
+ // once the fetch has completed.
159
+ return;
160
+ }
161
+ if (manyArray) {
162
+ notifyInternalSignal(manyArray[Context].signal);
163
+ if (meta.options.async) {
164
+ record.notifyPropertyChange(key);
165
+ }
166
+ }
167
+ }
168
+ }
169
+ function notifyAttribute(store, identifier, key, record) {
170
+ const currentValue = cacheFor(record, key);
171
+ const cache = store.cache;
172
+ if (currentValue !== cache.getAttr(identifier, key)) {
173
+ record.notifyPropertyChange(key);
174
+ }
175
+ }
176
+ const SOURCE_POINTER_REGEXP = /^\/?data\/(attributes|relationships)\/(.*)/;
177
+ const SOURCE_POINTER_PRIMARY_REGEXP = /^\/?data/;
178
+ const PRIMARY_ATTRIBUTE_KEY = 'base';
179
+ function isInvalidError(error) {
180
+ return !!error && error instanceof Error && 'isAdapterError' in error && error.isAdapterError === true && 'code' in error && error.code === 'InvalidError';
181
+ }
182
+
183
+ /**
184
+ Historically WarpDrive managed a state machine
185
+ for each record, the localState for which
186
+ was reflected onto Model.
187
+
188
+ This implements the flags and stateName for backwards compat
189
+ with the state tree that used to be possible (listed below).
190
+
191
+ stateName and dirtyType are candidates for deprecation.
192
+
193
+ root
194
+ empty
195
+ deleted // hidden from stateName
196
+ preloaded // hidden from stateName
197
+
198
+ loading
199
+ empty // hidden from stateName
200
+ preloaded // hidden from stateName
201
+
202
+ loaded
203
+ saved
204
+ updated
205
+ uncommitted
206
+ invalid
207
+ inFlight
208
+ created
209
+ uncommitted
210
+ invalid
211
+ inFlight
212
+
213
+ deleted
214
+ saved
215
+ new // hidden from stateName
216
+ uncommitted
217
+ invalid
218
+ inFlight
219
+
220
+ @hideconstructor
221
+ @private
222
+ */
223
+ class RecordState {
224
+ /** @internal */
225
+
226
+ /** @internal */
227
+
228
+ /** @internal */
229
+
230
+ /** @internal */
231
+
232
+ /** @internal */
233
+
234
+ /** @internal */
235
+
236
+ /** @internal */
237
+
238
+ /** @internal */
239
+
240
+ /** @internal */
241
+
242
+ /** @internal */
243
+
244
+ /** @internal */
245
+
246
+ constructor(record) {
247
+ const store = storeFor(record, false);
248
+ const identity = recordIdentifierFor$1(record);
249
+ this.identifier = identity;
250
+ this.record = record;
251
+ this.cache = store.cache;
252
+ this.pendingCount = 0;
253
+ this.fulfilledCount = 0;
254
+ this.rejectedCount = 0;
255
+ this._errorRequests = [];
256
+ this._lastError = null;
257
+ const requests = store.getRequestStateService();
258
+ const notifications = store.notifications;
259
+ const handleRequest = req => {
260
+ if (req.type === 'mutation') {
261
+ switch (req.state) {
262
+ case 'pending':
263
+ this.isSaving = true;
264
+ break;
265
+ case 'rejected':
266
+ this.isSaving = false;
267
+ this._lastError = req;
268
+ if (!(req.response && isInvalidError(req.response.data))) {
269
+ this._errorRequests.push(req);
270
+ }
271
+ notifyErrorsStateChanged(this);
272
+ break;
273
+ case 'fulfilled':
274
+ this._errorRequests = [];
275
+ this._lastError = null;
276
+ this.isSaving = false;
277
+ this.notify('isDirty');
278
+ notifyErrorsStateChanged(this);
279
+ break;
280
+ }
281
+ } else {
282
+ switch (req.state) {
283
+ case 'pending':
284
+ this.pendingCount++;
285
+ this.notify('isLoading');
286
+ break;
287
+ case 'rejected':
288
+ this.pendingCount--;
289
+ this._lastError = req;
290
+ if (!(req.response && isInvalidError(req.response.data))) {
291
+ this._errorRequests.push(req);
292
+ }
293
+ this.notify('isLoading');
294
+ notifyErrorsStateChanged(this);
295
+ break;
296
+ case 'fulfilled':
297
+ this.pendingCount--;
298
+ this.fulfilledCount++;
299
+ this.notify('isLoading');
300
+ this.notify('isDirty');
301
+ notifyErrorsStateChanged(this);
302
+ this._errorRequests = [];
303
+ this._lastError = null;
304
+ break;
305
+ }
306
+ }
307
+ };
308
+ requests.subscribeForRecord(identity, handleRequest);
309
+
310
+ // we instantiate lazily
311
+ // so we grab anything we don't have yet
312
+ const lastRequest = requests.getLastRequestForRecord(identity);
313
+ if (lastRequest) {
314
+ handleRequest(lastRequest);
315
+ }
316
+ this.handler = notifications.subscribe(identity, (identifier, type, key) => {
317
+ switch (type) {
318
+ case 'state':
319
+ this.notify('isSaved');
320
+ this.notify('isNew');
321
+ this.notify('isDeleted');
322
+ this.notify('isDirty');
323
+ break;
324
+ case 'attributes':
325
+ this.notify('isEmpty');
326
+ this.notify('isDirty');
327
+ break;
328
+ case 'errors':
329
+ this.updateInvalidErrors(this.record.errors);
330
+ this.notify('isValid');
331
+ break;
332
+ }
333
+ });
334
+ }
335
+
336
+ /** @internal */
337
+ destroy() {
338
+ storeFor(this.record, false).notifications.unsubscribe(this.handler);
339
+ }
340
+
341
+ /** @internal */
342
+ notify(key) {
343
+ const signals = withSignalStore(this);
344
+ const signal = peekInternalSignal(signals, key);
345
+ if (signal) {
346
+ notifyInternalSignal(signal);
347
+ }
348
+ }
349
+
350
+ /** @internal */
351
+ updateInvalidErrors(errors) {
352
+ const jsonApiErrors = this.cache.getErrors(this.identifier);
353
+ errors.clear();
354
+ for (let i = 0; i < jsonApiErrors.length; i++) {
355
+ const error = jsonApiErrors[i];
356
+ if (error.source && error.source.pointer) {
357
+ const keyMatch = error.source.pointer.match(SOURCE_POINTER_REGEXP);
358
+ let key;
359
+ if (keyMatch) {
360
+ key = keyMatch[2];
361
+ } else if (error.source.pointer.search(SOURCE_POINTER_PRIMARY_REGEXP) !== -1) {
362
+ key = PRIMARY_ATTRIBUTE_KEY;
363
+ }
364
+ if (key) {
365
+ const errMsg = error.detail || error.title;
366
+ errors.add(key, errMsg);
367
+ }
368
+ }
369
+ }
370
+ }
371
+
372
+ /** @internal */
373
+ cleanErrorRequests() {
374
+ this.notify('isValid');
375
+ this.notify('isError');
376
+ this.notify('adapterError');
377
+ this._errorRequests = [];
378
+ this._lastError = null;
379
+ }
380
+ get isLoading() {
381
+ return !this.isLoaded && this.pendingCount > 0 && this.fulfilledCount === 0;
382
+ }
383
+ static {
384
+ decorateMethodV2(this.prototype, "isLoading", [gate]);
385
+ }
386
+ get isLoaded() {
387
+ if (this.isNew) {
388
+ return true;
389
+ }
390
+ return this.fulfilledCount > 0 || !this.isEmpty;
391
+ }
392
+ static {
393
+ decorateMethodV2(this.prototype, "isLoaded", [gate]);
394
+ }
395
+ get isSaved() {
396
+ const rd = this.cache;
397
+ if (this.isDeleted) {
398
+ return rd.isDeletionCommitted(this.identifier);
399
+ }
400
+ if (this.isNew || this.isEmpty || !this.isValid || this.isDirty || this.isLoading) {
401
+ return false;
402
+ }
403
+ return true;
404
+ }
405
+ static {
406
+ decorateMethodV2(this.prototype, "isSaved", [gate]);
407
+ }
408
+ get isEmpty() {
409
+ const rd = this.cache;
410
+ return !this.isNew && rd.isEmpty(this.identifier);
411
+ }
412
+ static {
413
+ decorateMethodV2(this.prototype, "isEmpty", [gate]);
414
+ }
415
+ get isNew() {
416
+ const rd = this.cache;
417
+ return rd.isNew(this.identifier);
418
+ }
419
+ static {
420
+ decorateMethodV2(this.prototype, "isNew", [gate]);
421
+ }
422
+ get isDeleted() {
423
+ const rd = this.cache;
424
+ return rd.isDeleted(this.identifier);
425
+ }
426
+ static {
427
+ decorateMethodV2(this.prototype, "isDeleted", [gate]);
428
+ }
429
+ get isValid() {
430
+ return this.record.errors.length === 0;
431
+ }
432
+ static {
433
+ decorateMethodV2(this.prototype, "isValid", [gate]);
434
+ }
435
+ get isDirty() {
436
+ const rd = this.cache;
437
+ if (this.isEmpty || rd.isDeletionCommitted(this.identifier) || this.isDeleted && this.isNew) {
438
+ return false;
439
+ }
440
+ return this.isDeleted || this.isNew || rd.hasChangedAttrs(this.identifier);
441
+ }
442
+ static {
443
+ decorateMethodV2(this.prototype, "isDirty", [gate]);
444
+ }
445
+ get isError() {
446
+ const errorReq = this._errorRequests[this._errorRequests.length - 1];
447
+ if (!errorReq) {
448
+ return false;
449
+ } else {
450
+ return true;
451
+ }
452
+ }
453
+ static {
454
+ decorateMethodV2(this.prototype, "isError", [gate]);
455
+ }
456
+ get adapterError() {
457
+ const request = this._lastError;
458
+ if (!request) {
459
+ return null;
460
+ }
461
+ return request.state === 'rejected' && request.response.data;
462
+ }
463
+ static {
464
+ decorateMethodV2(this.prototype, "adapterError", [gate]);
465
+ }
466
+ get isPreloaded() {
467
+ return !this.isEmpty && this.isLoading;
468
+ }
469
+ static {
470
+ decorateMethodV2(this.prototype, "isPreloaded", [memoized]);
471
+ }
472
+ get stateName() {
473
+ // we might be empty while loading so check this first
474
+ if (this.isLoading) {
475
+ return 'root.loading';
476
+
477
+ // got nothing yet or were unloaded
478
+ } else if (this.isEmpty) {
479
+ return 'root.empty';
480
+
481
+ // deleted substates
482
+ } else if (this.isDeleted) {
483
+ if (this.isSaving) {
484
+ return 'root.deleted.inFlight';
485
+ } else if (this.isSaved) {
486
+ // TODO ensure isSaved isn't true from previous requests
487
+ return 'root.deleted.saved';
488
+ } else if (!this.isValid) {
489
+ return 'root.deleted.invalid';
490
+ } else {
491
+ return 'root.deleted.uncommitted';
492
+ }
493
+
494
+ // loaded.created substates
495
+ } else if (this.isNew) {
496
+ if (this.isSaving) {
497
+ return 'root.loaded.created.inFlight';
498
+ } else if (!this.isValid) {
499
+ return 'root.loaded.created.invalid';
500
+ }
501
+ return 'root.loaded.created.uncommitted';
502
+
503
+ // loaded.updated substates
504
+ } else if (this.isSaving) {
505
+ return 'root.loaded.updated.inFlight';
506
+ } else if (!this.isValid) {
507
+ return 'root.loaded.updated.invalid';
508
+ } else if (this.isDirty) {
509
+ return 'root.loaded.updated.uncommitted';
510
+
511
+ // if nothing remains, we are loaded saved!
512
+ } else {
513
+ return 'root.loaded.saved';
514
+ }
515
+ }
516
+ static {
517
+ decorateMethodV2(this.prototype, "stateName", [memoized]);
518
+ }
519
+ get dirtyType() {
520
+ // we might be empty while loading so check this first
521
+ if (this.isLoading || this.isEmpty) {
522
+ return '';
523
+
524
+ // deleted substates
525
+ } else if (this.isDirty && this.isDeleted) {
526
+ return 'deleted';
527
+
528
+ // loaded.created substates
529
+ } else if (this.isNew) {
530
+ return 'created';
531
+
532
+ // loaded.updated substates
533
+ } else if (this.isSaving || !this.isValid || this.isDirty) {
534
+ return 'updated';
535
+
536
+ // if nothing remains, we are loaded saved!
537
+ } else {
538
+ return '';
539
+ }
540
+ }
541
+ static {
542
+ decorateMethodV2(this.prototype, "dirtyType", [memoized]);
543
+ }
544
+ }
545
+ defineSignal(RecordState.prototype, 'isSaving', false);
546
+ function notifyErrorsStateChanged(state) {
547
+ state.notify('isValid');
548
+ state.notify('isError');
549
+ state.notify('adapterError');
550
+ }
551
+
552
+ /*
553
+ * This decorator allows us to lazily compute
554
+ * an expensive getter on first-access and thereafter
555
+ * never recompute it.
556
+ */
557
+ function computeOnce(target, propertyName, desc) {
558
+ const cache = new WeakMap();
559
+ // eslint-disable-next-line @typescript-eslint/unbound-method
560
+ const getter = desc.get;
561
+ desc.get = function () {
562
+ let meta = cache.get(this);
563
+ if (!meta) {
564
+ meta = {
565
+ hasComputed: false,
566
+ value: undefined
567
+ };
568
+ cache.set(this, meta);
569
+ }
570
+ if (!meta.hasComputed) {
571
+ meta.value = getter.call(this);
572
+ meta.hasComputed = true;
573
+ }
574
+ return meta.value;
575
+ };
576
+ return desc;
577
+ }
578
+ /**
579
+ * Base class from which Models can be defined.
580
+ *
581
+ * ::: code-group
582
+ *
583
+ * ```js [app/models/user.js]
584
+ * import { Model, attr, belongsTo, hasMany } from '@warp-drive/legacy/model';
585
+ *
586
+ * export default class User extends Model {
587
+ * @attr name;
588
+ * @attr('number') age;
589
+ * @hasMany('post', { async: true, inverse: null }) posts;
590
+ * @belongsTo('group', { async: false, inverse: 'users' }) group;
591
+ * }
592
+ * ```
593
+ *
594
+ * ```ts [app/models/user.ts]
595
+ * import { Model, attr, belongsTo, hasMany, type AsyncHasMany } from '@warp-drive/legacy/model';
596
+ * import type { NumberTransform } from '@ember-data/serializer/transform';
597
+ * import type Group from './group';
598
+ * import type Post from './post';
599
+ *
600
+ * export default class User extends Model {
601
+ * @attr declare name: string;
602
+ *
603
+ * @attr<NumberTransform>('number')
604
+ * declare age: number;
605
+ *
606
+ * @hasMany('post', { async: true, inverse: null })
607
+ * declare posts: AsyncHasMany<Post>;
608
+ *
609
+ * @belongsTo('group', { async: false, inverse: 'users' })
610
+ * declare group: Group | null;
611
+ * }
612
+ * ```
613
+ *
614
+ * :::
615
+ *
616
+ * Models both define the schema for a resource type and provide
617
+ * the class to use as the reactive object for data of resource
618
+ * of that type.
619
+ *
620
+ * @public
621
+ * @noInheritDoc
622
+ * @hideconstructor
623
+ * @legacy
624
+ */
625
+ class Model extends EmberObject {
626
+ /** @internal */
627
+ init(options) {
628
+ const createProps = options._createProps;
629
+ const _secretInit = options._secretInit;
630
+ options._createProps = null;
631
+ options._secretInit = null;
632
+ const store = this.store = _secretInit.store;
633
+ super.init(options);
634
+ this[RecordStore] = store;
635
+ const identity = _secretInit.identifier;
636
+ _secretInit.cb(this, identity, _secretInit.store);
637
+ this.___recordState = null;
638
+ this.setProperties(createProps);
639
+ const notifications = store.notifications;
640
+ this.___private_notifications = notifications.subscribe(identity, (identifier, type, field) => {
641
+ notifyChanges(identifier, type, field, this, store);
642
+ });
643
+ }
644
+
645
+ /** @private */
646
+ // @ts-expect-error destroy should not return a value, but ember's types force it to
647
+ destroy() {
648
+ const identifier = recordIdentifierFor(this);
649
+ this.___recordState?.destroy();
650
+ const store = storeFor(this, false);
651
+ store.notifications.unsubscribe(this.___private_notifications);
652
+ const support = LEGACY_SUPPORT.get(identifier);
653
+ if (support) {
654
+ support.destroy();
655
+ LEGACY_SUPPORT.delete(identifier);
656
+ }
657
+ super.destroy();
658
+ }
659
+
660
+ /**
661
+ If this property is `true` the record is in the `empty`
662
+ state. Empty is the first state all records enter after they have
663
+ been created. Most records created by the store will quickly
664
+ transition to the `loading` state if data needs to be fetched from
665
+ the server or the `created` state if the record is created on the
666
+ client. A record can also enter the empty state if the adapter is
667
+ unable to locate the record.
668
+ @public
669
+ */
670
+ get isEmpty() {
671
+ return this.currentState.isEmpty;
672
+ }
673
+
674
+ /**
675
+ If this property is `true` the record is in the `loading` state. A
676
+ record enters this state when the store asks the adapter for its
677
+ data. It remains in this state until the adapter provides the
678
+ requested data.
679
+ @public
680
+ */
681
+ static {
682
+ decorateMethodV2(this.prototype, "isEmpty", [memoized]);
683
+ }
684
+ get isLoading() {
685
+ return this.currentState.isLoading;
686
+ }
687
+
688
+ /**
689
+ If this property is `true` the record is in the `loaded` state. A
690
+ record enters this state when its data is populated. Most of a
691
+ record's lifecycle is spent inside substates of the `loaded`
692
+ state.
693
+ Example
694
+ ```javascript
695
+ let record = store.createRecord('model');
696
+ record.isLoaded; // true
697
+ const { content: { data: model } } = await store.request(findRecord({ type: 'model', id: '1' }));
698
+ model.isLoaded;
699
+ ```
700
+ @public
701
+ */
702
+ static {
703
+ decorateMethodV2(this.prototype, "isLoading", [memoized]);
704
+ }
705
+ get isLoaded() {
706
+ return this.currentState.isLoaded;
707
+ }
708
+
709
+ /**
710
+ If this property is `true` the record is in the `dirty` state. The
711
+ record has local changes that have not yet been saved by the
712
+ adapter. This includes records that have been created (but not yet
713
+ saved) or deleted.
714
+ Example
715
+ ```javascript
716
+ let record = store.createRecord('model');
717
+ record.hasDirtyAttributes; // true
718
+ const { content: { data: model } } = await store.request(findRecord({ type: 'model', id: '1' }));
719
+ model.hasDirtyAttributes; // false
720
+ model.foo = 'some value';
721
+ model.hasDirtyAttributes; // true
722
+ ```
723
+ @since 1.13.0
724
+ @public
725
+ */
726
+ static {
727
+ decorateMethodV2(this.prototype, "isLoaded", [memoized]);
728
+ }
729
+ get hasDirtyAttributes() {
730
+ return this.currentState.isDirty;
731
+ }
732
+
733
+ /**
734
+ If this property is `true` the record is in the `saving` state. A
735
+ record enters the saving state when `save` is called, but the
736
+ adapter has not yet acknowledged that the changes have been
737
+ persisted to the backend.
738
+ Example
739
+ ```javascript
740
+ let record = store.createRecord('model');
741
+ record.isSaving; // false
742
+ let promise = record.save();
743
+ record.isSaving; // true
744
+ promise.then(function() {
745
+ record.isSaving; // false
746
+ });
747
+ ```
748
+ @public
749
+ */
750
+ static {
751
+ decorateMethodV2(this.prototype, "hasDirtyAttributes", [memoized]);
752
+ }
753
+ get isSaving() {
754
+ return this.currentState.isSaving;
755
+ }
756
+
757
+ /**
758
+ If this property is `true` the record is in the `deleted` state
759
+ and has been marked for deletion. When `isDeleted` is true and
760
+ `hasDirtyAttributes` is true, the record is deleted locally but the deletion
761
+ was not yet persisted. When `isSaving` is true, the change is
762
+ in-flight. When both `hasDirtyAttributes` and `isSaving` are false, the
763
+ change has persisted.
764
+ Example
765
+ ```javascript
766
+ let record = store.createRecord('model');
767
+ record.isDeleted; // false
768
+ record.deleteRecord();
769
+ // Locally deleted
770
+ record.isDeleted; // true
771
+ record.hasDirtyAttributes; // true
772
+ record.isSaving; // false
773
+ // Persisting the deletion
774
+ let promise = record.save();
775
+ record.isDeleted; // true
776
+ record.isSaving; // true
777
+ // Deletion Persisted
778
+ promise.then(function() {
779
+ record.isDeleted; // true
780
+ record.isSaving; // false
781
+ record.hasDirtyAttributes; // false
782
+ });
783
+ ```
784
+ @public
785
+ */
786
+ static {
787
+ decorateMethodV2(this.prototype, "isSaving", [memoized]);
788
+ }
789
+ get isDeleted() {
790
+ return this.currentState.isDeleted;
791
+ }
792
+
793
+ /**
794
+ If this property is `true` the record is in the `new` state. A
795
+ record will be in the `new` state when it has been created on the
796
+ client and the adapter has not yet report that it was successfully
797
+ saved.
798
+ Example
799
+ ```javascript
800
+ let record = store.createRecord('model');
801
+ record.isNew; // true
802
+ record.save().then(function(model) {
803
+ model.isNew; // false
804
+ });
805
+ ```
806
+ @public
807
+ */
808
+ static {
809
+ decorateMethodV2(this.prototype, "isDeleted", [memoized]);
810
+ }
811
+ get isNew() {
812
+ return this.currentState.isNew;
813
+ }
814
+
815
+ /**
816
+ If this property is `true` the record is in the `valid` state.
817
+ A record will be in the `valid` state when the adapter did not report any
818
+ server-side validation failures.
819
+ @public
820
+ */
821
+ static {
822
+ decorateMethodV2(this.prototype, "isNew", [memoized]);
823
+ }
824
+ get isValid() {
825
+ return this.currentState.isValid;
826
+ }
827
+
828
+ /**
829
+ If the record is in the dirty state this property will report what
830
+ kind of change has caused it to move into the dirty
831
+ state. Possible values are:
832
+ - `created` The record has been created by the client and not yet saved to the adapter.
833
+ - `updated` The record has been updated by the client and not yet saved to the adapter.
834
+ - `deleted` The record has been deleted by the client and not yet saved to the adapter.
835
+ Example
836
+ ```javascript
837
+ let record = store.createRecord('model');
838
+ record.dirtyType; // 'created'
839
+ ```
840
+ @public
841
+ */
842
+ static {
843
+ decorateMethodV2(this.prototype, "isValid", [memoized]);
844
+ }
845
+ get dirtyType() {
846
+ return this.currentState.dirtyType;
847
+ }
848
+
849
+ /**
850
+ If `true` the adapter reported that it was unable to save local
851
+ changes to the backend for any reason other than a server-side
852
+ validation error.
853
+ Example
854
+ ```javascript
855
+ record.isError; // false
856
+ record.set('foo', 'valid value');
857
+ record.save().then(null, function() {
858
+ record.isError; // true
859
+ });
860
+ ```
861
+ @public
862
+ */
863
+ static {
864
+ decorateMethodV2(this.prototype, "dirtyType", [memoized]);
865
+ }
866
+ get isError() {
867
+ return this.currentState.isError;
868
+ }
869
+ static {
870
+ decorateMethodV2(this.prototype, "isError", [memoized]);
871
+ }
872
+ set isError(v) {}
873
+
874
+ /**
875
+ If `true` the store is attempting to reload the record from the adapter.
876
+ Example
877
+ ```javascript
878
+ record.isReloading; // false
879
+ record.reload();
880
+ record.isReloading; // true
881
+ ```
882
+ @public
883
+ */
884
+
885
+ /**
886
+ All ember models have an id property. This is an identifier
887
+ managed by an external source. These are always coerced to be
888
+ strings before being used internally. Note when declaring the
889
+ attributes for a model it is an error to declare an id
890
+ attribute.
891
+ ```javascript
892
+ let record = store.createRecord('model');
893
+ record.id; // null
894
+ const { content: { data: model } } = await store.request(findRecord({ type: 'model', id: '1' }));
895
+ model.id; // '1'
896
+ ```
897
+ @public
898
+ */
899
+ get id() {
900
+ // this guard exists, because some dev-only deprecation code
901
+ // (addListener via validatePropertyInjections) invokes toString before the
902
+ // object is real.
903
+
904
+ return recordIdentifierFor(this).id;
905
+ }
906
+ static {
907
+ decorateMethodV2(this.prototype, "id", [gate]);
908
+ }
909
+ set id(id) {
910
+ const normalizedId = coerceId(id);
911
+ const identifier = recordIdentifierFor(this);
912
+ const didChange = normalizedId !== identifier.id;
913
+ assertPrivateStore(this.store);
914
+ if (normalizedId !== null && didChange) {
915
+ this.store._instanceCache.setRecordId(identifier, normalizedId);
916
+ this.store.notifications.notify(identifier, 'identity', null);
917
+ }
918
+ }
919
+ toString() {
920
+ return `<model::${this.constructor.modelName}:${this.id}>`;
921
+ }
922
+
923
+ /**
924
+ @private
925
+ */
926
+ // TODO we can probably make this a computeOnce
927
+ // we likely do not need to notify the currentState root anymore
928
+ get currentState() {
929
+ // descriptors are called with the wrong `this` context during mergeMixins
930
+ // when using legacy/classic ember classes. Basically: lazy in prod and eager in dev.
931
+ // so we do this to try to steer folks to the nicer "dont user currentState"
932
+ // error.
933
+ {
934
+ if (!this.___recordState) {
935
+ this.___recordState = new RecordState(this);
936
+ }
937
+ }
938
+ return this.___recordState;
939
+ }
940
+ static {
941
+ decorateMethodV2(this.prototype, "currentState", [gate]);
942
+ }
943
+ set currentState(_v) {
944
+ throw new Error('cannot set currentState');
945
+ }
946
+
947
+ /**
948
+ The store service instance which created this record instance
949
+ @public
950
+ */
951
+
952
+ /**
953
+ When the record is in the `invalid` state this object will contain
954
+ any errors returned by the adapter. When present the errors hash
955
+ contains keys corresponding to the invalid property names
956
+ and values which are arrays of Javascript objects with two keys:
957
+ - `message` A string containing the error message from the backend
958
+ - `attribute` The name of the property associated with this error message
959
+ ```javascript
960
+ record.errors.length; // 0
961
+ record.set('foo', 'invalid value');
962
+ record.save().catch(function() {
963
+ record.errors.foo;
964
+ // [{message: 'foo should be a number.', attribute: 'foo'}]
965
+ });
966
+ ```
967
+ The `errors` property is useful for displaying error messages to
968
+ the user.
969
+ ```handlebars
970
+ <label>Username: <Input @value={{@model.username}} /> </label>
971
+ {{#each @model.errors.username as |error|}}
972
+ <div class="error">
973
+ {{error.message}}
974
+ </div>
975
+ {{/each}}
976
+ <label>Email: <Input @value={{@model.email}} /> </label>
977
+ {{#each @model.errors.email as |error|}}
978
+ <div class="error">
979
+ {{error.message}}
980
+ </div>
981
+ {{/each}}
982
+ ```
983
+ You can also access the special `messages` property on the error
984
+ object to get an array of all the error strings.
985
+ ```handlebars
986
+ {{#each @model.errors.messages as |message|}}
987
+ <div class="error">
988
+ {{message}}
989
+ </div>
990
+ {{/each}}
991
+ ```
992
+ @public
993
+ */
994
+ get errors() {
995
+ const errors = Errors.create({
996
+ __record: this
997
+ });
998
+ this.currentState.updateInvalidErrors(errors);
999
+ return errors;
1000
+ }
1001
+
1002
+ /**
1003
+ This property holds the `AdapterError` object with which
1004
+ last adapter operation was rejected.
1005
+ @public
1006
+ */
1007
+ static {
1008
+ decorateMethodV2(this.prototype, "errors", [computeOnce]);
1009
+ }
1010
+ get adapterError() {
1011
+ return this.currentState.adapterError;
1012
+ }
1013
+ static {
1014
+ decorateMethodV2(this.prototype, "adapterError", [memoized]);
1015
+ }
1016
+ set adapterError(v) {
1017
+ throw new Error(`adapterError is not directly settable`);
1018
+ }
1019
+
1020
+ /*
1021
+ We hook the default implementation to ensure
1022
+ our tagged properties are properly notified
1023
+ as well. We still super for everything because
1024
+ sync observers require a direct call occuring
1025
+ to trigger their flush. We wouldn't need to
1026
+ super in 4.0+ where sync observers are removed.
1027
+ */
1028
+ // @ts-expect-error no return is necessary, but Ember's types are forcing it
1029
+ notifyPropertyChange(prop) {
1030
+ const signals = withSignalStore(this);
1031
+ entangleSignal(signals, this, prop, undefined);
1032
+ super.notifyPropertyChange(prop);
1033
+ }
1034
+
1035
+ /** @internal */
1036
+ attr() {}
1037
+
1038
+ /**
1039
+ Given a callback, iterates over each of the relationships in the model,
1040
+ invoking the callback with the name of each relationship and its relationship
1041
+ descriptor.
1042
+ The callback method you provide should have the following signature (all
1043
+ parameters are optional):
1044
+ ```javascript
1045
+ function(name, descriptor);
1046
+ ```
1047
+ - `name` the name of the current property in the iteration
1048
+ - `descriptor` the meta object that describes this relationship
1049
+ The relationship descriptor argument is an object with the following properties.
1050
+ - **name** <span class="type">String</span> the name of this relationship on the Model
1051
+ - **kind** <span class="type">String</span> "hasMany" or "belongsTo"
1052
+ - **options** <span class="type">Object</span> the original options hash passed when the relationship was declared
1053
+ - **parentType** <span class="type">Model</span> the type of the Model that owns this relationship
1054
+ - **type** <span class="type">String</span> the type name of the related Model
1055
+ Note that in addition to a callback, you can also pass an optional target
1056
+ object that will be set as `this` on the context.
1057
+ Example
1058
+ ```js [app/serializers/application.js]
1059
+ import JSONSerializer from '@ember-data/serializer/json';
1060
+ export default class ApplicationSerializer extends JSONSerializer {
1061
+ serialize(record, options) {
1062
+ let json = {};
1063
+ record.eachRelationship(function(name, descriptor) {
1064
+ if (descriptor.kind === 'hasMany') {
1065
+ let serializedHasManyName = name.toUpperCase() + '_IDS';
1066
+ json[serializedHasManyName] = record.get(name).map(r => r.id);
1067
+ }
1068
+ });
1069
+ return json;
1070
+ }
1071
+ }
1072
+ ```
1073
+ @public
1074
+ @param callback the callback to invoke
1075
+ @param binding the value to which the callback's `this` should be bound
1076
+ */
1077
+ eachRelationship(callback, binding) {
1078
+ this.constructor.eachRelationship(callback, binding);
1079
+ }
1080
+ relationshipFor(name) {
1081
+ return this.constructor.relationshipsByName.get(name);
1082
+ }
1083
+ inverseFor(name) {
1084
+ return this.constructor.inverseFor(name, storeFor(this, false));
1085
+ }
1086
+ eachAttribute(callback, binding) {
1087
+ this.constructor.eachAttribute(callback, binding);
1088
+ }
1089
+
1090
+ /**
1091
+ * @internal
1092
+ */
1093
+ static isModel = true;
1094
+
1095
+ /**
1096
+ Represents the model's class name as a string. This can be used to look up the model's class name through
1097
+ `Store`'s modelFor method.
1098
+ `modelName` is generated for you by WarpDrive. It will be a lowercased, dasherized string.
1099
+ For example:
1100
+ ```javascript
1101
+ store.modelFor('post').modelName; // 'post'
1102
+ store.modelFor('blog-post').modelName; // 'blog-post'
1103
+ ```
1104
+ The most common place you'll want to access `modelName` is in your serializer's `payloadKeyFromModelName` method. For example, to change payload
1105
+ keys to underscore (instead of dasherized), you might use the following code:
1106
+ ```javascript
1107
+ import RESTSerializer from '@ember-data/serializer/rest';
1108
+ import { underscore } from '<app-name>/utils/string-utils';
1109
+ export default const PostSerializer = RESTSerializer.extend({
1110
+ payloadKeyFromModelName(modelName) {
1111
+ return underscore(modelName);
1112
+ }
1113
+ });
1114
+ ```
1115
+ @public
1116
+ */
1117
+ static modelName = null;
1118
+
1119
+ /*
1120
+ These class methods below provide relationship
1121
+ introspection abilities about relationships.
1122
+ A note about the computed properties contained here:
1123
+ **These properties are effectively sealed once called for the first time.**
1124
+ To avoid repeatedly doing expensive iteration over a model's fields, these
1125
+ values are computed once and then cached for the remainder of the runtime of
1126
+ your application.
1127
+ If your application needs to modify a class after its initial definition
1128
+ (for example, using `reopen()` to add additional attributes), make sure you
1129
+ do it before using your model with the store, which uses these properties
1130
+ extensively.
1131
+ */
1132
+
1133
+ /**
1134
+ For a given relationship name, returns the model type of the relationship.
1135
+ For example, if you define a model like this:
1136
+ ```js [app/models/post.js]
1137
+ import { Model, hasMany } from '@warp-drive/legacy/model';
1138
+ export default class PostModel extends Model {
1139
+ @hasMany('comment') comments;
1140
+ }
1141
+ ```
1142
+ Calling `store.modelFor('post').typeForRelationship('comments', store)` will return `Comment`.
1143
+ @public
1144
+ @param name the name of the relationship
1145
+ @param store an instance of Store
1146
+ @return the type of the relationship, or undefined
1147
+ */
1148
+ static typeForRelationship(name, store) {
1149
+ const relationship = this.relationshipsByName.get(name);
1150
+ return relationship && store.modelFor(relationship.type);
1151
+ }
1152
+ static get inverseMap() {
1153
+ return Object.create(null);
1154
+ }
1155
+
1156
+ /**
1157
+ Find the relationship which is the inverse of the one asked for.
1158
+ For example, if you define models like this:
1159
+ ```js [app/models/post.js]
1160
+ import { Model, hasMany } from '@warp-drive/legacy/model';
1161
+ export default class PostModel extends Model {
1162
+ @hasMany('message') comments;
1163
+ }
1164
+ ```
1165
+ ```js [app/models/message.js]
1166
+ import { Model, belongsTo } from '@warp-drive/legacy/model';
1167
+ export default class MessageModel extends Model {
1168
+ @belongsTo('post') owner;
1169
+ }
1170
+ ```
1171
+ ``` js
1172
+ store.modelFor('post').inverseFor('comments', store) // { type: 'message', name: 'owner', kind: 'belongsTo' }
1173
+ store.modelFor('message').inverseFor('owner', store) // { type: 'post', name: 'comments', kind: 'hasMany' }
1174
+ ```
1175
+ @public
1176
+ @param name the name of the relationship
1177
+ @param store
1178
+ @return the inverse relationship, or null
1179
+ */
1180
+ static {
1181
+ decorateMethodV2(this, "inverseMap", [computeOnce]);
1182
+ }
1183
+ static inverseFor(name, store) {
1184
+ const inverseMap = this.inverseMap;
1185
+ if (inverseMap[name]) {
1186
+ return inverseMap[name];
1187
+ } else {
1188
+ const inverse = this._findInverseFor(name, store);
1189
+ inverseMap[name] = inverse;
1190
+ return inverse;
1191
+ }
1192
+ }
1193
+
1194
+ //Calculate the inverse, ignoring the cache
1195
+ static _findInverseFor(name, store) {
1196
+ const relationship = this.relationshipsByName.get(name);
1197
+ if (!relationship) {
1198
+ return null;
1199
+ }
1200
+ const {
1201
+ options
1202
+ } = relationship;
1203
+ if (options.inverse === null) {
1204
+ return null;
1205
+ }
1206
+ const schemaExists = store.schema.hasResource(relationship);
1207
+ if (!schemaExists) {
1208
+ return null;
1209
+ }
1210
+ const inverseField = store.schema.fields(relationship).get(options.inverse);
1211
+ return inverseField || null;
1212
+ }
1213
+
1214
+ /**
1215
+ The model's relationships as a map, keyed on the type of the
1216
+ relationship. The value of each entry is an array containing a descriptor
1217
+ for each relationship with that type, describing the name of the relationship
1218
+ as well as the type.
1219
+ For example, given the following model definition:
1220
+ ```js [app/models/blog.js]
1221
+ import { Model, belongsTo, hasMany } from '@warp-drive/legacy/model';
1222
+ export default class BlogModel extends Model {
1223
+ @hasMany('user') users;
1224
+ @belongsTo('user') owner;
1225
+ @hasMany('post') posts;
1226
+ }
1227
+ ```
1228
+ This computed property would return a map describing these
1229
+ relationships, like this:
1230
+ ```javascript
1231
+ import Blog from 'app/models/blog';
1232
+ import User from 'app/models/user';
1233
+ import Post from 'app/models/post';
1234
+ let relationships = Blog.relationships;
1235
+ relationships.user;
1236
+ //=> [ { name: 'users', kind: 'hasMany' },
1237
+ // { name: 'owner', kind: 'belongsTo' } ]
1238
+ relationships.post;
1239
+ //=> [ { name: 'posts', kind: 'hasMany' } ]
1240
+ ```
1241
+ @public
1242
+ */
1243
+ static get relationships() {
1244
+ const map = new Map();
1245
+ const relationshipsByName = this.relationshipsByName;
1246
+
1247
+ // Loop through each computed property on the class
1248
+ relationshipsByName.forEach(desc => {
1249
+ const {
1250
+ type
1251
+ } = desc;
1252
+ if (!map.has(type)) {
1253
+ map.set(type, []);
1254
+ }
1255
+ map.get(type).push(desc);
1256
+ });
1257
+ return map;
1258
+ }
1259
+
1260
+ /**
1261
+ A hash containing lists of the model's relationships, grouped
1262
+ by the relationship kind. For example, given a model with this
1263
+ definition:
1264
+ ```js [app/models/blog.js]
1265
+ import { Model, belongsTo, hasMany } from '@warp-drive/legacy/model';
1266
+ export default class BlogModel extends Model {
1267
+ @hasMany('user') users;
1268
+ @belongsTo('user') owner;
1269
+ @hasMany('post') posts;
1270
+ }
1271
+ ```
1272
+ This property would contain the following:
1273
+ ```javascript
1274
+ import Blog from 'app/models/blog';
1275
+ let relationshipNames = Blog.relationshipNames;
1276
+ relationshipNames.hasMany;
1277
+ //=> ['users', 'posts']
1278
+ relationshipNames.belongsTo;
1279
+ //=> ['owner']
1280
+ ```
1281
+ @public
1282
+ */
1283
+ static {
1284
+ decorateMethodV2(this, "relationships", [computeOnce]);
1285
+ }
1286
+ static get relationshipNames() {
1287
+ const names = {
1288
+ hasMany: [],
1289
+ belongsTo: []
1290
+ };
1291
+ this.eachComputedProperty((name, meta) => {
1292
+ if (isRelationshipSchema(meta)) {
1293
+ names[meta.kind].push(name);
1294
+ }
1295
+ });
1296
+ return names;
1297
+ }
1298
+
1299
+ /**
1300
+ An array of types directly related to a model. Each type will be
1301
+ included once, regardless of the number of relationships it has with
1302
+ the model.
1303
+ For example, given a model with this definition:
1304
+ ```js [app/models/blog.js]
1305
+ import { Model, belongsTo, hasMany } from '@warp-drive/legacy/model';
1306
+ export default class BlogModel extends Model {
1307
+ @hasMany('user') users;
1308
+ @belongsTo('user') owner;
1309
+ @hasMany('post') posts;
1310
+ }
1311
+ ```
1312
+ This property would contain the following:
1313
+ ```javascript
1314
+ import Blog from 'app/models/blog';
1315
+ let relatedTypes = Blog.relatedTypes');
1316
+ //=> ['user', 'post']
1317
+ ```
1318
+ @public
1319
+ */
1320
+ static {
1321
+ decorateMethodV2(this, "relationshipNames", [computeOnce]);
1322
+ }
1323
+ static get relatedTypes() {
1324
+ const types = [];
1325
+ const rels = this.relationshipsObject;
1326
+ const relationships = Object.keys(rels);
1327
+
1328
+ // create an array of the unique types involved
1329
+ // in relationships
1330
+ for (let i = 0; i < relationships.length; i++) {
1331
+ const name = relationships[i];
1332
+ const meta = rels[name];
1333
+ const modelName = meta.type;
1334
+ if (!types.includes(modelName)) {
1335
+ types.push(modelName);
1336
+ }
1337
+ }
1338
+ return types;
1339
+ }
1340
+
1341
+ /**
1342
+ A map whose keys are the relationships of a model and whose values are
1343
+ relationship descriptors.
1344
+ For example, given a model with this
1345
+ definition:
1346
+ ```js [app/models/blog.js]
1347
+ import { Model, belongsTo, hasMany } from '@warp-drive/legacy/model';
1348
+ export default class BlogModel extends Model {
1349
+ @hasMany('user') users;
1350
+ @belongsTo('user') owner;
1351
+ @hasMany('post') posts;
1352
+ }
1353
+ ```
1354
+ This property would contain the following:
1355
+ ```javascript
1356
+ import Blog from 'app/models/blog';
1357
+ let relationshipsByName = Blog.relationshipsByName;
1358
+ relationshipsByName.users;
1359
+ //=> { name: 'users', kind: 'hasMany', type: 'user', options: Object }
1360
+ relationshipsByName.owner;
1361
+ //=> { name: 'owner', kind: 'belongsTo', type: 'user', options: Object }
1362
+ ```
1363
+ @public
1364
+ */
1365
+ static {
1366
+ decorateMethodV2(this, "relatedTypes", [computeOnce]);
1367
+ }
1368
+ static get relationshipsByName() {
1369
+ const map = new Map();
1370
+ const rels = this.relationshipsObject;
1371
+ const relationships = Object.keys(rels);
1372
+ for (let i = 0; i < relationships.length; i++) {
1373
+ const name = relationships[i];
1374
+ const value = rels[name];
1375
+ map.set(value.name, value);
1376
+ }
1377
+ return map;
1378
+ }
1379
+ static {
1380
+ decorateMethodV2(this, "relationshipsByName", [computeOnce]);
1381
+ }
1382
+ static get relationshipsObject() {
1383
+ const relationships = Object.create(null);
1384
+ this.modelName;
1385
+ this.eachComputedProperty((name, meta) => {
1386
+ if (!isRelationshipSchema(meta)) {
1387
+ return;
1388
+ }
1389
+ // TODO deprecate key being here
1390
+ meta.key = name;
1391
+ meta.name = name;
1392
+ relationships[name] = meta;
1393
+ });
1394
+ return relationships;
1395
+ }
1396
+
1397
+ /**
1398
+ A map whose keys are the fields of the model and whose values are strings
1399
+ describing the kind of the field. A model's fields are the union of all of its
1400
+ attributes and relationships.
1401
+ For example:
1402
+ ```js [app/models/blog.js]
1403
+ import { Model, attr, belongsTo, hasMany } from '@warp-drive/legacy/model';
1404
+ export default class BlogModel extends Model {
1405
+ @hasMany('user') users;
1406
+ @belongsTo('user') owner;
1407
+ @hasMany('post') posts;
1408
+ @attr('string') title;
1409
+ }
1410
+ ```
1411
+ ```js
1412
+ import Blog from 'app/models/blog'
1413
+ let fields = Blog.fields;
1414
+ fields.forEach(function(kind, field) {
1415
+ // do thing
1416
+ });
1417
+ // prints:
1418
+ // users, hasMany
1419
+ // owner, belongsTo
1420
+ // posts, hasMany
1421
+ // title, attribute
1422
+ ```
1423
+ @public
1424
+ */
1425
+ static {
1426
+ decorateMethodV2(this, "relationshipsObject", [computeOnce]);
1427
+ }
1428
+ static get fields() {
1429
+ const map = new Map();
1430
+ this.eachComputedProperty((name, meta) => {
1431
+ if (isRelationshipSchema(meta)) {
1432
+ map.set(name, meta.kind);
1433
+ } else if (isAttributeSchema(meta)) {
1434
+ map.set(name, 'attribute');
1435
+ }
1436
+ });
1437
+ return map;
1438
+ }
1439
+
1440
+ /**
1441
+ Given a callback, iterates over each of the relationships in the model,
1442
+ invoking the callback with the name of each relationship and its relationship
1443
+ descriptor.
1444
+ @public
1445
+ @param callback the callback to invoke
1446
+ @param binding the value to which the callback's `this` should be bound
1447
+ */
1448
+ static {
1449
+ decorateMethodV2(this, "fields", [computeOnce]);
1450
+ }
1451
+ static eachRelationship(callback, binding) {
1452
+ this.relationshipsByName.forEach((relationship, name) => {
1453
+ callback.call(binding, name, relationship);
1454
+ });
1455
+ }
1456
+
1457
+ /**
1458
+ Given a callback, iterates over each of the types related to a model,
1459
+ invoking the callback with the related type's class. Each type will be
1460
+ returned just once, regardless of how many different relationships it has
1461
+ with a model.
1462
+ @public
1463
+ @param callback the callback to invoke
1464
+ @param binding the value to which the callback's `this` should be bound
1465
+ */
1466
+ static eachRelatedType(callback, binding) {
1467
+ const relationshipTypes = this.relatedTypes;
1468
+ for (let i = 0; i < relationshipTypes.length; i++) {
1469
+ const type = relationshipTypes[i];
1470
+ callback.call(binding, type);
1471
+ }
1472
+ }
1473
+
1474
+ /**
1475
+ *
1476
+ * @private
1477
+ * @deprecated
1478
+ */
1479
+ static determineRelationshipType(knownSide, store) {
1480
+ const knownKey = knownSide.name;
1481
+ const knownKind = knownSide.kind;
1482
+ const inverse = this.inverseFor(knownKey, store);
1483
+ // let key;
1484
+
1485
+ if (!inverse) {
1486
+ return knownKind === 'belongsTo' ? 'oneToNone' : 'manyToNone';
1487
+ }
1488
+
1489
+ // key = inverse.name;
1490
+ const otherKind = inverse.kind;
1491
+ if (otherKind === 'belongsTo') {
1492
+ return knownKind === 'belongsTo' ? 'oneToOne' : 'manyToOne';
1493
+ } else {
1494
+ return knownKind === 'belongsTo' ? 'oneToMany' : 'manyToMany';
1495
+ }
1496
+ }
1497
+
1498
+ /**
1499
+ A map whose keys are the attributes of the model (properties
1500
+ described by attr) and whose values are the meta object for the
1501
+ property.
1502
+ Example
1503
+ ```js [app/models/person.js]
1504
+ import { Model, attr } from '@warp-drive/legacy/model';
1505
+ export default class PersonModel extends Model {
1506
+ @attr('string') firstName;
1507
+ @attr('string') lastName;
1508
+ @attr('date') birthday;
1509
+ }
1510
+ ```
1511
+ ```javascript
1512
+ import Person from 'app/models/person'
1513
+ let attributes = Person.attributes
1514
+ attributes.forEach(function(meta, name) {
1515
+ // do thing
1516
+ });
1517
+ // prints:
1518
+ // firstName {type: "string", kind: 'attribute', options: Object, parentType: function, name: "firstName"}
1519
+ // lastName {type: "string", kind: 'attribute', options: Object, parentType: function, name: "lastName"}
1520
+ // birthday {type: "date", kind: 'attribute', options: Object, parentType: function, name: "birthday"}
1521
+ ```
1522
+ @public
1523
+ */
1524
+ static get attributes() {
1525
+ const map = new Map();
1526
+ this.eachComputedProperty((name, meta) => {
1527
+ if (isAttributeSchema(meta)) {
1528
+ // TODO deprecate key being here
1529
+ meta.key = name;
1530
+ meta.name = name;
1531
+ map.set(name, meta);
1532
+ }
1533
+ });
1534
+ return map;
1535
+ }
1536
+
1537
+ /**
1538
+ A map whose keys are the attributes of the model (properties
1539
+ described by attr) and whose values are type of transformation
1540
+ applied to each attribute. This map does not include any
1541
+ attributes that do not have an transformation type.
1542
+ Example
1543
+ ```js [app/models/person.js]
1544
+ import { Model, attr } from '@warp-drive/legacy/model';
1545
+ export default class PersonModel extends Model {
1546
+ @attr firstName;
1547
+ @attr('string') lastName;
1548
+ @attr('date') birthday;
1549
+ }
1550
+ ```
1551
+ ```javascript
1552
+ import Person from 'app/models/person';
1553
+ let transformedAttributes = Person.transformedAttributes
1554
+ transformedAttributes.forEach(function(field, type) {
1555
+ // do thing
1556
+ });
1557
+ // prints:
1558
+ // lastName string
1559
+ // birthday date
1560
+ ```
1561
+ @public
1562
+ */
1563
+ static {
1564
+ decorateMethodV2(this, "attributes", [computeOnce]);
1565
+ }
1566
+ static get transformedAttributes() {
1567
+ const map = new Map();
1568
+ this.eachAttribute((name, meta) => {
1569
+ if (meta.type) {
1570
+ map.set(name, meta.type);
1571
+ }
1572
+ });
1573
+ return map;
1574
+ }
1575
+
1576
+ /**
1577
+ Iterates through the attributes of the model, calling the passed function on each
1578
+ attribute.
1579
+ The callback method you provide should have the following signature (all
1580
+ parameters are optional):
1581
+ ```javascript
1582
+ function(name, meta);
1583
+ ```
1584
+ - `name` the name of the current property in the iteration
1585
+ - `meta` the meta object for the attribute property in the iteration
1586
+ Note that in addition to a callback, you can also pass an optional target
1587
+ object that will be set as `this` on the context.
1588
+ Example
1589
+ ```javascript
1590
+ import { Model, attr } from '@warp-drive/legacy/model';
1591
+ class PersonModel extends Model {
1592
+ @attr('string') firstName;
1593
+ @attr('string') lastName;
1594
+ @attr('date') birthday;
1595
+ }
1596
+ PersonModel.eachAttribute(function(name, meta) {
1597
+ // do thing
1598
+ });
1599
+ // prints:
1600
+ // firstName {type: "string", kind: 'attribute', options: Object, parentType: function, name: "firstName"}
1601
+ // lastName {type: "string", kind: 'attribute', options: Object, parentType: function, name: "lastName"}
1602
+ // birthday {type: "date", kind: 'attribute', options: Object, parentType: function, name: "birthday"}
1603
+ ```
1604
+ @public
1605
+ @param callback The callback to execute
1606
+ @param binding [optional] the value to which the callback's `this` should be bound
1607
+ */
1608
+ static {
1609
+ decorateMethodV2(this, "transformedAttributes", [computeOnce]);
1610
+ }
1611
+ static eachAttribute(callback, binding) {
1612
+ this.attributes.forEach((meta, name) => {
1613
+ callback.call(binding, name, meta);
1614
+ });
1615
+ }
1616
+
1617
+ /**
1618
+ Iterates through the transformedAttributes of the model, calling
1619
+ the passed function on each attribute. Note the callback will not be
1620
+ called for any attributes that do not have an transformation type.
1621
+ The callback method you provide should have the following signature (all
1622
+ parameters are optional):
1623
+ ```javascript
1624
+ function(name, type);
1625
+ ```
1626
+ - `name` the name of the current property in the iteration
1627
+ - `type` a string containing the name of the type of transformed
1628
+ applied to the attribute
1629
+ Note that in addition to a callback, you can also pass an optional target
1630
+ object that will be set as `this` on the context.
1631
+ Example
1632
+ ```javascript
1633
+ import { Model, attr } from '@warp-drive/legacy/model';
1634
+ let Person = Model.extend({
1635
+ firstName: attr(),
1636
+ lastName: attr('string'),
1637
+ birthday: attr('date')
1638
+ });
1639
+ Person.eachTransformedAttribute(function(name, type) {
1640
+ // do thing
1641
+ });
1642
+ // prints:
1643
+ // lastName string
1644
+ // birthday date
1645
+ ```
1646
+ @public
1647
+ @param callback The callback to execute
1648
+ @param binding [optional] the value to which the callback's `this` should be bound
1649
+ */
1650
+ static eachTransformedAttribute(callback, binding) {
1651
+ this.transformedAttributes.forEach((type, name) => {
1652
+ callback.call(binding, name, type);
1653
+ });
1654
+ }
1655
+
1656
+ /**
1657
+ Returns the name of the model class.
1658
+ @public
1659
+ */
1660
+ static toString() {
1661
+ return `model:${this.modelName}`;
1662
+ }
1663
+ }
1664
+
1665
+ // @ts-expect-error TS doesn't know how to do `this` function overloads
1666
+ Model.prototype.save = save;
1667
+ // @ts-expect-error TS doesn't know how to do `this` function overloads
1668
+ Model.prototype.destroyRecord = destroyRecord;
1669
+ Model.prototype.unloadRecord = unloadRecord;
1670
+ Model.prototype.hasMany = hasMany;
1671
+ Model.prototype.belongsTo = belongsTo;
1672
+ Model.prototype.serialize = serialize;
1673
+ Model.prototype._createSnapshot = createSnapshot;
1674
+ Model.prototype.deleteRecord = deleteRecord;
1675
+ Model.prototype.changedAttributes = changedAttributes;
1676
+ Model.prototype.rollbackAttributes = rollbackAttributes;
1677
+ Model.prototype.reload = reload;
1678
+ defineGate(Model.prototype, 'isReloading', {
1679
+ get() {
1680
+ return this._isReloading ?? false;
1681
+ },
1682
+ set(v) {
1683
+ this._isReloading = v;
1684
+ },
1685
+ configurable: true,
1686
+ // @ts-expect-error specially handled prop
1687
+ isLocal: true
1688
+ });
1689
+ function restoreDeprecatedModelRequestBehaviors(ModelKlass) {
1690
+ // @ts-expect-error TS doesn't know how to do `this` function overloads
1691
+ ModelKlass.prototype.save = _save;
1692
+ // @ts-expect-error TS doesn't know how to do `this` function overloads
1693
+ ModelKlass.prototype.destroyRecord = _destroyRecord;
1694
+ ModelKlass.prototype.reload = _reload;
1695
+ defineGate(Model.prototype, 'isReloading', {
1696
+ get() {
1697
+ return this._isReloading ?? false;
1698
+ },
1699
+ set(v) {
1700
+ this._isReloading = v;
1701
+ },
1702
+ // @ts-expect-error specially handled prop
1703
+ isLocal: true
1704
+ });
1705
+ }
1706
+
1707
+ // this is required to prevent `init` from passing
1708
+ // the values initialized during create to `setUnknownProperty`
1709
+ Model.prototype._createProps = null;
1710
+ Model.prototype._secretInit = null;
1711
+ function isRelationshipSchema(meta) {
1712
+ const hasKind = typeof meta === 'object' && meta !== null && 'kind' in meta && 'options' in meta;
1713
+ return hasKind && (meta.kind === 'hasMany' || meta.kind === 'belongsTo');
1714
+ }
1715
+ function isAttributeSchema(meta) {
1716
+ return typeof meta === 'object' && meta !== null && 'kind' in meta && meta.kind === 'attribute';
1717
+ }
1718
+
1719
+ /*
1720
+ In case someone defined a relationship to a mixin, for example:
1721
+ ```ts
1722
+ class CommentModel extends Model {
1723
+ @belongsTo('commentable', { polymorphic: true }) owner;
1724
+ }
1725
+
1726
+ let Commentable = Mixin.create({
1727
+ @hasMany('comment') comments;
1728
+ });
1729
+ ```
1730
+ we want to look up a Commentable class which has all the necessary
1731
+ relationship meta data. Thus, we look up the mixin and create a mock
1732
+ Model, so we can access the relationship CPs of the mixin (`comments`)
1733
+ in this case
1734
+ */
1735
+ function modelForMixin(store, normalizedModelName) {
1736
+ const owner = getOwner(store);
1737
+ const MaybeMixin = owner.factoryFor(`mixin:${normalizedModelName}`);
1738
+ const mixin = MaybeMixin && MaybeMixin.class;
1739
+ if (mixin) {
1740
+ const ModelForMixin = Model.extend(mixin);
1741
+ ModelForMixin.__isMixin = true;
1742
+ ModelForMixin.__mixin = mixin;
1743
+ //Cache the class as a model
1744
+ owner.register(`model:${normalizedModelName}`, ModelForMixin);
1745
+ }
1746
+ return owner.factoryFor(`model:${normalizedModelName}`);
1747
+ }
1748
+ class ModelSchemaProvider {
1749
+ /** @internal */
1750
+
1751
+ /** @internal */
1752
+
1753
+ /** @internal */
1754
+
1755
+ constructor(store) {
1756
+ this.store = store;
1757
+ this._schemas = new Map();
1758
+ this._typeMisses = new Set();
1759
+ }
1760
+ resourceTypes() {
1761
+ return Array.from(this._schemas.keys());
1762
+ }
1763
+ hasTrait(type) {
1764
+ return false;
1765
+ }
1766
+ resourceHasTrait(resource, trait) {
1767
+ return false;
1768
+ }
1769
+ transformation(field) {}
1770
+ derivation(field) {}
1771
+ hashFn(field) {}
1772
+ resource(resource) {
1773
+ const type = normalizeModelName(resource.type);
1774
+ if (!this._schemas.has(type)) {
1775
+ this._loadModelSchema(type);
1776
+ }
1777
+ return this._schemas.get(type).schema;
1778
+ }
1779
+ registerResources(schemas) {}
1780
+ registerResource(schema) {}
1781
+ registerTransformation(transform) {}
1782
+ registerDerivation(derivation) {}
1783
+ registerHashFn(hashFn) {}
1784
+ /** @internal */
1785
+ _loadModelSchema(type) {
1786
+ const modelClass = this.store.modelFor(type);
1787
+ const attributeMap = modelClass.attributes;
1788
+ const attributes = Object.create(null);
1789
+ attributeMap.forEach((meta, name) => attributes[name] = meta);
1790
+ const relationships = modelClass.relationshipsObject || null;
1791
+ const fields = new Map();
1792
+ for (const attr of Object.values(attributes)) {
1793
+ fields.set(attr.name, attr);
1794
+ }
1795
+ for (const rel of Object.values(relationships)) {
1796
+ fields.set(rel.name, rel);
1797
+ }
1798
+ const schema = {
1799
+ legacy: true,
1800
+ identity: {
1801
+ name: 'id',
1802
+ kind: '@id'
1803
+ },
1804
+ type,
1805
+ fields: Array.from(fields.values())
1806
+ };
1807
+ const internalSchema = {
1808
+ schema,
1809
+ attributes,
1810
+ relationships,
1811
+ fields
1812
+ };
1813
+ this._schemas.set(type, internalSchema);
1814
+ return internalSchema;
1815
+ }
1816
+ fields(resource) {
1817
+ const type = normalizeModelName(resource.type);
1818
+ if (!this._schemas.has(type)) {
1819
+ this._loadModelSchema(type);
1820
+ }
1821
+ return this._schemas.get(type).fields;
1822
+ }
1823
+ hasResource(resource) {
1824
+ const type = normalizeModelName(resource.type);
1825
+ if (this._schemas.has(type)) {
1826
+ return true;
1827
+ }
1828
+ if (this._typeMisses.has(type)) {
1829
+ return false;
1830
+ }
1831
+ const factory = getModelFactory(this.store, type);
1832
+ const exists = factory !== null;
1833
+ if (!exists) {
1834
+ this._typeMisses.add(type);
1835
+ return false;
1836
+ }
1837
+ return true;
1838
+ }
1839
+ }
1840
+ {
1841
+ ModelSchemaProvider.prototype.doesTypeExist = function (type) {
1842
+ return this.hasResource({
1843
+ type
1844
+ });
1845
+ };
1846
+ ModelSchemaProvider.prototype.attributesDefinitionFor = function (resource) {
1847
+ const type = normalizeModelName(resource.type);
1848
+
1849
+ // @ts-expect-error intentional use of internal API
1850
+ if (!this._schemas.has(type)) {
1851
+ // @ts-expect-error intentional use of internal API
1852
+ this._loadModelSchema(type);
1853
+ }
1854
+
1855
+ // @ts-expect-error intentional use of internal API
1856
+ return this._schemas.get(type).attributes;
1857
+ };
1858
+ ModelSchemaProvider.prototype.relationshipsDefinitionFor = function (resource) {
1859
+ const type = normalizeModelName(resource.type);
1860
+
1861
+ // @ts-expect-error intentional use of internal API
1862
+ if (!this._schemas.has(type)) {
1863
+ // @ts-expect-error intentional use of internal API
1864
+ this._loadModelSchema(type);
1865
+ }
1866
+
1867
+ // @ts-expect-error intentional use of internal API
1868
+ return this._schemas.get(type).relationships;
1869
+ };
1870
+ }
1871
+ function buildSchema(store) {
1872
+ return new ModelSchemaProvider(store);
1873
+ }
1874
+ function getModelFactory(store, type) {
1875
+ if (!store._modelFactoryCache) {
1876
+ store._modelFactoryCache = Object.create(null);
1877
+ }
1878
+ const cache = store._modelFactoryCache;
1879
+ let factory = cache[type];
1880
+ if (!factory) {
1881
+ const owner = getOwner(store);
1882
+ factory = owner.factoryFor(`model:${type}`);
1883
+ if (!factory) {
1884
+ //Support looking up mixins as base types for polymorphic relationships
1885
+ factory = modelForMixin(store, type);
1886
+ }
1887
+ if (!factory) {
1888
+ // we don't cache misses in case someone wants to register a missing model
1889
+ return null;
1890
+ }
1891
+ const klass = factory.class;
1892
+ if (klass.isModel) {
1893
+ const hasOwnModelNameSet = klass.modelName && Object.prototype.hasOwnProperty.call(klass, 'modelName');
1894
+ if (!hasOwnModelNameSet) {
1895
+ Object.defineProperty(klass, 'modelName', {
1896
+ value: type
1897
+ });
1898
+ }
1899
+ }
1900
+ cache[type] = factory;
1901
+ }
1902
+ return factory;
1903
+ }
1904
+ export { Model as M, RecordState as R, _save as _, save as a, buildSchema as b, _reload as c, reload as d, _destroyRecord as e, destroyRecord as f, getModelFactory as g, hasMany as h, deleteRecord as i, changedAttributes as j, belongsTo as k, createSnapshot as l, restoreDeprecatedModelRequestBehaviors as m, rollbackAttributes as r, serialize as s, unloadRecord as u };