@ember-data/model 5.6.0-alpha.5 → 5.6.0-beta.1

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