@ember-data/model 5.4.0-alpha.7 → 5.4.0-alpha.71

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 (88) hide show
  1. package/README.md +19 -0
  2. package/addon-main.cjs +5 -0
  3. package/blueprints/model/index.js +18 -5
  4. package/blueprints/model-test/index.js +16 -10
  5. package/blueprints/model-test/qunit-files/__root__/__path__/__test__.js +4 -5
  6. package/{addon → dist}/-private.js +2 -2
  7. package/dist/has-many-CR10J-rp.js +675 -0
  8. package/dist/has-many-CR10J-rp.js.map +1 -0
  9. package/dist/hooks-BQaMzbZg.js +75 -0
  10. package/dist/hooks-BQaMzbZg.js.map +1 -0
  11. package/dist/hooks.js +2 -0
  12. package/{addon → dist}/hooks.js.map +1 -1
  13. package/dist/index.js +4 -0
  14. package/{addon → dist}/index.js.map +1 -1
  15. package/dist/migration-support.js +211 -0
  16. package/dist/migration-support.js.map +1 -0
  17. package/{addon/model-520a761c.js → dist/model-CsbDNxap.js} +2970 -2353
  18. package/dist/model-CsbDNxap.js.map +1 -0
  19. package/dist/schema-provider-Blw1xpOH.js +256 -0
  20. package/dist/schema-provider-Blw1xpOH.js.map +1 -0
  21. package/ember-data-logo-dark.svg +12 -0
  22. package/ember-data-logo-light.svg +12 -0
  23. package/package.json +76 -45
  24. package/unstable-preview-types/-private/attr.d.ts +174 -0
  25. package/unstable-preview-types/-private/attr.d.ts.map +1 -0
  26. package/unstable-preview-types/-private/attr.type-test.d.ts +4 -0
  27. package/unstable-preview-types/-private/attr.type-test.d.ts.map +1 -0
  28. package/unstable-preview-types/-private/belongs-to.d.ts +180 -0
  29. package/unstable-preview-types/-private/belongs-to.d.ts.map +1 -0
  30. package/unstable-preview-types/-private/belongs-to.type-test.d.ts +4 -0
  31. package/unstable-preview-types/-private/belongs-to.type-test.d.ts.map +1 -0
  32. package/unstable-preview-types/-private/debug/assert-polymorphic-type.d.ts +8 -0
  33. package/unstable-preview-types/-private/debug/assert-polymorphic-type.d.ts.map +1 -0
  34. package/unstable-preview-types/-private/errors.d.ts +305 -0
  35. package/unstable-preview-types/-private/errors.d.ts.map +1 -0
  36. package/unstable-preview-types/-private/has-many.d.ts +169 -0
  37. package/unstable-preview-types/-private/has-many.d.ts.map +1 -0
  38. package/unstable-preview-types/-private/has-many.type-test.d.ts +4 -0
  39. package/unstable-preview-types/-private/has-many.type-test.d.ts.map +1 -0
  40. package/unstable-preview-types/-private/hooks.d.ts +13 -0
  41. package/unstable-preview-types/-private/hooks.d.ts.map +1 -0
  42. package/unstable-preview-types/-private/legacy-relationships-support.d.ts +60 -0
  43. package/unstable-preview-types/-private/legacy-relationships-support.d.ts.map +1 -0
  44. package/unstable-preview-types/-private/many-array.d.ts +196 -0
  45. package/unstable-preview-types/-private/many-array.d.ts.map +1 -0
  46. package/unstable-preview-types/-private/model-for-mixin.d.ts +6 -0
  47. package/unstable-preview-types/-private/model-for-mixin.d.ts.map +1 -0
  48. package/unstable-preview-types/-private/model-methods.d.ts +37 -0
  49. package/unstable-preview-types/-private/model-methods.d.ts.map +1 -0
  50. package/unstable-preview-types/-private/model.d.ts +1306 -0
  51. package/unstable-preview-types/-private/model.d.ts.map +1 -0
  52. package/unstable-preview-types/-private/model.type-test.d.ts +4 -0
  53. package/unstable-preview-types/-private/model.type-test.d.ts.map +1 -0
  54. package/unstable-preview-types/-private/notify-changes.d.ts +8 -0
  55. package/unstable-preview-types/-private/notify-changes.d.ts.map +1 -0
  56. package/unstable-preview-types/-private/promise-belongs-to.d.ts +48 -0
  57. package/unstable-preview-types/-private/promise-belongs-to.d.ts.map +1 -0
  58. package/unstable-preview-types/-private/promise-many-array.d.ts +131 -0
  59. package/unstable-preview-types/-private/promise-many-array.d.ts.map +1 -0
  60. package/unstable-preview-types/-private/record-state.d.ts +90 -0
  61. package/unstable-preview-types/-private/record-state.d.ts.map +1 -0
  62. package/unstable-preview-types/-private/references/belongs-to.d.ts +503 -0
  63. package/unstable-preview-types/-private/references/belongs-to.d.ts.map +1 -0
  64. package/unstable-preview-types/-private/references/has-many.d.ts +515 -0
  65. package/unstable-preview-types/-private/references/has-many.d.ts.map +1 -0
  66. package/unstable-preview-types/-private/schema-provider.d.ts +64 -0
  67. package/unstable-preview-types/-private/schema-provider.d.ts.map +1 -0
  68. package/unstable-preview-types/-private/type-utils.d.ts +64 -0
  69. package/unstable-preview-types/-private/type-utils.d.ts.map +1 -0
  70. package/unstable-preview-types/-private/util.d.ts +11 -0
  71. package/unstable-preview-types/-private/util.d.ts.map +1 -0
  72. package/unstable-preview-types/-private.d.ts +13 -0
  73. package/unstable-preview-types/-private.d.ts.map +1 -0
  74. package/unstable-preview-types/hooks.d.ts +5 -0
  75. package/unstable-preview-types/hooks.d.ts.map +1 -0
  76. package/unstable-preview-types/index.d.ts +75 -0
  77. package/unstable-preview-types/index.d.ts.map +1 -0
  78. package/unstable-preview-types/migration-support.d.ts +56 -0
  79. package/unstable-preview-types/migration-support.d.ts.map +1 -0
  80. package/addon/has-many-665b8730.js +0 -507
  81. package/addon/has-many-665b8730.js.map +0 -1
  82. package/addon/hooks-7a58334b.js +0 -157
  83. package/addon/hooks-7a58334b.js.map +0 -1
  84. package/addon/hooks.js +0 -1
  85. package/addon/index.js +0 -3
  86. package/addon/model-520a761c.js.map +0 -1
  87. package/addon-main.js +0 -93
  88. /package/{addon → dist}/-private.js.map +0 -0
@@ -0,0 +1,13 @@
1
+ declare module '@ember-data/model/-private' {
2
+ export { attr } from '@ember-data/model/-private/attr';
3
+ export { belongsTo } from '@ember-data/model/-private/belongs-to';
4
+ export { hasMany } from '@ember-data/model/-private/has-many';
5
+ export { Model } from '@ember-data/model/-private/model';
6
+ export type { ModelStore } from '@ember-data/model/-private/model';
7
+ export { Errors } from '@ember-data/model/-private/errors';
8
+ export { RelatedCollection as ManyArray } from '@ember-data/model/-private/many-array';
9
+ export { PromiseBelongsTo } from '@ember-data/model/-private/promise-belongs-to';
10
+ export { PromiseManyArray } from '@ember-data/model/-private/promise-many-array';
11
+ export { lookupLegacySupport, LEGACY_SUPPORT } from '@ember-data/model/-private/legacy-relationships-support';
12
+ }
13
+ //# sourceMappingURL=-private.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"-private.d.ts","sourceRoot":"","sources":["../src/-private.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAClD,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AACzC,YAAY,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAE3C,OAAO,EAAE,iBAAiB,IAAI,SAAS,EAAE,MAAM,uBAAuB,CAAC;AACvE,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AAGjE,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,yCAAyC,CAAC"}
@@ -0,0 +1,5 @@
1
+ declare module '@ember-data/model/hooks' {
2
+ export { instantiateRecord, teardownRecord, modelFor } from '@ember-data/model/-private/hooks';
3
+ export { buildSchema } from '@ember-data/model/-private/schema-provider';
4
+ }
5
+ //# sourceMappingURL=hooks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC/E,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC"}
@@ -0,0 +1,75 @@
1
+ /// <reference path="./-private.d.ts" />
2
+ /// <reference path="./hooks.d.ts" />
3
+ /// <reference path="./migration-support.d.ts" />
4
+ /// <reference path="./-private/util.d.ts" />
5
+ /// <reference path="./-private/model.d.ts" />
6
+ /// <reference path="./-private/hooks.d.ts" />
7
+ /// <reference path="./-private/record-state.d.ts" />
8
+ /// <reference path="./-private/attr.type-test.d.ts" />
9
+ /// <reference path="./-private/legacy-relationships-support.d.ts" />
10
+ /// <reference path="./-private/has-many.d.ts" />
11
+ /// <reference path="./-private/promise-many-array.d.ts" />
12
+ /// <reference path="./-private/model-for-mixin.d.ts" />
13
+ /// <reference path="./-private/belongs-to.type-test.d.ts" />
14
+ /// <reference path="./-private/notify-changes.d.ts" />
15
+ /// <reference path="./-private/belongs-to.d.ts" />
16
+ /// <reference path="./-private/many-array.d.ts" />
17
+ /// <reference path="./-private/has-many.type-test.d.ts" />
18
+ /// <reference path="./-private/errors.d.ts" />
19
+ /// <reference path="./-private/type-utils.d.ts" />
20
+ /// <reference path="./-private/schema-provider.d.ts" />
21
+ /// <reference path="./-private/model-methods.d.ts" />
22
+ /// <reference path="./-private/promise-belongs-to.d.ts" />
23
+ /// <reference path="./-private/model.type-test.d.ts" />
24
+ /// <reference path="./-private/attr.d.ts" />
25
+ /// <reference path="./-private/debug/assert-polymorphic-type.d.ts" />
26
+ /// <reference path="./-private/references/has-many.d.ts" />
27
+ /// <reference path="./-private/references/belongs-to.d.ts" />
28
+ declare module '@ember-data/model' {
29
+ /**
30
+ * This package provides a Presentation Model for resource data in an EmberData Cache.
31
+ *
32
+ * Models are defined as classes extending from `import Model from '@ember-data/model';` and the
33
+ * attributes and relationships on these classes are parsed at runtime to supply static "schema"
34
+ * to EmberData's SchemaService.
35
+ *
36
+ * Resource data for individual resources fetched from your API is presented to the UI via instances
37
+ * of the `Model`s you define. An instantiated `Model` is referred to as a `record`.
38
+
39
+ When we refer to the `ModelClass` as opposed to a `Model` or `Record` we are referring
40
+ specifically to the class definition and the static schema methods present on it.
41
+
42
+ When we refer to a `record` we refer to a specific class instance presenting
43
+ the resource data for a given `type` and `id`.
44
+
45
+ ### Defining a Model
46
+
47
+ ```app/models/person.js
48
+ import Model, { attr, belongsTo, hasMany } from '@ember-data/model';
49
+
50
+ export default class PersonModel extends Model {
51
+ @attr name;
52
+
53
+ @belongsTo('pet', { inverse: 'owners', async: false }) dog;
54
+
55
+ @hasMany('person', { inverse: 'friends', async: true }) friends;
56
+ }
57
+ ```
58
+
59
+ ### modelName convention
60
+
61
+ By convention, the name of a given model (its `type`) matches the name
62
+ of the file in the `app/models` folder and should be lowercase, singular
63
+ and dasherized.
64
+
65
+ @module @ember-data/model
66
+ @main @ember-data/model
67
+ */
68
+ export { Model as default, attr, belongsTo, hasMany } from '@ember-data/model/-private';
69
+ export type { PromiseBelongsTo as AsyncBelongsTo } from '@ember-data/model/-private/promise-belongs-to';
70
+ export type { PromiseManyArray as AsyncHasMany } from '@ember-data/model/-private/promise-many-array';
71
+ export type { RelatedCollection as ManyArray } from '@ember-data/model/-private/many-array';
72
+ export { instantiateRecord, teardownRecord, modelFor } from '@ember-data/model/-private/hooks';
73
+ export { ModelSchemaProvider } from '@ember-data/model/-private/schema-provider';
74
+ }
75
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,OAAO,EAAE,KAAK,IAAI,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAExE,YAAY,EAAE,gBAAgB,IAAI,cAAc,EAAE,MAAM,+BAA+B,CAAC;AACxF,YAAY,EAAE,gBAAgB,IAAI,YAAY,EAAE,MAAM,+BAA+B,CAAC;AACtF,YAAY,EAAE,iBAAiB,IAAI,SAAS,EAAE,MAAM,uBAAuB,CAAC;AAC5E,OAAO,EAAE,iBAAiB,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC/E,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC"}
@@ -0,0 +1,56 @@
1
+ declare module '@ember-data/model/migration-support' {
2
+ import type Store from '@ember-data/store';
3
+ import type { SchemaService } from '@ember-data/store/types';
4
+ import type { StableRecordIdentifier } from '@warp-drive/core-types';
5
+ import type { ObjectValue } from '@warp-drive/core-types/json/raw';
6
+ import type { Derivation, HashFn, Transformation } from '@warp-drive/core-types/schema/concepts';
7
+ import type { ArrayField, DerivedField, FieldSchema, GenericField, HashField, ObjectField, ResourceSchema } from '@warp-drive/core-types/schema/fields';
8
+ import type { WithPartial } from '@warp-drive/core-types/utils';
9
+ type AttributesSchema = ReturnType<Exclude<SchemaService['attributesDefinitionFor'], undefined>>;
10
+ type RelationshipsSchema = ReturnType<Exclude<SchemaService['relationshipsDefinitionFor'], undefined>>;
11
+ export function withDefaults(schema: WithPartial<ResourceSchema, 'legacy' | 'identity'>): ResourceSchema;
12
+ export function registerDerivations(schema: SchemaService): void;
13
+ export interface DelegatingSchemaService {
14
+ attributesDefinitionFor?(resource: StableRecordIdentifier | {
15
+ type: string;
16
+ }): AttributesSchema;
17
+ relationshipsDefinitionFor?(resource: StableRecordIdentifier | {
18
+ type: string;
19
+ }): RelationshipsSchema;
20
+ doesTypeExist?(type: string): boolean;
21
+ }
22
+ export class DelegatingSchemaService implements SchemaService {
23
+ _preferred: SchemaService;
24
+ _secondary: SchemaService;
25
+ constructor(store: Store, schema: SchemaService);
26
+ hasResource(resource: StableRecordIdentifier | {
27
+ type: string;
28
+ }): boolean;
29
+ hasTrait(type: string): boolean;
30
+ resourceHasTrait(resource: StableRecordIdentifier | {
31
+ type: string;
32
+ }, trait: string): boolean;
33
+ fields(resource: StableRecordIdentifier | {
34
+ type: string;
35
+ }): Map<string, FieldSchema>;
36
+ transformation(field: GenericField | ObjectField | ArrayField | {
37
+ type: string;
38
+ }): Transformation;
39
+ hashFn(field: HashField | {
40
+ type: string;
41
+ }): HashFn;
42
+ derivation(field: DerivedField | {
43
+ type: string;
44
+ }): Derivation;
45
+ resource(resource: StableRecordIdentifier | {
46
+ type: string;
47
+ }): ResourceSchema;
48
+ registerResources(schemas: ResourceSchema[]): void;
49
+ registerResource(schema: ResourceSchema): void;
50
+ registerTransformation(transform: Transformation): void;
51
+ registerDerivation<R, T, FM extends ObjectValue | null>(derivation: Derivation<R, T, FM>): void;
52
+ registerHashFn(hashFn: HashFn): void;
53
+ }
54
+ export {};
55
+ }
56
+ //# sourceMappingURL=migration-support.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"migration-support.d.ts","sourceRoot":"","sources":["../src/migration-support.ts"],"names":[],"mappings":"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"}
@@ -1,507 +0,0 @@
1
- import { macroCondition, getOwnConfig } from '@embroider/macros';
2
- import { assert, warn, deprecate } from '@ember/debug';
3
- import { computed } from '@ember/object';
4
- import { recordIdentifierFor } from '@ember-data/store';
5
- import { peekCache } from '@ember-data/store/-private';
6
- import { c as computedMacroWithOptionalParams, n as normalizeModelName, l as lookupLegacySupport } from "./model-520a761c";
7
- import { A } from '@ember/array';
8
- import { dasherize } from '@ember/string';
9
- import { singularize } from 'ember-inflector';
10
-
11
- /**
12
- @module @ember-data/model
13
- */
14
-
15
- /**
16
- `attr` defines an attribute on a [Model](/ember-data/release/classes/Model).
17
- By default, attributes are passed through as-is, however you can specify an
18
- optional type to have the value automatically transformed.
19
- Ember Data ships with four basic transform types: `string`, `number`,
20
- `boolean` and `date`. You can define your own transforms by subclassing
21
- [Transform](/ember-data/release/classes/Transform).
22
-
23
- Note that you cannot use `attr` to define an attribute of `id`.
24
-
25
- `attr` takes an optional hash as a second parameter, currently
26
- supported options are:
27
-
28
- - `defaultValue`: Pass a string or a function to be called to set the attribute
29
- to a default value if and only if the key is absent from the payload response.
30
-
31
- Example
32
-
33
- ```app/models/user.js
34
- import Model, { attr } from '@ember-data/model';
35
-
36
- export default class UserModel extends Model {
37
- @attr('string') username;
38
- @attr('string') email;
39
- @attr('boolean', { defaultValue: false }) verified;
40
- }
41
- ```
42
-
43
- Default value can also be a function. This is useful it you want to return
44
- a new object for each attribute.
45
-
46
- ```app/models/user.js
47
- import Model, { attr } from '@ember-data/model';
48
-
49
- export default class UserModel extends Model {
50
- @attr('string') username;
51
- @attr('string') email;
52
-
53
- @attr({
54
- defaultValue() {
55
- return {};
56
- }
57
- })
58
- settings;
59
- }
60
- ```
61
-
62
- The `options` hash is passed as second argument to a transforms'
63
- `serialize` and `deserialize` method. This allows to configure a
64
- transformation and adapt the corresponding value, based on the config:
65
-
66
- ```app/models/post.js
67
- import Model, { attr } from '@ember-data/model';
68
-
69
- export default class PostModel extends Model {
70
- @attr('text', {
71
- uppercase: true
72
- })
73
- text;
74
- }
75
- ```
76
-
77
- ```app/transforms/text.js
78
- export default class TextTransform {
79
- serialize(value, options) {
80
- if (options.uppercase) {
81
- return value.toUpperCase();
82
- }
83
-
84
- return value;
85
- }
86
-
87
- deserialize(value) {
88
- return value;
89
- }
90
-
91
- static create() {
92
- return new this();
93
- }
94
- }
95
- ```
96
-
97
- @method attr
98
- @public
99
- @static
100
- @for @ember-data/model
101
- @param {String|Object} type the attribute type
102
- @param {Object} options a hash of options
103
- @return {Attribute}
104
- */
105
- function attr(type, options) {
106
- if (typeof type === 'object') {
107
- options = type;
108
- type = undefined;
109
- } else {
110
- options = options || {};
111
- }
112
- let meta = {
113
- type: type,
114
- isAttribute: true,
115
- options: options
116
- };
117
- return computed({
118
- get(key) {
119
- if (macroCondition(getOwnConfig().env.DEBUG)) {
120
- if (['currentState'].indexOf(key) !== -1) {
121
- throw new Error(`'${key}' is a reserved property name on instances of classes extending Model. Please choose a different property name for your attr on ${this.constructor.toString()}`);
122
- }
123
- }
124
- if (this.isDestroyed || this.isDestroying) {
125
- return;
126
- }
127
- return peekCache(this).getAttr(recordIdentifierFor(this), key);
128
- },
129
- set(key, value) {
130
- if (macroCondition(getOwnConfig().env.DEBUG)) {
131
- if (['currentState'].indexOf(key) !== -1) {
132
- throw new Error(`'${key}' is a reserved property name on instances of classes extending Model. Please choose a different property name for your attr on ${this.constructor.toString()}`);
133
- }
134
- }
135
- assert(`Attempted to set '${key}' on the deleted record ${recordIdentifierFor(this)}`, !this.currentState.isDeleted);
136
- const identifier = recordIdentifierFor(this);
137
- const cache = peekCache(this);
138
- let currentValue = cache.getAttr(identifier, key);
139
- if (currentValue !== value) {
140
- cache.setAttr(identifier, key, value);
141
- if (!this.isValid) {
142
- const {
143
- errors
144
- } = this;
145
- if (errors.get(key)) {
146
- errors.remove(key);
147
- this.currentState.cleanErrorRequests();
148
- }
149
- }
150
- }
151
- return value;
152
- }
153
- }).meta(meta);
154
- }
155
- var attr$1 = computedMacroWithOptionalParams(attr);
156
-
157
- /**
158
- @module @ember-data/model
159
- */
160
-
161
- /**
162
- `belongsTo` is used to define One-To-One and One-To-Many
163
- relationships on a [Model](/ember-data/release/classes/Model).
164
-
165
-
166
- `belongsTo` takes an optional hash as a second parameter, currently
167
- supported options are:
168
-
169
- - `async`: A boolean value used to explicitly declare this to be an async relationship. The default is true.
170
- - `inverse`: A string used to identify the inverse property on a
171
- related model in a One-To-Many relationship. See [Explicit Inverses](#explicit-inverses)
172
- - `polymorphic` A boolean value to mark the relationship as polymorphic
173
-
174
- #### One-To-One
175
- To declare a one-to-one relationship between two models, use
176
- `belongsTo`:
177
-
178
- ```app/models/user.js
179
- import Model, { belongsTo } from '@ember-data/model';
180
-
181
- export default class UserModel extends Model {
182
- @belongsTo('profile') profile;
183
- }
184
- ```
185
-
186
- ```app/models/profile.js
187
- import Model, { belongsTo } from '@ember-data/model';
188
-
189
- export default class ProfileModel extends Model {
190
- @belongsTo('user') user;
191
- }
192
- ```
193
-
194
- #### One-To-Many
195
-
196
- To declare a one-to-many relationship between two models, use
197
- `belongsTo` in combination with `hasMany`, like this:
198
-
199
- ```app/models/post.js
200
- import Model, { hasMany } from '@ember-data/model';
201
-
202
- export default class PostModel extends Model {
203
- @hasMany('comment', { async: false, inverse: 'post' }) comments;
204
- }
205
- ```
206
-
207
- ```app/models/comment.js
208
- import Model, { belongsTo } from '@ember-data/model';
209
-
210
- export default class CommentModel extends Model {
211
- @belongsTo('post', { async: false, inverse: 'comments' }) post;
212
- }
213
- ```
214
-
215
- #### Sync relationships
216
-
217
- Ember Data resolves sync relationships with the related resources
218
- available in its local store, hence it is expected these resources
219
- to be loaded before or along-side the primary resource.
220
-
221
- ```app/models/comment.js
222
- import Model, { belongsTo } from '@ember-data/model';
223
-
224
- export default class CommentModel extends Model {
225
- @belongsTo('post', {
226
- async: false,
227
- inverse: null
228
- })
229
- post;
230
- }
231
- ```
232
-
233
- In contrast to async relationship, accessing a sync relationship
234
- will always return the record (Model instance) for the existing
235
- local resource, or null. But it will error on access when
236
- a related resource is known to exist and it has not been loaded.
237
-
238
- ```
239
- let post = comment.post;
240
-
241
- ```
242
-
243
- @method belongsTo
244
- @public
245
- @static
246
- @for @ember-data/model
247
- @param {String} modelName (optional) type of the relationship
248
- @param {Object} options (optional) a hash of options
249
- @return {Ember.computed} relationship
250
- */
251
- function belongsTo(modelName, options) {
252
- let opts = options;
253
- let userEnteredModelName = modelName;
254
- assert(`Expected options.async from @belongsTo('${userEnteredModelName}', options) to be a boolean`, opts && typeof opts.async === 'boolean');
255
- assert(`Expected options.inverse from @belongsTo('${userEnteredModelName}', options) to be either null or the string type of the related resource.`, opts.inverse === null || typeof opts.inverse === 'string' && opts.inverse.length > 0);
256
- let meta = {
257
- type: normalizeModelName(userEnteredModelName),
258
- isRelationship: true,
259
- options: opts,
260
- kind: 'belongsTo',
261
- name: 'Belongs To',
262
- key: null
263
- };
264
- return computed({
265
- get(key) {
266
- // this is a legacy behavior we may not carry into a new model setup
267
- // it's better to error on disconnected records so users find errors
268
- // in their logic.
269
- if (this.isDestroying || this.isDestroyed) {
270
- return null;
271
- }
272
- const support = lookupLegacySupport(this);
273
- if (macroCondition(getOwnConfig().env.DEBUG)) {
274
- if (['currentState'].indexOf(key) !== -1) {
275
- throw new Error(`'${key}' is a reserved property name on instances of classes extending Model. Please choose a different property name for your belongsTo on ${this.constructor.toString()}`);
276
- }
277
- if (Object.prototype.hasOwnProperty.call(opts, 'serialize')) {
278
- warn(`You provided a serialize option on the "${key}" property in the "${support.identifier.type}" class, this belongs in the serializer. See Serializer and it's implementations https://api.emberjs.com/ember-data/release/classes/Serializer`, false, {
279
- id: 'ds.model.serialize-option-in-belongs-to'
280
- });
281
- }
282
- if (Object.prototype.hasOwnProperty.call(opts, 'embedded')) {
283
- warn(`You provided an embedded option on the "${key}" property in the "${support.identifier.type}" class, this belongs in the serializer. See EmbeddedRecordsMixin https://api.emberjs.com/ember-data/release/classes/EmbeddedRecordsMixin`, false, {
284
- id: 'ds.model.embedded-option-in-belongs-to'
285
- });
286
- }
287
- }
288
- return support.getBelongsTo(key);
289
- },
290
- set(key, value) {
291
- const support = lookupLegacySupport(this);
292
- if (macroCondition(getOwnConfig().env.DEBUG)) {
293
- if (['currentState'].indexOf(key) !== -1) {
294
- throw new Error(`'${key}' is a reserved property name on instances of classes extending Model. Please choose a different property name for your belongsTo on ${this.constructor.toString()}`);
295
- }
296
- }
297
- this.store._join(() => {
298
- support.setDirtyBelongsTo(key, value);
299
- });
300
- return support.getBelongsTo(key);
301
- }
302
- }).meta(meta);
303
- }
304
- var belongsTo$1 = computedMacroWithOptionalParams(belongsTo);
305
- function normalizeType(type) {
306
- if (macroCondition(getOwnConfig().deprecations.DEPRECATE_NON_STRICT_TYPES)) {
307
- const result = singularize(dasherize(type));
308
- deprecate(`The resource type '${type}' is not normalized. Update your application code to use '${result}' instead of '${type}'.`, result === type, {
309
- id: 'ember-data:deprecate-non-strict-types',
310
- until: '6.0',
311
- for: 'ember-data',
312
- since: {
313
- available: '5.3',
314
- enabled: '5.3'
315
- }
316
- });
317
- return result;
318
- }
319
- return type;
320
- }
321
-
322
- /**
323
- `hasMany` is used to define One-To-Many and Many-To-Many
324
- relationships on a [Model](/ember-data/release/classes/Model).
325
-
326
- `hasMany` takes an optional hash as a second parameter, currently
327
- supported options are:
328
-
329
- - `async`: A boolean value used to explicitly declare this to be an async relationship. The default is true.
330
- - `inverse`: A string used to identify the inverse property on a related model.
331
- - `polymorphic` A boolean value to mark the relationship as polymorphic
332
-
333
- #### One-To-Many
334
- To declare a one-to-many relationship between two models, use
335
- `belongsTo` in combination with `hasMany`, like this:
336
-
337
- ```app/models/post.js
338
- import Model, { hasMany } from '@ember-data/model';
339
-
340
- export default class PostModel extends Model {
341
- @hasMany('comment') comments;
342
- }
343
- ```
344
-
345
- ```app/models/comment.js
346
- import Model, { belongsTo } from '@ember-data/model';
347
-
348
- export default class CommentModel extends Model {
349
- @belongsTo('post') post;
350
- }
351
- ```
352
-
353
- #### Many-To-Many
354
- To declare a many-to-many relationship between two models, use
355
- `hasMany`:
356
-
357
- ```app/models/post.js
358
- import Model, { hasMany } from '@ember-data/model';
359
-
360
- export default class PostModel extends Model {
361
- @hasMany('tag') tags;
362
- }
363
- ```
364
-
365
- ```app/models/tag.js
366
- import Model, { hasMany } from '@ember-data/model';
367
-
368
- export default class TagModel extends Model {
369
- @hasMany('post') posts;
370
- }
371
- ```
372
-
373
- You can avoid passing a string as the first parameter. In that case Ember Data
374
- will infer the type from the singularized key name.
375
-
376
- ```app/models/post.js
377
- import Model, { hasMany } from '@ember-data/model';
378
-
379
- export default class PostModel extends Model {
380
- @hasMany tags;
381
- }
382
- ```
383
-
384
- will lookup for a Tag type.
385
-
386
- #### Explicit Inverses
387
-
388
- Ember Data will do its best to discover which relationships map to
389
- one another. In the one-to-many code above, for example, Ember Data
390
- can figure out that changing the `comments` relationship should update
391
- the `post` relationship on the inverse because post is the only
392
- relationship to that model.
393
-
394
- However, sometimes you may have multiple `belongsTo`/`hasMany` for the
395
- same type. You can specify which property on the related model is
396
- the inverse using `hasMany`'s `inverse` option:
397
-
398
- ```app/models/comment.js
399
- import Model, { belongsTo } from '@ember-data/model';
400
-
401
- export default class CommentModel extends Model {
402
- @belongsTo('post') onePost;
403
- @belongsTo('post') twoPost
404
- @belongsTo('post') redPost;
405
- @belongsTo('post') bluePost;
406
- }
407
- ```
408
-
409
- ```app/models/post.js
410
- import Model, { hasMany } from '@ember-data/model';
411
-
412
- export default class PostModel extends Model {
413
- @hasMany('comment', {
414
- inverse: 'redPost'
415
- })
416
- comments;
417
- }
418
- ```
419
-
420
- You can also specify an inverse on a `belongsTo`, which works how
421
- you'd expect.
422
-
423
- #### Sync relationships
424
-
425
- Ember Data resolves sync relationships with the related resources
426
- available in its local store, hence it is expected these resources
427
- to be loaded before or along-side the primary resource.
428
-
429
- ```app/models/post.js
430
- import Model, { hasMany } from '@ember-data/model';
431
-
432
- export default class PostModel extends Model {
433
- @hasMany('comment', {
434
- async: false
435
- })
436
- comments;
437
- }
438
- ```
439
-
440
- In contrast to async relationship, accessing a sync relationship
441
- will always return a [ManyArray](/ember-data/release/classes/ManyArray) instance
442
- containing the existing local resources. But it will error on access
443
- when any of the known related resources have not been loaded.
444
-
445
- ```
446
- post.comments.forEach((comment) => {
447
-
448
- });
449
-
450
- ```
451
-
452
- If you are using `links` with sync relationships, you have to use
453
- `ref.reload` to fetch the resources.
454
-
455
- @method hasMany
456
- @public
457
- @static
458
- @for @ember-data/model
459
- @param {String} type (optional) type of the relationship
460
- @param {Object} options (optional) a hash of options
461
- @return {Ember.computed} relationship
462
- */
463
- function hasMany(type, options) {
464
- assert(`Expected hasMany options.async to be a boolean`, options && typeof options.async === 'boolean');
465
-
466
- // Metadata about relationships is stored on the meta of
467
- // the relationship. This is used for introspection and
468
- // serialization. Note that `key` is populated lazily
469
- // the first time the CP is called.
470
- let meta = {
471
- type: normalizeType(type),
472
- options,
473
- isRelationship: true,
474
- kind: 'hasMany',
475
- name: 'Has Many',
476
- key: null
477
- };
478
- return computed({
479
- get(key) {
480
- if (macroCondition(getOwnConfig().env.DEBUG)) {
481
- if (['currentState'].indexOf(key) !== -1) {
482
- throw new Error(`'${key}' is a reserved property name on instances of classes extending Model. Please choose a different property name for your hasMany on ${this.constructor.toString()}`);
483
- }
484
- }
485
- if (this.isDestroying || this.isDestroyed) {
486
- return A();
487
- }
488
- return lookupLegacySupport(this).getHasMany(key);
489
- },
490
- set(key, records) {
491
- if (macroCondition(getOwnConfig().env.DEBUG)) {
492
- if (['currentState'].indexOf(key) !== -1) {
493
- throw new Error(`'${key}' is a reserved property name on instances of classes extending Model. Please choose a different property name for your hasMany on ${this.constructor.toString()}`);
494
- }
495
- }
496
- const support = lookupLegacySupport(this);
497
- const manyArray = support.getManyArray(key);
498
- assert(`You must pass an array of records to set a hasMany relationship`, Array.isArray(records));
499
- this.store._join(() => {
500
- manyArray.splice(0, manyArray.length, ...records);
501
- });
502
- return support.getHasMany(key);
503
- }
504
- }).meta(meta);
505
- }
506
- var hasMany$1 = computedMacroWithOptionalParams(hasMany);
507
- export { attr$1 as a, belongsTo$1 as b, hasMany$1 as h };