@ember-data/model 5.5.0-alpha.9 → 5.5.0

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