@ember-data/model 5.4.0-alpha.6 → 5.4.0-alpha.60

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 (78) hide show
  1. package/README.md +19 -0
  2. package/addon/-private.js +2 -2
  3. package/addon/has-many-BuTJfT5a.js +652 -0
  4. package/addon/has-many-BuTJfT5a.js.map +1 -0
  5. package/addon/{hooks-7a58334b.js → hooks-BMoEt0SI.js} +34 -15
  6. package/addon/hooks-BMoEt0SI.js.map +1 -0
  7. package/addon/hooks.js +1 -1
  8. package/addon/index.js +3 -3
  9. package/addon/index.js.map +1 -1
  10. package/addon/migration-support.js +118 -0
  11. package/addon/migration-support.js.map +1 -0
  12. package/addon/{model-520a761c.js → model-DJ9C4aZ-.js} +2159 -2113
  13. package/addon/model-DJ9C4aZ-.js.map +1 -0
  14. package/addon/util-CSYkrwfb.js +35 -0
  15. package/addon/util-CSYkrwfb.js.map +1 -0
  16. package/addon-main.js +1 -0
  17. package/ember-data-logo-dark.svg +12 -0
  18. package/ember-data-logo-light.svg +12 -0
  19. package/package.json +88 -37
  20. package/unstable-preview-types/-private/attr.d.ts +174 -0
  21. package/unstable-preview-types/-private/attr.d.ts.map +1 -0
  22. package/unstable-preview-types/-private/attr.type-test.d.ts +4 -0
  23. package/unstable-preview-types/-private/attr.type-test.d.ts.map +1 -0
  24. package/unstable-preview-types/-private/belongs-to.d.ts +180 -0
  25. package/unstable-preview-types/-private/belongs-to.d.ts.map +1 -0
  26. package/unstable-preview-types/-private/belongs-to.type-test.d.ts +4 -0
  27. package/unstable-preview-types/-private/belongs-to.type-test.d.ts.map +1 -0
  28. package/unstable-preview-types/-private/debug/assert-polymorphic-type.d.ts +8 -0
  29. package/unstable-preview-types/-private/debug/assert-polymorphic-type.d.ts.map +1 -0
  30. package/unstable-preview-types/-private/errors.d.ts +308 -0
  31. package/unstable-preview-types/-private/errors.d.ts.map +1 -0
  32. package/unstable-preview-types/-private/has-many.d.ts +169 -0
  33. package/unstable-preview-types/-private/has-many.d.ts.map +1 -0
  34. package/unstable-preview-types/-private/has-many.type-test.d.ts +4 -0
  35. package/unstable-preview-types/-private/has-many.type-test.d.ts.map +1 -0
  36. package/unstable-preview-types/-private/hooks.d.ts +13 -0
  37. package/unstable-preview-types/-private/hooks.d.ts.map +1 -0
  38. package/unstable-preview-types/-private/legacy-relationships-support.d.ts +64 -0
  39. package/unstable-preview-types/-private/legacy-relationships-support.d.ts.map +1 -0
  40. package/unstable-preview-types/-private/many-array.d.ts +198 -0
  41. package/unstable-preview-types/-private/many-array.d.ts.map +1 -0
  42. package/unstable-preview-types/-private/model-for-mixin.d.ts +6 -0
  43. package/unstable-preview-types/-private/model-for-mixin.d.ts.map +1 -0
  44. package/unstable-preview-types/-private/model-methods.d.ts +36 -0
  45. package/unstable-preview-types/-private/model-methods.d.ts.map +1 -0
  46. package/unstable-preview-types/-private/model.d.ts +1303 -0
  47. package/unstable-preview-types/-private/model.d.ts.map +1 -0
  48. package/unstable-preview-types/-private/model.type-test.d.ts +4 -0
  49. package/unstable-preview-types/-private/model.type-test.d.ts.map +1 -0
  50. package/unstable-preview-types/-private/notify-changes.d.ts +8 -0
  51. package/unstable-preview-types/-private/notify-changes.d.ts.map +1 -0
  52. package/unstable-preview-types/-private/promise-belongs-to.d.ts +49 -0
  53. package/unstable-preview-types/-private/promise-belongs-to.d.ts.map +1 -0
  54. package/unstable-preview-types/-private/promise-many-array.d.ts +131 -0
  55. package/unstable-preview-types/-private/promise-many-array.d.ts.map +1 -0
  56. package/unstable-preview-types/-private/promise-proxy-base.d.ts +35 -0
  57. package/unstable-preview-types/-private/record-state.d.ts +91 -0
  58. package/unstable-preview-types/-private/record-state.d.ts.map +1 -0
  59. package/unstable-preview-types/-private/references/belongs-to.d.ts +503 -0
  60. package/unstable-preview-types/-private/references/belongs-to.d.ts.map +1 -0
  61. package/unstable-preview-types/-private/references/has-many.d.ts +515 -0
  62. package/unstable-preview-types/-private/references/has-many.d.ts.map +1 -0
  63. package/unstable-preview-types/-private/schema-provider.d.ts +27 -0
  64. package/unstable-preview-types/-private/schema-provider.d.ts.map +1 -0
  65. package/unstable-preview-types/-private/util.d.ts +11 -0
  66. package/unstable-preview-types/-private/util.d.ts.map +1 -0
  67. package/unstable-preview-types/-private.d.ts +12 -0
  68. package/unstable-preview-types/-private.d.ts.map +1 -0
  69. package/unstable-preview-types/hooks.d.ts +5 -0
  70. package/unstable-preview-types/hooks.d.ts.map +1 -0
  71. package/unstable-preview-types/index.d.ts +75 -0
  72. package/unstable-preview-types/index.d.ts.map +1 -0
  73. package/unstable-preview-types/migration-support.d.ts +11 -0
  74. package/unstable-preview-types/migration-support.d.ts.map +1 -0
  75. package/addon/has-many-665b8730.js +0 -507
  76. package/addon/has-many-665b8730.js.map +0 -1
  77. package/addon/hooks-7a58334b.js.map +0 -1
  78. package/addon/model-520a761c.js.map +0 -1
@@ -0,0 +1,1303 @@
1
+ declare module '@ember-data/model/-private/model' {
2
+ /**
3
+ @module @ember-data/model
4
+ */
5
+ /// <reference types="ember-source/types" />
6
+ import EmberObject from '@ember/object';
7
+ import type Store from '@ember-data/store';
8
+ import type { StableRecordIdentifier } from '@warp-drive/core-types';
9
+ import type { Cache } from '@warp-drive/core-types/cache';
10
+ import type { AttributeSchema, RelationshipSchema } from '@warp-drive/core-types/schema';
11
+ import { RecordStore } from '@warp-drive/core-types/symbols';
12
+ import Errors from '@ember-data/model/-private/errors';
13
+ import type { MinimalLegacyRecord } from '@ember-data/model/-private/model-methods';
14
+ import { belongsTo, changedAttributes, createSnapshot, deleteRecord, destroyRecord, hasMany, reload, rollbackAttributes, save, serialize, unloadRecord } from '@ember-data/model/-private/model-methods';
15
+ import RecordState from '@ember-data/model/-private/record-state';
16
+ export type ModelCreateArgs = {
17
+ _createProps: Record<string, unknown>;
18
+ _secretInit: {
19
+ identifier: StableRecordIdentifier;
20
+ cache: Cache;
21
+ store: Store;
22
+ cb: (record: Model, cache: Cache, identifier: StableRecordIdentifier, store: Store) => void;
23
+ };
24
+ };
25
+ export type StaticModel = typeof Model & {
26
+ create(options: ModelCreateArgs): Model;
27
+ };
28
+ export type ModelFactory = {
29
+ class: StaticModel;
30
+ };
31
+ export type FactoryCache = Record<string, ModelFactory>;
32
+ export type ModelStore = Store & {
33
+ _modelFactoryCache: FactoryCache;
34
+ };
35
+ /**
36
+ Base class from which Models can be defined.
37
+
38
+ ```js
39
+ import Model, { attr } from '@ember-data/model';
40
+
41
+ export default class User extends Model {
42
+ @attr name;
43
+ }
44
+ ```
45
+
46
+ Models are used both to define the static schema for a
47
+ particular resource type as well as the class to instantiate
48
+ to present that data from cache.
49
+
50
+ @class Model
51
+ @public
52
+ @extends Ember.EmberObject
53
+ */
54
+ class Model extends EmberObject implements MinimalLegacyRecord {
55
+ store: Store;
56
+ ___recordState: RecordState;
57
+ ___private_notifications: object;
58
+ _createProps: null;
59
+ _secretInit: null;
60
+ [RecordStore]: Store;
61
+ init(options: ModelCreateArgs): void;
62
+ destroy(): void;
63
+ /**
64
+ If this property is `true` the record is in the `empty`
65
+ state. Empty is the first state all records enter after they have
66
+ been created. Most records created by the store will quickly
67
+ transition to the `loading` state if data needs to be fetched from
68
+ the server or the `created` state if the record is created on the
69
+ client. A record can also enter the empty state if the adapter is
70
+ unable to locate the record.
71
+
72
+ @property isEmpty
73
+ @public
74
+ @type {Boolean}
75
+ @readOnly
76
+ */
77
+ get isEmpty(): boolean;
78
+ /**
79
+ If this property is `true` the record is in the `loading` state. A
80
+ record enters this state when the store asks the adapter for its
81
+ data. It remains in this state until the adapter provides the
82
+ requested data.
83
+
84
+ @property isLoading
85
+ @public
86
+ @type {Boolean}
87
+ @readOnly
88
+ */
89
+ get isLoading(): boolean;
90
+ /**
91
+ If this property is `true` the record is in the `loaded` state. A
92
+ record enters this state when its data is populated. Most of a
93
+ record's lifecycle is spent inside substates of the `loaded`
94
+ state.
95
+
96
+ Example
97
+
98
+ ```javascript
99
+ let record = store.createRecord('model');
100
+ record.isLoaded; // true
101
+
102
+ store.findRecord('model', 1).then(function(model) {
103
+ model.isLoaded; // true
104
+ });
105
+ ```
106
+
107
+ @property isLoaded
108
+ @public
109
+ @type {Boolean}
110
+ @readOnly
111
+ */
112
+ get isLoaded(): boolean;
113
+ /**
114
+ If this property is `true` the record is in the `dirty` state. The
115
+ record has local changes that have not yet been saved by the
116
+ adapter. This includes records that have been created (but not yet
117
+ saved) or deleted.
118
+
119
+ Example
120
+
121
+ ```javascript
122
+ let record = store.createRecord('model');
123
+ record.hasDirtyAttributes; // true
124
+
125
+ store.findRecord('model', 1).then(function(model) {
126
+ model.hasDirtyAttributes; // false
127
+ model.set('foo', 'some value');
128
+ model.hasDirtyAttributes; // true
129
+ });
130
+ ```
131
+
132
+ @since 1.13.0
133
+ @property hasDirtyAttributes
134
+ @public
135
+ @type {Boolean}
136
+ @readOnly
137
+ */
138
+ get hasDirtyAttributes(): boolean;
139
+ /**
140
+ If this property is `true` the record is in the `saving` state. A
141
+ record enters the saving state when `save` is called, but the
142
+ adapter has not yet acknowledged that the changes have been
143
+ persisted to the backend.
144
+
145
+ Example
146
+
147
+ ```javascript
148
+ let record = store.createRecord('model');
149
+ record.isSaving; // false
150
+ let promise = record.save();
151
+ record.isSaving; // true
152
+ promise.then(function() {
153
+ record.isSaving; // false
154
+ });
155
+ ```
156
+
157
+ @property isSaving
158
+ @public
159
+ @type {Boolean}
160
+ @readOnly
161
+ */
162
+ get isSaving(): boolean;
163
+ /**
164
+ If this property is `true` the record is in the `deleted` state
165
+ and has been marked for deletion. When `isDeleted` is true and
166
+ `hasDirtyAttributes` is true, the record is deleted locally but the deletion
167
+ was not yet persisted. When `isSaving` is true, the change is
168
+ in-flight. When both `hasDirtyAttributes` and `isSaving` are false, the
169
+ change has persisted.
170
+
171
+ Example
172
+
173
+ ```javascript
174
+ let record = store.createRecord('model');
175
+ record.isDeleted; // false
176
+ record.deleteRecord();
177
+
178
+ // Locally deleted
179
+ record.isDeleted; // true
180
+ record.hasDirtyAttributes; // true
181
+ record.isSaving; // false
182
+
183
+ // Persisting the deletion
184
+ let promise = record.save();
185
+ record.isDeleted; // true
186
+ record.isSaving; // true
187
+
188
+ // Deletion Persisted
189
+ promise.then(function() {
190
+ record.isDeleted; // true
191
+ record.isSaving; // false
192
+ record.hasDirtyAttributes; // false
193
+ });
194
+ ```
195
+
196
+ @property isDeleted
197
+ @public
198
+ @type {Boolean}
199
+ @readOnly
200
+ */
201
+ get isDeleted(): boolean;
202
+ /**
203
+ If this property is `true` the record is in the `new` state. A
204
+ record will be in the `new` state when it has been created on the
205
+ client and the adapter has not yet report that it was successfully
206
+ saved.
207
+
208
+ Example
209
+
210
+ ```javascript
211
+ let record = store.createRecord('model');
212
+ record.isNew; // true
213
+
214
+ record.save().then(function(model) {
215
+ model.isNew; // false
216
+ });
217
+ ```
218
+
219
+ @property isNew
220
+ @public
221
+ @type {Boolean}
222
+ @readOnly
223
+ */
224
+ get isNew(): boolean;
225
+ /**
226
+ If this property is `true` the record is in the `valid` state.
227
+
228
+ A record will be in the `valid` state when the adapter did not report any
229
+ server-side validation failures.
230
+
231
+ @property isValid
232
+ @public
233
+ @type {Boolean}
234
+ @readOnly
235
+ */
236
+ get isValid(): boolean;
237
+ /**
238
+ If the record is in the dirty state this property will report what
239
+ kind of change has caused it to move into the dirty
240
+ state. Possible values are:
241
+
242
+ - `created` The record has been created by the client and not yet saved to the adapter.
243
+ - `updated` The record has been updated by the client and not yet saved to the adapter.
244
+ - `deleted` The record has been deleted by the client and not yet saved to the adapter.
245
+
246
+ Example
247
+
248
+ ```javascript
249
+ let record = store.createRecord('model');
250
+ record.dirtyType; // 'created'
251
+ ```
252
+
253
+ @property dirtyType
254
+ @public
255
+ @type {String}
256
+ @readOnly
257
+ */
258
+ get dirtyType(): 'created' | 'updated' | 'deleted' | '';
259
+ /**
260
+ If `true` the adapter reported that it was unable to save local
261
+ changes to the backend for any reason other than a server-side
262
+ validation error.
263
+
264
+ Example
265
+
266
+ ```javascript
267
+ record.isError; // false
268
+ record.set('foo', 'valid value');
269
+ record.save().then(null, function() {
270
+ record.isError; // true
271
+ });
272
+ ```
273
+
274
+ @property isError
275
+ @public
276
+ @type {Boolean}
277
+ @readOnly
278
+ */
279
+ get isError(): boolean;
280
+ set isError(v: boolean);
281
+ /**
282
+ If `true` the store is attempting to reload the record from the adapter.
283
+
284
+ Example
285
+
286
+ ```javascript
287
+ record.isReloading; // false
288
+ record.reload();
289
+ record.isReloading; // true
290
+ ```
291
+
292
+ @property isReloading
293
+ @public
294
+ @type {Boolean}
295
+ @readOnly
296
+ */
297
+ isReloading: boolean;
298
+ /**
299
+ All ember models have an id property. This is an identifier
300
+ managed by an external source. These are always coerced to be
301
+ strings before being used internally. Note when declaring the
302
+ attributes for a model it is an error to an id
303
+ attribute.
304
+
305
+ ```javascript
306
+ let record = store.createRecord('model');
307
+ record.id; // null
308
+
309
+ store.findRecord('model', 1).then(function(model) {
310
+ model.id; // '1'
311
+ });
312
+ ```
313
+
314
+ @property id
315
+ @public
316
+ @type {String}
317
+ */
318
+ get id(): string | null;
319
+ set id(id: string | null);
320
+ toString(): string;
321
+ /**
322
+ @property currentState
323
+ @private
324
+ @type {Object}
325
+ */
326
+ get currentState(): RecordState;
327
+ set currentState(_v: RecordState);
328
+ /**
329
+ The store service instance which created this record instance
330
+
331
+ @property store
332
+ @public
333
+ */
334
+ /**
335
+ When the record is in the `invalid` state this object will contain
336
+ any errors returned by the adapter. When present the errors hash
337
+ contains keys corresponding to the invalid property names
338
+ and values which are arrays of Javascript objects with two keys:
339
+
340
+ - `message` A string containing the error message from the backend
341
+ - `attribute` The name of the property associated with this error message
342
+
343
+ ```javascript
344
+ record.errors.length; // 0
345
+ record.set('foo', 'invalid value');
346
+ record.save().catch(function() {
347
+ record.errors.foo;
348
+ // [{message: 'foo should be a number.', attribute: 'foo'}]
349
+ });
350
+ ```
351
+
352
+ The `errors` property is useful for displaying error messages to
353
+ the user.
354
+
355
+ ```handlebars
356
+ <label>Username: <Input @value={{@model.username}} /> </label>
357
+ {{#each @model.errors.username as |error|}}
358
+ <div class="error">
359
+ {{error.message}}
360
+ </div>
361
+ {{/each}}
362
+ <label>Email: <Input @value={{@model.email}} /> </label>
363
+ {{#each @model.errors.email as |error|}}
364
+ <div class="error">
365
+ {{error.message}}
366
+ </div>
367
+ {{/each}}
368
+ ```
369
+
370
+
371
+ You can also access the special `messages` property on the error
372
+ object to get an array of all the error strings.
373
+
374
+ ```handlebars
375
+ {{#each @model.errors.messages as |message|}}
376
+ <div class="error">
377
+ {{message}}
378
+ </div>
379
+ {{/each}}
380
+ ```
381
+
382
+ @property errors
383
+ @public
384
+ @type {Errors}
385
+ */
386
+ get errors(): Errors;
387
+ /**
388
+ This property holds the `AdapterError` object with which
389
+ last adapter operation was rejected.
390
+
391
+ @property adapterError
392
+ @public
393
+ @type {AdapterError}
394
+ */
395
+ get adapterError(): unknown;
396
+ set adapterError(v: unknown);
397
+ /**
398
+ Create a JSON representation of the record, using the serialization
399
+ strategy of the store's adapter.
400
+
401
+ `serialize` takes an optional hash as a parameter, currently
402
+ supported options are:
403
+
404
+ - `includeId`: `true` if the record's ID should be included in the
405
+ JSON representation.
406
+
407
+ @method serialize
408
+ @public
409
+ @param {Object} options
410
+ @return {Object} an object whose values are primitive JSON values only
411
+ */
412
+ serialize: typeof serialize;
413
+ notifyPropertyChange(prop: string): void;
414
+ /**
415
+ Marks the record as deleted but does not save it. You must call
416
+ `save` afterwards if you want to persist it. You might use this
417
+ method if you want to allow the user to still `rollbackAttributes()`
418
+ after a delete was made.
419
+
420
+ Example
421
+
422
+ ```js
423
+ import Component from '@glimmer/component';
424
+
425
+ export default class extends Component {
426
+ softDelete = () => {
427
+ this.args.model.deleteRecord();
428
+ }
429
+
430
+ confirm = () => {
431
+ this.args.model.save();
432
+ }
433
+
434
+ undo = () => {
435
+ this.args.model.rollbackAttributes();
436
+ }
437
+ }
438
+ ```
439
+
440
+ @method deleteRecord
441
+ @public
442
+ */
443
+ deleteRecord: typeof deleteRecord;
444
+ /**
445
+ Same as `deleteRecord`, but saves the record immediately.
446
+
447
+ Example
448
+
449
+ ```js
450
+ import Component from '@glimmer/component';
451
+
452
+ export default class extends Component {
453
+ delete = () => {
454
+ this.args.model.destroyRecord().then(function() {
455
+ this.transitionToRoute('model.index');
456
+ });
457
+ }
458
+ }
459
+ ```
460
+
461
+ If you pass an object on the `adapterOptions` property of the options
462
+ argument it will be passed to your adapter via the snapshot
463
+
464
+ ```js
465
+ record.destroyRecord({ adapterOptions: { subscribe: false } });
466
+ ```
467
+
468
+ ```app/adapters/post.js
469
+ import MyCustomAdapter from './custom-adapter';
470
+
471
+ export default class PostAdapter extends MyCustomAdapter {
472
+ deleteRecord(store, type, snapshot) {
473
+ if (snapshot.adapterOptions.subscribe) {
474
+ // ...
475
+ }
476
+ // ...
477
+ }
478
+ }
479
+ ```
480
+
481
+ @method destroyRecord
482
+ @public
483
+ @param {Object} options
484
+ @return {Promise} a promise that will be resolved when the adapter returns
485
+ successfully or rejected if the adapter returns with an error.
486
+ */
487
+ destroyRecord: typeof destroyRecord;
488
+ /**
489
+ Unloads the record from the store. This will not send a delete request
490
+ to your server, it just unloads the record from memory.
491
+
492
+ @method unloadRecord
493
+ @public
494
+ */
495
+ unloadRecord: typeof unloadRecord;
496
+ /**
497
+ Returns an object, whose keys are changed properties, and value is
498
+ an [oldProp, newProp] array.
499
+
500
+ The array represents the diff of the canonical state with the local state
501
+ of the model. Note: if the model is created locally, the canonical state is
502
+ empty since the adapter hasn't acknowledged the attributes yet:
503
+
504
+ Example
505
+
506
+ ```app/models/mascot.js
507
+ import Model, { attr } from '@ember-data/model';
508
+
509
+ export default class MascotModel extends Model {
510
+ @attr('string') name;
511
+ @attr('boolean', {
512
+ defaultValue: false
513
+ })
514
+ isAdmin;
515
+ }
516
+ ```
517
+
518
+ ```javascript
519
+ let mascot = store.createRecord('mascot');
520
+
521
+ mascot.changedAttributes(); // {}
522
+
523
+ mascot.set('name', 'Tomster');
524
+ mascot.changedAttributes(); // { name: [undefined, 'Tomster'] }
525
+
526
+ mascot.set('isAdmin', true);
527
+ mascot.changedAttributes(); // { isAdmin: [undefined, true], name: [undefined, 'Tomster'] }
528
+
529
+ mascot.save().then(function() {
530
+ mascot.changedAttributes(); // {}
531
+
532
+ mascot.set('isAdmin', false);
533
+ mascot.changedAttributes(); // { isAdmin: [true, false] }
534
+ });
535
+ ```
536
+
537
+ @method changedAttributes
538
+ @public
539
+ @return {Object} an object, whose keys are changed properties,
540
+ and value is an [oldProp, newProp] array.
541
+ */
542
+ changedAttributes: typeof changedAttributes;
543
+ /**
544
+ If the model `hasDirtyAttributes` this function will discard any unsaved
545
+ changes. If the model `isNew` it will be removed from the store.
546
+
547
+ Example
548
+
549
+ ```javascript
550
+ record.name; // 'Untitled Document'
551
+ record.set('name', 'Doc 1');
552
+ record.name; // 'Doc 1'
553
+ record.rollbackAttributes();
554
+ record.name; // 'Untitled Document'
555
+ ```
556
+
557
+ @since 1.13.0
558
+ @method rollbackAttributes
559
+ @public
560
+ */
561
+ rollbackAttributes: typeof rollbackAttributes;
562
+ /**
563
+ @method _createSnapshot
564
+ @private
565
+ */
566
+ _createSnapshot: typeof createSnapshot;
567
+ /**
568
+ Save the record and persist any changes to the record to an
569
+ external source via the adapter.
570
+
571
+ Example
572
+
573
+ ```javascript
574
+ record.set('name', 'Tomster');
575
+ record.save().then(function() {
576
+ // Success callback
577
+ }, function() {
578
+ // Error callback
579
+ });
580
+ ```
581
+
582
+ If you pass an object using the `adapterOptions` property of the options
583
+ argument it will be passed to your adapter via the snapshot.
584
+
585
+ ```js
586
+ record.save({ adapterOptions: { subscribe: false } });
587
+ ```
588
+
589
+ ```app/adapters/post.js
590
+ import MyCustomAdapter from './custom-adapter';
591
+
592
+ export default class PostAdapter extends MyCustomAdapter {
593
+ updateRecord(store, type, snapshot) {
594
+ if (snapshot.adapterOptions.subscribe) {
595
+ // ...
596
+ }
597
+ // ...
598
+ }
599
+ }
600
+ ```
601
+
602
+ @method save
603
+ @public
604
+ @param {Object} options
605
+ @return {Promise} a promise that will be resolved when the adapter returns
606
+ successfully or rejected if the adapter returns with an error.
607
+ */
608
+ save: typeof save;
609
+ /**
610
+ Reload the record from the adapter.
611
+
612
+ This will only work if the record has already finished loading.
613
+
614
+ Example
615
+
616
+ ```js
617
+ import Component from '@glimmer/component';
618
+
619
+ export default class extends Component {
620
+ async reload = () => {
621
+ await this.args.model.reload();
622
+ // do something with the reloaded model
623
+ }
624
+ }
625
+ ```
626
+
627
+ @method reload
628
+ @public
629
+ @param {Object} options optional, may include `adapterOptions` hash which will be passed to adapter request
630
+
631
+ @return {Promise} a promise that will be resolved with the record when the
632
+ adapter returns successfully or rejected if the adapter returns
633
+ with an error.
634
+ */
635
+ reload: typeof reload;
636
+ attr(): void;
637
+ /**
638
+ Get the reference for the specified belongsTo relationship.
639
+
640
+ For instance, given the following model
641
+
642
+ ```app/models/blog-post.js
643
+ import Model, { belongsTo } from '@ember-data/model';
644
+
645
+ export default class BlogPost extends Model {
646
+ @belongsTo('user', { async: true, inverse: null }) author;
647
+ }
648
+ ```
649
+
650
+ Then the reference for the author relationship would be
651
+ retrieved from a record instance like so:
652
+
653
+ ```js
654
+ blogPost.belongsTo('author');
655
+ ```
656
+
657
+ A `BelongsToReference` is a low-level API that allows access
658
+ and manipulation of a belongsTo relationship.
659
+
660
+ It is especially useful when you're dealing with `async` relationships
661
+ as it allows synchronous access to the relationship data if loaded, as
662
+ well as APIs for loading, reloading the data or accessing available
663
+ information without triggering a load.
664
+
665
+ It may also be useful when using `sync` relationships that need to be
666
+ loaded/reloaded with more precise timing than marking the
667
+ relationship as `async` and relying on autofetch would have allowed.
668
+
669
+ However,keep in mind that marking a relationship as `async: false` will introduce
670
+ bugs into your application if the data is not always guaranteed to be available
671
+ by the time the relationship is accessed. Ergo, it is recommended when using this
672
+ approach to utilize `links` for unloaded relationship state instead of identifiers.
673
+
674
+ Reference APIs are entangled with the relationship's underlying state,
675
+ thus any getters or cached properties that utilize these will properly
676
+ invalidate if the relationship state changes.
677
+
678
+ References are "stable", meaning that multiple calls to retrieve the reference
679
+ for a given relationship will always return the same HasManyReference.
680
+
681
+ @method belongsTo
682
+ @public
683
+ @param {String} name of the relationship
684
+ @since 2.5.0
685
+ @return {BelongsToReference} reference for this relationship
686
+ */
687
+ belongsTo: typeof belongsTo;
688
+ /**
689
+ Get the reference for the specified hasMany relationship.
690
+
691
+ For instance, given the following model
692
+
693
+ ```app/models/blog-post.js
694
+ import Model, { hasMany } from '@ember-data/model';
695
+
696
+ export default class BlogPost extends Model {
697
+ @hasMany('comment', { async: true, inverse: null }) comments;
698
+ }
699
+ ```
700
+
701
+ Then the reference for the comments relationship would be
702
+ retrieved from a record instance like so:
703
+
704
+ ```js
705
+ blogPost.hasMany('comments');
706
+ ```
707
+
708
+ A `HasManyReference` is a low-level API that allows access
709
+ and manipulation of a hasMany relationship.
710
+
711
+ It is especially useful when you are dealing with `async` relationships
712
+ as it allows synchronous access to the relationship data if loaded, as
713
+ well as APIs for loading, reloading the data or accessing available
714
+ information without triggering a load.
715
+
716
+ It may also be useful when using `sync` relationships with `@ember-data/model`
717
+ that need to be loaded/reloaded with more precise timing than marking the
718
+ relationship as `async` and relying on autofetch would have allowed.
719
+
720
+ However,keep in mind that marking a relationship as `async: false` will introduce
721
+ bugs into your application if the data is not always guaranteed to be available
722
+ by the time the relationship is accessed. Ergo, it is recommended when using this
723
+ approach to utilize `links` for unloaded relationship state instead of identifiers.
724
+
725
+ Reference APIs are entangled with the relationship's underlying state,
726
+ thus any getters or cached properties that utilize these will properly
727
+ invalidate if the relationship state changes.
728
+
729
+ References are "stable", meaning that multiple calls to retrieve the reference
730
+ for a given relationship will always return the same HasManyReference.
731
+
732
+ @method hasMany
733
+ @public
734
+ @param {String} name of the relationship
735
+ @since 2.5.0
736
+ @return {HasManyReference} reference for this relationship
737
+ */
738
+ hasMany: typeof hasMany;
739
+ /**
740
+ Given a callback, iterates over each of the relationships in the model,
741
+ invoking the callback with the name of each relationship and its relationship
742
+ descriptor.
743
+
744
+
745
+ The callback method you provide should have the following signature (all
746
+ parameters are optional):
747
+
748
+ ```javascript
749
+ function(name, descriptor);
750
+ ```
751
+
752
+ - `name` the name of the current property in the iteration
753
+ - `descriptor` the meta object that describes this relationship
754
+
755
+ The relationship descriptor argument is an object with the following properties.
756
+
757
+ - **name** <span class="type">String</span> the name of this relationship on the Model
758
+ - **kind** <span class="type">String</span> "hasMany" or "belongsTo"
759
+ - **options** <span class="type">Object</span> the original options hash passed when the relationship was declared
760
+ - **parentType** <span class="type">Model</span> the type of the Model that owns this relationship
761
+ - **type** <span class="type">String</span> the type name of the related Model
762
+
763
+ Note that in addition to a callback, you can also pass an optional target
764
+ object that will be set as `this` on the context.
765
+
766
+ Example
767
+
768
+ ```app/serializers/application.js
769
+ import JSONSerializer from '@ember-data/serializer/json';
770
+
771
+ export default class ApplicationSerializer extends JSONSerializer {
772
+ serialize(record, options) {
773
+ let json = {};
774
+
775
+ record.eachRelationship(function(name, descriptor) {
776
+ if (descriptor.kind === 'hasMany') {
777
+ let serializedHasManyName = name.toUpperCase() + '_IDS';
778
+ json[serializedHasManyName] = record.get(name).map(r => r.id);
779
+ }
780
+ });
781
+
782
+ return json;
783
+ }
784
+ }
785
+ ```
786
+
787
+ @method eachRelationship
788
+ @public
789
+ @param {Function} callback the callback to invoke
790
+ @param {any} binding the value to which the callback's `this` should be bound
791
+ */
792
+ eachRelationship<T>(callback: (this: NoInfer<T> | undefined, key: Exclude<keyof this & string, keyof Model & string>, meta: RelationshipSchema) => void, binding?: T): void;
793
+ relationshipFor(name: string): RelationshipSchema | undefined;
794
+ inverseFor(name: string): RelationshipSchema | null;
795
+ eachAttribute<T>(callback: (this: NoInfer<T> | undefined, key: Exclude<keyof this & string, keyof Model & string>, meta: AttributeSchema) => void, binding?: T): void;
796
+ static isModel: boolean;
797
+ /**
798
+ Create should only ever be called by the store. To create an instance of a
799
+ `Model` in a dirty state use `store.createRecord`.
800
+
801
+ To create instances of `Model` in a clean state, use `store.push`
802
+
803
+ @method create
804
+ @private
805
+ @static
806
+ */
807
+ /**
808
+ Represents the model's class name as a string. This can be used to look up the model's class name through
809
+ `Store`'s modelFor method.
810
+
811
+ `modelName` is generated for you by EmberData. It will be a lowercased, dasherized string.
812
+ For example:
813
+
814
+ ```javascript
815
+ store.modelFor('post').modelName; // 'post'
816
+ store.modelFor('blog-post').modelName; // 'blog-post'
817
+ ```
818
+
819
+ The most common place you'll want to access `modelName` is in your serializer's `payloadKeyFromModelName` method. For example, to change payload
820
+ keys to underscore (instead of dasherized), you might use the following code:
821
+
822
+ ```javascript
823
+ import RESTSerializer from '@ember-data/serializer/rest';
824
+ import { underscore } from '<app-name>/utils/string-utils';
825
+
826
+ export default const PostSerializer = RESTSerializer.extend({
827
+ payloadKeyFromModelName(modelName) {
828
+ return underscore(modelName);
829
+ }
830
+ });
831
+ ```
832
+ @property modelName
833
+ @public
834
+ @type String
835
+ @readonly
836
+ @static
837
+ */
838
+ static modelName: string;
839
+ /**
840
+ For a given relationship name, returns the model type of the relationship.
841
+
842
+ For example, if you define a model like this:
843
+
844
+ ```app/models/post.js
845
+ import Model, { hasMany } from '@ember-data/model';
846
+
847
+ export default class PostModel extends Model {
848
+ @hasMany('comment') comments;
849
+ }
850
+ ```
851
+
852
+ Calling `store.modelFor('post').typeForRelationship('comments', store)` will return `Comment`.
853
+
854
+ @method typeForRelationship
855
+ @public
856
+ @static
857
+ @param {String} name the name of the relationship
858
+ @param {store} store an instance of Store
859
+ @return {Model} the type of the relationship, or undefined
860
+ */
861
+ static typeForRelationship(name: string, store: Store): import("@ember-data/store/-types/q/ds-model").ModelSchema<unknown> | undefined;
862
+ static get inverseMap(): Record<string, RelationshipSchema | null>;
863
+ /**
864
+ Find the relationship which is the inverse of the one asked for.
865
+
866
+ For example, if you define models like this:
867
+
868
+ ```app/models/post.js
869
+ import Model, { hasMany } from '@ember-data/model';
870
+
871
+ export default class PostModel extends Model {
872
+ @hasMany('message') comments;
873
+ }
874
+ ```
875
+
876
+ ```app/models/message.js
877
+ import Model, { belongsTo } from '@ember-data/model';
878
+
879
+ export default class MessageModel extends Model {
880
+ @belongsTo('post') owner;
881
+ }
882
+ ```
883
+
884
+ ``` js
885
+ store.modelFor('post').inverseFor('comments', store) // { type: 'message', name: 'owner', kind: 'belongsTo' }
886
+ store.modelFor('message').inverseFor('owner', store) // { type: 'post', name: 'comments', kind: 'hasMany' }
887
+ ```
888
+
889
+ @method inverseFor
890
+ @public
891
+ @static
892
+ @param {String} name the name of the relationship
893
+ @param {Store} store
894
+ @return {Object} the inverse relationship, or null
895
+ */
896
+ static inverseFor(name: string, store: Store): RelationshipSchema | null;
897
+ static _findInverseFor(name: string, store: Store): RelationshipSchema | null;
898
+ /**
899
+ The model's relationships as a map, keyed on the type of the
900
+ relationship. The value of each entry is an array containing a descriptor
901
+ for each relationship with that type, describing the name of the relationship
902
+ as well as the type.
903
+
904
+ For example, given the following model definition:
905
+
906
+ ```app/models/blog.js
907
+ import Model, { belongsTo, hasMany } from '@ember-data/model';
908
+
909
+ export default class BlogModel extends Model {
910
+ @hasMany('user') users;
911
+ @belongsTo('user') owner;
912
+ @hasMany('post') posts;
913
+ }
914
+ ```
915
+
916
+ This computed property would return a map describing these
917
+ relationships, like this:
918
+
919
+ ```javascript
920
+ import Blog from 'app/models/blog';
921
+ import User from 'app/models/user';
922
+ import Post from 'app/models/post';
923
+
924
+ let relationships = Blog.relationships;
925
+ relationships.user;
926
+ //=> [ { name: 'users', kind: 'hasMany' },
927
+ // { name: 'owner', kind: 'belongsTo' } ]
928
+ relationships.post;
929
+ //=> [ { name: 'posts', kind: 'hasMany' } ]
930
+ ```
931
+
932
+ @property relationships
933
+ @public
934
+ @static
935
+ @type Map
936
+ @readOnly
937
+ */
938
+ static get relationships(): Map<string, RelationshipSchema[]>;
939
+ /**
940
+ A hash containing lists of the model's relationships, grouped
941
+ by the relationship kind. For example, given a model with this
942
+ definition:
943
+
944
+ ```app/models/blog.js
945
+ import Model, { belongsTo, hasMany } from '@ember-data/model';
946
+
947
+ export default class BlogModel extends Model {
948
+ @hasMany('user') users;
949
+ @belongsTo('user') owner;
950
+
951
+ @hasMany('post') posts;
952
+ }
953
+ ```
954
+
955
+ This property would contain the following:
956
+
957
+ ```javascript
958
+ import Blog from 'app/models/blog';
959
+
960
+ let relationshipNames = Blog.relationshipNames;
961
+ relationshipNames.hasMany;
962
+ //=> ['users', 'posts']
963
+ relationshipNames.belongsTo;
964
+ //=> ['owner']
965
+ ```
966
+
967
+ @property relationshipNames
968
+ @public
969
+ @static
970
+ @type Object
971
+ @readOnly
972
+ */
973
+ static get relationshipNames(): {
974
+ hasMany: string[];
975
+ belongsTo: string[];
976
+ };
977
+ /**
978
+ An array of types directly related to a model. Each type will be
979
+ included once, regardless of the number of relationships it has with
980
+ the model.
981
+
982
+ For example, given a model with this definition:
983
+
984
+ ```app/models/blog.js
985
+ import Model, { belongsTo, hasMany } from '@ember-data/model';
986
+
987
+ export default class BlogModel extends Model {
988
+ @hasMany('user') users;
989
+ @belongsTo('user') owner;
990
+
991
+ @hasMany('post') posts;
992
+ }
993
+ ```
994
+
995
+ This property would contain the following:
996
+
997
+ ```javascript
998
+ import Blog from 'app/models/blog';
999
+
1000
+ let relatedTypes = Blog.relatedTypes');
1001
+ //=> ['user', 'post']
1002
+ ```
1003
+
1004
+ @property relatedTypes
1005
+ @public
1006
+ @static
1007
+ @type Array
1008
+ @readOnly
1009
+ */
1010
+ static get relatedTypes(): string[];
1011
+ /**
1012
+ A map whose keys are the relationships of a model and whose values are
1013
+ relationship descriptors.
1014
+
1015
+ For example, given a model with this
1016
+ definition:
1017
+
1018
+ ```app/models/blog.js
1019
+ import Model, { belongsTo, hasMany } from '@ember-data/model';
1020
+
1021
+ export default class BlogModel extends Model {
1022
+ @hasMany('user') users;
1023
+ @belongsTo('user') owner;
1024
+
1025
+ @hasMany('post') posts;
1026
+ }
1027
+ ```
1028
+
1029
+ This property would contain the following:
1030
+
1031
+ ```javascript
1032
+ import Blog from 'app/models/blog';
1033
+
1034
+ let relationshipsByName = Blog.relationshipsByName;
1035
+ relationshipsByName.users;
1036
+ //=> { name: 'users', kind: 'hasMany', type: 'user', options: Object }
1037
+ relationshipsByName.owner;
1038
+ //=> { name: 'owner', kind: 'belongsTo', type: 'user', options: Object }
1039
+ ```
1040
+
1041
+ @property relationshipsByName
1042
+ @public
1043
+ @static
1044
+ @type Map
1045
+ @readOnly
1046
+ */
1047
+ static get relationshipsByName(): Map<string, RelationshipSchema>;
1048
+ static get relationshipsObject(): Record<string, RelationshipSchema>;
1049
+ /**
1050
+ A map whose keys are the fields of the model and whose values are strings
1051
+ describing the kind of the field. A model's fields are the union of all of its
1052
+ attributes and relationships.
1053
+
1054
+ For example:
1055
+
1056
+ ```app/models/blog.js
1057
+ import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
1058
+
1059
+ export default class BlogModel extends Model {
1060
+ @hasMany('user') users;
1061
+ @belongsTo('user') owner;
1062
+
1063
+ @hasMany('post') posts;
1064
+
1065
+ @attr('string') title;
1066
+ }
1067
+ ```
1068
+
1069
+ ```js
1070
+ import Blog from 'app/models/blog'
1071
+
1072
+ let fields = Blog.fields;
1073
+ fields.forEach(function(kind, field) {
1074
+ // do thing
1075
+ });
1076
+
1077
+ // prints:
1078
+ // users, hasMany
1079
+ // owner, belongsTo
1080
+ // posts, hasMany
1081
+ // title, attribute
1082
+ ```
1083
+
1084
+ @property fields
1085
+ @public
1086
+ @static
1087
+ @type Map
1088
+ @readOnly
1089
+ */
1090
+ static get fields(): Map<any, any>;
1091
+ /**
1092
+ Given a callback, iterates over each of the relationships in the model,
1093
+ invoking the callback with the name of each relationship and its relationship
1094
+ descriptor.
1095
+
1096
+ @method eachRelationship
1097
+ @public
1098
+ @static
1099
+ @param {Function} callback the callback to invoke
1100
+ @param {any} binding the value to which the callback's `this` should be bound
1101
+ */
1102
+ static eachRelationship<T, Schema extends Model>(callback: (this: T | undefined, key: Exclude<keyof Schema & string, keyof Model & string>, relationship: RelationshipSchema) => void, binding?: T): void;
1103
+ /**
1104
+ Given a callback, iterates over each of the types related to a model,
1105
+ invoking the callback with the related type's class. Each type will be
1106
+ returned just once, regardless of how many different relationships it has
1107
+ with a model.
1108
+
1109
+ @method eachRelatedType
1110
+ @public
1111
+ @static
1112
+ @param {Function} callback the callback to invoke
1113
+ @param {any} binding the value to which the callback's `this` should be bound
1114
+ */
1115
+ static eachRelatedType<T>(callback: (this: T | undefined, type: string) => void, binding?: T): void;
1116
+ /**
1117
+ *
1118
+ * @method determineRelationshipType
1119
+ * @private
1120
+ * @deprecated
1121
+ */
1122
+ static determineRelationshipType(knownSide: RelationshipSchema, store: Store): 'oneToOne' | 'oneToMany' | 'manyToOne' | 'manyToMany' | 'oneToNone' | 'manyToNone';
1123
+ /**
1124
+ A map whose keys are the attributes of the model (properties
1125
+ described by attr) and whose values are the meta object for the
1126
+ property.
1127
+
1128
+ Example
1129
+
1130
+ ```app/models/person.js
1131
+ import Model, { attr } from '@ember-data/model';
1132
+
1133
+ export default class PersonModel extends Model {
1134
+ @attr('string') firstName;
1135
+ @attr('string') lastName;
1136
+ @attr('date') birthday;
1137
+ }
1138
+ ```
1139
+
1140
+ ```javascript
1141
+ import Person from 'app/models/person'
1142
+
1143
+ let attributes = Person.attributes
1144
+
1145
+ attributes.forEach(function(meta, name) {
1146
+ // do thing
1147
+ });
1148
+
1149
+ // prints:
1150
+ // firstName {type: "string", kind: 'attribute', options: Object, parentType: function, name: "firstName"}
1151
+ // lastName {type: "string", kind: 'attribute', options: Object, parentType: function, name: "lastName"}
1152
+ // birthday {type: "date", kind: 'attribute', options: Object, parentType: function, name: "birthday"}
1153
+ ```
1154
+
1155
+ @property attributes
1156
+ @public
1157
+ @static
1158
+ @type {Map}
1159
+ @readOnly
1160
+ */
1161
+ static get attributes(): Map<any, any>;
1162
+ /**
1163
+ A map whose keys are the attributes of the model (properties
1164
+ described by attr) and whose values are type of transformation
1165
+ applied to each attribute. This map does not include any
1166
+ attributes that do not have an transformation type.
1167
+
1168
+ Example
1169
+
1170
+ ```app/models/person.js
1171
+ import Model, { attr } from '@ember-data/model';
1172
+
1173
+ export default class PersonModel extends Model {
1174
+ @attr firstName;
1175
+ @attr('string') lastName;
1176
+ @attr('date') birthday;
1177
+ }
1178
+ ```
1179
+
1180
+ ```javascript
1181
+ import Person from 'app/models/person';
1182
+
1183
+ let transformedAttributes = Person.transformedAttributes
1184
+
1185
+ transformedAttributes.forEach(function(field, type) {
1186
+ // do thing
1187
+ });
1188
+
1189
+ // prints:
1190
+ // lastName string
1191
+ // birthday date
1192
+ ```
1193
+
1194
+ @property transformedAttributes
1195
+ @public
1196
+ @static
1197
+ @type {Map}
1198
+ @readOnly
1199
+ */
1200
+ static get transformedAttributes(): Map<string, string>;
1201
+ /**
1202
+ Iterates through the attributes of the model, calling the passed function on each
1203
+ attribute.
1204
+
1205
+ The callback method you provide should have the following signature (all
1206
+ parameters are optional):
1207
+
1208
+ ```javascript
1209
+ function(name, meta);
1210
+ ```
1211
+
1212
+ - `name` the name of the current property in the iteration
1213
+ - `meta` the meta object for the attribute property in the iteration
1214
+
1215
+ Note that in addition to a callback, you can also pass an optional target
1216
+ object that will be set as `this` on the context.
1217
+
1218
+ Example
1219
+
1220
+ ```javascript
1221
+ import Model, { attr } from '@ember-data/model';
1222
+
1223
+ class PersonModel extends Model {
1224
+ @attr('string') firstName;
1225
+ @attr('string') lastName;
1226
+ @attr('date') birthday;
1227
+ }
1228
+
1229
+ PersonModel.eachAttribute(function(name, meta) {
1230
+ // do thing
1231
+ });
1232
+
1233
+ // prints:
1234
+ // firstName {type: "string", kind: 'attribute', options: Object, parentType: function, name: "firstName"}
1235
+ // lastName {type: "string", kind: 'attribute', options: Object, parentType: function, name: "lastName"}
1236
+ // birthday {type: "date", kind: 'attribute', options: Object, parentType: function, name: "birthday"}
1237
+ ```
1238
+
1239
+ @method eachAttribute
1240
+ @public
1241
+ @param {Function} callback The callback to execute
1242
+ @param {Object} [binding] the value to which the callback's `this` should be bound
1243
+ @static
1244
+ */
1245
+ static eachAttribute<T, Schema extends Model>(callback: (this: T | undefined, key: Exclude<keyof Schema & string, keyof Model & string>, attribute: AttributeSchema) => void, binding?: T): void;
1246
+ /**
1247
+ Iterates through the transformedAttributes of the model, calling
1248
+ the passed function on each attribute. Note the callback will not be
1249
+ called for any attributes that do not have an transformation type.
1250
+
1251
+ The callback method you provide should have the following signature (all
1252
+ parameters are optional):
1253
+
1254
+ ```javascript
1255
+ function(name, type);
1256
+ ```
1257
+
1258
+ - `name` the name of the current property in the iteration
1259
+ - `type` a string containing the name of the type of transformed
1260
+ applied to the attribute
1261
+
1262
+ Note that in addition to a callback, you can also pass an optional target
1263
+ object that will be set as `this` on the context.
1264
+
1265
+ Example
1266
+
1267
+ ```javascript
1268
+ import Model, { attr } from '@ember-data/model';
1269
+
1270
+ let Person = Model.extend({
1271
+ firstName: attr(),
1272
+ lastName: attr('string'),
1273
+ birthday: attr('date')
1274
+ });
1275
+
1276
+ Person.eachTransformedAttribute(function(name, type) {
1277
+ // do thing
1278
+ });
1279
+
1280
+ // prints:
1281
+ // lastName string
1282
+ // birthday date
1283
+ ```
1284
+
1285
+ @method eachTransformedAttribute
1286
+ @public
1287
+ @param {Function} callback The callback to execute
1288
+ @param {Object} [binding] the value to which the callback's `this` should be bound
1289
+ @static
1290
+ */
1291
+ static eachTransformedAttribute<T, Schema extends Model>(callback: (this: T | undefined, key: Exclude<keyof Schema & string, keyof Model & string>, type: string) => void, binding?: T): void;
1292
+ /**
1293
+ Returns the name of the model class.
1294
+
1295
+ @method toString
1296
+ @public
1297
+ @static
1298
+ */
1299
+ static toString(): string;
1300
+ }
1301
+ export { Model };
1302
+ }
1303
+ //# sourceMappingURL=model.d.ts.map