@ember-data/model 5.4.0-beta.2 → 5.4.0-beta.20

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 (106) 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-DT3JBYYG.js +674 -0
  10. package/dist/has-many-DT3JBYYG.js.map +1 -0
  11. package/dist/hooks-CNY5g4Ab.js +75 -0
  12. package/dist/hooks-CNY5g4Ab.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 +213 -0
  18. package/dist/migration-support.js.map +1 -0
  19. package/{addon/model-YsOraZ6y.js → dist/model-rk3atPqV.js} +899 -936
  20. package/dist/model-rk3atPqV.js.map +1 -0
  21. package/dist/schema-provider-Bvogtzx9.js +259 -0
  22. package/dist/schema-provider-Bvogtzx9.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 +58 -103
  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 +131 -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 +503 -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 +515 -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 +76 -0
  91. package/unstable-preview-types/index.d.ts.map +1 -0
  92. package/unstable-preview-types/migration-support.d.ts +64 -0
  93. package/unstable-preview-types/migration-support.d.ts.map +1 -0
  94. package/addon/-private.js +0 -2
  95. package/addon/has-many-PdQBns8a.js +0 -323
  96. package/addon/has-many-PdQBns8a.js.map +0 -1
  97. package/addon/hooks-dXmQbIOF.js +0 -176
  98. package/addon/hooks-dXmQbIOF.js.map +0 -1
  99. package/addon/hooks.js +0 -1
  100. package/addon/index.js +0 -3
  101. package/addon/migration-support.js +0 -118
  102. package/addon/migration-support.js.map +0 -1
  103. package/addon/model-YsOraZ6y.js.map +0 -1
  104. package/addon/util-3DHZJC9h.js +0 -38
  105. package/addon/util-3DHZJC9h.js.map +0 -1
  106. package/addon-main.js +0 -93
@@ -0,0 +1,674 @@
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-rk3atPqV.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
+ const support = lookupLegacySupport(this);
240
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
241
+ if (['currentState'].includes(key)) {
242
+ 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()}`);
243
+ }
244
+ }
245
+ this[RecordStore]._join(() => {
246
+ support.setDirtyBelongsTo(key, value);
247
+ });
248
+ return support.getBelongsTo(key);
249
+ }
250
+ }).meta(meta);
251
+ }
252
+
253
+ /**
254
+ `belongsTo` is used to define One-To-One and One-To-Many, and One-To-None
255
+ relationships on a [Model](/ember-data/release/classes/Model).
256
+
257
+ `belongsTo` takes a configuration hash as a second parameter, currently
258
+ supported options are:
259
+
260
+ - `async`: (*required*) A boolean value used to declare whether this is a sync (false) or async (true) relationship.
261
+ - `inverse`: (*required*) A string used to identify the inverse property on a related model, or `null`.
262
+ - `polymorphic`: (*optional*) A boolean value to mark the relationship as polymorphic
263
+ - `as`: (*optional*) A string used to declare the abstract type "this" record satisfies for polymorphism.
264
+
265
+ ### Examples
266
+
267
+ To declare a **one-to-many** (or many-to-many) relationship, use
268
+ `belongsTo` in combination with `hasMany`:
269
+
270
+ ```js
271
+ // app/models/comment.js
272
+ import Model, { belongsTo } from '@ember-data/model';
273
+
274
+ export default class Comment extends Model {
275
+ @belongsTo('post', { async: false, inverse: 'comments' }) post;
276
+ }
277
+
278
+ // app/models/post.js
279
+ import Model, { hasMany } from '@ember-data/model';
280
+
281
+ export default class Post extends Model {
282
+ @hasMany('comment', { async: false, inverse: 'post' }) comments;
283
+ }
284
+ ```
285
+
286
+ To declare a **one-to-one** relationship with managed inverses, use `belongsTo` for both sides:
287
+
288
+ ```js
289
+ // app/models/author.js
290
+ import Model, { belongsTo } from '@ember-data/model';
291
+
292
+ export default class Author extends Model {
293
+ @belongsTo('address', { async: true, inverse: 'owner' }) address;
294
+ }
295
+
296
+ // app/models/address.js
297
+ import Model, { belongsTo } from '@ember-data/model';
298
+
299
+ export default class Address extends Model {
300
+ @belongsTo('author', { async: true, inverse: 'address' }) owner;
301
+ }
302
+ ```
303
+
304
+ To declare a **one-to-one** relationship without managed inverses, use `belongsTo` for both sides
305
+ with `null` as the inverse:
306
+
307
+ ```js
308
+ // app/models/author.js
309
+ import Model, { belongsTo } from '@ember-data/model';
310
+
311
+ export default class Author extends Model {
312
+ @belongsTo('address', { async: true, inverse: null }) address;
313
+ }
314
+
315
+ // app/models/address.js
316
+ import Model, { belongsTo } from '@ember-data/model';
317
+
318
+ export default class Address extends Model {
319
+ @belongsTo('author', { async: true, inverse: null }) owner;
320
+ }
321
+ ```
322
+
323
+ To declare a one-to-none relationship between two models, use
324
+ `belongsTo` with inverse set to `null` on just one side::
325
+
326
+ ```js
327
+ // app/models/person.js
328
+ import Model, { belongsTo } from '@ember-data/model';
329
+
330
+ export default class Person extends Model {
331
+ @belongsTo('person', { async: false, inverse: null }) bestFriend;
332
+ }
333
+ ```
334
+
335
+ #### Sync vs Async Relationships
336
+
337
+ EmberData fulfills relationships using resource data available in
338
+ the cache.
339
+
340
+ Sync relationships point directly to the known related resources.
341
+
342
+ When a relationship is declared as async, if any of the known related
343
+ resources have not been loaded, they will be fetched. The property
344
+ on the record when accessed provides a promise that resolves once
345
+ all resources are loaded.
346
+
347
+ Async relationships may take advantage of links. On access, if the related
348
+ link has not been loaded, or if any known resources are not available in
349
+ the cache, the fresh state will be fetched using the link.
350
+
351
+ In contrast to async relationship, accessing a sync relationship
352
+ will error on access when any of the known related resources have
353
+ not been loaded.
354
+
355
+ If you are using `links` with sync relationships, you have to use
356
+ the BelongsTo reference API to fetch or refresh related resources
357
+ that aren't loaded. For instance, for a `bestFriend` relationship:
358
+
359
+ ```js
360
+ person.belongsTo('bestFriend').reload();
361
+ ```
362
+
363
+ #### Polymorphic Relationships
364
+
365
+ To declare a polymorphic relationship, use `hasMany` with the `polymorphic`
366
+ option set to `true`:
367
+
368
+ ```js
369
+ // app/models/comment.js
370
+ import Model, { belongsTo } from '@ember-data/model';
371
+
372
+ export default class Comment extends Model {
373
+ @belongsTo('commentable', { async: false, inverse: 'comments', polymorphic: true }) parent;
374
+ }
375
+ ```
376
+
377
+ `'commentable'` here is referred to as the "abstract type" for the polymorphic
378
+ relationship.
379
+
380
+ Polymorphic relationships with `inverse: null` will accept any type of record as their content.
381
+ Polymorphic relationships with `inverse` set to a string will only accept records with a matching
382
+ inverse relationships declaring itself as satisfying the abstract type.
383
+
384
+ Below, 'as' is used to declare the that 'post' record satisfies the abstract type 'commentable'
385
+ for this relationship.
386
+
387
+ ```js
388
+ // app/models/post.js
389
+ import Model, { hasMany } from '@ember-data/model';
390
+
391
+ export default class Post extends Model {
392
+ @hasMany('comment', { async: false, inverse: 'parent', as: 'commentable' }) comments;
393
+ }
394
+ ```
395
+
396
+ Note: every Model that declares an inverse to a polymorphic relationship must
397
+ declare itself exactly the same. This is because polymorphism is based on structural
398
+ traits.
399
+
400
+ Polymorphic to polymorphic relationships are supported. Both sides of the relationship
401
+ must be declared as polymorphic, and the `as` option must be used to declare the abstract
402
+ type each record satisfies on both sides.
403
+
404
+ @method belongsTo
405
+ @public
406
+ @static
407
+ @for @ember-data/model
408
+ @param {string} type (optional) the name of the related resource
409
+ @param {object} options (optional) a hash of options
410
+ @return {PropertyDescriptor} relationship
411
+ */
412
+
413
+ // export function belongsTo<K extends Promise<unknown>, T extends Awaited<K> = Awaited<K>>(
414
+ // type: TypeFromInstance<NoNull<T>>,
415
+ // options: RelationshipOptions<T, true>
416
+ // ): RelationshipDecorator<K>;
417
+
418
+ function belongsTo(type, options) {
419
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
420
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
421
+ if (!test) {
422
+ throw new Error(`belongsTo must be invoked with a type and options. Did you mean \`@belongsTo(${type}, { async: false, inverse: null })\`?`);
423
+ }
424
+ })(!isElementDescriptor(arguments)) : {};
425
+ }
426
+ return _belongsTo(type, options);
427
+ }
428
+
429
+ /**
430
+ @module @ember-data/model
431
+ */
432
+ function normalizeType(type) {
433
+ if (macroCondition(getGlobalConfig().WarpDrive.deprecations.DEPRECATE_NON_STRICT_TYPES)) {
434
+ const result = singularize(dasherize(type));
435
+ deprecate(`The resource type '${type}' is not normalized. Update your application code to use '${result}' instead of '${type}'.`, result === type, {
436
+ id: 'ember-data:deprecate-non-strict-types',
437
+ until: '6.0',
438
+ for: 'ember-data',
439
+ since: {
440
+ available: '4.13',
441
+ enabled: '5.3'
442
+ }
443
+ });
444
+ return result;
445
+ }
446
+ return type;
447
+ }
448
+ function _hasMany(type, options) {
449
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
450
+ if (!test) {
451
+ throw new Error(`Expected hasMany options.async to be a boolean`);
452
+ }
453
+ })(options && typeof options.async === 'boolean') : {};
454
+
455
+ // Metadata about relationships is stored on the meta of
456
+ // the relationship. This is used for introspection and
457
+ // serialization. Note that `key` is populated lazily
458
+ // the first time the CP is called.
459
+ const meta = {
460
+ type: normalizeType(type),
461
+ options,
462
+ kind: 'hasMany',
463
+ name: '<Unknown BelongsTo>'
464
+ };
465
+ return computed({
466
+ get(key) {
467
+ if (macroCondition(getGlobalConfig().WarpDrive.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
+ if (this.isDestroying || this.isDestroyed) {
473
+ return [];
474
+ }
475
+ return lookupLegacySupport(this).getHasMany(key);
476
+ },
477
+ set(key, records) {
478
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
479
+ if (['currentState'].includes(key)) {
480
+ 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()}`);
481
+ }
482
+ }
483
+ const support = lookupLegacySupport(this);
484
+ const manyArray = support.getManyArray(key);
485
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
486
+ if (!test) {
487
+ throw new Error(`You must pass an array of records to set a hasMany relationship`);
488
+ }
489
+ })(Array.isArray(records)) : {};
490
+ this[RecordStore]._join(() => {
491
+ manyArray.splice(0, manyArray.length, ...records);
492
+ });
493
+ return support.getHasMany(key);
494
+ }
495
+ }).meta(meta);
496
+ }
497
+
498
+ /**
499
+ `hasMany` is used to define Many-To-One and Many-To-Many, and Many-To-None
500
+ relationships on a [Model](/ember-data/release/classes/Model).
501
+
502
+ `hasMany` takes a configuration hash as a second parameter, currently
503
+ supported options are:
504
+
505
+ - `async`: (*required*) A boolean value used to declare whether this is a sync (false) or async (true) relationship.
506
+ - `inverse`: (*required*) A string used to identify the inverse property on a related model, or `null`.
507
+ - `polymorphic`: (*optional*) A boolean value to mark the relationship as polymorphic
508
+ - `as`: (*optional*) A string used to declare the abstract type "this" record satisfies for polymorphism.
509
+
510
+ ### Examples
511
+
512
+ To declare a **many-to-one** (or one-to-many) relationship, use
513
+ `belongsTo` in combination with `hasMany`:
514
+
515
+ ```js
516
+ // app/models/post.js
517
+ import Model, { hasMany } from '@ember-data/model';
518
+
519
+ export default class Post extends Model {
520
+ @hasMany('comment', { async: false, inverse: 'post' }) comments;
521
+ }
522
+
523
+
524
+ // app/models/comment.js
525
+ import Model, { belongsTo } from '@ember-data/model';
526
+
527
+ export default class Comment extends Model {
528
+ @belongsTo('post', { async: false, inverse: 'comments' }) post;
529
+ }
530
+ ```
531
+
532
+ To declare a **many-to-many** relationship with managed inverses, use `hasMany` for both sides:
533
+
534
+ ```js
535
+ // app/models/post.js
536
+ import Model, { hasMany } from '@ember-data/model';
537
+
538
+ export default class Post extends Model {
539
+ @hasMany('tag', { async: true, inverse: 'posts' }) tags;
540
+ }
541
+
542
+ // app/models/tag.js
543
+ import Model, { hasMany } from '@ember-data/model';
544
+
545
+ export default class Tag extends Model {
546
+ @hasMany('post', { async: true, inverse: 'tags' }) posts;
547
+ }
548
+ ```
549
+
550
+ To declare a **many-to-many** relationship without managed inverses, use `hasMany` for both sides
551
+ with `null` as the inverse:
552
+
553
+ ```js
554
+ // app/models/post.js
555
+ import Model, { hasMany } from '@ember-data/model';
556
+
557
+ export default class Post extends Model {
558
+ @hasMany('tag', { async: true, inverse: null }) tags;
559
+ }
560
+
561
+ // app/models/tag.js
562
+ import Model, { hasMany } from '@ember-data/model';
563
+
564
+ export default class Tag extends Model {
565
+ @hasMany('post', { async: true, inverse: null }) posts;
566
+ }
567
+ ```
568
+
569
+ To declare a many-to-none relationship between two models, use
570
+ `hasMany` with inverse set to `null` on just one side::
571
+
572
+ ```js
573
+ // app/models/post.js
574
+ import Model, { hasMany } from '@ember-data/model';
575
+
576
+ export default class Post extends Model {
577
+ @hasMany('category', { async: true, inverse: null }) categories;
578
+ }
579
+ ```
580
+
581
+ #### Sync vs Async Relationships
582
+
583
+ EmberData fulfills relationships using resource data available in
584
+ the cache.
585
+
586
+ Sync relationships point directly to the known related resources.
587
+
588
+ When a relationship is declared as async, if any of the known related
589
+ resources have not been loaded, they will be fetched. The property
590
+ on the record when accessed provides a promise that resolves once
591
+ all resources are loaded.
592
+
593
+ Async relationships may take advantage of links. On access, if the related
594
+ link has not been loaded, or if any known resources are not available in
595
+ the cache, the fresh state will be fetched using the link.
596
+
597
+ In contrast to async relationship, accessing a sync relationship
598
+ will error on access when any of the known related resources have
599
+ not been loaded.
600
+
601
+ If you are using `links` with sync relationships, you have to use
602
+ the HasMany reference API to fetch or refresh related resources
603
+ that aren't loaded. For instance, for a `comments` relationship:
604
+
605
+ ```js
606
+ post.hasMany('comments').reload();
607
+ ```
608
+
609
+ #### Polymorphic Relationships
610
+
611
+ To declare a polymorphic relationship, use `hasMany` with the `polymorphic`
612
+ option set to `true`:
613
+
614
+ ```js
615
+ // app/models/comment.js
616
+ import Model, { belongsTo } from '@ember-data/model';
617
+
618
+ export default class Comment extends Model {
619
+ @belongsTo('commentable', { async: false, inverse: 'comments', polymorphic: true }) parent;
620
+ }
621
+ ```
622
+
623
+ `'commentable'` here is referred to as the "abstract type" for the polymorphic
624
+ relationship.
625
+
626
+ Polymorphic relationships with `inverse: null` will accept any type of record as their content.
627
+ Polymorphic relationships with `inverse` set to a string will only accept records with a matching
628
+ inverse relationships declaring itself as satisfying the abstract type.
629
+
630
+ Below, 'as' is used to declare the that 'post' record satisfies the abstract type 'commentable'
631
+ for this relationship.
632
+
633
+ ```js
634
+ // app/models/post.js
635
+ import Model, { hasMany } from '@ember-data/model';
636
+
637
+ export default class Post extends Model {
638
+ @hasMany('comment', { async: false, inverse: 'parent', as: 'commentable' }) comments;
639
+ }
640
+ ```
641
+
642
+ Note: every Model that declares an inverse to a polymorphic relationship must
643
+ declare itself exactly the same. This is because polymorphism is based on structural
644
+ traits.
645
+
646
+ Polymorphic to polymorphic relationships are supported. Both sides of the relationship
647
+ must be declared as polymorphic, and the `as` option must be used to declare the abstract
648
+ type each record satisfies on both sides.
649
+
650
+ @method hasMany
651
+ @public
652
+ @static
653
+ @for @ember-data/model
654
+ @param {string} type (optional) the name of the related resource
655
+ @param {object} options (optional) a hash of options
656
+ @return {PropertyDescriptor} relationship
657
+ */
658
+
659
+ // export function hasMany<K extends Promise<unknown>, T extends Awaited<K> = Awaited<K>>(
660
+ // type: TypeFromInstance<NoNull<T>>,
661
+ // options: RelationshipOptions<T, true>
662
+ // ): RelationshipDecorator<K>;
663
+
664
+ function hasMany(type, options) {
665
+ if (macroCondition(getGlobalConfig().WarpDrive.env.DEBUG)) {
666
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
667
+ if (!test) {
668
+ throw new Error(`hasMany must be invoked with a type and options. Did you mean \`@hasMany(${type}, { async: false, inverse: null })\`?`);
669
+ }
670
+ })(!isElementDescriptor(arguments)) : {};
671
+ }
672
+ return _hasMany(type, options);
673
+ }
674
+ export { attr as a, belongsTo as b, hasMany as h };