@warp-drive/legacy 5.6.0-alpha.14 → 5.6.0-alpha.17
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/declarations/adapter/-private/build-url-mixin.d.ts +41 -29
- package/declarations/adapter/-private/fastboot-interface.d.ts +4 -5
- package/declarations/adapter/-private/utils/continue-on-reject.d.ts +5 -6
- package/declarations/adapter/-private/utils/determine-body-promise.d.ts +1 -2
- package/declarations/adapter/-private/utils/fetch.d.ts +2 -3
- package/declarations/adapter/-private/utils/parse-response-headers.d.ts +0 -1
- package/declarations/adapter/-private/utils/serialize-into-hash.d.ts +5 -6
- package/declarations/adapter/-private/utils/serialize-query-params.d.ts +5 -2
- package/declarations/adapter/-private.d.ts +5 -6
- package/declarations/adapter/error.d.ts +206 -178
- package/declarations/adapter/json-api.d.ts +206 -207
- package/declarations/adapter/rest.d.ts +805 -808
- package/declarations/adapter.d.ts +742 -766
- package/declarations/compat/-private.d.ts +10 -11
- package/declarations/compat/builders/find-all.d.ts +26 -24
- package/declarations/compat/builders/find-record.d.ts +41 -39
- package/declarations/compat/builders/query.d.ts +50 -45
- package/declarations/compat/builders/save-record.d.ts +26 -24
- package/declarations/compat/builders/utils.d.ts +1 -2
- package/declarations/compat/builders.d.ts +10 -11
- package/declarations/compat/extensions.d.ts +59 -0
- package/declarations/compat/legacy-network-handler/fetch-manager.d.ts +39 -38
- package/declarations/compat/legacy-network-handler/identifier-has-id.d.ts +1 -2
- package/declarations/compat/legacy-network-handler/legacy-data-fetch.d.ts +9 -10
- package/declarations/compat/legacy-network-handler/legacy-data-utils.d.ts +1 -2
- package/declarations/compat/legacy-network-handler/legacy-network-handler.d.ts +1 -2
- package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts +521 -517
- package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +214 -215
- package/declarations/compat/legacy-network-handler/serializer-response.d.ts +5 -6
- package/declarations/compat/legacy-network-handler/snapshot-record-array.d.ts +83 -84
- package/declarations/compat/legacy-network-handler/snapshot.d.ts +228 -229
- package/declarations/compat/utils.d.ts +118 -119
- package/declarations/compat.d.ts +106 -104
- package/declarations/model/-private/attr.d.ts +164 -145
- package/declarations/model/-private/belongs-to.d.ts +135 -122
- package/declarations/model/-private/debug/assert-polymorphic-type.d.ts +15 -4
- package/declarations/model/-private/errors.d.ts +272 -267
- package/declarations/model/-private/has-many.d.ts +119 -116
- package/declarations/model/-private/hooks.d.ts +5 -6
- package/declarations/model/-private/legacy-relationships-support.d.ts +51 -52
- package/declarations/model/-private/model-for-mixin.d.ts +18 -3
- package/declarations/model/-private/model-methods.d.ts +29 -24
- package/declarations/model/-private/model.d.ts +1306 -1254
- package/declarations/model/-private/notify-changes.d.ts +3 -4
- package/declarations/model/-private/promise-belongs-to.d.ts +33 -27
- package/declarations/model/-private/promise-many-array.d.ts +125 -116
- package/declarations/model/-private/record-state.d.ts +63 -60
- package/declarations/model/-private/references/belongs-to.d.ts +491 -487
- package/declarations/model/-private/references/has-many.d.ts +495 -492
- package/declarations/model/-private/schema-provider.d.ts +53 -56
- package/declarations/model/-private/type-utils.d.ts +41 -41
- package/declarations/model/-private/util.d.ts +1 -2
- package/declarations/model/-private.d.ts +8 -8
- package/declarations/model/migration-support.d.ts +281 -271
- package/declarations/model.d.ts +48 -49
- package/declarations/serializer/-private/embedded-records-mixin.d.ts +70 -71
- package/declarations/serializer/-private/transforms/boolean.d.ts +37 -37
- package/declarations/serializer/-private/transforms/date.d.ts +22 -22
- package/declarations/serializer/-private/transforms/number.d.ts +22 -22
- package/declarations/serializer/-private/transforms/string.d.ts +22 -22
- package/declarations/serializer/-private/transforms/transform.d.ts +109 -110
- package/declarations/serializer/-private/utils.d.ts +0 -1
- package/declarations/serializer/json-api.d.ts +124 -494
- package/declarations/serializer/json.d.ts +62 -1031
- package/declarations/serializer/rest.d.ts +52 -552
- package/declarations/serializer/transform.d.ts +5 -6
- package/declarations/serializer.d.ts +217 -218
- package/dist/{-private-DFfBszo5.js → -private-CKrP0ogQ.js} +1 -1
- package/dist/adapter/-private.js +1 -1
- package/dist/adapter/error.js +17 -0
- package/dist/adapter/json-api.js +1 -1
- package/dist/adapter/rest.js +5 -7
- package/dist/adapter.js +6 -35
- package/dist/compat/-private.js +1 -1
- package/dist/compat/extensions.js +243 -0
- package/dist/compat.js +3 -4
- package/dist/{errors-D74uk36r.js → errors-BX5wowuz.js} +12 -10
- package/dist/{json-DSOlH9A8.js → json-Et4mt_LM.js} +84 -157
- package/dist/model/-private.js +1 -1
- package/dist/model/migration-support.js +15 -4
- package/dist/model.js +3 -3
- package/dist/{schema-provider-CXFLTMjg.js → schema-provider-BdQhkT-Q.js} +20 -13
- package/dist/{serialize-into-hash-BxfqWC8u.js → serialize-into-hash-Bp58npke.js} +1 -1
- package/dist/serializer/json-api.js +42 -2
- package/dist/serializer/json.js +1 -1
- package/dist/serializer/rest.js +46 -41
- package/dist/serializer/transform.js +0 -44
- package/package.json +7 -7
- package/declarations/adapter/-private/build-url-mixin.d.ts.map +0 -1
- package/declarations/adapter/-private/fastboot-interface.d.ts.map +0 -1
- package/declarations/adapter/-private/utils/continue-on-reject.d.ts.map +0 -1
- package/declarations/adapter/-private/utils/determine-body-promise.d.ts.map +0 -1
- package/declarations/adapter/-private/utils/fetch.d.ts.map +0 -1
- package/declarations/adapter/-private/utils/parse-response-headers.d.ts.map +0 -1
- package/declarations/adapter/-private/utils/serialize-into-hash.d.ts.map +0 -1
- package/declarations/adapter/-private/utils/serialize-query-params.d.ts.map +0 -1
- package/declarations/adapter/-private.d.ts.map +0 -1
- package/declarations/adapter/error.d.ts.map +0 -1
- package/declarations/adapter/json-api.d.ts.map +0 -1
- package/declarations/adapter/rest.d.ts.map +0 -1
- package/declarations/adapter.d.ts.map +0 -1
- package/declarations/compat/-private.d.ts.map +0 -1
- package/declarations/compat/builders/find-all.d.ts.map +0 -1
- package/declarations/compat/builders/find-record.d.ts.map +0 -1
- package/declarations/compat/builders/query.d.ts.map +0 -1
- package/declarations/compat/builders/save-record.d.ts.map +0 -1
- package/declarations/compat/builders/utils.d.ts.map +0 -1
- package/declarations/compat/builders.d.ts.map +0 -1
- package/declarations/compat/legacy-network-handler/fetch-manager.d.ts.map +0 -1
- package/declarations/compat/legacy-network-handler/identifier-has-id.d.ts.map +0 -1
- package/declarations/compat/legacy-network-handler/legacy-data-fetch.d.ts.map +0 -1
- package/declarations/compat/legacy-network-handler/legacy-data-utils.d.ts.map +0 -1
- package/declarations/compat/legacy-network-handler/legacy-network-handler.d.ts.map +0 -1
- package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts.map +0 -1
- package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts.map +0 -1
- package/declarations/compat/legacy-network-handler/serializer-response.d.ts.map +0 -1
- package/declarations/compat/legacy-network-handler/snapshot-record-array.d.ts.map +0 -1
- package/declarations/compat/legacy-network-handler/snapshot.d.ts.map +0 -1
- package/declarations/compat/utils.d.ts.map +0 -1
- package/declarations/compat.d.ts.map +0 -1
- package/declarations/model/-private/attr.d.ts.map +0 -1
- package/declarations/model/-private/attr.type-test.d.ts +0 -2
- package/declarations/model/-private/attr.type-test.d.ts.map +0 -1
- package/declarations/model/-private/belongs-to.d.ts.map +0 -1
- package/declarations/model/-private/belongs-to.type-test.d.ts +0 -2
- package/declarations/model/-private/belongs-to.type-test.d.ts.map +0 -1
- package/declarations/model/-private/debug/assert-polymorphic-type.d.ts.map +0 -1
- package/declarations/model/-private/errors.d.ts.map +0 -1
- package/declarations/model/-private/has-many.d.ts.map +0 -1
- package/declarations/model/-private/has-many.type-test.d.ts +0 -2
- package/declarations/model/-private/has-many.type-test.d.ts.map +0 -1
- package/declarations/model/-private/hooks.d.ts.map +0 -1
- package/declarations/model/-private/legacy-relationships-support.d.ts.map +0 -1
- package/declarations/model/-private/model-for-mixin.d.ts.map +0 -1
- package/declarations/model/-private/model-methods.d.ts.map +0 -1
- package/declarations/model/-private/model.d.ts.map +0 -1
- package/declarations/model/-private/model.type-test.d.ts +0 -2
- package/declarations/model/-private/model.type-test.d.ts.map +0 -1
- package/declarations/model/-private/notify-changes.d.ts.map +0 -1
- package/declarations/model/-private/promise-belongs-to.d.ts.map +0 -1
- package/declarations/model/-private/promise-many-array.d.ts.map +0 -1
- package/declarations/model/-private/promise-proxy-base.d.ts +0 -3
- package/declarations/model/-private/promise-proxy-base.d.ts.map +0 -1
- package/declarations/model/-private/record-state.d.ts.map +0 -1
- package/declarations/model/-private/references/belongs-to.d.ts.map +0 -1
- package/declarations/model/-private/references/has-many.d.ts.map +0 -1
- package/declarations/model/-private/schema-provider.d.ts.map +0 -1
- package/declarations/model/-private/type-utils.d.ts.map +0 -1
- package/declarations/model/-private/util.d.ts.map +0 -1
- package/declarations/model/-private.d.ts.map +0 -1
- package/declarations/model/migration-support.d.ts.map +0 -1
- package/declarations/model/migration-support.type-test.d.ts +0 -2
- package/declarations/model/migration-support.type-test.d.ts.map +0 -1
- package/declarations/model.d.ts.map +0 -1
- package/declarations/serializer/-private/embedded-records-mixin.d.ts.map +0 -1
- package/declarations/serializer/-private/transforms/boolean.d.ts.map +0 -1
- package/declarations/serializer/-private/transforms/boolean.type-test.d.ts +0 -2
- package/declarations/serializer/-private/transforms/boolean.type-test.d.ts.map +0 -1
- package/declarations/serializer/-private/transforms/date.d.ts.map +0 -1
- package/declarations/serializer/-private/transforms/number.d.ts.map +0 -1
- package/declarations/serializer/-private/transforms/string.d.ts.map +0 -1
- package/declarations/serializer/-private/transforms/transform.d.ts.map +0 -1
- package/declarations/serializer/-private/utils.d.ts.map +0 -1
- package/declarations/serializer/json-api.d.ts.map +0 -1
- package/declarations/serializer/json.d.ts.map +0 -1
- package/declarations/serializer/rest.d.ts.map +0 -1
- package/declarations/serializer/transform.d.ts.map +0 -1
- package/declarations/serializer.d.ts.map +0 -1
- package/dist/-private-DFfBszo5.js.map +0 -1
- package/dist/adapter/-private.js.map +0 -1
- package/dist/adapter/error.js.map +0 -1
- package/dist/adapter/json-api.js.map +0 -1
- package/dist/adapter/rest.js.map +0 -1
- package/dist/adapter.js.map +0 -1
- package/dist/compat/-private.js.map +0 -1
- package/dist/compat/builders.js.map +0 -1
- package/dist/compat/utils.js.map +0 -1
- package/dist/compat.js.map +0 -1
- package/dist/errors-D74uk36r.js.map +0 -1
- package/dist/json-DSOlH9A8.js.map +0 -1
- package/dist/model/-private.js.map +0 -1
- package/dist/model/migration-support.js.map +0 -1
- package/dist/model.js.map +0 -1
- package/dist/runtime-BPCpkOf1-BKOwiRJp.js.map +0 -1
- package/dist/schema-provider-CXFLTMjg.js.map +0 -1
- package/dist/serialize-into-hash-BxfqWC8u.js.map +0 -1
- package/dist/serializer/json-api.js.map +0 -1
- package/dist/serializer/json.js.map +0 -1
- package/dist/serializer/rest.js.map +0 -1
- package/dist/serializer/transform.js.map +0 -1
- package/dist/serializer.js.map +0 -1
|
@@ -1,158 +1,178 @@
|
|
|
1
|
-
import type { ArrayValue, ObjectValue, PrimitiveValue } from
|
|
2
|
-
import type { TransformName } from
|
|
3
|
-
import type { DecoratorPropertyDescriptor } from
|
|
1
|
+
import type { ArrayValue, ObjectValue, PrimitiveValue } from "@warp-drive/core/types/json/raw";
|
|
2
|
+
import type { TransformName } from "@warp-drive/core/types/symbols";
|
|
3
|
+
import type { DecoratorPropertyDescriptor } from "./util.js";
|
|
4
4
|
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
5
|
+
* Options provided to the attr decorator are
|
|
6
|
+
* supplied to the associated transform. Any
|
|
7
|
+
* key-value pair is valid; however, it is highly
|
|
8
|
+
* recommended to only use statically defined values
|
|
9
|
+
* that could be serialized to JSON.
|
|
10
|
+
*
|
|
11
|
+
* If no transform is provided, the only valid
|
|
12
|
+
* option is `defaultValue`.
|
|
13
|
+
*
|
|
14
|
+
* Examples:
|
|
15
|
+
*
|
|
16
|
+
* ```ts
|
|
17
|
+
* class User extends Model {
|
|
18
|
+
* @attr('string', { defaultValue: 'Anonymous' }) name;
|
|
19
|
+
* @attr('date', { defaultValue: () => new Date() }) createdAt;
|
|
20
|
+
* @attr({ defaultValue: () => ({}) }) preferences;
|
|
21
|
+
* @attr('boolean') hasVerifiedEmail;
|
|
22
|
+
* @attr address;
|
|
23
|
+
* }
|
|
24
|
+
*
|
|
25
|
+
* @class NOTATHING
|
|
26
|
+
*/
|
|
27
27
|
export type AttrOptions<DV = PrimitiveValue | object | unknown[]> = {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
28
|
+
/**
|
|
29
|
+
* The default value for this attribute.
|
|
30
|
+
*
|
|
31
|
+
* Default values can be provided as a value or a function that will be
|
|
32
|
+
* executed to generate the default value.
|
|
33
|
+
*
|
|
34
|
+
* Default values *should not* be stateful (object, arrays, etc.) as
|
|
35
|
+
* they will be shared across all instances of the record.
|
|
36
|
+
*
|
|
37
|
+
*/
|
|
38
|
+
defaultValue?: DV extends PrimitiveValue ? DV : () => DV;
|
|
39
39
|
};
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
40
|
+
// NOTE: Usage of Explicit ANY
|
|
41
|
+
// -------------------------------------------------------------------
|
|
42
|
+
// any is required here because we are the maximal not the minimal
|
|
43
|
+
// subset of options allowed. If we used unknown, object, or
|
|
44
|
+
// Record<string, unknown> we would get type errors when we try to
|
|
45
|
+
// assert against a more specific implementation with precise options.
|
|
46
|
+
// -------------------------------------------------------------------
|
|
47
|
+
type LooseTransformInstance<
|
|
48
|
+
V,
|
|
49
|
+
Raw,
|
|
50
|
+
Name extends string
|
|
51
|
+
> = {
|
|
52
|
+
/**
|
|
53
|
+
* value type must match the return type of the deserialize method
|
|
54
|
+
*
|
|
55
|
+
*/
|
|
56
|
+
// see note on Explicit ANY above
|
|
57
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
58
|
+
serialize: (value: V, options: any) => Raw;
|
|
59
|
+
/**
|
|
60
|
+
* defaultValue type must match the return type of the deserialize method
|
|
61
|
+
*
|
|
62
|
+
*/
|
|
63
|
+
// see note on Explicit ANY above
|
|
64
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
65
|
+
deserialize: (value: Raw, options: any) => V;
|
|
66
|
+
[TransformName]: Name;
|
|
52
67
|
};
|
|
53
68
|
export type TransformHasType = {
|
|
54
|
-
|
|
69
|
+
[TransformName]: string;
|
|
55
70
|
};
|
|
56
|
-
export type TypedTransformInstance<
|
|
71
|
+
export type TypedTransformInstance<
|
|
72
|
+
V,
|
|
73
|
+
T extends string
|
|
74
|
+
> = LooseTransformInstance<V, string, T> | LooseTransformInstance<V, number, T> | LooseTransformInstance<V, boolean, T> | LooseTransformInstance<V, null, T> | LooseTransformInstance<V, ObjectValue, T> | LooseTransformInstance<V, ArrayValue, T> | LooseTransformInstance<V, string | null, T> | LooseTransformInstance<V, number | null, T> | LooseTransformInstance<V, boolean | null, T> | LooseTransformInstance<V, ObjectValue | null, T> | LooseTransformInstance<V, ArrayValue | null, T>;
|
|
75
|
+
// see note on Explicit ANY above
|
|
76
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
57
77
|
export type GetMaybeDeserializeValue<T> = T extends {
|
|
58
|
-
|
|
59
|
-
} ? ReturnType<T[
|
|
78
|
+
deserialize: (...args: any[]) => unknown;
|
|
79
|
+
} ? ReturnType<T["deserialize"]> : never;
|
|
60
80
|
export type TypeFromInstance<T> = T extends TransformHasType ? T[typeof TransformName] : never;
|
|
61
|
-
export type ExtractOptions<T extends TypedTransformInstance<GetMaybeDeserializeValue<T>, TypeFromInstance<T>>> = Parameters<T[
|
|
62
|
-
export type OptionsFromInstance<T> = TypeFromInstance<T> extends never ? never : GetMaybeDeserializeValue<T> extends never ? never : T extends TypedTransformInstance<GetMaybeDeserializeValue<T>, TypeFromInstance<T>> ? Parameters<T[
|
|
81
|
+
export type ExtractOptions<T extends TypedTransformInstance<GetMaybeDeserializeValue<T>, TypeFromInstance<T>>> = Parameters<T["deserialize"]>[1] & Parameters<T["serialize"]>[1] & AttrOptions<ReturnType<T["deserialize"]>>;
|
|
82
|
+
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;
|
|
63
83
|
/**
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
84
|
+
* The return type of `void` is a lie to appease TypeScript. The actual return type
|
|
85
|
+
* is a descriptor, but typescript incorrectly insists that decorator functions return
|
|
86
|
+
* `void` or `any`.
|
|
87
|
+
*
|
|
88
|
+
*/
|
|
69
89
|
export type DataDecorator = (target: object, key: string, desc?: DecoratorPropertyDescriptor) => void;
|
|
70
90
|
/**
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
91
|
+
`attr` defines an attribute on a [Model](/ember-data/release/classes/Model).
|
|
92
|
+
By default, attributes are passed through as-is, however you can specify an
|
|
93
|
+
optional type to have the value automatically transformed.
|
|
94
|
+
EmberData ships with four basic transform types: `string`, `number`,
|
|
95
|
+
`boolean` and `date`. You can define your own transforms by subclassing
|
|
96
|
+
[Transform](/ember-data/release/classes/Transform).
|
|
97
|
+
|
|
98
|
+
Note that you cannot use `attr` to define an attribute of `id`.
|
|
99
|
+
|
|
100
|
+
`attr` takes an optional hash as a second parameter, currently
|
|
101
|
+
supported options are:
|
|
102
|
+
|
|
103
|
+
- `defaultValue`: Pass a string or a function to be called to set the attribute
|
|
104
|
+
to a default value if and only if the key is absent from the payload response.
|
|
105
|
+
|
|
106
|
+
Example
|
|
107
|
+
|
|
108
|
+
```js [app/models/user.js]
|
|
109
|
+
import { Model, attr } from '@warp-drive/legacy/model';
|
|
110
|
+
|
|
111
|
+
export default class UserModel extends Model {
|
|
112
|
+
@attr('string') username;
|
|
113
|
+
@attr('string') email;
|
|
114
|
+
@attr('boolean', { defaultValue: false }) verified;
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Default value can also be a function. This is useful it you want to return
|
|
119
|
+
a new object for each attribute.
|
|
120
|
+
|
|
121
|
+
```js [app/models/user.js]
|
|
122
|
+
import { Model, attr } from '@warp-drive/legacy/model';
|
|
123
|
+
|
|
124
|
+
export default class UserModel extends Model {
|
|
125
|
+
@attr('string') username;
|
|
126
|
+
@attr('string') email;
|
|
127
|
+
|
|
128
|
+
@attr({
|
|
129
|
+
defaultValue() {
|
|
130
|
+
return {};
|
|
131
|
+
}
|
|
132
|
+
})
|
|
133
|
+
settings;
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
The `options` hash is passed as second argument to a transforms'
|
|
138
|
+
`serialize` and `deserialize` method. This allows to configure a
|
|
139
|
+
transformation and adapt the corresponding value, based on the config:
|
|
140
|
+
|
|
141
|
+
```js [app/models/post.js]
|
|
142
|
+
import { Model, attr } from '@warp-drive/legacy/model';
|
|
143
|
+
|
|
144
|
+
export default class PostModel extends Model {
|
|
145
|
+
@attr('text', {
|
|
146
|
+
uppercase: true
|
|
147
|
+
})
|
|
148
|
+
text;
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
```js [app/transforms/text.js]
|
|
153
|
+
export default class TextTransform {
|
|
154
|
+
serialize(value, options) {
|
|
155
|
+
if (options.uppercase) {
|
|
156
|
+
return value.toUpperCase();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return value;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
deserialize(value) {
|
|
163
|
+
return value;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
static create() {
|
|
167
|
+
return new this();
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
@public
|
|
173
|
+
@param {String|Object} type the attribute type
|
|
174
|
+
@param {Object} options a hash of options
|
|
175
|
+
@return {Attribute}
|
|
156
176
|
*/
|
|
157
177
|
export declare function attr(): DataDecorator;
|
|
158
178
|
export declare function attr<T>(type: TypeFromInstance<T>): DataDecorator;
|
|
@@ -162,4 +182,3 @@ export declare function attr<T>(type: TypeFromInstance<T>, options?: OptionsFrom
|
|
|
162
182
|
export declare function attr(type: string, options?: AttrOptions & object): DataDecorator;
|
|
163
183
|
export declare function attr(target: object, key: string | symbol, desc?: PropertyDescriptor): void;
|
|
164
184
|
export {};
|
|
165
|
-
//# sourceMappingURL=attr.d.ts.map
|
|
@@ -1,173 +1,186 @@
|
|
|
1
|
-
import type { TypeFromInstance } from
|
|
1
|
+
import type { TypeFromInstance } from "@warp-drive/core/types/record";
|
|
2
2
|
export type IsUnknown<T> = unknown extends T ? true : false;
|
|
3
|
-
export type RelationshipOptions<
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
export type RelationshipOptions<
|
|
4
|
+
T,
|
|
5
|
+
Async extends boolean
|
|
6
|
+
> = {
|
|
7
|
+
async: Async;
|
|
8
|
+
inverse: null | (IsUnknown<T> extends true ? string : keyof NoNull<T> & string);
|
|
9
|
+
polymorphic?: boolean;
|
|
10
|
+
as?: string;
|
|
11
|
+
linksMode?: true;
|
|
12
|
+
resetOnRemoteUpdate?: boolean;
|
|
10
13
|
};
|
|
11
14
|
export type NoNull<T> = Exclude<T, null>;
|
|
15
|
+
// type BelongsToDecoratorObject<getT> = {
|
|
16
|
+
// get: () => getT;
|
|
17
|
+
// // set: (value: Awaited<getT>) => void;
|
|
18
|
+
// set: (value: getT) => void;
|
|
19
|
+
// // init: () => getT;
|
|
20
|
+
// };
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
12
22
|
export type RelationshipDecorator<T> = <This>(target: This, key: string, desc?: PropertyDescriptor) => void;
|
|
13
23
|
/**
|
|
14
|
-
|
|
15
|
-
|
|
24
|
+
`belongsTo` is used to define One-To-One and One-To-Many, and One-To-None
|
|
25
|
+
relationships on a [Model](/ember-data/release/classes/Model).
|
|
16
26
|
|
|
17
|
-
|
|
18
|
-
|
|
27
|
+
`belongsTo` takes a configuration hash as a second parameter, currently
|
|
28
|
+
supported options are:
|
|
19
29
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
30
|
+
- `async`: (*required*) A boolean value used to declare whether this is a sync (false) or async (true) relationship.
|
|
31
|
+
- `inverse`: (*required*) A string used to identify the inverse property on a related model, or `null`.
|
|
32
|
+
- `polymorphic`: (*optional*) A boolean value to mark the relationship as polymorphic
|
|
33
|
+
- `as`: (*optional*) A string used to declare the abstract type "this" record satisfies for polymorphism.
|
|
24
34
|
|
|
25
|
-
|
|
35
|
+
### Examples
|
|
26
36
|
|
|
27
|
-
|
|
28
|
-
|
|
37
|
+
To declare a **one-to-many** (or many-to-many) relationship, use
|
|
38
|
+
`belongsTo` in combination with `hasMany`:
|
|
29
39
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
40
|
+
```js
|
|
41
|
+
// app/models/comment.js
|
|
42
|
+
import { Model, belongsTo } from '@warp-drive/legacy/model';
|
|
33
43
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
44
|
+
export default class Comment extends Model {
|
|
45
|
+
@belongsTo('post', { async: false, inverse: 'comments' }) post;
|
|
46
|
+
}
|
|
37
47
|
|
|
38
|
-
|
|
39
|
-
|
|
48
|
+
// app/models/post.js
|
|
49
|
+
import { Model, hasMany } from '@warp-drive/legacy/model';
|
|
40
50
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
51
|
+
export default class Post extends Model {
|
|
52
|
+
@hasMany('comment', { async: false, inverse: 'post' }) comments;
|
|
53
|
+
}
|
|
54
|
+
```
|
|
45
55
|
|
|
46
|
-
|
|
56
|
+
To declare a **one-to-one** relationship with managed inverses, use `belongsTo` for both sides:
|
|
47
57
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
58
|
+
```js
|
|
59
|
+
// app/models/author.js
|
|
60
|
+
import { Model, belongsTo } from '@warp-drive/legacy/model';
|
|
51
61
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
62
|
+
export default class Author extends Model {
|
|
63
|
+
@belongsTo('address', { async: true, inverse: 'owner' }) address;
|
|
64
|
+
}
|
|
55
65
|
|
|
56
|
-
|
|
57
|
-
|
|
66
|
+
// app/models/address.js
|
|
67
|
+
import { Model, belongsTo } from '@warp-drive/legacy/model';
|
|
58
68
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
69
|
+
export default class Address extends Model {
|
|
70
|
+
@belongsTo('author', { async: true, inverse: 'address' }) owner;
|
|
71
|
+
}
|
|
72
|
+
```
|
|
63
73
|
|
|
64
|
-
|
|
65
|
-
|
|
74
|
+
To declare a **one-to-one** relationship without managed inverses, use `belongsTo` for both sides
|
|
75
|
+
with `null` as the inverse:
|
|
66
76
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
77
|
+
```js
|
|
78
|
+
// app/models/author.js
|
|
79
|
+
import { Model, belongsTo } from '@warp-drive/legacy/model';
|
|
70
80
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
81
|
+
export default class Author extends Model {
|
|
82
|
+
@belongsTo('address', { async: true, inverse: null }) address;
|
|
83
|
+
}
|
|
74
84
|
|
|
75
|
-
|
|
76
|
-
|
|
85
|
+
// app/models/address.js
|
|
86
|
+
import { Model, belongsTo } from '@warp-drive/legacy/model';
|
|
77
87
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
88
|
+
export default class Address extends Model {
|
|
89
|
+
@belongsTo('author', { async: true, inverse: null }) owner;
|
|
90
|
+
}
|
|
91
|
+
```
|
|
82
92
|
|
|
83
|
-
|
|
84
|
-
|
|
93
|
+
To declare a one-to-none relationship between two models, use
|
|
94
|
+
`belongsTo` with inverse set to `null` on just one side::
|
|
85
95
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
96
|
+
```js
|
|
97
|
+
// app/models/person.js
|
|
98
|
+
import { Model, belongsTo } from '@warp-drive/legacy/model';
|
|
89
99
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
100
|
+
export default class Person extends Model {
|
|
101
|
+
@belongsTo('person', { async: false, inverse: null }) bestFriend;
|
|
102
|
+
}
|
|
103
|
+
```
|
|
94
104
|
|
|
95
|
-
|
|
105
|
+
#### Sync vs Async Relationships
|
|
96
106
|
|
|
97
|
-
|
|
98
|
-
|
|
107
|
+
EmberData fulfills relationships using resource data available in
|
|
108
|
+
the cache.
|
|
99
109
|
|
|
100
|
-
|
|
110
|
+
Sync relationships point directly to the known related resources.
|
|
101
111
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
112
|
+
When a relationship is declared as async, if any of the known related
|
|
113
|
+
resources have not been loaded, they will be fetched. The property
|
|
114
|
+
on the record when accessed provides a promise that resolves once
|
|
115
|
+
all resources are loaded.
|
|
106
116
|
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
117
|
+
Async relationships may take advantage of links. On access, if the related
|
|
118
|
+
link has not been loaded, or if any known resources are not available in
|
|
119
|
+
the cache, the fresh state will be fetched using the link.
|
|
110
120
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
121
|
+
In contrast to async relationship, accessing a sync relationship
|
|
122
|
+
will error on access when any of the known related resources have
|
|
123
|
+
not been loaded.
|
|
114
124
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
125
|
+
If you are using `links` with sync relationships, you have to use
|
|
126
|
+
the BelongsTo reference API to fetch or refresh related resources
|
|
127
|
+
that aren't loaded. For instance, for a `bestFriend` relationship:
|
|
118
128
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
129
|
+
```js
|
|
130
|
+
person.belongsTo('bestFriend').reload();
|
|
131
|
+
```
|
|
122
132
|
|
|
123
|
-
|
|
133
|
+
#### Polymorphic Relationships
|
|
124
134
|
|
|
125
|
-
|
|
126
|
-
|
|
135
|
+
To declare a polymorphic relationship, use `hasMany` with the `polymorphic`
|
|
136
|
+
option set to `true`:
|
|
127
137
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
138
|
+
```js
|
|
139
|
+
// app/models/comment.js
|
|
140
|
+
import { Model, belongsTo } from '@warp-drive/legacy/model';
|
|
131
141
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
142
|
+
export default class Comment extends Model {
|
|
143
|
+
@belongsTo('commentable', { async: false, inverse: 'comments', polymorphic: true }) parent;
|
|
144
|
+
}
|
|
145
|
+
```
|
|
136
146
|
|
|
137
|
-
|
|
138
|
-
|
|
147
|
+
`'commentable'` here is referred to as the "abstract type" for the polymorphic
|
|
148
|
+
relationship.
|
|
139
149
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
150
|
+
Polymorphic relationships with `inverse: null` will accept any type of record as their content.
|
|
151
|
+
Polymorphic relationships with `inverse` set to a string will only accept records with a matching
|
|
152
|
+
inverse relationships declaring itself as satisfying the abstract type.
|
|
143
153
|
|
|
144
|
-
|
|
145
|
-
|
|
154
|
+
Below, 'as' is used to declare the that 'post' record satisfies the abstract type 'commentable'
|
|
155
|
+
for this relationship.
|
|
146
156
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
157
|
+
```js
|
|
158
|
+
// app/models/post.js
|
|
159
|
+
import { Model, hasMany } from '@warp-drive/legacy/model';
|
|
150
160
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
161
|
+
export default class Post extends Model {
|
|
162
|
+
@hasMany('comment', { async: false, inverse: 'parent', as: 'commentable' }) comments;
|
|
163
|
+
}
|
|
164
|
+
```
|
|
155
165
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
166
|
+
Note: every Model that declares an inverse to a polymorphic relationship must
|
|
167
|
+
declare itself exactly the same. This is because polymorphism is based on structural
|
|
168
|
+
traits.
|
|
159
169
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
170
|
+
Polymorphic to polymorphic relationships are supported. Both sides of the relationship
|
|
171
|
+
must be declared as polymorphic, and the `as` option must be used to declare the abstract
|
|
172
|
+
type each record satisfies on both sides.
|
|
163
173
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
174
|
+
@public
|
|
175
|
+
@param {String} type (optional) the name of the related resource
|
|
176
|
+
@param {Object} options (optional) a hash of options
|
|
177
|
+
@return {PropertyDescriptor} relationship
|
|
168
178
|
*/
|
|
169
179
|
export declare function belongsTo(): never;
|
|
170
180
|
export declare function belongsTo(type: string): never;
|
|
171
181
|
export declare function belongsTo<T>(type: TypeFromInstance<NoNull<T>>, options: RelationshipOptions<T, boolean>): RelationshipDecorator<T>;
|
|
182
|
+
// export function belongsTo<K extends Promise<unknown>, T extends Awaited<K> = Awaited<K>>(
|
|
183
|
+
// type: TypeFromInstance<NoNull<T>>,
|
|
184
|
+
// options: RelationshipOptions<T, true>
|
|
185
|
+
// ): RelationshipDecorator<K>;
|
|
172
186
|
export declare function belongsTo(type: string, options: RelationshipOptions<unknown, boolean>): RelationshipDecorator<unknown>;
|
|
173
|
-
//# sourceMappingURL=belongs-to.d.ts.map
|