@ember-data/model 5.3.2 → 5.3.4
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.
- package/README.md +19 -0
- package/addon-main.cjs +5 -0
- package/blueprints/model/index.js +18 -5
- package/blueprints/model-test/index.js +16 -10
- package/blueprints/model-test/qunit-files/__root__/__path__/__test__.js +4 -5
- package/{addon → dist}/-private.js +2 -2
- package/dist/has-many-78F2FHCC.js +674 -0
- package/dist/has-many-78F2FHCC.js.map +1 -0
- package/dist/hooks-BgrTI_ff.js +75 -0
- package/dist/hooks-BgrTI_ff.js.map +1 -0
- package/dist/hooks.js +2 -0
- package/{addon → dist}/hooks.js.map +1 -1
- package/dist/index.js +4 -0
- package/{addon → dist}/index.js.map +1 -1
- package/dist/migration-support.js +210 -0
- package/dist/migration-support.js.map +1 -0
- package/{addon/model-YsOraZ6y.js → dist/model-BFnMMaye.js} +889 -424
- package/dist/model-BFnMMaye.js.map +1 -0
- package/dist/schema-provider-Cgpcwfn7.js +256 -0
- package/dist/schema-provider-Cgpcwfn7.js.map +1 -0
- package/ember-data-logo-dark.svg +12 -0
- package/ember-data-logo-light.svg +12 -0
- package/package.json +48 -75
- package/unstable-preview-types/-private/attr.d.ts +174 -0
- package/unstable-preview-types/-private/attr.d.ts.map +1 -0
- package/unstable-preview-types/-private/attr.type-test.d.ts +4 -0
- package/unstable-preview-types/-private/attr.type-test.d.ts.map +1 -0
- package/unstable-preview-types/-private/belongs-to.d.ts +180 -0
- package/unstable-preview-types/-private/belongs-to.d.ts.map +1 -0
- package/unstable-preview-types/-private/belongs-to.type-test.d.ts +4 -0
- package/unstable-preview-types/-private/belongs-to.type-test.d.ts.map +1 -0
- package/unstable-preview-types/-private/debug/assert-polymorphic-type.d.ts +8 -0
- package/unstable-preview-types/-private/debug/assert-polymorphic-type.d.ts.map +1 -0
- package/unstable-preview-types/-private/errors.d.ts +305 -0
- package/unstable-preview-types/-private/errors.d.ts.map +1 -0
- package/unstable-preview-types/-private/has-many.d.ts +169 -0
- package/unstable-preview-types/-private/has-many.d.ts.map +1 -0
- package/unstable-preview-types/-private/has-many.type-test.d.ts +4 -0
- package/unstable-preview-types/-private/has-many.type-test.d.ts.map +1 -0
- package/unstable-preview-types/-private/hooks.d.ts +13 -0
- package/unstable-preview-types/-private/hooks.d.ts.map +1 -0
- package/unstable-preview-types/-private/legacy-relationships-support.d.ts +60 -0
- package/unstable-preview-types/-private/legacy-relationships-support.d.ts.map +1 -0
- package/unstable-preview-types/-private/many-array.d.ts +196 -0
- package/unstable-preview-types/-private/many-array.d.ts.map +1 -0
- package/unstable-preview-types/-private/model-for-mixin.d.ts +6 -0
- package/unstable-preview-types/-private/model-for-mixin.d.ts.map +1 -0
- package/unstable-preview-types/-private/model-methods.d.ts +37 -0
- package/unstable-preview-types/-private/model-methods.d.ts.map +1 -0
- package/unstable-preview-types/-private/model.d.ts +1306 -0
- package/unstable-preview-types/-private/model.d.ts.map +1 -0
- package/unstable-preview-types/-private/model.type-test.d.ts +4 -0
- package/unstable-preview-types/-private/model.type-test.d.ts.map +1 -0
- package/unstable-preview-types/-private/notify-changes.d.ts +8 -0
- package/unstable-preview-types/-private/notify-changes.d.ts.map +1 -0
- package/unstable-preview-types/-private/promise-belongs-to.d.ts +48 -0
- package/unstable-preview-types/-private/promise-belongs-to.d.ts.map +1 -0
- package/unstable-preview-types/-private/promise-many-array.d.ts +131 -0
- package/unstable-preview-types/-private/promise-many-array.d.ts.map +1 -0
- package/unstable-preview-types/-private/record-state.d.ts +90 -0
- package/unstable-preview-types/-private/record-state.d.ts.map +1 -0
- package/unstable-preview-types/-private/references/belongs-to.d.ts +503 -0
- package/unstable-preview-types/-private/references/belongs-to.d.ts.map +1 -0
- package/unstable-preview-types/-private/references/has-many.d.ts +515 -0
- package/unstable-preview-types/-private/references/has-many.d.ts.map +1 -0
- package/unstable-preview-types/-private/schema-provider.d.ts +64 -0
- package/unstable-preview-types/-private/schema-provider.d.ts.map +1 -0
- package/unstable-preview-types/-private/type-utils.d.ts +64 -0
- package/unstable-preview-types/-private/type-utils.d.ts.map +1 -0
- package/unstable-preview-types/-private/util.d.ts +11 -0
- package/unstable-preview-types/-private/util.d.ts.map +1 -0
- package/unstable-preview-types/-private.d.ts +13 -0
- package/unstable-preview-types/-private.d.ts.map +1 -0
- package/unstable-preview-types/hooks.d.ts +5 -0
- package/unstable-preview-types/hooks.d.ts.map +1 -0
- package/unstable-preview-types/index.d.ts +76 -0
- package/unstable-preview-types/index.d.ts.map +1 -0
- package/unstable-preview-types/migration-support.d.ts +56 -0
- package/unstable-preview-types/migration-support.d.ts.map +1 -0
- package/addon/has-many-PdQBns8a.js +0 -323
- package/addon/has-many-PdQBns8a.js.map +0 -1
- package/addon/hooks-dXmQbIOF.js +0 -176
- package/addon/hooks-dXmQbIOF.js.map +0 -1
- package/addon/hooks.js +0 -1
- package/addon/index.js +0 -3
- package/addon/migration-support.js +0 -118
- package/addon/migration-support.js.map +0 -1
- package/addon/model-YsOraZ6y.js.map +0 -1
- package/addon/util-3DHZJC9h.js +0 -38
- package/addon/util-3DHZJC9h.js.map +0 -1
- package/addon-main.js +0 -93
- /package/{addon → dist}/-private.js.map +0 -0
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
declare module '@ember-data/model/-private/attr' {
|
|
2
|
+
import type { ArrayValue, ObjectValue, PrimitiveValue } from '@warp-drive/core-types/json/raw';
|
|
3
|
+
import type { TransformName } from '@warp-drive/core-types/symbols';
|
|
4
|
+
import type { DecoratorPropertyDescriptor } from '@ember-data/model/-private/util';
|
|
5
|
+
/**
|
|
6
|
+
* Options provided to the attr decorator are
|
|
7
|
+
* supplied to the associated transform. Any
|
|
8
|
+
* key-value pair is valid; however, it is highly
|
|
9
|
+
* recommended to only use statically defined values
|
|
10
|
+
* that could be serialized to JSON.
|
|
11
|
+
*
|
|
12
|
+
* If no transform is provided, the only valid
|
|
13
|
+
* option is `defaultValue`.
|
|
14
|
+
*
|
|
15
|
+
* Examples:
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* class User extends Model {
|
|
19
|
+
* @attr('string', { defaultValue: 'Anonymous' }) name;
|
|
20
|
+
* @attr('date', { defaultValue: () => new Date() }) createdAt;
|
|
21
|
+
* @attr({ defaultValue: () => ({}) }) preferences;
|
|
22
|
+
* @attr('boolean') hasVerifiedEmail;
|
|
23
|
+
* @attr address;
|
|
24
|
+
* }
|
|
25
|
+
*
|
|
26
|
+
* @class NOTATHING
|
|
27
|
+
* @typedoc
|
|
28
|
+
*/
|
|
29
|
+
export type AttrOptions<DV = PrimitiveValue | object | unknown[]> = {
|
|
30
|
+
/**
|
|
31
|
+
* The default value for this attribute.
|
|
32
|
+
*
|
|
33
|
+
* Default values can be provided as a value or a function that will be
|
|
34
|
+
* executed to generate the default value.
|
|
35
|
+
*
|
|
36
|
+
* Default values *should not* be stateful (object, arrays, etc.) as
|
|
37
|
+
* they will be shared across all instances of the record.
|
|
38
|
+
*
|
|
39
|
+
* @typedoc
|
|
40
|
+
*/
|
|
41
|
+
defaultValue?: DV extends PrimitiveValue ? DV : () => DV;
|
|
42
|
+
};
|
|
43
|
+
type LooseTransformInstance<V, Raw, Name extends string> = {
|
|
44
|
+
/**
|
|
45
|
+
* value type must match the return type of the deserialize method
|
|
46
|
+
*
|
|
47
|
+
* @typedoc
|
|
48
|
+
*/
|
|
49
|
+
serialize: (value: V, options: any) => Raw;
|
|
50
|
+
/**
|
|
51
|
+
* defaultValue type must match the return type of the deserialize method
|
|
52
|
+
*
|
|
53
|
+
* @typedoc
|
|
54
|
+
*/
|
|
55
|
+
deserialize: (value: Raw, options: any) => V;
|
|
56
|
+
[TransformName]: Name;
|
|
57
|
+
};
|
|
58
|
+
export type TransformHasType = {
|
|
59
|
+
[TransformName]: string;
|
|
60
|
+
};
|
|
61
|
+
export type TypedTransformInstance<V, T extends string> = LooseTransformInstance<V, string, T> | LooseTransformInstance<V, number, T> | LooseTransformInstance<V, boolean, T> | LooseTransformInstance<V, null, T> | LooseTransformInstance<V, ObjectValue, T> | LooseTransformInstance<V, ArrayValue, T>;
|
|
62
|
+
export type GetMaybeDeserializeValue<T> = T extends {
|
|
63
|
+
deserialize: (...args: any[]) => unknown;
|
|
64
|
+
} ? ReturnType<T['deserialize']> : never;
|
|
65
|
+
export type TypeFromInstance<T> = T extends TransformHasType ? T[typeof TransformName] : never;
|
|
66
|
+
export type OptionsFromInstance<T> = TypeFromInstance<T> extends never ? never : GetMaybeDeserializeValue<T> extends never ? never : T extends TypedTransformInstance<GetMaybeDeserializeValue<T>, TypeFromInstance<T>> ? Parameters<T['deserialize']>[1] & Parameters<T['serialize']>[1] & AttrOptions<ReturnType<T['deserialize']>> : never;
|
|
67
|
+
/**
|
|
68
|
+
* The return type of `void` is a lie to appease TypeScript. The actual return type
|
|
69
|
+
* is a descriptor, but typescript incorrectly insists that decorator functions return
|
|
70
|
+
* `void` or `any`.
|
|
71
|
+
*
|
|
72
|
+
* @typedoc
|
|
73
|
+
*/
|
|
74
|
+
export type DataDecorator = (target: object, key: string, desc?: DecoratorPropertyDescriptor) => void;
|
|
75
|
+
/**
|
|
76
|
+
`attr` defines an attribute on a [Model](/ember-data/release/classes/Model).
|
|
77
|
+
By default, attributes are passed through as-is, however you can specify an
|
|
78
|
+
optional type to have the value automatically transformed.
|
|
79
|
+
EmberData ships with four basic transform types: `string`, `number`,
|
|
80
|
+
`boolean` and `date`. You can define your own transforms by subclassing
|
|
81
|
+
[Transform](/ember-data/release/classes/Transform).
|
|
82
|
+
|
|
83
|
+
Note that you cannot use `attr` to define an attribute of `id`.
|
|
84
|
+
|
|
85
|
+
`attr` takes an optional hash as a second parameter, currently
|
|
86
|
+
supported options are:
|
|
87
|
+
|
|
88
|
+
- `defaultValue`: Pass a string or a function to be called to set the attribute
|
|
89
|
+
to a default value if and only if the key is absent from the payload response.
|
|
90
|
+
|
|
91
|
+
Example
|
|
92
|
+
|
|
93
|
+
```app/models/user.js
|
|
94
|
+
import Model, { attr } from '@ember-data/model';
|
|
95
|
+
|
|
96
|
+
export default class UserModel extends Model {
|
|
97
|
+
@attr('string') username;
|
|
98
|
+
@attr('string') email;
|
|
99
|
+
@attr('boolean', { defaultValue: false }) verified;
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Default value can also be a function. This is useful it you want to return
|
|
104
|
+
a new object for each attribute.
|
|
105
|
+
|
|
106
|
+
```app/models/user.js
|
|
107
|
+
import Model, { attr } from '@ember-data/model';
|
|
108
|
+
|
|
109
|
+
export default class UserModel extends Model {
|
|
110
|
+
@attr('string') username;
|
|
111
|
+
@attr('string') email;
|
|
112
|
+
|
|
113
|
+
@attr({
|
|
114
|
+
defaultValue() {
|
|
115
|
+
return {};
|
|
116
|
+
}
|
|
117
|
+
})
|
|
118
|
+
settings;
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
The `options` hash is passed as second argument to a transforms'
|
|
123
|
+
`serialize` and `deserialize` method. This allows to configure a
|
|
124
|
+
transformation and adapt the corresponding value, based on the config:
|
|
125
|
+
|
|
126
|
+
```app/models/post.js
|
|
127
|
+
import Model, { attr } from '@ember-data/model';
|
|
128
|
+
|
|
129
|
+
export default class PostModel extends Model {
|
|
130
|
+
@attr('text', {
|
|
131
|
+
uppercase: true
|
|
132
|
+
})
|
|
133
|
+
text;
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
```app/transforms/text.js
|
|
138
|
+
export default class TextTransform {
|
|
139
|
+
serialize(value, options) {
|
|
140
|
+
if (options.uppercase) {
|
|
141
|
+
return value.toUpperCase();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return value;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
deserialize(value) {
|
|
148
|
+
return value;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
static create() {
|
|
152
|
+
return new this();
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
@method attr
|
|
158
|
+
@public
|
|
159
|
+
@static
|
|
160
|
+
@for @ember-data/model
|
|
161
|
+
@param {String|Object} type the attribute type
|
|
162
|
+
@param {Object} options a hash of options
|
|
163
|
+
@return {Attribute}
|
|
164
|
+
*/
|
|
165
|
+
export function attr(): DataDecorator;
|
|
166
|
+
export function attr<T>(type: TypeFromInstance<T>): DataDecorator;
|
|
167
|
+
export function attr(type: string): DataDecorator;
|
|
168
|
+
export function attr(options: AttrOptions): DataDecorator;
|
|
169
|
+
export function attr<T>(type: TypeFromInstance<T>, options?: OptionsFromInstance<T>): DataDecorator;
|
|
170
|
+
export function attr(type: string, options?: AttrOptions & object): DataDecorator;
|
|
171
|
+
export function attr(target: object, key: string | symbol, desc?: PropertyDescriptor): void;
|
|
172
|
+
export {};
|
|
173
|
+
}
|
|
174
|
+
//# sourceMappingURL=attr.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attr.d.ts","sourceRoot":"","sources":["../../src/-private/attr.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,cAAc,EAAS,MAAM,iCAAiC,CAAC;AACtG,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,gCAAgC,CAAC;AAGpE,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,QAAQ,CAAC;AAG1D;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,MAAM,WAAW,CAAC,EAAE,GAAG,cAAc,GAAG,MAAM,GAAG,OAAO,EAAE,IAAI;IAClE;;;;;;;;;;OAUG;IACH,YAAY,CAAC,EAAE,EAAE,SAAS,cAAc,GAAG,EAAE,GAAG,MAAM,EAAE,CAAC;CAC1D,CAAC;AA0EF,KAAK,sBAAsB,CAAC,CAAC,EAAE,GAAG,EAAE,IAAI,SAAS,MAAM,IAAI;IACzD;;;;OAIG;IAGH,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,EAAE,OAAO,EAA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/attr.type-test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
declare module '@ember-data/model/-private/belongs-to' {
|
|
2
|
+
import type { TypeFromInstance } from '@warp-drive/core-types/record';
|
|
3
|
+
/**
|
|
4
|
+
@module @ember-data/model
|
|
5
|
+
*/
|
|
6
|
+
export type IsUnknown<T> = unknown extends T ? true : false;
|
|
7
|
+
export type RelationshipOptions<T, Async extends boolean> = {
|
|
8
|
+
async: Async;
|
|
9
|
+
inverse: null | (IsUnknown<T> extends true ? string : keyof NoNull<T> & string);
|
|
10
|
+
polymorphic?: boolean;
|
|
11
|
+
as?: string;
|
|
12
|
+
resetOnRemoteUpdate?: boolean;
|
|
13
|
+
};
|
|
14
|
+
export type NoNull<T> = Exclude<T, null>;
|
|
15
|
+
export type RelationshipDecorator<T> = <This>(target: This, key: string, desc?: PropertyDescriptor) => void;
|
|
16
|
+
/**
|
|
17
|
+
`belongsTo` is used to define One-To-One and One-To-Many, and One-To-None
|
|
18
|
+
relationships on a [Model](/ember-data/release/classes/Model).
|
|
19
|
+
|
|
20
|
+
`belongsTo` takes a configuration hash as a second parameter, currently
|
|
21
|
+
supported options are:
|
|
22
|
+
|
|
23
|
+
- `async`: (*required*) A boolean value used to whether this is a sync (false) or async (true) relationship.
|
|
24
|
+
- `inverse`: (*required*) A string used to identify the inverse property on a related model, or `null`.
|
|
25
|
+
- `polymorphic`: (*optional*) A boolean value to mark the relationship as polymorphic
|
|
26
|
+
- `as`: (*optional*) A string used to the abstract type "this" record satisfies for polymorphism.
|
|
27
|
+
|
|
28
|
+
### Examples
|
|
29
|
+
|
|
30
|
+
To a **one-to-many** (or many-to-many) relationship, use
|
|
31
|
+
`belongsTo` in combination with `hasMany`:
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
// app/models/comment.js
|
|
35
|
+
import Model, { belongsTo } from '@ember-data/model';
|
|
36
|
+
|
|
37
|
+
export default class Comment extends Model {
|
|
38
|
+
@belongsTo('post', { async: false, inverse: 'comments' }) post;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// app/models/post.js
|
|
42
|
+
import Model, { hasMany } from '@ember-data/model';
|
|
43
|
+
|
|
44
|
+
export default class Post extends Model {
|
|
45
|
+
@hasMany('comment', { async: false, inverse: 'post' }) comments;
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
To a **one-to-one** relationship with managed inverses, use `belongsTo` for both sides:
|
|
50
|
+
|
|
51
|
+
```js
|
|
52
|
+
// app/models/author.js
|
|
53
|
+
import Model, { belongsTo } from '@ember-data/model';
|
|
54
|
+
|
|
55
|
+
export default class Author extends Model {
|
|
56
|
+
@belongsTo('address', { async: true, inverse: 'owner' }) address;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// app/models/address.js
|
|
60
|
+
import Model, { belongsTo } from '@ember-data/model';
|
|
61
|
+
|
|
62
|
+
export default class Address extends Model {
|
|
63
|
+
@belongsTo('author', { async: true, inverse: 'address' }) owner;
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
To a **one-to-one** relationship without managed inverses, use `belongsTo` for both sides
|
|
68
|
+
with `null` as the inverse:
|
|
69
|
+
|
|
70
|
+
```js
|
|
71
|
+
// app/models/author.js
|
|
72
|
+
import Model, { belongsTo } from '@ember-data/model';
|
|
73
|
+
|
|
74
|
+
export default class Author extends Model {
|
|
75
|
+
@belongsTo('address', { async: true, inverse: null }) address;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// app/models/address.js
|
|
79
|
+
import Model, { belongsTo } from '@ember-data/model';
|
|
80
|
+
|
|
81
|
+
export default class Address extends Model {
|
|
82
|
+
@belongsTo('author', { async: true, inverse: null }) owner;
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
To a one-to-none relationship between two models, use
|
|
87
|
+
`belongsTo` with inverse set to `null` on just one side::
|
|
88
|
+
|
|
89
|
+
```js
|
|
90
|
+
// app/models/person.js
|
|
91
|
+
import Model, { belongsTo } from '@ember-data/model';
|
|
92
|
+
|
|
93
|
+
export default class Person extends Model {
|
|
94
|
+
@belongsTo('person', { async: false, inverse: null }) bestFriend;
|
|
95
|
+
}
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
#### Sync vs Async Relationships
|
|
99
|
+
|
|
100
|
+
EmberData fulfills relationships using resource data available in
|
|
101
|
+
the cache.
|
|
102
|
+
|
|
103
|
+
Sync relationships point directly to the known related resources.
|
|
104
|
+
|
|
105
|
+
When a relationship is declared as async, if any of the known related
|
|
106
|
+
resources have not been loaded, they will be fetched. The property
|
|
107
|
+
on the record when accessed provides a promise that resolves once
|
|
108
|
+
all resources are loaded.
|
|
109
|
+
|
|
110
|
+
Async relationships may take advantage of links. On access, if the related
|
|
111
|
+
link has not been loaded, or if any known resources are not available in
|
|
112
|
+
the cache, the fresh state will be fetched using the link.
|
|
113
|
+
|
|
114
|
+
In contrast to async relationship, accessing a sync relationship
|
|
115
|
+
will error on access when any of the known related resources have
|
|
116
|
+
not been loaded.
|
|
117
|
+
|
|
118
|
+
If you are using `links` with sync relationships, you have to use
|
|
119
|
+
the BelongsTo reference API to fetch or refresh related resources
|
|
120
|
+
that aren't loaded. For instance, for a `bestFriend` relationship:
|
|
121
|
+
|
|
122
|
+
```js
|
|
123
|
+
person.belongsTo('bestFriend').reload();
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
#### Polymorphic Relationships
|
|
127
|
+
|
|
128
|
+
To a polymorphic relationship, use `hasMany` with the `polymorphic`
|
|
129
|
+
option set to `true`:
|
|
130
|
+
|
|
131
|
+
```js
|
|
132
|
+
// app/models/comment.js
|
|
133
|
+
import Model, { belongsTo } from '@ember-data/model';
|
|
134
|
+
|
|
135
|
+
export default class Comment extends Model {
|
|
136
|
+
@belongsTo('commentable', { async: false, inverse: 'comments', polymorphic: true }) parent;
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
`'commentable'` here is referred to as the "abstract type" for the polymorphic
|
|
141
|
+
relationship.
|
|
142
|
+
|
|
143
|
+
Polymorphic relationships with `inverse: null` will accept any type of record as their content.
|
|
144
|
+
Polymorphic relationships with `inverse` set to a string will only accept records with a matching
|
|
145
|
+
inverse relationships declaring itself as satisfying the abstract type.
|
|
146
|
+
|
|
147
|
+
Below, 'as' is used to the that 'post' record satisfies the abstract type 'commentable'
|
|
148
|
+
for this relationship.
|
|
149
|
+
|
|
150
|
+
```js
|
|
151
|
+
// app/models/post.js
|
|
152
|
+
import Model, { hasMany } from '@ember-data/model';
|
|
153
|
+
|
|
154
|
+
export default class Post extends Model {
|
|
155
|
+
@hasMany('comment', { async: false, inverse: 'parent', as: 'commentable' }) comments;
|
|
156
|
+
}
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Note: every Model that declares an inverse to a polymorphic relationship must
|
|
160
|
+
itself exactly the same. This is because polymorphism is based on structural
|
|
161
|
+
traits.
|
|
162
|
+
|
|
163
|
+
Polymorphic to polymorphic relationships are supported. Both sides of the relationship
|
|
164
|
+
must be declared as polymorphic, and the `as` option must be used to the abstract
|
|
165
|
+
type each record satisfies on both sides.
|
|
166
|
+
|
|
167
|
+
@method belongsTo
|
|
168
|
+
@public
|
|
169
|
+
@static
|
|
170
|
+
@for @ember-data/model
|
|
171
|
+
@param {string} type (optional) the name of the related resource
|
|
172
|
+
@param {object} options (optional) a hash of options
|
|
173
|
+
@return {PropertyDescriptor} relationship
|
|
174
|
+
*/
|
|
175
|
+
export function belongsTo(): never;
|
|
176
|
+
export function belongsTo(type: string): never;
|
|
177
|
+
export function belongsTo<T>(type: TypeFromInstance<NoNull<T>>, options: RelationshipOptions<T, boolean>): RelationshipDecorator<T>;
|
|
178
|
+
export function belongsTo(type: string, options: RelationshipOptions<unknown, boolean>): RelationshipDecorator<unknown>;
|
|
179
|
+
}
|
|
180
|
+
//# sourceMappingURL=belongs-to.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{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AC,CAAC,CAAC,EACjC,OAAO,EAAE,mBAAmB,CAAC,CAAC,EAAE,OAAO,CAAC,GACvC,qBAAqB,CAAC,CAAC,CAAC,CAAC;AAK5B,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,GAAG,qBAAqB,CAAC,OAAO,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"belongs-to.type-test.d.ts","sourceRoot":"","sources":["../../src/-private/belongs-to.type-test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
declare module '@ember-data/model/-private/debug/assert-polymorphic-type' {
|
|
2
|
+
import type { UpgradedMeta } from '@ember-data/graph/-private';
|
|
3
|
+
import type Store from '@ember-data/store';
|
|
4
|
+
import type { StableRecordIdentifier } from '@warp-drive/core-types';
|
|
5
|
+
let assertPolymorphicType: (parentIdentifier: StableRecordIdentifier, parentDefinition: UpgradedMeta, addedIdentifier: StableRecordIdentifier, store: Store) => void;
|
|
6
|
+
export { assertPolymorphicType };
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=assert-polymorphic-type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"assert-polymorphic-type.d.ts","sourceRoot":"","sources":["../../../src/-private/debug/assert-polymorphic-type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC/D,OAAO,KAAK,KAAK,MAAM,mBAAmB,CAAC;AAG3C,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAcrE,QAAA,IAAI,qBAAqB,EAAE,CACzB,gBAAgB,EAAE,sBAAsB,EACxC,gBAAgB,EAAE,YAAY,EAC9B,eAAe,EAAE,sBAAsB,EACvC,KAAK,EAAE,KAAK,KACT,IAAI,CAAC;AA2BV,OAAO,EAAE,qBAAqB,EAAE,CAAC"}
|