@warp-drive/legacy 5.6.0-alpha.11

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 (211) hide show
  1. package/CHANGELOG.md +1 -0
  2. package/LICENSE.md +23 -0
  3. package/README.md +54 -0
  4. package/addon-main.cjs +5 -0
  5. package/declarations/adapter/-private/build-url-mixin.d.ts +34 -0
  6. package/declarations/adapter/-private/build-url-mixin.d.ts.map +1 -0
  7. package/declarations/adapter/-private/fastboot-interface.d.ts +9 -0
  8. package/declarations/adapter/-private/fastboot-interface.d.ts.map +1 -0
  9. package/declarations/adapter/-private/utils/continue-on-reject.d.ts +8 -0
  10. package/declarations/adapter/-private/utils/continue-on-reject.d.ts.map +1 -0
  11. package/declarations/adapter/-private/utils/determine-body-promise.d.ts +5 -0
  12. package/declarations/adapter/-private/utils/determine-body-promise.d.ts.map +1 -0
  13. package/declarations/adapter/-private/utils/fetch.d.ts +9 -0
  14. package/declarations/adapter/-private/utils/fetch.d.ts.map +1 -0
  15. package/declarations/adapter/-private/utils/parse-response-headers.d.ts +2 -0
  16. package/declarations/adapter/-private/utils/parse-response-headers.d.ts.map +1 -0
  17. package/declarations/adapter/-private/utils/serialize-into-hash.d.ts +7 -0
  18. package/declarations/adapter/-private/utils/serialize-into-hash.d.ts.map +1 -0
  19. package/declarations/adapter/-private/utils/serialize-query-params.d.ts +6 -0
  20. package/declarations/adapter/-private/utils/serialize-query-params.d.ts.map +1 -0
  21. package/declarations/adapter/-private.d.ts +6 -0
  22. package/declarations/adapter/-private.d.ts.map +1 -0
  23. package/declarations/adapter/error.d.ts +188 -0
  24. package/declarations/adapter/error.d.ts.map +1 -0
  25. package/declarations/adapter/json-api.d.ts +234 -0
  26. package/declarations/adapter/json-api.d.ts.map +1 -0
  27. package/declarations/adapter/rest.d.ts +823 -0
  28. package/declarations/adapter/rest.d.ts.map +1 -0
  29. package/declarations/adapter.d.ts +801 -0
  30. package/declarations/adapter.d.ts.map +1 -0
  31. package/declarations/compat/-private.d.ts +14 -0
  32. package/declarations/compat/-private.d.ts.map +1 -0
  33. package/declarations/compat/builders/find-all.d.ts +33 -0
  34. package/declarations/compat/builders/find-all.d.ts.map +1 -0
  35. package/declarations/compat/builders/find-record.d.ts +54 -0
  36. package/declarations/compat/builders/find-record.d.ts.map +1 -0
  37. package/declarations/compat/builders/query.d.ts +61 -0
  38. package/declarations/compat/builders/query.d.ts.map +1 -0
  39. package/declarations/compat/builders/save-record.d.ts +32 -0
  40. package/declarations/compat/builders/save-record.d.ts.map +1 -0
  41. package/declarations/compat/builders/utils.d.ts +4 -0
  42. package/declarations/compat/builders/utils.d.ts.map +1 -0
  43. package/declarations/compat/builders.d.ts +15 -0
  44. package/declarations/compat/builders.d.ts.map +1 -0
  45. package/declarations/compat/legacy-network-handler/fetch-manager.d.ts +47 -0
  46. package/declarations/compat/legacy-network-handler/fetch-manager.d.ts.map +1 -0
  47. package/declarations/compat/legacy-network-handler/identifier-has-id.d.ts +3 -0
  48. package/declarations/compat/legacy-network-handler/identifier-has-id.d.ts.map +1 -0
  49. package/declarations/compat/legacy-network-handler/legacy-data-fetch.d.ts +12 -0
  50. package/declarations/compat/legacy-network-handler/legacy-data-fetch.d.ts.map +1 -0
  51. package/declarations/compat/legacy-network-handler/legacy-data-utils.d.ts +6 -0
  52. package/declarations/compat/legacy-network-handler/legacy-data-utils.d.ts.map +1 -0
  53. package/declarations/compat/legacy-network-handler/legacy-network-handler.d.ts +3 -0
  54. package/declarations/compat/legacy-network-handler/legacy-network-handler.d.ts.map +1 -0
  55. package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts +527 -0
  56. package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts.map +1 -0
  57. package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +224 -0
  58. package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts.map +1 -0
  59. package/declarations/compat/legacy-network-handler/serializer-response.d.ts +7 -0
  60. package/declarations/compat/legacy-network-handler/serializer-response.d.ts.map +1 -0
  61. package/declarations/compat/legacy-network-handler/snapshot-record-array.d.ts +89 -0
  62. package/declarations/compat/legacy-network-handler/snapshot-record-array.d.ts.map +1 -0
  63. package/declarations/compat/legacy-network-handler/snapshot.d.ts +237 -0
  64. package/declarations/compat/legacy-network-handler/snapshot.d.ts.map +1 -0
  65. package/declarations/compat/utils.d.ts +138 -0
  66. package/declarations/compat/utils.d.ts.map +1 -0
  67. package/declarations/compat.d.ts +134 -0
  68. package/declarations/compat.d.ts.map +1 -0
  69. package/declarations/model/-private/attr.d.ts +165 -0
  70. package/declarations/model/-private/attr.d.ts.map +1 -0
  71. package/declarations/model/-private/attr.type-test.d.ts +2 -0
  72. package/declarations/model/-private/attr.type-test.d.ts.map +1 -0
  73. package/declarations/model/-private/belongs-to.d.ts +173 -0
  74. package/declarations/model/-private/belongs-to.d.ts.map +1 -0
  75. package/declarations/model/-private/belongs-to.type-test.d.ts +2 -0
  76. package/declarations/model/-private/belongs-to.type-test.d.ts.map +1 -0
  77. package/declarations/model/-private/debug/assert-polymorphic-type.d.ts +6 -0
  78. package/declarations/model/-private/debug/assert-polymorphic-type.d.ts.map +1 -0
  79. package/declarations/model/-private/errors.d.ts +290 -0
  80. package/declarations/model/-private/errors.d.ts.map +1 -0
  81. package/declarations/model/-private/has-many.d.ts +164 -0
  82. package/declarations/model/-private/has-many.d.ts.map +1 -0
  83. package/declarations/model/-private/has-many.type-test.d.ts +2 -0
  84. package/declarations/model/-private/has-many.type-test.d.ts.map +1 -0
  85. package/declarations/model/-private/hooks.d.ts +11 -0
  86. package/declarations/model/-private/hooks.d.ts.map +1 -0
  87. package/declarations/model/-private/legacy-relationships-support.d.ts +57 -0
  88. package/declarations/model/-private/legacy-relationships-support.d.ts.map +1 -0
  89. package/declarations/model/-private/model-for-mixin.d.ts +4 -0
  90. package/declarations/model/-private/model-for-mixin.d.ts.map +1 -0
  91. package/declarations/model/-private/model-methods.d.ts +35 -0
  92. package/declarations/model/-private/model-methods.d.ts.map +1 -0
  93. package/declarations/model/-private/model.d.ts +1270 -0
  94. package/declarations/model/-private/model.d.ts.map +1 -0
  95. package/declarations/model/-private/model.type-test.d.ts +2 -0
  96. package/declarations/model/-private/model.type-test.d.ts.map +1 -0
  97. package/declarations/model/-private/notify-changes.d.ts +5 -0
  98. package/declarations/model/-private/notify-changes.d.ts.map +1 -0
  99. package/declarations/model/-private/promise-belongs-to.d.ts +40 -0
  100. package/declarations/model/-private/promise-belongs-to.d.ts.map +1 -0
  101. package/declarations/model/-private/promise-many-array.d.ts +126 -0
  102. package/declarations/model/-private/promise-many-array.d.ts.map +1 -0
  103. package/declarations/model/-private/promise-proxy-base.d.ts +3 -0
  104. package/declarations/model/-private/promise-proxy-base.d.ts.map +1 -0
  105. package/declarations/model/-private/record-state.d.ts +78 -0
  106. package/declarations/model/-private/record-state.d.ts.map +1 -0
  107. package/declarations/model/-private/references/belongs-to.d.ts +495 -0
  108. package/declarations/model/-private/references/belongs-to.d.ts.map +1 -0
  109. package/declarations/model/-private/references/has-many.d.ts +504 -0
  110. package/declarations/model/-private/references/has-many.d.ts.map +1 -0
  111. package/declarations/model/-private/schema-provider.d.ts +63 -0
  112. package/declarations/model/-private/schema-provider.d.ts.map +1 -0
  113. package/declarations/model/-private/type-utils.d.ts +57 -0
  114. package/declarations/model/-private/type-utils.d.ts.map +1 -0
  115. package/declarations/model/-private/util.d.ts +6 -0
  116. package/declarations/model/-private/util.d.ts.map +1 -0
  117. package/declarations/model/-private.d.ts +8 -0
  118. package/declarations/model/-private.d.ts.map +1 -0
  119. package/declarations/model/migration-support.d.ts +287 -0
  120. package/declarations/model/migration-support.d.ts.map +1 -0
  121. package/declarations/model/migration-support.type-test.d.ts +2 -0
  122. package/declarations/model/migration-support.type-test.d.ts.map +1 -0
  123. package/declarations/model.d.ts +50 -0
  124. package/declarations/model.d.ts.map +1 -0
  125. package/declarations/serializer/-private/embedded-records-mixin.d.ts +97 -0
  126. package/declarations/serializer/-private/embedded-records-mixin.d.ts.map +1 -0
  127. package/declarations/serializer/-private/transforms/boolean.d.ts +47 -0
  128. package/declarations/serializer/-private/transforms/boolean.d.ts.map +1 -0
  129. package/declarations/serializer/-private/transforms/boolean.type-test.d.ts +2 -0
  130. package/declarations/serializer/-private/transforms/boolean.type-test.d.ts.map +1 -0
  131. package/declarations/serializer/-private/transforms/date.d.ts +28 -0
  132. package/declarations/serializer/-private/transforms/date.d.ts.map +1 -0
  133. package/declarations/serializer/-private/transforms/number.d.ts +29 -0
  134. package/declarations/serializer/-private/transforms/number.d.ts.map +1 -0
  135. package/declarations/serializer/-private/transforms/string.d.ts +29 -0
  136. package/declarations/serializer/-private/transforms/string.d.ts.map +1 -0
  137. package/declarations/serializer/-private/transforms/transform.d.ts +119 -0
  138. package/declarations/serializer/-private/transforms/transform.d.ts.map +1 -0
  139. package/declarations/serializer/-private/utils.d.ts +4 -0
  140. package/declarations/serializer/-private/utils.d.ts.map +1 -0
  141. package/declarations/serializer/json-api.d.ts +496 -0
  142. package/declarations/serializer/json-api.d.ts.map +1 -0
  143. package/declarations/serializer/json.d.ts +1047 -0
  144. package/declarations/serializer/json.d.ts.map +1 -0
  145. package/declarations/serializer/rest.d.ts +554 -0
  146. package/declarations/serializer/rest.d.ts.map +1 -0
  147. package/declarations/serializer/transform.d.ts +6 -0
  148. package/declarations/serializer/transform.d.ts.map +1 -0
  149. package/declarations/serializer.d.ts +258 -0
  150. package/declarations/serializer.d.ts.map +1 -0
  151. package/dist/-private-DFfBszo5.js +1182 -0
  152. package/dist/-private-DFfBszo5.js.map +1 -0
  153. package/dist/adapter/-private.js +1 -0
  154. package/dist/adapter/-private.js.map +1 -0
  155. package/dist/adapter/error.js +254 -0
  156. package/dist/adapter/error.js.map +1 -0
  157. package/dist/adapter/json-api.js +129 -0
  158. package/dist/adapter/json-api.js.map +1 -0
  159. package/dist/adapter/rest.js +1262 -0
  160. package/dist/adapter/rest.js.map +1 -0
  161. package/dist/adapter.js +1284 -0
  162. package/dist/adapter.js.map +1 -0
  163. package/dist/compat/-private.js +1 -0
  164. package/dist/compat/-private.js.map +1 -0
  165. package/dist/compat/builders.js +292 -0
  166. package/dist/compat/builders.js.map +1 -0
  167. package/dist/compat/utils.js +225 -0
  168. package/dist/compat/utils.js.map +1 -0
  169. package/dist/compat.js +999 -0
  170. package/dist/compat.js.map +1 -0
  171. package/dist/errors-D74uk36r.js +2541 -0
  172. package/dist/errors-D74uk36r.js.map +1 -0
  173. package/dist/json-BCH3fil7.js +1349 -0
  174. package/dist/json-BCH3fil7.js.map +1 -0
  175. package/dist/model/-private.js +2 -0
  176. package/dist/model/-private.js.map +1 -0
  177. package/dist/model/migration-support.js +453 -0
  178. package/dist/model/migration-support.js.map +1 -0
  179. package/dist/model.js +736 -0
  180. package/dist/model.js.map +1 -0
  181. package/dist/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  182. package/dist/runtime-BPCpkOf1-BKOwiRJp.js.map +1 -0
  183. package/dist/schema-provider-CXFLTMjg.js +2228 -0
  184. package/dist/schema-provider-CXFLTMjg.js.map +1 -0
  185. package/dist/serialize-into-hash-BxfqWC8u.js +260 -0
  186. package/dist/serialize-into-hash-BxfqWC8u.js.map +1 -0
  187. package/dist/serializer/json-api.js +514 -0
  188. package/dist/serializer/json-api.js.map +1 -0
  189. package/dist/serializer/json.js +6 -0
  190. package/dist/serializer/json.js.map +1 -0
  191. package/dist/serializer/rest.js +1245 -0
  192. package/dist/serializer/rest.js.map +1 -0
  193. package/dist/serializer/transform.js +313 -0
  194. package/dist/serializer/transform.js.map +1 -0
  195. package/dist/serializer.js +252 -0
  196. package/dist/serializer.js.map +1 -0
  197. package/logos/NCC-1701-a-blue.svg +4 -0
  198. package/logos/NCC-1701-a-gold.svg +4 -0
  199. package/logos/NCC-1701-a-gold_100.svg +1 -0
  200. package/logos/NCC-1701-a-gold_base-64.txt +1 -0
  201. package/logos/NCC-1701-a.svg +4 -0
  202. package/logos/README.md +4 -0
  203. package/logos/docs-badge.svg +2 -0
  204. package/logos/ember-data-logo-dark.svg +12 -0
  205. package/logos/ember-data-logo-light.svg +12 -0
  206. package/logos/github-header.svg +444 -0
  207. package/logos/social1.png +0 -0
  208. package/logos/social2.png +0 -0
  209. package/logos/warp-drive-logo-dark.svg +4 -0
  210. package/logos/warp-drive-logo-gold.svg +4 -0
  211. package/package.json +72 -0
package/dist/model.js ADDED
@@ -0,0 +1,736 @@
1
+ import { computed } from '@ember/object';
2
+ import { recordIdentifierFor } from '@warp-drive/core';
3
+ import { peekCache, setRecordIdentifier, StoreMap, setCacheFor } from '@warp-drive/core/store/-private';
4
+ import { j as isElementDescriptor, n as normalizeModelName, k as getModelFactory } from "./schema-provider-CXFLTMjg.js";
5
+ export { M as Model, b as buildSchema, M as default } from "./schema-provider-CXFLTMjg.js";
6
+ import { macroCondition, getGlobalConfig } from '@embroider/macros';
7
+ import { warn, deprecate } from '@ember/debug';
8
+ import { RecordStore } from '@warp-drive/core/types/symbols';
9
+ import { l as lookupLegacySupport } from "./errors-D74uk36r.js";
10
+ import { singularize, dasherize } from '@warp-drive/utilities/string';
11
+ import { setOwner, getOwner } from '@ember/application';
12
+ function _attr(type, options) {
13
+ if (typeof type === 'object') {
14
+ options = type;
15
+ type = undefined;
16
+ } else {
17
+ options = options || {};
18
+ }
19
+ const meta = {
20
+ type: type,
21
+ kind: 'attribute',
22
+ isAttribute: true,
23
+ options: options,
24
+ key: null
25
+ };
26
+ return computed({
27
+ get(key) {
28
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
29
+ if (['currentState'].includes(key)) {
30
+ throw new Error(`'${key}' is a reserved property name on instances of classes extending Model. Please choose a different property name for your attr on ${this.constructor.toString()}`);
31
+ }
32
+ }
33
+ if (this.isDestroyed || this.isDestroying) {
34
+ return;
35
+ }
36
+ return peekCache(this).getAttr(recordIdentifierFor(this), key);
37
+ },
38
+ set(key, value) {
39
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
40
+ if (['currentState'].includes(key)) {
41
+ throw new Error(`'${key}' is a reserved property name on instances of classes extending Model. Please choose a different property name for your attr on ${this.constructor.toString()}`);
42
+ }
43
+ }
44
+ const identifier = recordIdentifierFor(this);
45
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
46
+ if (!test) {
47
+ throw new Error(`Attempted to set '${key}' on the deleted record ${identifier.type}:${identifier.id} (${identifier.lid})`);
48
+ }
49
+ })(!this.currentState.isDeleted) : {};
50
+ const cache = peekCache(this);
51
+ const currentValue = cache.getAttr(identifier, key);
52
+ if (currentValue !== value) {
53
+ cache.setAttr(identifier, key, value);
54
+ if (!this.isValid) {
55
+ const {
56
+ errors
57
+ } = this;
58
+ if (errors.get(key)) {
59
+ errors.remove(key);
60
+ this.currentState.cleanErrorRequests();
61
+ }
62
+ }
63
+ }
64
+ return value;
65
+ }
66
+ }).meta(meta);
67
+ }
68
+
69
+ // NOTE: Usage of Explicit ANY
70
+ // -------------------------------------------------------------------
71
+ // any is required here because we are the maximal not the minimal
72
+ // subset of options allowed. If we used unknown, object, or
73
+ // Record<string, unknown> we would get type errors when we try to
74
+ // assert against a more specific implementation with precise options.
75
+ // -------------------------------------------------------------------
76
+
77
+ // see note on Explicit ANY above
78
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
79
+
80
+ /**
81
+ * The return type of `void` is a lie to appease TypeScript. The actual return type
82
+ * is a descriptor, but typescript incorrectly insists that decorator functions return
83
+ * `void` or `any`.
84
+ *
85
+ */
86
+
87
+ /**
88
+ `attr` defines an attribute on a [Model](/ember-data/release/classes/Model).
89
+ By default, attributes are passed through as-is, however you can specify an
90
+ optional type to have the value automatically transformed.
91
+ EmberData ships with four basic transform types: `string`, `number`,
92
+ `boolean` and `date`. You can define your own transforms by subclassing
93
+ [Transform](/ember-data/release/classes/Transform).
94
+
95
+ Note that you cannot use `attr` to define an attribute of `id`.
96
+
97
+ `attr` takes an optional hash as a second parameter, currently
98
+ supported options are:
99
+
100
+ - `defaultValue`: Pass a string or a function to be called to set the attribute
101
+ to a default value if and only if the key is absent from the payload response.
102
+
103
+ Example
104
+
105
+ ```js [app/models/user.js]
106
+ import { Model, attr } from '@warp-drive/legacy/model';
107
+
108
+ export default class UserModel extends Model {
109
+ @attr('string') username;
110
+ @attr('string') email;
111
+ @attr('boolean', { defaultValue: false }) verified;
112
+ }
113
+ ```
114
+
115
+ Default value can also be a function. This is useful it you want to return
116
+ a new object for each attribute.
117
+
118
+ ```js [app/models/user.js]
119
+ import { Model, attr } from '@warp-drive/legacy/model';
120
+
121
+ export default class UserModel extends Model {
122
+ @attr('string') username;
123
+ @attr('string') email;
124
+
125
+ @attr({
126
+ defaultValue() {
127
+ return {};
128
+ }
129
+ })
130
+ settings;
131
+ }
132
+ ```
133
+
134
+ The `options` hash is passed as second argument to a transforms'
135
+ `serialize` and `deserialize` method. This allows to configure a
136
+ transformation and adapt the corresponding value, based on the config:
137
+
138
+ ```js [app/models/post.js]
139
+ import { Model, attr } from '@warp-drive/legacy/model';
140
+
141
+ export default class PostModel extends Model {
142
+ @attr('text', {
143
+ uppercase: true
144
+ })
145
+ text;
146
+ }
147
+ ```
148
+
149
+ ```js [app/transforms/text.js]
150
+ export default class TextTransform {
151
+ serialize(value, options) {
152
+ if (options.uppercase) {
153
+ return value.toUpperCase();
154
+ }
155
+
156
+ return value;
157
+ }
158
+
159
+ deserialize(value) {
160
+ return value;
161
+ }
162
+
163
+ static create() {
164
+ return new this();
165
+ }
166
+ }
167
+ ```
168
+
169
+ @public
170
+ @param {String|Object} type the attribute type
171
+ @param {Object} options a hash of options
172
+ @return {Attribute}
173
+ */
174
+
175
+ // see note on DataDecorator for why void
176
+ function attr(type, options, desc) {
177
+ const args = [type, options, desc];
178
+ // see note on DataDecorator for why void
179
+ return isElementDescriptor(args) ? _attr()(...args) : _attr(type, options);
180
+ }
181
+
182
+ // get: () => getT;
183
+ // // set: (value: Awaited<getT>) => void;
184
+ // set: (value: getT) => void;
185
+ // // init: () => getT;
186
+ // };
187
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
188
+ // BelongsToDecoratorObject<getT>;
189
+
190
+ function _belongsTo(type, options) {
191
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
192
+ if (!test) {
193
+ throw new Error(`Expected options.async from @belongsTo('${type}', options) to be a boolean`);
194
+ }
195
+ })(options && typeof options.async === 'boolean') : {};
196
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
197
+ if (!test) {
198
+ throw new Error(`Expected options.inverse from @belongsTo('${type}', options) to be either null or the string type of the related resource.`);
199
+ }
200
+ })(options.inverse === null || typeof options.inverse === 'string' && options.inverse.length > 0) : {};
201
+ const meta = {
202
+ type: normalizeModelName(type),
203
+ options: options,
204
+ kind: 'belongsTo',
205
+ name: '<Unknown BelongsTo>'
206
+ };
207
+ return computed({
208
+ get(key) {
209
+ // this is a legacy behavior we may not carry into a new model setup
210
+ // it's better to error on disconnected records so users find errors
211
+ // in their logic.
212
+ if (this.isDestroying || this.isDestroyed) {
213
+ return null;
214
+ }
215
+ const support = lookupLegacySupport(this);
216
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
217
+ if (['currentState'].includes(key)) {
218
+ throw new Error(`'${key}' is a reserved property name on instances of classes extending Model. Please choose a different property name for your belongsTo on ${this.constructor.toString()}`);
219
+ }
220
+ if (Object.prototype.hasOwnProperty.call(options, 'serialize')) {
221
+ warn(`You provided a serialize option on the "${key}" property in the "${support.identifier.type}" class, this belongs in the serializer. See Serializer and it's implementations https://api.emberjs.com/ember-data/release/classes/Serializer`, false, {
222
+ id: 'ds.model.serialize-option-in-belongs-to'
223
+ });
224
+ }
225
+ if (Object.prototype.hasOwnProperty.call(options, 'embedded')) {
226
+ warn(`You provided an embedded option on the "${key}" property in the "${support.identifier.type}" class, this belongs in the serializer. See EmbeddedRecordsMixin https://api.emberjs.com/ember-data/release/classes/EmbeddedRecordsMixin`, false, {
227
+ id: 'ds.model.embedded-option-in-belongs-to'
228
+ });
229
+ }
230
+ }
231
+ return support.getBelongsTo(key);
232
+ },
233
+ set(key, value) {
234
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
235
+ if (!test) {
236
+ throw new Error(`Cannot set belongsTo relationship ${key} on a destroyed record`);
237
+ }
238
+ })(!this.isDestroying && !this.isDestroyed) : {};
239
+ if (this.isDestroying || this.isDestroyed) {
240
+ return null;
241
+ }
242
+ const support = lookupLegacySupport(this);
243
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
244
+ if (['currentState'].includes(key)) {
245
+ throw new Error(`'${key}' is a reserved property name on instances of classes extending Model. Please choose a different property name for your belongsTo on ${this.constructor.toString()}`);
246
+ }
247
+ }
248
+ this[RecordStore]._join(() => {
249
+ support.setDirtyBelongsTo(key, value);
250
+ });
251
+ return support.getBelongsTo(key);
252
+ }
253
+ }).meta(meta);
254
+ }
255
+
256
+ /**
257
+ `belongsTo` is used to define One-To-One and One-To-Many, and One-To-None
258
+ relationships on a [Model](/ember-data/release/classes/Model).
259
+
260
+ `belongsTo` takes a configuration hash as a second parameter, currently
261
+ supported options are:
262
+
263
+ - `async`: (*required*) A boolean value used to declare whether this is a sync (false) or async (true) relationship.
264
+ - `inverse`: (*required*) A string used to identify the inverse property on a related model, or `null`.
265
+ - `polymorphic`: (*optional*) A boolean value to mark the relationship as polymorphic
266
+ - `as`: (*optional*) A string used to declare the abstract type "this" record satisfies for polymorphism.
267
+
268
+ ### Examples
269
+
270
+ To declare a **one-to-many** (or many-to-many) relationship, use
271
+ `belongsTo` in combination with `hasMany`:
272
+
273
+ ```js
274
+ // app/models/comment.js
275
+ import { Model, belongsTo } from '@warp-drive/legacy/model';
276
+
277
+ export default class Comment extends Model {
278
+ @belongsTo('post', { async: false, inverse: 'comments' }) post;
279
+ }
280
+
281
+ // app/models/post.js
282
+ import { Model, hasMany } from '@warp-drive/legacy/model';
283
+
284
+ export default class Post extends Model {
285
+ @hasMany('comment', { async: false, inverse: 'post' }) comments;
286
+ }
287
+ ```
288
+
289
+ To declare a **one-to-one** relationship with managed inverses, use `belongsTo` for both sides:
290
+
291
+ ```js
292
+ // app/models/author.js
293
+ import { Model, belongsTo } from '@warp-drive/legacy/model';
294
+
295
+ export default class Author extends Model {
296
+ @belongsTo('address', { async: true, inverse: 'owner' }) address;
297
+ }
298
+
299
+ // app/models/address.js
300
+ import { Model, belongsTo } from '@warp-drive/legacy/model';
301
+
302
+ export default class Address extends Model {
303
+ @belongsTo('author', { async: true, inverse: 'address' }) owner;
304
+ }
305
+ ```
306
+
307
+ To declare a **one-to-one** relationship without managed inverses, use `belongsTo` for both sides
308
+ with `null` as the inverse:
309
+
310
+ ```js
311
+ // app/models/author.js
312
+ import { Model, belongsTo } from '@warp-drive/legacy/model';
313
+
314
+ export default class Author extends Model {
315
+ @belongsTo('address', { async: true, inverse: null }) address;
316
+ }
317
+
318
+ // app/models/address.js
319
+ import { Model, belongsTo } from '@warp-drive/legacy/model';
320
+
321
+ export default class Address extends Model {
322
+ @belongsTo('author', { async: true, inverse: null }) owner;
323
+ }
324
+ ```
325
+
326
+ To declare a one-to-none relationship between two models, use
327
+ `belongsTo` with inverse set to `null` on just one side::
328
+
329
+ ```js
330
+ // app/models/person.js
331
+ import { Model, belongsTo } from '@warp-drive/legacy/model';
332
+
333
+ export default class Person extends Model {
334
+ @belongsTo('person', { async: false, inverse: null }) bestFriend;
335
+ }
336
+ ```
337
+
338
+ #### Sync vs Async Relationships
339
+
340
+ EmberData fulfills relationships using resource data available in
341
+ the cache.
342
+
343
+ Sync relationships point directly to the known related resources.
344
+
345
+ When a relationship is declared as async, if any of the known related
346
+ resources have not been loaded, they will be fetched. The property
347
+ on the record when accessed provides a promise that resolves once
348
+ all resources are loaded.
349
+
350
+ Async relationships may take advantage of links. On access, if the related
351
+ link has not been loaded, or if any known resources are not available in
352
+ the cache, the fresh state will be fetched using the link.
353
+
354
+ In contrast to async relationship, accessing a sync relationship
355
+ will error on access when any of the known related resources have
356
+ not been loaded.
357
+
358
+ If you are using `links` with sync relationships, you have to use
359
+ the BelongsTo reference API to fetch or refresh related resources
360
+ that aren't loaded. For instance, for a `bestFriend` relationship:
361
+
362
+ ```js
363
+ person.belongsTo('bestFriend').reload();
364
+ ```
365
+
366
+ #### Polymorphic Relationships
367
+
368
+ To declare a polymorphic relationship, use `hasMany` with the `polymorphic`
369
+ option set to `true`:
370
+
371
+ ```js
372
+ // app/models/comment.js
373
+ import { Model, belongsTo } from '@warp-drive/legacy/model';
374
+
375
+ export default class Comment extends Model {
376
+ @belongsTo('commentable', { async: false, inverse: 'comments', polymorphic: true }) parent;
377
+ }
378
+ ```
379
+
380
+ `'commentable'` here is referred to as the "abstract type" for the polymorphic
381
+ relationship.
382
+
383
+ Polymorphic relationships with `inverse: null` will accept any type of record as their content.
384
+ Polymorphic relationships with `inverse` set to a string will only accept records with a matching
385
+ inverse relationships declaring itself as satisfying the abstract type.
386
+
387
+ Below, 'as' is used to declare the that 'post' record satisfies the abstract type 'commentable'
388
+ for this relationship.
389
+
390
+ ```js
391
+ // app/models/post.js
392
+ import { Model, hasMany } from '@warp-drive/legacy/model';
393
+
394
+ export default class Post extends Model {
395
+ @hasMany('comment', { async: false, inverse: 'parent', as: 'commentable' }) comments;
396
+ }
397
+ ```
398
+
399
+ Note: every Model that declares an inverse to a polymorphic relationship must
400
+ declare itself exactly the same. This is because polymorphism is based on structural
401
+ traits.
402
+
403
+ Polymorphic to polymorphic relationships are supported. Both sides of the relationship
404
+ must be declared as polymorphic, and the `as` option must be used to declare the abstract
405
+ type each record satisfies on both sides.
406
+
407
+ @public
408
+ @param {String} type (optional) the name of the related resource
409
+ @param {Object} options (optional) a hash of options
410
+ @return {PropertyDescriptor} relationship
411
+ */
412
+
413
+ // export function belongsTo<K extends Promise<unknown>, T extends Awaited<K> = Awaited<K>>(
414
+ // type: TypeFromInstance<NoNull<T>>,
415
+ // options: RelationshipOptions<T, true>
416
+ // ): RelationshipDecorator<K>;
417
+
418
+ function belongsTo(type, options) {
419
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
420
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
421
+ if (!test) {
422
+ throw new Error(`belongsTo must be invoked with a type and options. Did you mean \`@belongsTo(${type}, { async: false, inverse: null })\`?`);
423
+ }
424
+ })(!isElementDescriptor(arguments)) : {};
425
+ }
426
+ return _belongsTo(type, options);
427
+ }
428
+ function normalizeType(type) {
429
+ if (macroCondition(getGlobalConfig().WarpDrive.deprecations.DEPRECATE_NON_STRICT_TYPES)) {
430
+ const result = singularize(dasherize(type));
431
+ deprecate(`The resource type '${type}' is not normalized. Update your application code to use '${result}' instead of '${type}'.`, result === type, {
432
+ id: 'ember-data:deprecate-non-strict-types',
433
+ until: '6.0',
434
+ for: 'ember-data',
435
+ since: {
436
+ available: '4.13',
437
+ enabled: '5.3'
438
+ }
439
+ });
440
+ return result;
441
+ }
442
+ return type;
443
+ }
444
+ function _hasMany(type, options) {
445
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
446
+ if (!test) {
447
+ throw new Error(`Expected hasMany options.async to be a boolean`);
448
+ }
449
+ })(options && typeof options.async === 'boolean') : {};
450
+
451
+ // Metadata about relationships is stored on the meta of
452
+ // the relationship. This is used for introspection and
453
+ // serialization. Note that `key` is populated lazily
454
+ // the first time the CP is called.
455
+ const meta = {
456
+ type: normalizeType(type),
457
+ options,
458
+ kind: 'hasMany',
459
+ name: '<Unknown BelongsTo>'
460
+ };
461
+ return computed({
462
+ get(key) {
463
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
464
+ if (['currentState'].includes(key)) {
465
+ throw new Error(`'${key}' is a reserved property name on instances of classes extending Model. Please choose a different property name for your hasMany on ${this.constructor.toString()}`);
466
+ }
467
+ }
468
+ if (this.isDestroying || this.isDestroyed) {
469
+ return [];
470
+ }
471
+ return lookupLegacySupport(this).getHasMany(key);
472
+ },
473
+ set(key, records) {
474
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
475
+ if (['currentState'].includes(key)) {
476
+ throw new Error(`'${key}' is a reserved property name on instances of classes extending Model. Please choose a different property name for your hasMany on ${this.constructor.toString()}`);
477
+ }
478
+ }
479
+ const support = lookupLegacySupport(this);
480
+ const manyArray = support.getManyArray(key);
481
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
482
+ if (!test) {
483
+ throw new Error(`You must pass an array of records to set a hasMany relationship`);
484
+ }
485
+ })(Array.isArray(records)) : {};
486
+ this[RecordStore]._join(() => {
487
+ manyArray.splice(0, manyArray.length, ...records);
488
+ });
489
+ return support.getHasMany(key);
490
+ }
491
+ }).meta(meta);
492
+ }
493
+
494
+ /**
495
+ `hasMany` is used to define Many-To-One and Many-To-Many, and Many-To-None
496
+ relationships on a [Model](/ember-data/release/classes/Model).
497
+
498
+ `hasMany` takes a configuration hash as a second parameter, currently
499
+ supported options are:
500
+
501
+ - `async`: (*required*) A boolean value used to declare whether this is a sync (false) or async (true) relationship.
502
+ - `inverse`: (*required*) A string used to identify the inverse property on a related model, or `null`.
503
+ - `polymorphic`: (*optional*) A boolean value to mark the relationship as polymorphic
504
+ - `as`: (*optional*) A string used to declare the abstract type "this" record satisfies for polymorphism.
505
+
506
+ ### Examples
507
+
508
+ To declare a **many-to-one** (or one-to-many) relationship, use
509
+ `belongsTo` in combination with `hasMany`:
510
+
511
+ ```js
512
+ // app/models/post.js
513
+ import { Model, hasMany } from '@warp-drive/legacy/model';
514
+
515
+ export default class Post extends Model {
516
+ @hasMany('comment', { async: false, inverse: 'post' }) comments;
517
+ }
518
+
519
+
520
+ // app/models/comment.js
521
+ import { Model, belongsTo } from '@warp-drive/legacy/model';
522
+
523
+ export default class Comment extends Model {
524
+ @belongsTo('post', { async: false, inverse: 'comments' }) post;
525
+ }
526
+ ```
527
+
528
+ To declare a **many-to-many** relationship with managed inverses, use `hasMany` for both sides:
529
+
530
+ ```js
531
+ // app/models/post.js
532
+ import { Model, hasMany } from '@warp-drive/legacy/model';
533
+
534
+ export default class Post extends Model {
535
+ @hasMany('tag', { async: true, inverse: 'posts' }) tags;
536
+ }
537
+
538
+ // app/models/tag.js
539
+ import { Model, hasMany } from '@warp-drive/legacy/model';
540
+
541
+ export default class Tag extends Model {
542
+ @hasMany('post', { async: true, inverse: 'tags' }) posts;
543
+ }
544
+ ```
545
+
546
+ To declare a **many-to-many** relationship without managed inverses, use `hasMany` for both sides
547
+ with `null` as the inverse:
548
+
549
+ ```js
550
+ // app/models/post.js
551
+ import { Model, hasMany } from '@warp-drive/legacy/model';
552
+
553
+ export default class Post extends Model {
554
+ @hasMany('tag', { async: true, inverse: null }) tags;
555
+ }
556
+
557
+ // app/models/tag.js
558
+ import { Model, hasMany } from '@warp-drive/legacy/model';
559
+
560
+ export default class Tag extends Model {
561
+ @hasMany('post', { async: true, inverse: null }) posts;
562
+ }
563
+ ```
564
+
565
+ To declare a many-to-none relationship between two models, use
566
+ `hasMany` with inverse set to `null` on just one side::
567
+
568
+ ```js
569
+ // app/models/post.js
570
+ import { Model, hasMany } from '@warp-drive/legacy/model';
571
+
572
+ export default class Post extends Model {
573
+ @hasMany('category', { async: true, inverse: null }) categories;
574
+ }
575
+ ```
576
+
577
+ #### Sync vs Async Relationships
578
+
579
+ EmberData fulfills relationships using resource data available in
580
+ the cache.
581
+
582
+ Sync relationships point directly to the known related resources.
583
+
584
+ When a relationship is declared as async, if any of the known related
585
+ resources have not been loaded, they will be fetched. The property
586
+ on the record when accessed provides a promise that resolves once
587
+ all resources are loaded.
588
+
589
+ Async relationships may take advantage of links. On access, if the related
590
+ link has not been loaded, or if any known resources are not available in
591
+ the cache, the fresh state will be fetched using the link.
592
+
593
+ In contrast to async relationship, accessing a sync relationship
594
+ will error on access when any of the known related resources have
595
+ not been loaded.
596
+
597
+ If you are using `links` with sync relationships, you have to use
598
+ the HasMany reference API to fetch or refresh related resources
599
+ that aren't loaded. For instance, for a `comments` relationship:
600
+
601
+ ```js
602
+ post.hasMany('comments').reload();
603
+ ```
604
+
605
+ #### Polymorphic Relationships
606
+
607
+ To declare a polymorphic relationship, use `hasMany` with the `polymorphic`
608
+ option set to `true`:
609
+
610
+ ```js
611
+ // app/models/comment.js
612
+ import { Model, belongsTo } from '@warp-drive/legacy/model';
613
+
614
+ export default class Comment extends Model {
615
+ @belongsTo('commentable', { async: false, inverse: 'comments', polymorphic: true }) parent;
616
+ }
617
+ ```
618
+
619
+ `'commentable'` here is referred to as the "abstract type" for the polymorphic
620
+ relationship.
621
+
622
+ Polymorphic relationships with `inverse: null` will accept any type of record as their content.
623
+ Polymorphic relationships with `inverse` set to a string will only accept records with a matching
624
+ inverse relationships declaring itself as satisfying the abstract type.
625
+
626
+ Below, 'as' is used to declare the that 'post' record satisfies the abstract type 'commentable'
627
+ for this relationship.
628
+
629
+ ```js
630
+ // app/models/post.js
631
+ import { Model, hasMany } from '@warp-drive/legacy/model';
632
+
633
+ export default class Post extends Model {
634
+ @hasMany('comment', { async: false, inverse: 'parent', as: 'commentable' }) comments;
635
+ }
636
+ ```
637
+
638
+ Note: every Model that declares an inverse to a polymorphic relationship must
639
+ declare itself exactly the same. This is because polymorphism is based on structural
640
+ traits.
641
+
642
+ Polymorphic to polymorphic relationships are supported. Both sides of the relationship
643
+ must be declared as polymorphic, and the `as` option must be used to declare the abstract
644
+ type each record satisfies on both sides.
645
+
646
+ @public
647
+ @param {String} type (optional) the name of the related resource
648
+ @param {Object} options (optional) a hash of options
649
+ @return {PropertyDescriptor} relationship
650
+ */
651
+
652
+ // export function hasMany<K extends Promise<unknown>, T extends Awaited<K> = Awaited<K>>(
653
+ // type: TypeFromInstance<NoNull<T>>,
654
+ // options: RelationshipOptions<T, true>
655
+ // ): RelationshipDecorator<K>;
656
+
657
+ function hasMany(type, options) {
658
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
659
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
660
+ if (!test) {
661
+ throw new Error(`hasMany must be invoked with a type and options. Did you mean \`@hasMany(${type}, { async: false, inverse: null })\`?`);
662
+ }
663
+ })(!isElementDescriptor(arguments)) : {};
664
+ }
665
+ return _hasMany(type, options);
666
+ }
667
+ function instantiateRecord(identifier, createRecordArgs) {
668
+ const type = identifier.type;
669
+ const cache = this.cache;
670
+ // TODO deprecate allowing unknown args setting
671
+ const createOptions = {
672
+ _createProps: createRecordArgs,
673
+ // TODO @deprecate consider deprecating accessing record properties during init which the below is necessary for
674
+ _secretInit: {
675
+ identifier,
676
+ cache,
677
+ store: this,
678
+ cb: secretInit
679
+ }
680
+ };
681
+
682
+ // ensure that `getOwner(this)` works inside a model instance
683
+ setOwner(createOptions, getOwner(this));
684
+ const factory = getModelFactory(this, type);
685
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
686
+ if (!test) {
687
+ throw new Error(`No model was found for '${type}'`);
688
+ }
689
+ })(factory) : {};
690
+ return factory.class.create(createOptions);
691
+ }
692
+ function teardownRecord(record) {
693
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
694
+ if (!test) {
695
+ throw new Error(`expected to receive an instance of Model from @ember-data/model. If using a custom model make sure you implement teardownRecord`);
696
+ }
697
+ })('destroy' in record) : {};
698
+ record.destroy();
699
+ }
700
+ function modelFor(modelName) {
701
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
702
+ if (!test) {
703
+ throw new Error(`Attempted to call store.modelFor(), but the store instance has already been destroyed.`);
704
+ }
705
+ })(!this.isDestroyed && !this.isDestroying) : {};
706
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
707
+ if (!test) {
708
+ throw new Error(`You need to pass a model name to the store's modelFor method`);
709
+ }
710
+ })(modelName) : {};
711
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
712
+ if (!test) {
713
+ throw new Error(`Please pass a proper model name to the store's modelFor method`);
714
+ }
715
+ })(typeof modelName === 'string' && modelName.length) : {};
716
+ const type = normalizeModelName(modelName);
717
+ const maybeFactory = getModelFactory(this, type);
718
+ const klass = maybeFactory && maybeFactory.class ? maybeFactory.class : null;
719
+ const ignoreType = !klass || !klass.isModel || this._forceShim;
720
+ if (!ignoreType) {
721
+ return klass;
722
+ }
723
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
724
+ if (!test) {
725
+ throw new Error(`No model was found for '${type}' and no schema handles the type`);
726
+ }
727
+ })(this.schema.hasResource({
728
+ type
729
+ })) : {};
730
+ }
731
+ function secretInit(record, cache, identifier, store) {
732
+ setRecordIdentifier(record, identifier);
733
+ StoreMap.set(record, store);
734
+ setCacheFor(record, cache);
735
+ }
736
+ export { attr, belongsTo, hasMany, instantiateRecord, modelFor, teardownRecord };