@ember-data/model 4.10.0-alpha.2 → 4.10.0-alpha.21

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 (40) hide show
  1. package/addon/{-private/diff-array.ts → -private.js} +41 -13
  2. package/addon/-private.js.map +1 -0
  3. package/addon/has-many-357b6265.js +6277 -0
  4. package/addon/has-many-357b6265.js.map +1 -0
  5. package/addon/index.js +1 -0
  6. package/addon/index.js.map +1 -0
  7. package/addon-main.js +90 -0
  8. package/package.json +43 -15
  9. package/addon/-private/attr.js +0 -162
  10. package/addon/-private/belongs-to.js +0 -268
  11. package/addon/-private/debug/assert-polymorphic-type.js +0 -71
  12. package/addon/-private/deprecated-promise-proxy.ts +0 -76
  13. package/addon/-private/errors.ts +0 -425
  14. package/addon/-private/has-many.js +0 -280
  15. package/addon/-private/index.ts +0 -14
  16. package/addon/-private/legacy-data-fetch.js +0 -395
  17. package/addon/-private/legacy-data-utils.ts +0 -92
  18. package/addon/-private/legacy-relationships-support.ts +0 -774
  19. package/addon/-private/many-array.ts +0 -401
  20. package/addon/-private/model-for-mixin.ts +0 -38
  21. package/addon/-private/model.js +0 -2516
  22. package/addon/-private/notify-changes.ts +0 -72
  23. package/addon/-private/promise-belongs-to.ts +0 -73
  24. package/addon/-private/promise-many-array.ts +0 -425
  25. package/addon/-private/promise-proxy-base.js +0 -4
  26. package/addon/-private/record-state.ts +0 -468
  27. package/addon/-private/references/belongs-to.ts +0 -624
  28. package/addon/-private/references/has-many.ts +0 -669
  29. package/addon/-private/relationship-meta.ts +0 -98
  30. package/addon/-private/util.ts +0 -31
  31. package/addon/index.ts +0 -39
  32. package/blueprints/model/HELP.md +0 -26
  33. package/blueprints/model/files/__root__/__path__/__name__.js +0 -5
  34. package/blueprints/model/index.js +0 -158
  35. package/blueprints/model/native-files/__root__/__path__/__name__.js +0 -5
  36. package/blueprints/model-test/index.js +0 -33
  37. package/blueprints/model-test/mocha-files/__root__/__path__/__test__.js +0 -18
  38. package/blueprints/model-test/mocha-rfc-232-files/__root__/__path__/__test__.js +0 -15
  39. package/blueprints/model-test/qunit-files/__root__/__path__/__test__.js +0 -14
  40. package/index.js +0 -47
@@ -1,76 +0,0 @@
1
- import { deprecate } from '@ember/debug';
2
- import { get } from '@ember/object';
3
- import { DEBUG } from '@glimmer/env';
4
-
5
- import { resolve } from 'rsvp';
6
-
7
- import { PromiseObject } from './promise-proxy-base';
8
-
9
- function promiseObject<T>(promise: Promise<T>): PromiseObject<T> {
10
- return PromiseObject.create({
11
- promise: resolve(promise),
12
- }) as PromiseObject<T>;
13
- }
14
-
15
- // constructor is accessed in some internals but not including it in the copyright for the deprecation
16
- const ALLOWABLE_METHODS = ['constructor', 'then', 'catch', 'finally'];
17
- const ALLOWABLE_PROPS = ['__ec_yieldable__', '__ec_cancel__'];
18
- const PROXIED_OBJECT_PROPS = ['content', 'isPending', 'isSettled', 'isRejected', 'isFulfilled', 'promise', 'reason'];
19
-
20
- const ProxySymbolString = String(Symbol.for('PROXY_CONTENT'));
21
-
22
- export function deprecatedPromiseObject<T>(promise: Promise<T>): PromiseObject<T> {
23
- const promiseObjectProxy: PromiseObject<T> = promiseObject(promise);
24
- if (!DEBUG) {
25
- return promiseObjectProxy;
26
- }
27
- const handler = {
28
- get(target: object, prop: string, receiver: object): unknown {
29
- if (typeof prop === 'symbol') {
30
- if (String(prop) === ProxySymbolString) {
31
- return;
32
- }
33
- return Reflect.get(target, prop, receiver);
34
- }
35
-
36
- if (prop === 'constructor') {
37
- return target.constructor;
38
- }
39
-
40
- if (ALLOWABLE_PROPS.includes(prop)) {
41
- return target[prop];
42
- }
43
-
44
- if (!ALLOWABLE_METHODS.includes(prop)) {
45
- deprecate(
46
- `Accessing ${prop} is deprecated. The return type is being changed from PromiseObjectProxy to a Promise. The only available methods to access on this promise are .then, .catch and .finally`,
47
- false,
48
- {
49
- id: 'ember-data:model-save-promise',
50
- until: '5.0',
51
- for: '@ember-data/store',
52
- since: {
53
- available: '4.4',
54
- enabled: '4.4',
55
- },
56
- }
57
- );
58
- } else {
59
- return (target[prop] as () => unknown).bind(target);
60
- }
61
-
62
- if (PROXIED_OBJECT_PROPS.includes(prop)) {
63
- return target[prop];
64
- }
65
-
66
- const value: unknown = get(target, prop);
67
- if (value && typeof value === 'function' && typeof value.bind === 'function') {
68
- return value.bind(receiver);
69
- }
70
-
71
- return undefined;
72
- },
73
- };
74
-
75
- return new Proxy(promiseObjectProxy, handler);
76
- }
@@ -1,425 +0,0 @@
1
- import { A } from '@ember/array';
2
- import type NativeArray from '@ember/array/-private/native-array';
3
- import ArrayProxy from '@ember/array/proxy';
4
- import { computed, get } from '@ember/object';
5
- import { mapBy, not } from '@ember/object/computed';
6
-
7
- import type RecordState from './record-state';
8
-
9
- type ValidationError = {
10
- attribute: string;
11
- message: string;
12
- };
13
- /**
14
- @module @ember-data/model
15
- */
16
- interface ArrayProxyWithCustomOverrides<T, M = T> extends Omit<ArrayProxy<T, M>, 'clear' | 'content'> {
17
- // Omit causes `content` to be merged with the class def for ArrayProxy
18
- // which then causes it to be seen as a property, disallowing defining it
19
- // as an accessor. This restores our ability to define it as an accessor.
20
- content: NativeArray<T>;
21
- clear(): void;
22
- _has(name: string): boolean;
23
- }
24
-
25
- // we force the type here to our own construct because mixin and extend patterns
26
- // lose generic signatures. We also do this because we need to Omit `clear` from
27
- // the type of ArrayProxy as we override it's signature.
28
- const ArrayProxyWithCustomOverrides = ArrayProxy as unknown as new <T, M = T>() => ArrayProxyWithCustomOverrides<T, M>;
29
-
30
- /**
31
- Holds validation errors for a given record, organized by attribute names.
32
-
33
- This class is not directly instantiable.
34
-
35
- Every `Model` has an `errors` property that is an instance of
36
- `Errors`. This can be used to display validation error
37
- messages returned from the server when a `record.save()` rejects.
38
-
39
- For Example, if you had a `User` model that looked like this:
40
-
41
- ```app/models/user.js
42
- import Model, { attr } from '@ember-data/model';
43
-
44
- export default class UserModel extends Model {
45
- @attr('string') username;
46
- @attr('string') email;
47
- }
48
- ```
49
- And you attempted to save a record that did not validate on the backend:
50
-
51
- ```javascript
52
- let user = store.createRecord('user', {
53
- username: 'tomster',
54
- email: 'invalidEmail'
55
- });
56
- user.save();
57
- ```
58
-
59
- Your backend would be expected to return an error response that described
60
- the problem, so that error messages can be generated on the app.
61
-
62
- API responses will be translated into instances of `Errors` differently,
63
- depending on the specific combination of adapter and serializer used. You
64
- may want to check the documentation or the source code of the libraries
65
- that you are using, to know how they expect errors to be communicated.
66
-
67
- Errors can be displayed to the user by accessing their property name
68
- to get an array of all the error objects for that property. Each
69
- error object is a JavaScript object with two keys:
70
-
71
- - `message` A string containing the error message from the backend
72
- - `attribute` The name of the property associated with this error message
73
-
74
- ```handlebars
75
- <label>Username: <Input @value={{@model.username}} /> </label>
76
- {{#each @model.errors.username as |error|}}
77
- <div class="error">
78
- {{error.message}}
79
- </div>
80
- {{/each}}
81
-
82
- <label>Email: <Input @value={{@model.email}} /> </label>
83
- {{#each @model.errors.email as |error|}}
84
- <div class="error">
85
- {{error.message}}
86
- </div>
87
- {{/each}}
88
- ```
89
-
90
- You can also access the special `messages` property on the error
91
- object to get an array of all the error strings.
92
-
93
- ```handlebars
94
- {{#each @model.errors.messages as |message|}}
95
- <div class="error">
96
- {{message}}
97
- </div>
98
- {{/each}}
99
- ```
100
-
101
- @class Errors
102
- @public
103
- @extends Ember.ArrayProxy
104
- */
105
- export default class Errors extends ArrayProxyWithCustomOverrides<ValidationError> {
106
- declare __record: { currentState: RecordState };
107
- /**
108
- @property errorsByAttributeName
109
- @type {MapWithDefault}
110
- @private
111
- */
112
- @computed()
113
- get errorsByAttributeName(): Map<string, NativeArray<ValidationError>> {
114
- return new Map();
115
- }
116
-
117
- /**
118
- Returns errors for a given attribute
119
-
120
- ```javascript
121
- let user = store.createRecord('user', {
122
- username: 'tomster',
123
- email: 'invalidEmail'
124
- });
125
- user.save().catch(function(){
126
- user.errors.errorsFor('email'); // returns:
127
- // [{attribute: "email", message: "Doesn't look like a valid email."}]
128
- });
129
- ```
130
-
131
- @method errorsFor
132
- @public
133
- @param {String} attribute
134
- @return {Array}
135
- */
136
- errorsFor(attribute: string): NativeArray<ValidationError> {
137
- let map = this.errorsByAttributeName;
138
-
139
- let errors = map.get(attribute);
140
-
141
- if (errors === undefined) {
142
- errors = A<ValidationError>();
143
- map.set(attribute, errors);
144
- }
145
-
146
- // Errors may be a native array with extensions turned on. Since we access
147
- // the array via a method, and not a computed or using `Ember.get`, it does
148
- // not entangle properly with autotracking, so we entangle manually by
149
- // getting the `[]` property.
150
- get(errors, '[]');
151
-
152
- return errors;
153
- }
154
-
155
- /**
156
- An array containing all of the error messages for this
157
- record. This is useful for displaying all errors to the user.
158
-
159
- ```handlebars
160
- {{#each @model.errors.messages as |message|}}
161
- <div class="error">
162
- {{message}}
163
- </div>
164
- {{/each}}
165
- ```
166
-
167
- @property messages
168
- @public
169
- @type {Array}
170
- */
171
- @mapBy('content', 'message')
172
- declare messages: string[];
173
-
174
- /**
175
- @property content
176
- @type {Array}
177
- @private
178
- */
179
- @computed()
180
- get content(): NativeArray<ValidationError> {
181
- return A();
182
- }
183
-
184
- /**
185
- @method unknownProperty
186
- @private
187
- */
188
- unknownProperty(attribute: string) {
189
- let errors = this.errorsFor(attribute);
190
- if (errors.length === 0) {
191
- return undefined;
192
- }
193
- return errors;
194
- }
195
-
196
- /**
197
- Total number of errors.
198
-
199
- @property length
200
- @type {Number}
201
- @public
202
- @readOnly
203
- */
204
-
205
- /**
206
- `true` if we have no errors.
207
-
208
- @property isEmpty
209
- @type {Boolean}
210
- @public
211
- @readOnly
212
- */
213
- @not('length')
214
- declare isEmpty: boolean;
215
-
216
- /**
217
- Manually adds errors to the record. This will trigger the `becameInvalid` event/ lifecycle method on
218
- the record and transition the record into an `invalid` state.
219
-
220
- Example
221
- ```javascript
222
- let errors = user.errors;
223
-
224
- // add multiple errors
225
- errors.add('password', [
226
- 'Must be at least 12 characters',
227
- 'Must contain at least one symbol',
228
- 'Cannot contain your name'
229
- ]);
230
-
231
- errors.errorsFor('password');
232
- // =>
233
- // [
234
- // { attribute: 'password', message: 'Must be at least 12 characters' },
235
- // { attribute: 'password', message: 'Must contain at least one symbol' },
236
- // { attribute: 'password', message: 'Cannot contain your name' },
237
- // ]
238
-
239
- // add a single error
240
- errors.add('username', 'This field is required');
241
-
242
- errors.errorsFor('username');
243
- // =>
244
- // [
245
- // { attribute: 'username', message: 'This field is required' },
246
- // ]
247
- ```
248
- @method add
249
- @public
250
- @param {string} attribute - the property name of an attribute or relationship
251
- @param {string[]|string} messages - an error message or array of error messages for the attribute
252
- */
253
- add(attribute: string, messages: string[] | string): void {
254
- const errors = this._findOrCreateMessages(attribute, messages);
255
- this.addObjects(errors);
256
-
257
- this.errorsFor(attribute).addObjects(errors);
258
- this.__record.currentState.notify('isValid');
259
-
260
- this.notifyPropertyChange(attribute);
261
- }
262
-
263
- /**
264
- @method _findOrCreateMessages
265
- @private
266
- */
267
- _findOrCreateMessages(attribute: string, messages: string | string[]): ValidationError[] {
268
- let errors = this.errorsFor(attribute);
269
- let messagesArray = Array.isArray(messages) ? messages : [messages];
270
- let _messages: ValidationError[] = new Array(messagesArray.length) as ValidationError[];
271
-
272
- for (let i = 0; i < messagesArray.length; i++) {
273
- let message = messagesArray[i];
274
- let err = errors.findBy('message', message);
275
- if (err) {
276
- _messages[i] = err;
277
- } else {
278
- _messages[i] = {
279
- attribute: attribute,
280
- message,
281
- };
282
- }
283
- }
284
-
285
- return _messages;
286
- }
287
-
288
- /**
289
- Manually removes all errors for a given member from the record.
290
- This will transition the record into a `valid` state, and
291
- triggers the `becameValid` event and lifecycle method.
292
-
293
- Example:
294
-
295
- ```javascript
296
- let errors = user.errors;
297
- errors.add('phone', ['error-1', 'error-2']);
298
-
299
- errors.errorsFor('phone');
300
- // =>
301
- // [
302
- // { attribute: 'phone', message: 'error-1' },
303
- // { attribute: 'phone', message: 'error-2' },
304
- // ]
305
-
306
- errors.remove('phone');
307
-
308
- errors.errorsFor('phone');
309
- // => undefined
310
- ```
311
- @method remove
312
- @public
313
- @param {string} member - the property name of an attribute or relationship
314
- */
315
- remove(attribute: string) {
316
- if (this.isEmpty) {
317
- return;
318
- }
319
-
320
- let content = this.rejectBy('attribute', attribute);
321
- this.content.setObjects(content);
322
-
323
- // Although errorsByAttributeName.delete is technically enough to sync errors state, we also
324
- // must mutate the array as well for autotracking
325
- let errors = this.errorsFor(attribute);
326
- for (let i = 0; i < errors.length; i++) {
327
- if (errors[i].attribute === attribute) {
328
- // .replace from Ember.NativeArray is necessary. JS splice will not work.
329
- errors.replace(i, 1);
330
- }
331
- }
332
- this.errorsByAttributeName.delete(attribute);
333
-
334
- this.__record.currentState.notify('isValid');
335
- this.notifyPropertyChange(attribute);
336
- this.notifyPropertyChange('length');
337
- }
338
-
339
- /**
340
- Manually clears all errors for the record.
341
- This will transition the record into a `valid` state, and
342
- will trigger the `becameValid` event and lifecycle method.
343
-
344
- Example:
345
-
346
- ```javascript
347
- let errors = user.errors;
348
- errors.add('username', ['error-a']);
349
- errors.add('phone', ['error-1', 'error-2']);
350
-
351
- errors.errorsFor('username');
352
- // =>
353
- // [
354
- // { attribute: 'username', message: 'error-a' },
355
- // ]
356
-
357
- errors.errorsFor('phone');
358
- // =>
359
- // [
360
- // { attribute: 'phone', message: 'error-1' },
361
- // { attribute: 'phone', message: 'error-2' },
362
- // ]
363
-
364
- errors.clear();
365
-
366
- errors.errorsFor('username');
367
- // => undefined
368
-
369
- errors.errorsFor('phone');
370
- // => undefined
371
-
372
- errors.messages
373
- // => []
374
- ```
375
- @method clear
376
- @public
377
- */
378
- clear(): void {
379
- if (this.isEmpty) {
380
- return;
381
- }
382
-
383
- let errorsByAttributeName = this.errorsByAttributeName;
384
- let attributes: string[] = [];
385
-
386
- errorsByAttributeName.forEach(function (_, attribute) {
387
- attributes.push(attribute);
388
- });
389
-
390
- errorsByAttributeName.clear();
391
- attributes.forEach((attribute) => {
392
- this.notifyPropertyChange(attribute);
393
- });
394
-
395
- this.__record.currentState.notify('isValid');
396
- super.clear();
397
- }
398
-
399
- /**
400
- Checks if there are error messages for the given attribute.
401
-
402
- ```app/controllers/user/edit.js
403
- import Controller from '@ember/controller';
404
- import { action } from '@ember/object';
405
-
406
- export default class UserEditController extends Controller {
407
- @action
408
- save(user) {
409
- if (user.errors.has('email')) {
410
- return alert('Please update your email before attempting to save.');
411
- }
412
- user.save();
413
- }
414
- }
415
- ```
416
-
417
- @method has
418
- @public
419
- @param {String} attribute
420
- @return {Boolean} true if there some errors on given attribute
421
- */
422
- has(attribute: string): boolean {
423
- return this.errorsFor(attribute).length > 0;
424
- }
425
- }