@ember-data/model 5.4.0-alpha.4 → 5.4.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 (74) hide show
  1. package/addon/-private.js +2 -2
  2. package/addon/has-many-B3FpdTTp.js +655 -0
  3. package/addon/has-many-B3FpdTTp.js.map +1 -0
  4. package/addon/{hooks-62d36619.js → hooks-CHMQx9Ce.js} +34 -15
  5. package/addon/hooks-CHMQx9Ce.js.map +1 -0
  6. package/addon/hooks.js +1 -1
  7. package/addon/index.js +3 -3
  8. package/addon/index.js.map +1 -1
  9. package/addon/migration-support.js +118 -0
  10. package/addon/migration-support.js.map +1 -0
  11. package/addon/{model-e642556d.js → model-DvL_NWQw.js} +2035 -1881
  12. package/addon/model-DvL_NWQw.js.map +1 -0
  13. package/addon/util-CSYkrwfb.js +35 -0
  14. package/addon/util-CSYkrwfb.js.map +1 -0
  15. package/addon-main.js +1 -0
  16. package/package.json +88 -37
  17. package/unstable-preview-types/-private/attr.d.ts +174 -0
  18. package/unstable-preview-types/-private/attr.d.ts.map +1 -0
  19. package/unstable-preview-types/-private/attr.type-test.d.ts +4 -0
  20. package/unstable-preview-types/-private/attr.type-test.d.ts.map +1 -0
  21. package/unstable-preview-types/-private/belongs-to.d.ts +180 -0
  22. package/unstable-preview-types/-private/belongs-to.d.ts.map +1 -0
  23. package/unstable-preview-types/-private/belongs-to.type-test.d.ts +4 -0
  24. package/unstable-preview-types/-private/belongs-to.type-test.d.ts.map +1 -0
  25. package/unstable-preview-types/-private/debug/assert-polymorphic-type.d.ts +8 -0
  26. package/unstable-preview-types/-private/debug/assert-polymorphic-type.d.ts.map +1 -0
  27. package/unstable-preview-types/-private/errors.d.ts +308 -0
  28. package/unstable-preview-types/-private/errors.d.ts.map +1 -0
  29. package/unstable-preview-types/-private/has-many.d.ts +169 -0
  30. package/unstable-preview-types/-private/has-many.d.ts.map +1 -0
  31. package/unstable-preview-types/-private/has-many.type-test.d.ts +4 -0
  32. package/unstable-preview-types/-private/has-many.type-test.d.ts.map +1 -0
  33. package/unstable-preview-types/-private/hooks.d.ts +13 -0
  34. package/unstable-preview-types/-private/hooks.d.ts.map +1 -0
  35. package/unstable-preview-types/-private/legacy-relationships-support.d.ts +64 -0
  36. package/unstable-preview-types/-private/legacy-relationships-support.d.ts.map +1 -0
  37. package/unstable-preview-types/-private/many-array.d.ts +198 -0
  38. package/unstable-preview-types/-private/many-array.d.ts.map +1 -0
  39. package/unstable-preview-types/-private/model-for-mixin.d.ts +6 -0
  40. package/unstable-preview-types/-private/model-for-mixin.d.ts.map +1 -0
  41. package/unstable-preview-types/-private/model-methods.d.ts +36 -0
  42. package/unstable-preview-types/-private/model-methods.d.ts.map +1 -0
  43. package/unstable-preview-types/-private/model.d.ts +108 -0
  44. package/unstable-preview-types/-private/model.type-test.d.ts +4 -0
  45. package/unstable-preview-types/-private/model.type-test.d.ts.map +1 -0
  46. package/unstable-preview-types/-private/notify-changes.d.ts +8 -0
  47. package/unstable-preview-types/-private/notify-changes.d.ts.map +1 -0
  48. package/unstable-preview-types/-private/promise-belongs-to.d.ts +47 -0
  49. package/unstable-preview-types/-private/promise-belongs-to.d.ts.map +1 -0
  50. package/unstable-preview-types/-private/promise-many-array.d.ts +129 -0
  51. package/unstable-preview-types/-private/promise-many-array.d.ts.map +1 -0
  52. package/unstable-preview-types/-private/promise-proxy-base.d.ts +35 -0
  53. package/unstable-preview-types/-private/record-state.d.ts +91 -0
  54. package/unstable-preview-types/-private/record-state.d.ts.map +1 -0
  55. package/unstable-preview-types/-private/references/belongs-to.d.ts +503 -0
  56. package/unstable-preview-types/-private/references/belongs-to.d.ts.map +1 -0
  57. package/unstable-preview-types/-private/references/has-many.d.ts +515 -0
  58. package/unstable-preview-types/-private/references/has-many.d.ts.map +1 -0
  59. package/unstable-preview-types/-private/schema-provider.d.ts +27 -0
  60. package/unstable-preview-types/-private/schema-provider.d.ts.map +1 -0
  61. package/unstable-preview-types/-private/util.d.ts +11 -0
  62. package/unstable-preview-types/-private/util.d.ts.map +1 -0
  63. package/unstable-preview-types/-private.d.ts +12 -0
  64. package/unstable-preview-types/-private.d.ts.map +1 -0
  65. package/unstable-preview-types/hooks.d.ts +5 -0
  66. package/unstable-preview-types/hooks.d.ts.map +1 -0
  67. package/unstable-preview-types/index.d.ts +75 -0
  68. package/unstable-preview-types/index.d.ts.map +1 -0
  69. package/unstable-preview-types/migration-support.d.ts +11 -0
  70. package/unstable-preview-types/migration-support.d.ts.map +1 -0
  71. package/addon/has-many-d1ca9997.js +0 -507
  72. package/addon/has-many-d1ca9997.js.map +0 -1
  73. package/addon/hooks-62d36619.js.map +0 -1
  74. package/addon/model-e642556d.js.map +0 -1
package/addon/-private.js CHANGED
@@ -1,2 +1,2 @@
1
- export { a as attr, b as belongsTo, h as hasMany } from "./has-many-d1ca9997";
2
- export { E as Errors, L as LEGACY_SUPPORT, R as ManyArray, M as Model, P as PromiseBelongsTo, a as PromiseManyArray } from "./model-e642556d";
1
+ export { a as attr, b as belongsTo, h as hasMany } from "./has-many-B3FpdTTp";
2
+ export { E as Errors, L as LEGACY_SUPPORT, R as ManyArray, M as Model, P as PromiseBelongsTo, a as PromiseManyArray, l as lookupLegacySupport } from "./model-DvL_NWQw";
@@ -0,0 +1,655 @@
1
+ import { assert, warn, deprecate } from '@ember/debug';
2
+ import { computed } from '@ember/object';
3
+ import { recordIdentifierFor } from '@ember-data/store';
4
+ import { peekCache } from '@ember-data/store/-private';
5
+ import { i as isElementDescriptor, n as normalizeModelName } from "./util-CSYkrwfb";
6
+ import { macroCondition, getOwnConfig } from '@embroider/macros';
7
+ import { RecordStore } from '@warp-drive/core-types/symbols';
8
+ import { l as lookupLegacySupport } from "./model-DvL_NWQw";
9
+ import { dasherize } from '@ember/string';
10
+ import { singularize } from 'ember-inflector';
11
+
12
+ /**
13
+ @module @ember-data/model
14
+ */
15
+ function _attr(type, options) {
16
+ if (typeof type === 'object') {
17
+ options = type;
18
+ type = undefined;
19
+ } else {
20
+ options = options || {};
21
+ }
22
+ const meta = {
23
+ type: type,
24
+ kind: 'attribute',
25
+ isAttribute: true,
26
+ options: options,
27
+ key: null
28
+ };
29
+ return computed({
30
+ get(key) {
31
+ if (macroCondition(getOwnConfig().env.DEBUG)) {
32
+ if (['currentState'].includes(key)) {
33
+ 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()}`);
34
+ }
35
+ }
36
+ if (this.isDestroyed || this.isDestroying) {
37
+ return;
38
+ }
39
+ return peekCache(this).getAttr(recordIdentifierFor(this), key);
40
+ },
41
+ set(key, value) {
42
+ if (macroCondition(getOwnConfig().env.DEBUG)) {
43
+ if (['currentState'].includes(key)) {
44
+ 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()}`);
45
+ }
46
+ }
47
+ const identifier = recordIdentifierFor(this);
48
+ assert(`Attempted to set '${key}' on the deleted record ${identifier.type}:${identifier.id} (${identifier.lid})`, !this.currentState.isDeleted);
49
+ const cache = peekCache(this);
50
+ const currentValue = cache.getAttr(identifier, key);
51
+ if (currentValue !== value) {
52
+ cache.setAttr(identifier, key, value);
53
+ if (!this.isValid) {
54
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
55
+ const {
56
+ errors
57
+ } = this;
58
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
59
+ if (errors.get(key)) {
60
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
61
+ errors.remove(key);
62
+ this.currentState.cleanErrorRequests();
63
+ }
64
+ }
65
+ }
66
+ return value;
67
+ }
68
+ }).meta(meta);
69
+ }
70
+
71
+ // NOTE: Usage of Explicit ANY
72
+ // -------------------------------------------------------------------
73
+ // any is required here because we are the maximal not the minimal
74
+ // subset of options allowed. If we used unknown, object, or
75
+ // Record<string, unknown> we would get type errors when we try to
76
+ // assert against a more specific implementation with precise options.
77
+ // -------------------------------------------------------------------
78
+
79
+ // see note on Explicit ANY above
80
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
81
+
82
+ /**
83
+ * The return type of `void` is a lie to appease TypeScript. The actual return type
84
+ * is a descriptor, but typescript incorrectly insists that decorator functions return
85
+ * `void` or `any`.
86
+ *
87
+ * @typedoc
88
+ */
89
+
90
+ /**
91
+ `attr` defines an attribute on a [Model](/ember-data/release/classes/Model).
92
+ By default, attributes are passed through as-is, however you can specify an
93
+ optional type to have the value automatically transformed.
94
+ EmberData ships with four basic transform types: `string`, `number`,
95
+ `boolean` and `date`. You can define your own transforms by subclassing
96
+ [Transform](/ember-data/release/classes/Transform).
97
+
98
+ Note that you cannot use `attr` to define an attribute of `id`.
99
+
100
+ `attr` takes an optional hash as a second parameter, currently
101
+ supported options are:
102
+
103
+ - `defaultValue`: Pass a string or a function to be called to set the attribute
104
+ to a default value if and only if the key is absent from the payload response.
105
+
106
+ Example
107
+
108
+ ```app/models/user.js
109
+ import Model, { attr } from '@ember-data/model';
110
+
111
+ export default class UserModel extends Model {
112
+ @attr('string') username;
113
+ @attr('string') email;
114
+ @attr('boolean', { defaultValue: false }) verified;
115
+ }
116
+ ```
117
+
118
+ Default value can also be a function. This is useful it you want to return
119
+ a new object for each attribute.
120
+
121
+ ```app/models/user.js
122
+ import Model, { attr } from '@ember-data/model';
123
+
124
+ export default class UserModel extends Model {
125
+ @attr('string') username;
126
+ @attr('string') email;
127
+
128
+ @attr({
129
+ defaultValue() {
130
+ return {};
131
+ }
132
+ })
133
+ settings;
134
+ }
135
+ ```
136
+
137
+ The `options` hash is passed as second argument to a transforms'
138
+ `serialize` and `deserialize` method. This allows to configure a
139
+ transformation and adapt the corresponding value, based on the config:
140
+
141
+ ```app/models/post.js
142
+ import Model, { attr } from '@ember-data/model';
143
+
144
+ export default class PostModel extends Model {
145
+ @attr('text', {
146
+ uppercase: true
147
+ })
148
+ text;
149
+ }
150
+ ```
151
+
152
+ ```app/transforms/text.js
153
+ export default class TextTransform {
154
+ serialize(value, options) {
155
+ if (options.uppercase) {
156
+ return value.toUpperCase();
157
+ }
158
+
159
+ return value;
160
+ }
161
+
162
+ deserialize(value) {
163
+ return value;
164
+ }
165
+
166
+ static create() {
167
+ return new this();
168
+ }
169
+ }
170
+ ```
171
+
172
+ @method attr
173
+ @public
174
+ @static
175
+ @for @ember-data/model
176
+ @param {String|Object} type the attribute type
177
+ @param {Object} options a hash of options
178
+ @return {Attribute}
179
+ */
180
+
181
+ // see note on DataDecorator for why void
182
+ function attr(type, options, desc) {
183
+ const args = [type, options, desc];
184
+ // see note on DataDecorator for why void
185
+ return isElementDescriptor(args) ? _attr()(...args) : _attr(type, options);
186
+ }
187
+
188
+ // get: () => getT;
189
+ // // set: (value: Awaited<getT>) => void;
190
+ // set: (value: getT) => void;
191
+ // // init: () => getT;
192
+ // };
193
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
194
+ // BelongsToDecoratorObject<getT>;
195
+
196
+ function _belongsTo(type, options) {
197
+ assert(`Expected options.async from @belongsTo('${type}', options) to be a boolean`, options && typeof options.async === 'boolean');
198
+ assert(`Expected options.inverse from @belongsTo('${type}', options) to be either null or the string type of the related resource.`, options.inverse === null || typeof options.inverse === 'string' && options.inverse.length > 0);
199
+ const meta = {
200
+ type: normalizeModelName(type),
201
+ isRelationship: true,
202
+ options: options,
203
+ kind: 'belongsTo',
204
+ name: '<Unknown BelongsTo>',
205
+ key: null
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(getOwnConfig().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
+ const support = lookupLegacySupport(this);
235
+ if (macroCondition(getOwnConfig().env.DEBUG)) {
236
+ if (['currentState'].includes(key)) {
237
+ 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()}`);
238
+ }
239
+ }
240
+ this[RecordStore]._join(() => {
241
+ support.setDirtyBelongsTo(key, value);
242
+ });
243
+ return support.getBelongsTo(key);
244
+ }
245
+ }).meta(meta);
246
+ }
247
+
248
+ /**
249
+ `belongsTo` is used to define One-To-One and One-To-Many, and One-To-None
250
+ relationships on a [Model](/ember-data/release/classes/Model).
251
+
252
+ `belongsTo` takes a configuration hash as a second parameter, currently
253
+ supported options are:
254
+
255
+ - `async`: (*required*) A boolean value used to declare whether this is a sync (false) or async (true) relationship.
256
+ - `inverse`: (*required*) A string used to identify the inverse property on a related model, or `null`.
257
+ - `polymorphic`: (*optional*) A boolean value to mark the relationship as polymorphic
258
+ - `as`: (*optional*) A string used to declare the abstract type "this" record satisfies for polymorphism.
259
+
260
+ ### Examples
261
+
262
+ To declare a **one-to-many** (or many-to-many) relationship, use
263
+ `belongsTo` in combination with `hasMany`:
264
+
265
+ ```js
266
+ // app/models/comment.js
267
+ import Model, { belongsTo } from '@ember-data/model';
268
+
269
+ export default class Comment extends Model {
270
+ @belongsTo('post', { async: false, inverse: 'comments' }) post;
271
+ }
272
+
273
+ // app/models/post.js
274
+ import Model, { hasMany } from '@ember-data/model';
275
+
276
+ export default class Post extends Model {
277
+ @hasMany('comment', { async: false, inverse: 'post' }) comments;
278
+ }
279
+ ```
280
+
281
+ To declare a **one-to-one** relationship with managed inverses, use `belongsTo` for both sides:
282
+
283
+ ```js
284
+ // app/models/author.js
285
+ import Model, { belongsTo } from '@ember-data/model';
286
+
287
+ export default class Author extends Model {
288
+ @belongsTo('address', { async: true, inverse: 'owner' }) address;
289
+ }
290
+
291
+ // app/models/address.js
292
+ import Model, { belongsTo } from '@ember-data/model';
293
+
294
+ export default class Address extends Model {
295
+ @belongsTo('author', { async: true, inverse: 'address' }) owner;
296
+ }
297
+ ```
298
+
299
+ To declare a **one-to-one** relationship without managed inverses, use `belongsTo` for both sides
300
+ with `null` as the inverse:
301
+
302
+ ```js
303
+ // app/models/author.js
304
+ import Model, { belongsTo } from '@ember-data/model';
305
+
306
+ export default class Author extends Model {
307
+ @belongsTo('address', { async: true, inverse: null }) address;
308
+ }
309
+
310
+ // app/models/address.js
311
+ import Model, { belongsTo } from '@ember-data/model';
312
+
313
+ export default class Address extends Model {
314
+ @belongsTo('author', { async: true, inverse: null }) owner;
315
+ }
316
+ ```
317
+
318
+ To declare a one-to-none relationship between two models, use
319
+ `belongsTo` with inverse set to `null` on just one side::
320
+
321
+ ```js
322
+ // app/models/person.js
323
+ import Model, { belongsTo } from '@ember-data/model';
324
+
325
+ export default class Person extends Model {
326
+ @belongsTo('person', { async: false, inverse: null }) bestFriend;
327
+ }
328
+ ```
329
+
330
+ #### Sync vs Async Relationships
331
+
332
+ EmberData fulfills relationships using resource data available in
333
+ the cache.
334
+
335
+ Sync relationships point directly to the known related resources.
336
+
337
+ When a relationship is declared as async, if any of the known related
338
+ resources have not been loaded, they will be fetched. The property
339
+ on the record when accessed provides a promise that resolves once
340
+ all resources are loaded.
341
+
342
+ Async relationships may take advantage of links. On access, if the related
343
+ link has not been loaded, or if any known resources are not available in
344
+ the cache, the fresh state will be fetched using the link.
345
+
346
+ In contrast to async relationship, accessing a sync relationship
347
+ will error on access when any of the known related resources have
348
+ not been loaded.
349
+
350
+ If you are using `links` with sync relationships, you have to use
351
+ the BelongsTo reference API to fetch or refresh related resources
352
+ that aren't loaded. For instance, for a `bestFriend` relationship:
353
+
354
+ ```js
355
+ person.belongsTo('bestFriend').reload();
356
+ ```
357
+
358
+ #### Polymorphic Relationships
359
+
360
+ To declare a polymorphic relationship, use `hasMany` with the `polymorphic`
361
+ option set to `true`:
362
+
363
+ ```js
364
+ // app/models/comment.js
365
+ import Model, { belongsTo } from '@ember-data/model';
366
+
367
+ export default class Comment extends Model {
368
+ @belongsTo('commentable', { async: false, inverse: 'comments', polymorphic: true }) parent;
369
+ }
370
+ ```
371
+
372
+ `'commentable'` here is referred to as the "abstract type" for the polymorphic
373
+ relationship.
374
+
375
+ Polymorphic relationships with `inverse: null` will accept any type of record as their content.
376
+ Polymorphic relationships with `inverse` set to a string will only accept records with a matching
377
+ inverse relationships declaring itself as satisfying the abstract type.
378
+
379
+ Below, 'as' is used to declare the that 'post' record satisfies the abstract type 'commentable'
380
+ for this relationship.
381
+
382
+ ```js
383
+ // app/models/post.js
384
+ import Model, { hasMany } from '@ember-data/model';
385
+
386
+ export default class Post extends Model {
387
+ @hasMany('comment', { async: false, inverse: 'parent', as: 'commentable' }) comments;
388
+ }
389
+ ```
390
+
391
+ Note: every Model that declares an inverse to a polymorphic relationship must
392
+ declare itself exactly the same. This is because polymorphism is based on structural
393
+ traits.
394
+
395
+ Polymorphic to polymorphic relationships are supported. Both sides of the relationship
396
+ must be declared as polymorphic, and the `as` option must be used to declare the abstract
397
+ type each record satisfies on both sides.
398
+
399
+ @method belongsTo
400
+ @public
401
+ @static
402
+ @for @ember-data/model
403
+ @param {string} type (optional) the name of the related resource
404
+ @param {object} options (optional) a hash of options
405
+ @return {PropertyDescriptor} relationship
406
+ */
407
+
408
+ // export function belongsTo<K extends Promise<unknown>, T extends Awaited<K> = Awaited<K>>(
409
+ // type: TypeFromInstance<NoNull<T>>,
410
+ // options: RelationshipOptions<T, true>
411
+ // ): RelationshipDecorator<K>;
412
+
413
+ function belongsTo(type, options) {
414
+ if (macroCondition(getOwnConfig().env.DEBUG)) {
415
+ assert(`belongsTo must be invoked with a type and options. Did you mean \`@belongsTo(${type}, { async: false, inverse: null })\`?`, !isElementDescriptor(arguments));
416
+ }
417
+ return _belongsTo(type, options);
418
+ }
419
+
420
+ /**
421
+ @module @ember-data/model
422
+ */
423
+ function normalizeType(type) {
424
+ if (macroCondition(getOwnConfig().deprecations.DEPRECATE_NON_STRICT_TYPES)) {
425
+ const result = singularize(dasherize(type));
426
+ deprecate(`The resource type '${type}' is not normalized. Update your application code to use '${result}' instead of '${type}'.`, result === type, {
427
+ id: 'ember-data:deprecate-non-strict-types',
428
+ until: '6.0',
429
+ for: 'ember-data',
430
+ since: {
431
+ available: '5.3',
432
+ enabled: '5.3'
433
+ }
434
+ });
435
+ return result;
436
+ }
437
+ return type;
438
+ }
439
+ function _hasMany(type, options) {
440
+ assert(`Expected hasMany options.async to be a boolean`, options && typeof options.async === 'boolean');
441
+
442
+ // Metadata about relationships is stored on the meta of
443
+ // the relationship. This is used for introspection and
444
+ // serialization. Note that `key` is populated lazily
445
+ // the first time the CP is called.
446
+ const meta = {
447
+ type: normalizeType(type),
448
+ options,
449
+ isRelationship: true,
450
+ kind: 'hasMany',
451
+ name: '<Unknown BelongsTo>',
452
+ key: null
453
+ };
454
+ return computed({
455
+ get(key) {
456
+ if (macroCondition(getOwnConfig().env.DEBUG)) {
457
+ if (['currentState'].includes(key)) {
458
+ 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()}`);
459
+ }
460
+ }
461
+ if (this.isDestroying || this.isDestroyed) {
462
+ return [];
463
+ }
464
+ return lookupLegacySupport(this).getHasMany(key);
465
+ },
466
+ set(key, records) {
467
+ if (macroCondition(getOwnConfig().env.DEBUG)) {
468
+ if (['currentState'].includes(key)) {
469
+ 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()}`);
470
+ }
471
+ }
472
+ const support = lookupLegacySupport(this);
473
+ const manyArray = support.getManyArray(key);
474
+ assert(`You must pass an array of records to set a hasMany relationship`, Array.isArray(records));
475
+ this[RecordStore]._join(() => {
476
+ manyArray.splice(0, manyArray.length, ...records);
477
+ });
478
+ return support.getHasMany(key);
479
+ }
480
+ }).meta(meta);
481
+ }
482
+
483
+ /**
484
+ `hasMany` is used to define Many-To-One and Many-To-Many, and Many-To-None
485
+ relationships on a [Model](/ember-data/release/classes/Model).
486
+
487
+ `hasMany` takes a configuration hash as a second parameter, currently
488
+ supported options are:
489
+
490
+ - `async`: (*required*) A boolean value used to declare whether this is a sync (false) or async (true) relationship.
491
+ - `inverse`: (*required*) A string used to identify the inverse property on a related model, or `null`.
492
+ - `polymorphic`: (*optional*) A boolean value to mark the relationship as polymorphic
493
+ - `as`: (*optional*) A string used to declare the abstract type "this" record satisfies for polymorphism.
494
+
495
+ ### Examples
496
+
497
+ To declare a **many-to-one** (or one-to-many) relationship, use
498
+ `belongsTo` in combination with `hasMany`:
499
+
500
+ ```js
501
+ // app/models/post.js
502
+ import Model, { hasMany } from '@ember-data/model';
503
+
504
+ export default class Post extends Model {
505
+ @hasMany('comment', { async: false, inverse: 'post' }) comments;
506
+ }
507
+
508
+
509
+ // app/models/comment.js
510
+ import Model, { belongsTo } from '@ember-data/model';
511
+
512
+ export default class Comment extends Model {
513
+ @belongsTo('post', { async: false, inverse: 'comments' }) post;
514
+ }
515
+ ```
516
+
517
+ To declare a **many-to-many** relationship with managed inverses, use `hasMany` for both sides:
518
+
519
+ ```js
520
+ // app/models/post.js
521
+ import Model, { hasMany } from '@ember-data/model';
522
+
523
+ export default class Post extends Model {
524
+ @hasMany('tag', { async: true, inverse: 'posts' }) tags;
525
+ }
526
+
527
+ // app/models/tag.js
528
+ import Model, { hasMany } from '@ember-data/model';
529
+
530
+ export default class Tag extends Model {
531
+ @hasMany('post', { async: true, inverse: 'tags' }) posts;
532
+ }
533
+ ```
534
+
535
+ To declare a **many-to-many** relationship without managed inverses, use `hasMany` for both sides
536
+ with `null` as the inverse:
537
+
538
+ ```js
539
+ // app/models/post.js
540
+ import Model, { hasMany } from '@ember-data/model';
541
+
542
+ export default class Post extends Model {
543
+ @hasMany('tag', { async: true, inverse: null }) tags;
544
+ }
545
+
546
+ // app/models/tag.js
547
+ import Model, { hasMany } from '@ember-data/model';
548
+
549
+ export default class Tag extends Model {
550
+ @hasMany('post', { async: true, inverse: null }) posts;
551
+ }
552
+ ```
553
+
554
+ To declare a many-to-none relationship between two models, use
555
+ `hasMany` with inverse set to `null` on just one side::
556
+
557
+ ```js
558
+ // app/models/post.js
559
+ import Model, { hasMany } from '@ember-data/model';
560
+
561
+ export default class Post extends Model {
562
+ @hasMany('category', { async: true, inverse: null }) categories;
563
+ }
564
+ ```
565
+
566
+ #### Sync vs Async Relationships
567
+
568
+ EmberData fulfills relationships using resource data available in
569
+ the cache.
570
+
571
+ Sync relationships point directly to the known related resources.
572
+
573
+ When a relationship is declared as async, if any of the known related
574
+ resources have not been loaded, they will be fetched. The property
575
+ on the record when accessed provides a promise that resolves once
576
+ all resources are loaded.
577
+
578
+ Async relationships may take advantage of links. On access, if the related
579
+ link has not been loaded, or if any known resources are not available in
580
+ the cache, the fresh state will be fetched using the link.
581
+
582
+ In contrast to async relationship, accessing a sync relationship
583
+ will error on access when any of the known related resources have
584
+ not been loaded.
585
+
586
+ If you are using `links` with sync relationships, you have to use
587
+ the HasMany reference API to fetch or refresh related resources
588
+ that aren't loaded. For instance, for a `comments` relationship:
589
+
590
+ ```js
591
+ post.hasMany('comments').reload();
592
+ ```
593
+
594
+ #### Polymorphic Relationships
595
+
596
+ To declare a polymorphic relationship, use `hasMany` with the `polymorphic`
597
+ option set to `true`:
598
+
599
+ ```js
600
+ // app/models/comment.js
601
+ import Model, { belongsTo } from '@ember-data/model';
602
+
603
+ export default class Comment extends Model {
604
+ @belongsTo('commentable', { async: false, inverse: 'comments', polymorphic: true }) parent;
605
+ }
606
+ ```
607
+
608
+ `'commentable'` here is referred to as the "abstract type" for the polymorphic
609
+ relationship.
610
+
611
+ Polymorphic relationships with `inverse: null` will accept any type of record as their content.
612
+ Polymorphic relationships with `inverse` set to a string will only accept records with a matching
613
+ inverse relationships declaring itself as satisfying the abstract type.
614
+
615
+ Below, 'as' is used to declare the that 'post' record satisfies the abstract type 'commentable'
616
+ for this relationship.
617
+
618
+ ```js
619
+ // app/models/post.js
620
+ import Model, { hasMany } from '@ember-data/model';
621
+
622
+ export default class Post extends Model {
623
+ @hasMany('comment', { async: false, inverse: 'parent', as: 'commentable' }) comments;
624
+ }
625
+ ```
626
+
627
+ Note: every Model that declares an inverse to a polymorphic relationship must
628
+ declare itself exactly the same. This is because polymorphism is based on structural
629
+ traits.
630
+
631
+ Polymorphic to polymorphic relationships are supported. Both sides of the relationship
632
+ must be declared as polymorphic, and the `as` option must be used to declare the abstract
633
+ type each record satisfies on both sides.
634
+
635
+ @method hasMany
636
+ @public
637
+ @static
638
+ @for @ember-data/model
639
+ @param {string} type (optional) the name of the related resource
640
+ @param {object} options (optional) a hash of options
641
+ @return {PropertyDescriptor} relationship
642
+ */
643
+
644
+ // export function hasMany<K extends Promise<unknown>, T extends Awaited<K> = Awaited<K>>(
645
+ // type: TypeFromInstance<NoNull<T>>,
646
+ // options: RelationshipOptions<T, true>
647
+ // ): RelationshipDecorator<K>;
648
+
649
+ function hasMany(type, options) {
650
+ if (macroCondition(getOwnConfig().env.DEBUG)) {
651
+ assert(`hasMany must be invoked with a type and options. Did you mean \`@hasMany(${type}, { async: false, inverse: null })\`?`, !isElementDescriptor(arguments));
652
+ }
653
+ return _hasMany(type, options);
654
+ }
655
+ export { attr as a, belongsTo as b, hasMany as h };