@warp-drive/legacy 5.8.0-alpha.4 → 5.8.0-alpha.41

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 +14 -27
  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 +70 -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-DziiodPf.js → json-ChdEfB0X.js} +18 -35
  61. package/dist/model/-private.js +1 -1
  62. package/dist/model/migration-support.js +57 -25
  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 +14 -6
  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,593 @@
1
+ import { computed } from '@ember/object';
2
+ import { recordIdentifierFor } from '@warp-drive/core';
3
+ import { RecordStore } from '@warp-drive/core/types/symbols';
4
+ import { i as isElementDescriptor, n as normalizeModelName } from "./util-DvanW33H.js";
5
+ import { assertPrivateStore } from '@warp-drive/core/store/-private';
6
+ import { l as lookupLegacySupport } from "./errors-BGVFCBmi.js";
7
+ import '@warp-drive/utilities/string';
8
+ export { M as Model, b as buildSchema, M as default, m as restoreDeprecatedModelRequestBehaviors } from "./schema-provider-DJtD_8jZ.js";
9
+ export { i as instantiateRecord, m as modelFor, t as teardownRecord } from "./hooks-BztVA_x0.js";
10
+
11
+ /**
12
+ * Options provided to the attr decorator are
13
+ * supplied to the associated transform. Any
14
+ * key-value pair is valid; however, it is highly
15
+ * recommended to only use statically defined values
16
+ * that could be serialized to JSON.
17
+ *
18
+ * If no transform is provided, the only valid
19
+ * option is `defaultValue`.
20
+ *
21
+ * Examples:
22
+ *
23
+ * ```ts
24
+ * class User extends Model {
25
+ * @attr('string', { defaultValue: 'Anonymous' }) name;
26
+ * @attr('date', { defaultValue: () => new Date() }) createdAt;
27
+ * @attr({ defaultValue: () => ({}) }) preferences;
28
+ * @attr('boolean') hasVerifiedEmail;
29
+ * @attr address;
30
+ * }
31
+ *
32
+ * @class NOTATHING
33
+ */
34
+
35
+ function _attr(type, options) {
36
+ if (typeof type === 'object') {
37
+ options = type;
38
+ type = undefined;
39
+ } else {
40
+ options = options || {};
41
+ }
42
+ const meta = {
43
+ type: type,
44
+ kind: 'attribute',
45
+ isAttribute: true,
46
+ options: options,
47
+ key: null
48
+ };
49
+ return computed({
50
+ get(key) {
51
+ if (this.isDestroyed || this.isDestroying) {
52
+ return;
53
+ }
54
+ const cache = this[RecordStore].cache;
55
+ return cache.getAttr(recordIdentifierFor(this), key);
56
+ },
57
+ set(key, value) {
58
+ const identifier = recordIdentifierFor(this);
59
+ const cache = this[RecordStore].cache;
60
+ const currentValue = cache.getAttr(identifier, key);
61
+ if (currentValue !== value) {
62
+ cache.setAttr(identifier, key, value);
63
+ if (!this.isValid) {
64
+ const {
65
+ errors
66
+ } = this;
67
+ if (errors.get(key)) {
68
+ errors.remove(key);
69
+ this.currentState.cleanErrorRequests();
70
+ }
71
+ }
72
+ }
73
+ return value;
74
+ }
75
+ }).meta(meta);
76
+ }
77
+
78
+ // NOTE: Usage of Explicit ANY
79
+ // -------------------------------------------------------------------
80
+ // any is required here because we are the maximal not the minimal
81
+ // subset of options allowed. If we used unknown, object, or
82
+ // Record<string, unknown> we would get type errors when we try to
83
+ // assert against a more specific implementation with precise options.
84
+ // -------------------------------------------------------------------
85
+
86
+ // see note on Explicit ANY above
87
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
88
+
89
+ /**
90
+ * The return type of `void` is a lie to appease TypeScript. The actual return type
91
+ * is a descriptor, but typescript incorrectly insists that decorator functions return
92
+ * `void` or `any`.
93
+ *
94
+ */
95
+
96
+ /**
97
+ `attr` defines an attribute on a {@link Model}.
98
+ By default, attributes are passed through as-is, however you can specify an
99
+ optional type to have the value automatically transformed.
100
+ WarpDrive ships with four basic transform types: `string`, `number`,
101
+ `boolean` and `date`. You can define your own transforms by subclassing
102
+ {@link Transform}.
103
+
104
+ Note that you cannot use `attr` to define an attribute of `id`.
105
+
106
+ `attr` takes an optional hash as a second parameter, currently
107
+ supported options are:
108
+
109
+ - `defaultValue`: Pass a string or a function to be called to set the attribute
110
+ to a default value if and only if the key is absent from the payload response.
111
+
112
+ Example
113
+
114
+ ```js [app/models/user.js]
115
+ import { Model, attr } from '@warp-drive/legacy/model';
116
+
117
+ export default class UserModel extends Model {
118
+ @attr('string') username;
119
+ @attr('string') email;
120
+ @attr('boolean', { defaultValue: false }) verified;
121
+ }
122
+ ```
123
+
124
+ Default value can also be a function. This is useful it you want to return
125
+ a new object for each attribute.
126
+
127
+ ```js [app/models/user.js]
128
+ import { Model, attr } from '@warp-drive/legacy/model';
129
+
130
+ export default class UserModel extends Model {
131
+ @attr('string') username;
132
+ @attr('string') email;
133
+
134
+ @attr({
135
+ defaultValue() {
136
+ return {};
137
+ }
138
+ })
139
+ settings;
140
+ }
141
+ ```
142
+
143
+ The `options` hash is passed as second argument to a transforms'
144
+ `serialize` and `deserialize` method. This allows to configure a
145
+ transformation and adapt the corresponding value, based on the config:
146
+
147
+ ```js [app/models/post.js]
148
+ import { Model, attr } from '@warp-drive/legacy/model';
149
+
150
+ export default class PostModel extends Model {
151
+ @attr('text', {
152
+ uppercase: true
153
+ })
154
+ text;
155
+ }
156
+ ```
157
+
158
+ ```js [app/transforms/text.js]
159
+ export default class TextTransform {
160
+ serialize(value, options) {
161
+ if (options.uppercase) {
162
+ return value.toUpperCase();
163
+ }
164
+
165
+ return value;
166
+ }
167
+
168
+ deserialize(value) {
169
+ return value;
170
+ }
171
+
172
+ static create() {
173
+ return new this();
174
+ }
175
+ }
176
+ ```
177
+
178
+ @public
179
+ @param type the attribute type
180
+ @param options a hash of options
181
+ */
182
+
183
+ // see note on DataDecorator for why void
184
+ function attr(type, options, desc) {
185
+ const args = [type, options, desc];
186
+ // see note on DataDecorator for why void
187
+ return isElementDescriptor(args) ? _attr()(...args) : _attr(type, options);
188
+ }
189
+
190
+ // type BelongsToDecoratorObject<getT> = {
191
+ // get: () => getT;
192
+ // // set: (value: Awaited<getT>) => void;
193
+ // set: (value: getT) => void;
194
+ // // init: () => getT;
195
+ // };
196
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
197
+ // BelongsToDecoratorObject<getT>;
198
+
199
+ function _belongsTo(type, options) {
200
+ const meta = {
201
+ type: normalizeModelName(type),
202
+ options: options,
203
+ kind: 'belongsTo',
204
+ name: '<Unknown BelongsTo>'
205
+ };
206
+ return computed({
207
+ get(key) {
208
+ // this is a legacy behavior we may not carry into a new model setup
209
+ // it's better to error on disconnected records so users find errors
210
+ // in their logic.
211
+ if (this.isDestroying || this.isDestroyed) {
212
+ return null;
213
+ }
214
+ const support = lookupLegacySupport(this);
215
+ return support.getBelongsTo(key);
216
+ },
217
+ set(key, value) {
218
+ if (this.isDestroying || this.isDestroyed) {
219
+ return null;
220
+ }
221
+ const support = lookupLegacySupport(this);
222
+ assertPrivateStore(this[RecordStore]);
223
+ this[RecordStore]._join(() => {
224
+ support.setDirtyBelongsTo(key, value);
225
+ });
226
+ return support.getBelongsTo(key);
227
+ }
228
+ }).meta(meta);
229
+ }
230
+
231
+ /**
232
+ `belongsTo` is used to define One-To-One and One-To-Many, and One-To-None
233
+ relationships on a {@link Model}.
234
+
235
+ `belongsTo` takes a configuration hash as a second parameter, currently
236
+ supported options are:
237
+
238
+ - `async`: (*required*) A boolean value used to declare whether this is a sync (false) or async (true) relationship.
239
+ - `inverse`: (*required*) A string used to identify the inverse property on a related model, or `null`.
240
+ - `polymorphic`: (*optional*) A boolean value to mark the relationship as polymorphic
241
+ - `as`: (*optional*) A string used to declare the abstract type "this" record satisfies for polymorphism.
242
+
243
+ ### Examples
244
+
245
+ To declare a **one-to-many** (or many-to-many) relationship, use
246
+ `belongsTo` in combination with `hasMany`:
247
+
248
+ ```js
249
+ // app/models/comment.js
250
+ import { Model, belongsTo } from '@warp-drive/legacy/model';
251
+
252
+ export default class Comment extends Model {
253
+ @belongsTo('post', { async: false, inverse: 'comments' }) post;
254
+ }
255
+
256
+ // app/models/post.js
257
+ import { Model, hasMany } from '@warp-drive/legacy/model';
258
+
259
+ export default class Post extends Model {
260
+ @hasMany('comment', { async: false, inverse: 'post' }) comments;
261
+ }
262
+ ```
263
+
264
+ To declare a **one-to-one** relationship with managed inverses, use `belongsTo` for both sides:
265
+
266
+ ```js
267
+ // app/models/author.js
268
+ import { Model, belongsTo } from '@warp-drive/legacy/model';
269
+
270
+ export default class Author extends Model {
271
+ @belongsTo('address', { async: true, inverse: 'owner' }) address;
272
+ }
273
+
274
+ // app/models/address.js
275
+ import { Model, belongsTo } from '@warp-drive/legacy/model';
276
+
277
+ export default class Address extends Model {
278
+ @belongsTo('author', { async: true, inverse: 'address' }) owner;
279
+ }
280
+ ```
281
+
282
+ To declare a **one-to-one** relationship without managed inverses, use `belongsTo` for both sides
283
+ with `null` as the inverse:
284
+
285
+ ```js
286
+ // app/models/author.js
287
+ import { Model, belongsTo } from '@warp-drive/legacy/model';
288
+
289
+ export default class Author extends Model {
290
+ @belongsTo('address', { async: true, inverse: null }) address;
291
+ }
292
+
293
+ // app/models/address.js
294
+ import { Model, belongsTo } from '@warp-drive/legacy/model';
295
+
296
+ export default class Address extends Model {
297
+ @belongsTo('author', { async: true, inverse: null }) owner;
298
+ }
299
+ ```
300
+
301
+ To declare a one-to-none relationship between two models, use
302
+ `belongsTo` with inverse set to `null` on just one side::
303
+
304
+ ```js
305
+ // app/models/person.js
306
+ import { Model, belongsTo } from '@warp-drive/legacy/model';
307
+
308
+ export default class Person extends Model {
309
+ @belongsTo('person', { async: false, inverse: null }) bestFriend;
310
+ }
311
+ ```
312
+
313
+ #### Sync vs Async Relationships
314
+
315
+ WarpDrive fulfills relationships using resource data available in
316
+ the cache.
317
+
318
+ Sync relationships point directly to the known related resources.
319
+
320
+ When a relationship is declared as async, if any of the known related
321
+ resources have not been loaded, they will be fetched. The property
322
+ on the record when accessed provides a promise that resolves once
323
+ all resources are loaded.
324
+
325
+ Async relationships may take advantage of links. On access, if the related
326
+ link has not been loaded, or if any known resources are not available in
327
+ the cache, the fresh state will be fetched using the link.
328
+
329
+ In contrast to async relationship, accessing a sync relationship
330
+ will error on access when any of the known related resources have
331
+ not been loaded.
332
+
333
+ If you are using `links` with sync relationships, you have to use
334
+ the BelongsTo reference API to fetch or refresh related resources
335
+ that aren't loaded. For instance, for a `bestFriend` relationship:
336
+
337
+ ```js
338
+ person.belongsTo('bestFriend').reload();
339
+ ```
340
+
341
+ #### Polymorphic Relationships
342
+
343
+ To declare a polymorphic relationship, use `hasMany` with the `polymorphic`
344
+ option set to `true`:
345
+
346
+ ```js
347
+ // app/models/comment.js
348
+ import { Model, belongsTo } from '@warp-drive/legacy/model';
349
+
350
+ export default class Comment extends Model {
351
+ @belongsTo('commentable', { async: false, inverse: 'comments', polymorphic: true }) parent;
352
+ }
353
+ ```
354
+
355
+ `'commentable'` here is referred to as the "abstract type" for the polymorphic
356
+ relationship.
357
+
358
+ Polymorphic relationships with `inverse: null` will accept any type of record as their content.
359
+ Polymorphic relationships with `inverse` set to a string will only accept records with a matching
360
+ inverse relationships declaring itself as satisfying the abstract type.
361
+
362
+ Below, 'as' is used to declare the that 'post' record satisfies the abstract type 'commentable'
363
+ for this relationship.
364
+
365
+ ```js
366
+ // app/models/post.js
367
+ import { Model, hasMany } from '@warp-drive/legacy/model';
368
+
369
+ export default class Post extends Model {
370
+ @hasMany('comment', { async: false, inverse: 'parent', as: 'commentable' }) comments;
371
+ }
372
+ ```
373
+
374
+ Note: every Model that declares an inverse to a polymorphic relationship must
375
+ declare itself exactly the same. This is because polymorphism is based on structural
376
+ traits.
377
+
378
+ Polymorphic to polymorphic relationships are supported. Both sides of the relationship
379
+ must be declared as polymorphic, and the `as` option must be used to declare the abstract
380
+ type each record satisfies on both sides.
381
+
382
+ @public
383
+ @param type the name of the related resource
384
+ @param options a hash of options
385
+ */
386
+
387
+ // export function belongsTo<K extends Promise<unknown>, T extends Awaited<K> = Awaited<K>>(
388
+ // type: TypeFromInstance<NoNull<T>>,
389
+ // options: RelationshipOptions<T, true>
390
+ // ): RelationshipDecorator<K>;
391
+
392
+ function belongsTo(type, options) {
393
+ return _belongsTo(type, options);
394
+ }
395
+ function normalizeType(type) {
396
+ return type;
397
+ }
398
+ function _hasMany(type, options) {
399
+ // Metadata about relationships is stored on the meta of
400
+ // the relationship. This is used for introspection and
401
+ // serialization. Note that `key` is populated lazily
402
+ // the first time the CP is called.
403
+ const meta = {
404
+ type: normalizeType(type),
405
+ options,
406
+ kind: 'hasMany',
407
+ name: '<Unknown BelongsTo>'
408
+ };
409
+ return computed({
410
+ get(key) {
411
+ if (this.isDestroying || this.isDestroyed) {
412
+ return [];
413
+ }
414
+ return lookupLegacySupport(this).getHasMany(key);
415
+ },
416
+ set(key, records) {
417
+ const support = lookupLegacySupport(this);
418
+ const manyArray = support.getManyArray(key);
419
+ assertPrivateStore(this[RecordStore]);
420
+ this[RecordStore]._join(() => {
421
+ manyArray.splice(0, manyArray.length, ...records);
422
+ });
423
+ return support.getHasMany(key);
424
+ }
425
+ }).meta(meta);
426
+ }
427
+
428
+ /**
429
+ `hasMany` is used to define Many-To-One and Many-To-Many, and Many-To-None
430
+ relationships on a {@link Model}.
431
+
432
+ `hasMany` takes a configuration hash as a second parameter, currently
433
+ supported options are:
434
+
435
+ - `async`: (*required*) A boolean value used to declare whether this is a sync (false) or async (true) relationship.
436
+ - `inverse`: (*required*) A string used to identify the inverse property on a related model, or `null`.
437
+ - `polymorphic`: (*optional*) A boolean value to mark the relationship as polymorphic
438
+ - `as`: (*optional*) A string used to declare the abstract type "this" record satisfies for polymorphism.
439
+
440
+ ### Examples
441
+
442
+ To declare a **many-to-one** (or one-to-many) relationship, use
443
+ `belongsTo` in combination with `hasMany`:
444
+
445
+ ```js
446
+ // app/models/post.js
447
+ import { Model, hasMany } from '@warp-drive/legacy/model';
448
+
449
+ export default class Post extends Model {
450
+ @hasMany('comment', { async: false, inverse: 'post' }) comments;
451
+ }
452
+
453
+
454
+ // app/models/comment.js
455
+ import { Model, belongsTo } from '@warp-drive/legacy/model';
456
+
457
+ export default class Comment extends Model {
458
+ @belongsTo('post', { async: false, inverse: 'comments' }) post;
459
+ }
460
+ ```
461
+
462
+ To declare a **many-to-many** relationship with managed inverses, use `hasMany` for both sides:
463
+
464
+ ```js
465
+ // app/models/post.js
466
+ import { Model, hasMany } from '@warp-drive/legacy/model';
467
+
468
+ export default class Post extends Model {
469
+ @hasMany('tag', { async: true, inverse: 'posts' }) tags;
470
+ }
471
+
472
+ // app/models/tag.js
473
+ import { Model, hasMany } from '@warp-drive/legacy/model';
474
+
475
+ export default class Tag extends Model {
476
+ @hasMany('post', { async: true, inverse: 'tags' }) posts;
477
+ }
478
+ ```
479
+
480
+ To declare a **many-to-many** relationship without managed inverses, use `hasMany` for both sides
481
+ with `null` as the inverse:
482
+
483
+ ```js
484
+ // app/models/post.js
485
+ import { Model, hasMany } from '@warp-drive/legacy/model';
486
+
487
+ export default class Post extends Model {
488
+ @hasMany('tag', { async: true, inverse: null }) tags;
489
+ }
490
+
491
+ // app/models/tag.js
492
+ import { Model, hasMany } from '@warp-drive/legacy/model';
493
+
494
+ export default class Tag extends Model {
495
+ @hasMany('post', { async: true, inverse: null }) posts;
496
+ }
497
+ ```
498
+
499
+ To declare a many-to-none relationship between two models, use
500
+ `hasMany` with inverse set to `null` on just one side::
501
+
502
+ ```js
503
+ // app/models/post.js
504
+ import { Model, hasMany } from '@warp-drive/legacy/model';
505
+
506
+ export default class Post extends Model {
507
+ @hasMany('category', { async: true, inverse: null }) categories;
508
+ }
509
+ ```
510
+
511
+ #### Sync vs Async Relationships
512
+
513
+ WarpDrive fulfills relationships using resource data available in
514
+ the cache.
515
+
516
+ Sync relationships point directly to the known related resources.
517
+
518
+ When a relationship is declared as async, if any of the known related
519
+ resources have not been loaded, they will be fetched. The property
520
+ on the record when accessed provides a promise that resolves once
521
+ all resources are loaded.
522
+
523
+ Async relationships may take advantage of links. On access, if the related
524
+ link has not been loaded, or if any known resources are not available in
525
+ the cache, the fresh state will be fetched using the link.
526
+
527
+ In contrast to async relationship, accessing a sync relationship
528
+ will error on access when any of the known related resources have
529
+ not been loaded.
530
+
531
+ If you are using `links` with sync relationships, you have to use
532
+ the HasMany reference API to fetch or refresh related resources
533
+ that aren't loaded. For instance, for a `comments` relationship:
534
+
535
+ ```js
536
+ post.hasMany('comments').reload();
537
+ ```
538
+
539
+ #### Polymorphic Relationships
540
+
541
+ To declare a polymorphic relationship, use `hasMany` with the `polymorphic`
542
+ option set to `true`:
543
+
544
+ ```js
545
+ // app/models/comment.js
546
+ import { Model, belongsTo } from '@warp-drive/legacy/model';
547
+
548
+ export default class Comment extends Model {
549
+ @belongsTo('commentable', { async: false, inverse: 'comments', polymorphic: true }) parent;
550
+ }
551
+ ```
552
+
553
+ `'commentable'` here is referred to as the "abstract type" for the polymorphic
554
+ relationship.
555
+
556
+ Polymorphic relationships with `inverse: null` will accept any type of record as their content.
557
+ Polymorphic relationships with `inverse` set to a string will only accept records with a matching
558
+ inverse relationships declaring itself as satisfying the abstract type.
559
+
560
+ Below, 'as' is used to declare the that 'post' record satisfies the abstract type 'commentable'
561
+ for this relationship.
562
+
563
+ ```js
564
+ // app/models/post.js
565
+ import { Model, hasMany } from '@warp-drive/legacy/model';
566
+
567
+ export default class Post extends Model {
568
+ @hasMany('comment', { async: false, inverse: 'parent', as: 'commentable' }) comments;
569
+ }
570
+ ```
571
+
572
+ Note: every Model that declares an inverse to a polymorphic relationship must
573
+ declare itself exactly the same. This is because polymorphism is based on structural
574
+ traits.
575
+
576
+ Polymorphic to polymorphic relationships are supported. Both sides of the relationship
577
+ must be declared as polymorphic, and the `as` option must be used to declare the abstract
578
+ type each record satisfies on both sides.
579
+
580
+ @public
581
+ @param type the name of the related resource
582
+ @param options a hash of options
583
+ */
584
+
585
+ // export function hasMany<K extends Promise<unknown>, T extends Awaited<K> = Awaited<K>>(
586
+ // type: TypeFromInstance<NoNull<T>>,
587
+ // options: RelationshipOptions<T, true>
588
+ // ): RelationshipDecorator<K>;
589
+
590
+ function hasMany(type, options) {
591
+ return _hasMany(type, options);
592
+ }
593
+ export { attr, belongsTo, hasMany };
@@ -0,0 +1,65 @@
1
+ const deferred = /* @__PURE__ */new WeakMap();
2
+ function deferDecorator(proto, prop, desc) {
3
+ let map = deferred.get(proto);
4
+ if (!map) {
5
+ map = /* @__PURE__ */new Map();
6
+ deferred.set(proto, map);
7
+ }
8
+ map.set(prop, desc);
9
+ }
10
+ function findDeferredDecorator(target, prop) {
11
+ var _a;
12
+ let cursor = target.prototype;
13
+ while (cursor) {
14
+ let desc = (_a = deferred.get(cursor)) == null ? void 0 : _a.get(prop);
15
+ if (desc) {
16
+ return desc;
17
+ }
18
+ cursor = cursor.prototype;
19
+ }
20
+ }
21
+ function decorateFieldV2(prototype, prop, decorators, initializer) {
22
+ let desc = {
23
+ configurable: true,
24
+ enumerable: true,
25
+ writable: true,
26
+ initializer: null
27
+ };
28
+ if (initializer) {
29
+ desc.initializer = initializer;
30
+ }
31
+ for (let decorator of decorators) {
32
+ desc = decorator(prototype, prop, desc) || desc;
33
+ }
34
+ if (desc.initializer === void 0) {
35
+ Object.defineProperty(prototype, prop, desc);
36
+ } else {
37
+ deferDecorator(prototype, prop, desc);
38
+ }
39
+ }
40
+ function decorateMethodV2(prototype, prop, decorators) {
41
+ const origDesc = Object.getOwnPropertyDescriptor(prototype, prop);
42
+ let desc = {
43
+ ...origDesc
44
+ };
45
+ for (let decorator of decorators) {
46
+ desc = decorator(prototype, prop, desc) || desc;
47
+ }
48
+ if (desc.initializer !== void 0) {
49
+ desc.value = desc.initializer ? desc.initializer.call(prototype) : void 0;
50
+ desc.initializer = void 0;
51
+ }
52
+ Object.defineProperty(prototype, prop, desc);
53
+ }
54
+ function initializeDeferredDecorator(target, prop) {
55
+ let desc = findDeferredDecorator(target.constructor, prop);
56
+ if (desc) {
57
+ Object.defineProperty(target, prop, {
58
+ enumerable: desc.enumerable,
59
+ configurable: desc.configurable,
60
+ writable: desc.writable,
61
+ value: desc.initializer ? desc.initializer.call(target) : void 0
62
+ });
63
+ }
64
+ }
65
+ export { decorateFieldV2 as a, decorateMethodV2 as d, initializeDeferredDecorator as i };