@ember-data/legacy-compat 5.4.0-alpha.32 → 5.4.0-alpha.34

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.
@@ -1,246 +1,248 @@
1
- import type Store from '@ember-data/store';
2
- import type { FindRecordOptions } from '@ember-data/store/-types/q/store';
3
- import type { StableRecordIdentifier } from '@warp-drive/core-types';
4
- import type { ChangedAttributesHash } from '@warp-drive/core-types/cache';
5
- import type { TypedRecordInstance, TypeFromInstance } from '@warp-drive/core-types/record';
6
- import type { AttributeSchema, RelationshipSchema } from '@warp-drive/core-types/schema';
7
- import type { SerializerOptions } from './minimum-serializer-interface';
8
- type RecordId = string | null;
9
- /**
10
- Snapshot is not directly instantiable.
11
- Instances are provided to a consuming application's
12
- adapters and serializers for certain requests.
13
-
14
- Snapshots are only available when using `@ember-data/legacy-compat`
15
- for legacy compatibility with adapters and serializers.
16
-
17
- @class Snapshot
18
- @public
19
- */
20
- export default class Snapshot<R = unknown> {
21
- __attributes: Record<keyof R & string, unknown> | null;
22
- _belongsToRelationships: Record<string, Snapshot>;
23
- _belongsToIds: Record<string, RecordId>;
24
- _hasManyRelationships: Record<string, Snapshot[]>;
25
- _hasManyIds: Record<string, RecordId[]>;
26
- _changedAttributes: ChangedAttributesHash;
27
- identifier: StableRecordIdentifier<R extends TypedRecordInstance ? TypeFromInstance<R> : string>;
28
- modelName: R extends TypedRecordInstance ? TypeFromInstance<R> : string;
29
- id: string | null;
30
- include?: string | string[];
31
- adapterOptions?: Record<string, unknown>;
32
- _store: Store;
33
- /**
34
- * @method constructor
35
- * @constructor
36
- * @private
37
- * @param options
38
- * @param identifier
39
- * @param _store
40
- */
41
- constructor(options: FindRecordOptions, identifier: StableRecordIdentifier<R extends TypedRecordInstance ? TypeFromInstance<R> : string>, store: Store);
42
- /**
43
- The underlying record for this snapshot. Can be used to access methods and
44
- properties defined on the record.
45
-
46
- Example
47
-
48
- ```javascript
49
- let json = snapshot.record.toJSON();
50
- ```
51
-
52
- @property record
53
- @type {Model}
54
- @public
55
- */
56
- get record(): R | null;
57
- get _attributes(): Record<keyof R & string, unknown>;
58
- get isNew(): boolean;
59
- /**
60
- Returns the value of an attribute.
61
-
62
- Example
63
-
64
- ```javascript
65
- // store.push('post', { id: 1, author: 'Tomster', title: 'Ember.js rocks' });
66
- postSnapshot.attr('author'); // => 'Tomster'
67
- postSnapshot.attr('title'); // => 'Ember.js rocks'
68
- ```
69
-
70
- Note: Values are loaded eagerly and cached when the snapshot is created.
71
-
72
- @method attr
73
- @param {String} keyName
74
- @return {Object} The attribute value or undefined
75
- @public
76
- */
77
- attr(keyName: keyof R & string): unknown;
78
- /**
79
- Returns all attributes and their corresponding values.
80
-
81
- Example
82
-
83
- ```javascript
84
- // store.push('post', { id: 1, author: 'Tomster', title: 'Ember.js rocks' });
85
- postSnapshot.attributes(); // => { author: 'Tomster', title: 'Ember.js rocks' }
86
- ```
87
-
88
- @method attributes
89
- @return {Object} All attributes of the current snapshot
90
- @public
91
- */
92
- attributes(): Record<keyof R & string, unknown>;
93
- /**
94
- Returns all changed attributes and their old and new values.
95
-
96
- Example
97
-
98
- ```javascript
99
- // store.push('post', { id: 1, author: 'Tomster', title: 'Ember.js rocks' });
100
- postModel.set('title', 'Ember.js rocks!');
101
- postSnapshot.changedAttributes(); // => { title: ['Ember.js rocks', 'Ember.js rocks!'] }
102
- ```
103
-
104
- @method changedAttributes
105
- @return {Object} All changed attributes of the current snapshot
106
- @public
107
- */
108
- changedAttributes(): ChangedAttributesHash;
109
- /**
110
- Returns the current value of a belongsTo relationship.
111
-
112
- `belongsTo` takes an optional hash of options as a second parameter,
113
- currently supported options are:
114
-
115
- - `id`: set to `true` if you only want the ID of the related record to be
116
- returned.
117
-
118
- Example
119
-
120
- ```javascript
121
- // store.push('post', { id: 1, title: 'Hello World' });
122
- // store.createRecord('comment', { body: 'Lorem ipsum', post: post });
123
- commentSnapshot.belongsTo('post'); // => Snapshot
124
- commentSnapshot.belongsTo('post', { id: true }); // => '1'
125
-
126
- // store.push('comment', { id: 1, body: 'Lorem ipsum' });
127
- commentSnapshot.belongsTo('post'); // => undefined
128
- ```
129
-
130
- Calling `belongsTo` will return a new Snapshot as long as there's any known
131
- data for the relationship available, such as an ID. If the relationship is
132
- known but unset, `belongsTo` will return `null`. If the contents of the
133
- relationship is unknown `belongsTo` will return `undefined`.
134
-
135
- Note: Relationships are loaded lazily and cached upon first access.
136
-
137
- @method belongsTo
138
- @param {String} keyName
139
- @param {Object} [options]
140
- @public
141
- @return {(Snapshot|String|null|undefined)} A snapshot or ID of a known
142
- relationship or null if the relationship is known but unset. undefined
143
- will be returned if the contents of the relationship is unknown.
144
- */
145
- belongsTo(keyName: string, options?: {
146
- id?: boolean;
147
- }): Snapshot | RecordId | undefined;
148
- /**
149
- Returns the current value of a hasMany relationship.
150
-
151
- `hasMany` takes an optional hash of options as a second parameter,
152
- currently supported options are:
153
-
154
- - `ids`: set to `true` if you only want the IDs of the related records to be
155
- returned.
156
-
157
- Example
158
-
159
- ```javascript
160
- // store.push('post', { id: 1, title: 'Hello World', comments: [2, 3] });
161
- postSnapshot.hasMany('comments'); // => [Snapshot, Snapshot]
162
- postSnapshot.hasMany('comments', { ids: true }); // => ['2', '3']
163
-
164
- // store.push('post', { id: 1, title: 'Hello World' });
165
- postSnapshot.hasMany('comments'); // => undefined
166
- ```
167
-
168
- Note: Relationships are loaded lazily and cached upon first access.
169
-
170
- @method hasMany
171
- @param {String} keyName
172
- @param {Object} [options]
173
- @public
174
- @return {(Array|undefined)} An array of snapshots or IDs of a known
175
- relationship or an empty array if the relationship is known but unset.
176
- undefined will be returned if the contents of the relationship is unknown.
177
- */
178
- hasMany(keyName: string, options?: {
179
- ids?: boolean;
180
- }): RecordId[] | Snapshot[] | undefined;
181
- /**
182
- Iterates through all the attributes of the model, calling the passed
183
- function on each attribute.
184
-
185
- Example
186
-
187
- ```javascript
188
- snapshot.eachAttribute(function(name, meta) {
189
- // ...
190
- });
191
- ```
192
-
193
- @method eachAttribute
194
- @param {Function} callback the callback to execute
195
- @param {Object} [binding] the value to which the callback's `this` should be bound
196
- @public
197
- */
198
- eachAttribute(callback: (key: string, meta: AttributeSchema) => void, binding?: unknown): void;
199
- /**
200
- Iterates through all the relationships of the model, calling the passed
201
- function on each relationship.
202
-
203
- Example
204
-
205
- ```javascript
206
- snapshot.eachRelationship(function(name, relationship) {
207
- // ...
208
- });
209
- ```
210
-
211
- @method eachRelationship
212
- @param {Function} callback the callback to execute
213
- @param {Object} [binding] the value to which the callback's `this` should be bound
214
- @public
215
- */
216
- eachRelationship(callback: (key: string, meta: RelationshipSchema) => void, binding?: unknown): void;
217
- /**
218
- Serializes the snapshot using the serializer for the model.
219
-
220
- Example
221
-
222
- ```app/adapters/application.js
223
- import Adapter from '@ember-data/adapter';
224
-
225
- export default Adapter.extend({
226
- createRecord(store, type, snapshot) {
227
- let data = snapshot.serialize({ includeId: true });
228
- let url = `/${type.modelName}`;
229
-
230
- return fetch(url, {
231
- method: 'POST',
232
- body: data,
233
- }).then((response) => response.json())
234
- }
235
- });
236
- ```
237
-
238
- @method serialize
239
- @param {Object} options
240
- @return {Object} an object whose values are primitive JSON values only
241
- @public
242
- */
243
- serialize(options?: SerializerOptions): unknown;
1
+ declare module '@ember-data/legacy-compat/legacy-network-handler/snapshot' {
2
+ import type Store from '@ember-data/store';
3
+ import type { FindRecordOptions } from '@ember-data/store/-types/q/store';
4
+ import type { StableRecordIdentifier } from '@warp-drive/core-types';
5
+ import type { ChangedAttributesHash } from '@warp-drive/core-types/cache';
6
+ import type { TypedRecordInstance, TypeFromInstance } from '@warp-drive/core-types/record';
7
+ import type { AttributeSchema, RelationshipSchema } from '@warp-drive/core-types/schema';
8
+ import type { SerializerOptions } from '@ember-data/legacy-compat/legacy-network-handler/minimum-serializer-interface';
9
+ type RecordId = string | null;
10
+ /**
11
+ Snapshot is not directly instantiable.
12
+ Instances are provided to a consuming application's
13
+ adapters and serializers for certain requests.
14
+
15
+ Snapshots are only available when using `@ember-data/legacy-compat`
16
+ for legacy compatibility with adapters and serializers.
17
+
18
+ @class Snapshot
19
+ @public
20
+ */
21
+ export default class Snapshot<R = unknown> {
22
+ __attributes: Record<keyof R & string, unknown> | null;
23
+ _belongsToRelationships: Record<string, Snapshot>;
24
+ _belongsToIds: Record<string, RecordId>;
25
+ _hasManyRelationships: Record<string, Snapshot[]>;
26
+ _hasManyIds: Record<string, RecordId[]>;
27
+ _changedAttributes: ChangedAttributesHash;
28
+ identifier: StableRecordIdentifier<R extends TypedRecordInstance ? TypeFromInstance<R> : string>;
29
+ modelName: R extends TypedRecordInstance ? TypeFromInstance<R> : string;
30
+ id: string | null;
31
+ include?: string | string[];
32
+ adapterOptions?: Record<string, unknown>;
33
+ _store: Store;
34
+ /**
35
+ * @method constructor
36
+ * @constructor
37
+ * @private
38
+ * @param options
39
+ * @param identifier
40
+ * @param _store
41
+ */
42
+ constructor(options: FindRecordOptions, identifier: StableRecordIdentifier<R extends TypedRecordInstance ? TypeFromInstance<R> : string>, store: Store);
43
+ /**
44
+ The underlying record for this snapshot. Can be used to access methods and
45
+ properties defined on the record.
46
+
47
+ Example
48
+
49
+ ```javascript
50
+ let json = snapshot.record.toJSON();
51
+ ```
52
+
53
+ @property record
54
+ @type {Model}
55
+ @public
56
+ */
57
+ get record(): R | null;
58
+ get _attributes(): Record<keyof R & string, unknown>;
59
+ get isNew(): boolean;
60
+ /**
61
+ Returns the value of an attribute.
62
+
63
+ Example
64
+
65
+ ```javascript
66
+ // store.push('post', { id: 1, author: 'Tomster', title: 'Ember.js rocks' });
67
+ postSnapshot.attr('author'); // => 'Tomster'
68
+ postSnapshot.attr('title'); // => 'Ember.js rocks'
69
+ ```
70
+
71
+ Note: Values are loaded eagerly and cached when the snapshot is created.
72
+
73
+ @method attr
74
+ @param {String} keyName
75
+ @return {Object} The attribute value or undefined
76
+ @public
77
+ */
78
+ attr(keyName: keyof R & string): unknown;
79
+ /**
80
+ Returns all attributes and their corresponding values.
81
+
82
+ Example
83
+
84
+ ```javascript
85
+ // store.push('post', { id: 1, author: 'Tomster', title: 'Ember.js rocks' });
86
+ postSnapshot.attributes(); // => { author: 'Tomster', title: 'Ember.js rocks' }
87
+ ```
88
+
89
+ @method attributes
90
+ @return {Object} All attributes of the current snapshot
91
+ @public
92
+ */
93
+ attributes(): Record<keyof R & string, unknown>;
94
+ /**
95
+ Returns all changed attributes and their old and new values.
96
+
97
+ Example
98
+
99
+ ```javascript
100
+ // store.push('post', { id: 1, author: 'Tomster', title: 'Ember.js rocks' });
101
+ postModel.set('title', 'Ember.js rocks!');
102
+ postSnapshot.changedAttributes(); // => { title: ['Ember.js rocks', 'Ember.js rocks!'] }
103
+ ```
104
+
105
+ @method changedAttributes
106
+ @return {Object} All changed attributes of the current snapshot
107
+ @public
108
+ */
109
+ changedAttributes(): ChangedAttributesHash;
110
+ /**
111
+ Returns the current value of a belongsTo relationship.
112
+
113
+ `belongsTo` takes an optional hash of options as a second parameter,
114
+ currently supported options are:
115
+
116
+ - `id`: set to `true` if you only want the ID of the related record to be
117
+ returned.
118
+
119
+ Example
120
+
121
+ ```javascript
122
+ // store.push('post', { id: 1, title: 'Hello World' });
123
+ // store.createRecord('comment', { body: 'Lorem ipsum', post: post });
124
+ commentSnapshot.belongsTo('post'); // => Snapshot
125
+ commentSnapshot.belongsTo('post', { id: true }); // => '1'
126
+
127
+ // store.push('comment', { id: 1, body: 'Lorem ipsum' });
128
+ commentSnapshot.belongsTo('post'); // => undefined
129
+ ```
130
+
131
+ Calling `belongsTo` will return a new Snapshot as long as there's any known
132
+ data for the relationship available, such as an ID. If the relationship is
133
+ known but unset, `belongsTo` will return `null`. If the contents of the
134
+ relationship is unknown `belongsTo` will return `undefined`.
135
+
136
+ Note: Relationships are loaded lazily and cached upon first access.
137
+
138
+ @method belongsTo
139
+ @param {String} keyName
140
+ @param {Object} [options]
141
+ @public
142
+ @return {(Snapshot|String|null|undefined)} A snapshot or ID of a known
143
+ relationship or null if the relationship is known but unset. undefined
144
+ will be returned if the contents of the relationship is unknown.
145
+ */
146
+ belongsTo(keyName: string, options?: {
147
+ id?: boolean;
148
+ }): Snapshot | RecordId | undefined;
149
+ /**
150
+ Returns the current value of a hasMany relationship.
151
+
152
+ `hasMany` takes an optional hash of options as a second parameter,
153
+ currently supported options are:
154
+
155
+ - `ids`: set to `true` if you only want the IDs of the related records to be
156
+ returned.
157
+
158
+ Example
159
+
160
+ ```javascript
161
+ // store.push('post', { id: 1, title: 'Hello World', comments: [2, 3] });
162
+ postSnapshot.hasMany('comments'); // => [Snapshot, Snapshot]
163
+ postSnapshot.hasMany('comments', { ids: true }); // => ['2', '3']
164
+
165
+ // store.push('post', { id: 1, title: 'Hello World' });
166
+ postSnapshot.hasMany('comments'); // => undefined
167
+ ```
168
+
169
+ Note: Relationships are loaded lazily and cached upon first access.
170
+
171
+ @method hasMany
172
+ @param {String} keyName
173
+ @param {Object} [options]
174
+ @public
175
+ @return {(Array|undefined)} An array of snapshots or IDs of a known
176
+ relationship or an empty array if the relationship is known but unset.
177
+ undefined will be returned if the contents of the relationship is unknown.
178
+ */
179
+ hasMany(keyName: string, options?: {
180
+ ids?: boolean;
181
+ }): RecordId[] | Snapshot[] | undefined;
182
+ /**
183
+ Iterates through all the attributes of the model, calling the passed
184
+ function on each attribute.
185
+
186
+ Example
187
+
188
+ ```javascript
189
+ snapshot.eachAttribute(function(name, meta) {
190
+ // ...
191
+ });
192
+ ```
193
+
194
+ @method eachAttribute
195
+ @param {Function} callback the callback to execute
196
+ @param {Object} [binding] the value to which the callback's `this` should be bound
197
+ @public
198
+ */
199
+ eachAttribute(callback: (key: string, meta: AttributeSchema) => void, binding?: unknown): void;
200
+ /**
201
+ Iterates through all the relationships of the model, calling the passed
202
+ function on each relationship.
203
+
204
+ Example
205
+
206
+ ```javascript
207
+ snapshot.eachRelationship(function(name, relationship) {
208
+ // ...
209
+ });
210
+ ```
211
+
212
+ @method eachRelationship
213
+ @param {Function} callback the callback to execute
214
+ @param {Object} [binding] the value to which the callback's `this` should be bound
215
+ @public
216
+ */
217
+ eachRelationship(callback: (key: string, meta: RelationshipSchema) => void, binding?: unknown): void;
218
+ /**
219
+ Serializes the snapshot using the serializer for the model.
220
+
221
+ Example
222
+
223
+ ```app/adapters/application.js
224
+ import Adapter from '@ember-data/adapter';
225
+
226
+ export default Adapter.extend({
227
+ createRecord(store, type, snapshot) {
228
+ let data = snapshot.serialize({ includeId: true });
229
+ let url = `/${type.modelName}`;
230
+
231
+ return fetch(url, {
232
+ method: 'POST',
233
+ body: data,
234
+ }).then((response) => response.json())
235
+ }
236
+ });
237
+ ```
238
+
239
+ @method serialize
240
+ @param {Object} options
241
+ @return {Object} an object whose values are primitive JSON values only
242
+ @public
243
+ */
244
+ serialize(options?: SerializerOptions): unknown;
245
+ }
246
+ export {};
244
247
  }
245
- export {};
246
248
  //# sourceMappingURL=snapshot.d.ts.map