@warp-drive/legacy 5.10.0-alpha.21 → 5.10.0-alpha.22
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/dist/adapter/-private.d.ts +1 -1
- package/dist/adapter/json-api.d.ts +6 -1
- package/dist/adapter/json-api.d.ts.map +1 -1
- package/dist/adapter/rest.d.ts +1 -1
- package/dist/adapter/rest.js +2 -2
- package/dist/adapter.d.ts +218 -0
- package/dist/compat/builders.d.ts +12 -0
- package/dist/compat/builders.d.ts.map +1 -1
- package/dist/compat/extensions.d.ts +5 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -3
- package/dist/model/migration-support.js +1 -1
- package/dist/{model-Be-e84FX.js → model-EM9IS3kJ.js} +2 -2
- package/dist/{model-Be-e84FX.js.map → model-EM9IS3kJ.js.map} +1 -1
- package/dist/{unpkg/dev/model-fragments-DCHMDr5f.js → model-fragments-bM9vucwt.js} +2 -2
- package/dist/{model-fragments-DCHMDr5f.js.map → model-fragments-bM9vucwt.js.map} +1 -1
- package/dist/model-fragments.d.ts +5 -0
- package/dist/model-fragments.d.ts.map +1 -1
- package/dist/model-fragments.js +1 -1
- package/dist/model.d.ts +68 -0
- package/dist/model.d.ts.map +1 -1
- package/dist/model.js +2 -2
- package/dist/{rest-CmFneLJH.d.ts → rest-CzVG2wcS.d.ts} +2 -2
- package/dist/{rest-CmFneLJH.d.ts.map → rest-CzVG2wcS.d.ts.map} +1 -1
- package/dist/{schema-provider-BAB8DyMr.js → schema-provider-BsP-TyJN.js} +3 -3
- package/dist/{schema-provider-BAB8DyMr.js.map → schema-provider-BsP-TyJN.js.map} +1 -1
- package/dist/serializer/transform.d.ts +5 -0
- package/dist/serializer/transform.d.ts.map +1 -1
- package/dist/serializer.d.ts +143 -0
- package/dist/serializer.d.ts.map +1 -1
- package/dist/{store-BLKEsJyw.js → store-09VISmB-.js} +2 -2
- package/dist/{store-BLKEsJyw.js.map → store-09VISmB-.js.map} +1 -1
- package/dist/store.d.ts +5 -0
- package/dist/store.d.ts.map +1 -1
- package/dist/store.js +1 -1
- package/dist/unpkg/dev/adapter/rest.js +2 -2
- package/dist/unpkg/dev/index.js +3 -3
- package/dist/unpkg/dev/model/migration-support.js +1 -1
- package/dist/unpkg/dev/{model-QCZr8eec.js → model-BRhfRvm7.js} +2 -2
- package/dist/unpkg/dev/{model-QCZr8eec.js.map → model-BRhfRvm7.js.map} +1 -1
- package/dist/{model-fragments-DCHMDr5f.js → unpkg/dev/model-fragments-bM9vucwt.js} +2 -2
- package/dist/unpkg/dev/{model-fragments-DCHMDr5f.js.map → model-fragments-bM9vucwt.js.map} +1 -1
- package/dist/unpkg/dev/model-fragments.js +1 -1
- package/dist/unpkg/dev/model.js +2 -2
- package/dist/unpkg/dev/{schema-provider-IMFO9Kgw.js → schema-provider-KpRvBrnz.js} +3 -3
- package/dist/unpkg/dev/{schema-provider-IMFO9Kgw.js.map → schema-provider-KpRvBrnz.js.map} +1 -1
- package/dist/unpkg/dev/{store-BiXsLLAu.js → store-DwneLtlP.js} +2 -2
- package/dist/unpkg/{dev-deprecated/store-Ci8k7Lh6.js.map → dev/store-DwneLtlP.js.map} +1 -1
- package/dist/unpkg/dev/store.js +1 -1
- package/dist/unpkg/dev-deprecated/adapter/rest.js +2 -2
- package/dist/unpkg/dev-deprecated/index.js +3 -3
- package/dist/unpkg/dev-deprecated/model/migration-support.js +1 -1
- package/dist/unpkg/dev-deprecated/{model-B0ZNLaHO.js → model-C5LZIABq.js} +2 -2
- package/dist/unpkg/dev-deprecated/{model-B0ZNLaHO.js.map → model-C5LZIABq.js.map} +1 -1
- package/dist/unpkg/dev-deprecated/{model-fragments-DCHMDr5f.js → model-fragments-bM9vucwt.js} +2 -2
- package/dist/unpkg/dev-deprecated/{model-fragments-DCHMDr5f.js.map → model-fragments-bM9vucwt.js.map} +1 -1
- package/dist/unpkg/dev-deprecated/model-fragments.js +1 -1
- package/dist/unpkg/dev-deprecated/model.js +2 -2
- package/dist/unpkg/dev-deprecated/{schema-provider-DGWYnFno.js → schema-provider-BHW1XG6Q.js} +3 -3
- package/dist/unpkg/dev-deprecated/{schema-provider-DGWYnFno.js.map → schema-provider-BHW1XG6Q.js.map} +1 -1
- package/dist/unpkg/dev-deprecated/{store-Ci8k7Lh6.js → store-CBtAELz5.js} +2 -2
- package/dist/unpkg/{dev/store-BiXsLLAu.js.map → dev-deprecated/store-CBtAELz5.js.map} +1 -1
- package/dist/unpkg/dev-deprecated/store.js +1 -1
- package/dist/unpkg/prod/adapter/rest.js +2 -2
- package/dist/unpkg/prod/index.js +3 -3
- package/dist/unpkg/prod/model/migration-support.js +1 -1
- package/dist/unpkg/prod/{model-QtC0IWbE.js → model-QIzJrhcV.js} +2 -2
- package/dist/unpkg/prod/{model-QtC0IWbE.js.map → model-QIzJrhcV.js.map} +1 -1
- package/dist/unpkg/prod/model-fragments-bM9vucwt.js +348 -0
- package/dist/unpkg/prod/{model-fragments-DCHMDr5f.js.map → model-fragments-bM9vucwt.js.map} +1 -1
- package/dist/unpkg/prod/model-fragments.js +1 -1
- package/dist/unpkg/prod/model.js +2 -2
- package/dist/unpkg/prod/{schema-provider-DPOzTa8e.js → schema-provider-D_H0kna2.js} +3 -3
- package/dist/unpkg/prod/{schema-provider-DPOzTa8e.js.map → schema-provider-D_H0kna2.js.map} +1 -1
- package/dist/unpkg/prod/{store-BsSkzWgk.js → store-BB1Ab-3t.js} +2 -2
- package/dist/unpkg/{prod-deprecated/store-Dsw2f4s-.js.map → prod/store-BB1Ab-3t.js.map} +1 -1
- package/dist/unpkg/prod/store.js +1 -1
- package/dist/unpkg/prod-deprecated/adapter/rest.js +2 -2
- package/dist/unpkg/prod-deprecated/index.js +3 -3
- package/dist/unpkg/prod-deprecated/model/migration-support.js +1 -1
- package/dist/unpkg/prod-deprecated/{model-B946S2pl.js → model-Dik7yYTm.js} +2 -2
- package/dist/unpkg/prod-deprecated/{model-B946S2pl.js.map → model-Dik7yYTm.js.map} +1 -1
- package/dist/unpkg/prod-deprecated/{model-fragments-DCHMDr5f.js → model-fragments-bM9vucwt.js} +2 -2
- package/dist/unpkg/prod-deprecated/{model-fragments-DCHMDr5f.js.map → model-fragments-bM9vucwt.js.map} +1 -1
- package/dist/unpkg/prod-deprecated/model-fragments.js +1 -1
- package/dist/unpkg/prod-deprecated/model.js +2 -2
- package/dist/unpkg/prod-deprecated/{schema-provider-xcaytn9y.js → schema-provider-BKBnIcPE.js} +3 -3
- package/dist/unpkg/prod-deprecated/{schema-provider-xcaytn9y.js.map → schema-provider-BKBnIcPE.js.map} +1 -1
- package/dist/unpkg/prod-deprecated/{store-Dsw2f4s-.js → store-Cp64p5ZC.js} +2 -2
- package/dist/unpkg/{prod/store-BsSkzWgk.js.map → prod-deprecated/store-Cp64p5ZC.js.map} +1 -1
- package/dist/unpkg/prod-deprecated/store.js +1 -1
- package/package.json +8 -8
- package/dist/unpkg/prod/model-fragments-DCHMDr5f.js +0 -348
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
import { withDefaults } from "./model/migration-support.js";
|
|
2
|
+
import { n as decorateMethodV2, r as initializeDeferredDecorator, t as decorateFieldV2 } from "./runtime--fcdnjmJ-5yaSpa-d.js";
|
|
3
|
+
import { Context } from "@warp-drive/core/reactive/-private";
|
|
4
|
+
import { pluralize, singularize } from "@warp-drive/utilities/string";
|
|
5
|
+
import { cached, tracked } from "@glimmer/tracking";
|
|
6
|
+
|
|
7
|
+
// src/model-fragments/utilities/with-array-defaults.ts
|
|
8
|
+
function withArrayDefaults(arrayName, primitiveType) {
|
|
9
|
+
return {
|
|
10
|
+
kind: "array",
|
|
11
|
+
name: arrayName,
|
|
12
|
+
type: primitiveType ? `array:${primitiveType}` : "array",
|
|
13
|
+
options: { arrayExtensions: [
|
|
14
|
+
"ember-object",
|
|
15
|
+
"ember-array-like",
|
|
16
|
+
"fragment-array"
|
|
17
|
+
] }
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/model-fragments/utilities/with-fragment-defaults.ts
|
|
22
|
+
/**
|
|
23
|
+
* Used as a helper to setup the relevant parts of a fragment schema
|
|
24
|
+
* and add extensions etc.
|
|
25
|
+
*
|
|
26
|
+
* @summary Legacy ModelFragments migration helper that builds a `schema-object` field for a single `fragment:`
|
|
27
|
+
* type with the `ember-object` and `fragment` extensions.
|
|
28
|
+
* @param fragmentType The type of the fragment
|
|
29
|
+
* @param fragmentName The optional name of the fragment. If not provided, it will default to the fragmentType.
|
|
30
|
+
* @returns The schema for a fragment
|
|
31
|
+
*/
|
|
32
|
+
function withFragmentDefaults(fragmentType, fragmentName) {
|
|
33
|
+
return {
|
|
34
|
+
kind: "schema-object",
|
|
35
|
+
type: `fragment:${fragmentType}`,
|
|
36
|
+
name: fragmentName ?? fragmentType,
|
|
37
|
+
options: { objectExtensions: ["ember-object", "fragment"] }
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/model-fragments/utilities/with-fragment-array-defaults.ts
|
|
42
|
+
/**
|
|
43
|
+
* Used as a helper to setup the relevant parts of a fragment-array
|
|
44
|
+
* schema and add extensions etc.
|
|
45
|
+
*
|
|
46
|
+
* @summary Legacy ModelFragments migration helper that builds a `schema-array` field of `fragment:` items,
|
|
47
|
+
* defaulting to an empty array, with Ember and fragment-array extensions.
|
|
48
|
+
* @param fragmentArrayType The type of the fragment-array
|
|
49
|
+
* @param fragmentArrayName The name of the fragment-array
|
|
50
|
+
* @returns The schema for a fragment-array
|
|
51
|
+
*/
|
|
52
|
+
function withFragmentArrayDefaults(fragmentArrayType, fragmentArrayName) {
|
|
53
|
+
return {
|
|
54
|
+
kind: "schema-array",
|
|
55
|
+
type: `fragment:${singularize(fragmentArrayType)}`,
|
|
56
|
+
name: fragmentArrayName ?? pluralize(fragmentArrayType),
|
|
57
|
+
options: {
|
|
58
|
+
arrayExtensions: [
|
|
59
|
+
"ember-object",
|
|
60
|
+
"ember-array-like",
|
|
61
|
+
"fragment-array"
|
|
62
|
+
],
|
|
63
|
+
defaultValue: true
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// src/model-fragments/utilities/with-legacy.ts
|
|
69
|
+
/**
|
|
70
|
+
* Used as a helper to setup the relevant parts of a legacy resource schema
|
|
71
|
+
* migrated from `Model`, applying the `ember-object` and `fragment` object
|
|
72
|
+
* extensions and a default `@id` identity field.
|
|
73
|
+
*
|
|
74
|
+
* @summary Legacy ModelFragments migration helper that completes a legacy resource schema with an `id` identity,
|
|
75
|
+
* `Model` defaults, and the `ember-object` and `fragment` extensions.
|
|
76
|
+
* @param schema the partial legacy resource schema to complete
|
|
77
|
+
* @returns the completed resource schema
|
|
78
|
+
*/
|
|
79
|
+
function withLegacy(schema) {
|
|
80
|
+
return withDefaults({
|
|
81
|
+
...schema,
|
|
82
|
+
identity: {
|
|
83
|
+
kind: "@id",
|
|
84
|
+
name: "id"
|
|
85
|
+
},
|
|
86
|
+
objectExtensions: ["ember-object", "fragment"]
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// src/model-fragments/extensions/fragment-array.ts
|
|
91
|
+
/**
|
|
92
|
+
* The features added to an array resource by {@link FragmentArrayExtension}, providing
|
|
93
|
+
* a subset of the legacy `ModelFragments` fragment-array API for migrated resources.
|
|
94
|
+
*
|
|
95
|
+
* @summary Legacy `ModelFragments`-compatible array API, such as `addFragment`, `removeFragment`, and
|
|
96
|
+
* `rollbackAttributes`, for reactive arrays migrated off fragments.
|
|
97
|
+
*/
|
|
98
|
+
var FragmentArray = class {
|
|
99
|
+
static {
|
|
100
|
+
decorateFieldV2(this.prototype, "isDestroying", [tracked], function() {
|
|
101
|
+
return false;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
#isDestroying = (initializeDeferredDecorator(this, "isDestroying"), void 0);
|
|
105
|
+
/**
|
|
106
|
+
* Whether this fragment array is in the process of being destroyed.
|
|
107
|
+
*/
|
|
108
|
+
static {
|
|
109
|
+
decorateFieldV2(this.prototype, "isDestroyed", [tracked], function() {
|
|
110
|
+
return false;
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
#isDestroyed = (initializeDeferredDecorator(this, "isDestroyed"), void 0);
|
|
114
|
+
/**
|
|
115
|
+
* Whether this fragment array has been destroyed.
|
|
116
|
+
*/
|
|
117
|
+
/**
|
|
118
|
+
* Whether this fragment array (or any of its members) has uncommitted changes.
|
|
119
|
+
*/
|
|
120
|
+
get hasDirtyAttributes() {
|
|
121
|
+
const { path, resourceKey, store } = this[Context];
|
|
122
|
+
const record = store.peekRecord(resourceKey);
|
|
123
|
+
if (record.hasDirtyAttributes && path) return path.at(0) in record.changedAttributes();
|
|
124
|
+
return false;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Adds an existing fragment to this array, if one was given.
|
|
128
|
+
*/
|
|
129
|
+
static {
|
|
130
|
+
decorateMethodV2(this.prototype, "hasDirtyAttributes", [cached]);
|
|
131
|
+
}
|
|
132
|
+
addFragment(fragment) {
|
|
133
|
+
if (!fragment) return;
|
|
134
|
+
return this.addObject(fragment);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Appends a new fragment to the end of this array, if one was given.
|
|
138
|
+
*/
|
|
139
|
+
createFragment(fragment) {
|
|
140
|
+
if (!fragment) return;
|
|
141
|
+
return this.pushObject(fragment);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Removes the given fragment from this array, if present.
|
|
145
|
+
*/
|
|
146
|
+
removeFragment(fragment) {
|
|
147
|
+
if (!fragment) return;
|
|
148
|
+
const index = this.indexOf(fragment);
|
|
149
|
+
if (index !== -1) this.splice(index, 1);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Reverts each member fragment's attribute back to its last known remote value.
|
|
153
|
+
*/
|
|
154
|
+
rollbackAttributes() {
|
|
155
|
+
for (const fragment of this) fragment?.rollbackAttributes?.();
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* A schema extension that adds the {@link FragmentArray} API to migrated
|
|
160
|
+
* `ModelFragments` array resources.
|
|
161
|
+
*
|
|
162
|
+
* @summary Legacy schema extension named `fragment-array` that adds the `FragmentArray` API to reactive arrays
|
|
163
|
+
* migrated from `ModelFragments`.
|
|
164
|
+
*/
|
|
165
|
+
const FragmentArrayExtension = {
|
|
166
|
+
kind: "array",
|
|
167
|
+
name: "fragment-array",
|
|
168
|
+
features: FragmentArray
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
// src/model-fragments/extensions/fragment.ts
|
|
172
|
+
/**
|
|
173
|
+
* The features added to an object resource by {@link FragmentExtension}, providing
|
|
174
|
+
* a subset of the legacy `ModelFragments` fragment API for migrated resources.
|
|
175
|
+
*
|
|
176
|
+
* @summary Legacy `ModelFragments`-compatible object API, such as `hasDirtyAttributes`, `isFragment`, and
|
|
177
|
+
* `rollbackAttributes`, for reactive objects migrated off fragments.
|
|
178
|
+
*/
|
|
179
|
+
var Fragment = class {
|
|
180
|
+
static {
|
|
181
|
+
decorateFieldV2(this.prototype, "isDestroying", [tracked], function() {
|
|
182
|
+
return false;
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
#isDestroying = (initializeDeferredDecorator(this, "isDestroying"), void 0);
|
|
186
|
+
/**
|
|
187
|
+
* Whether this fragment is in the process of being destroyed.
|
|
188
|
+
*/
|
|
189
|
+
static {
|
|
190
|
+
decorateFieldV2(this.prototype, "isDestroyed", [tracked], function() {
|
|
191
|
+
return false;
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
#isDestroyed = (initializeDeferredDecorator(this, "isDestroyed"), void 0);
|
|
195
|
+
/**
|
|
196
|
+
* Whether this fragment has been destroyed.
|
|
197
|
+
*/
|
|
198
|
+
/**
|
|
199
|
+
* Whether this fragment (or the attribute it is rooted at) has uncommitted changes.
|
|
200
|
+
*/
|
|
201
|
+
get hasDirtyAttributes() {
|
|
202
|
+
const { path, resourceKey, store } = this[Context];
|
|
203
|
+
const record = store.peekRecord(resourceKey);
|
|
204
|
+
if (record.hasDirtyAttributes && path) return path.at(0) in record.changedAttributes();
|
|
205
|
+
return false;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Always `true`. Used to distinguish fragments from other resources.
|
|
209
|
+
*/
|
|
210
|
+
static {
|
|
211
|
+
decorateMethodV2(this.prototype, "hasDirtyAttributes", [cached]);
|
|
212
|
+
}
|
|
213
|
+
get isFragment() {
|
|
214
|
+
return true;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* The resource type of this fragment, if known.
|
|
218
|
+
*/
|
|
219
|
+
get $type() {
|
|
220
|
+
const { field } = this[Context];
|
|
221
|
+
return field?.type;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Reverts this fragment's attribute back to its last known remote value.
|
|
225
|
+
*/
|
|
226
|
+
rollbackAttributes() {
|
|
227
|
+
const { path, resourceKey, store } = this[Context];
|
|
228
|
+
if (path) {
|
|
229
|
+
const oldValue = store.cache.getRemoteAttr(resourceKey, path);
|
|
230
|
+
store.cache.setAttr(resourceKey, path, oldValue);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
/**
|
|
235
|
+
* A schema extension that adds the {@link Fragment} API to migrated
|
|
236
|
+
* `ModelFragments` object resources.
|
|
237
|
+
*
|
|
238
|
+
* @summary Legacy schema extension named `fragment` that adds the `Fragment` API to reactive objects migrated from
|
|
239
|
+
* `ModelFragments`.
|
|
240
|
+
*/
|
|
241
|
+
const FragmentExtension = {
|
|
242
|
+
kind: "object",
|
|
243
|
+
name: "fragment",
|
|
244
|
+
features: Fragment
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
// src/model-fragments/instance-initializers/fragment-extensions.ts
|
|
248
|
+
/**
|
|
249
|
+
* Registers the {@link FragmentExtension}/{@link FragmentArrayExtension} schema
|
|
250
|
+
* extensions on the given `SchemaService`, enabling ModelFragments migration support.
|
|
251
|
+
*
|
|
252
|
+
* @summary Legacy setup function that registers the fragment and fragment-array schema extensions on a
|
|
253
|
+
* `SchemaService` to support migrating off ModelFragments.
|
|
254
|
+
* @public
|
|
255
|
+
*/
|
|
256
|
+
function registerFragmentExtensions(schema) {
|
|
257
|
+
schema.CAUTION_MEGA_DANGER_ZONE_registerExtension?.(FragmentExtension);
|
|
258
|
+
schema.CAUTION_MEGA_DANGER_ZONE_registerExtension?.(FragmentArrayExtension);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// src/model-fragments/hooks/model-for.ts
|
|
262
|
+
const AvailableShims = /* @__PURE__ */ new WeakMap();
|
|
263
|
+
function getShimClass(store, modelName) {
|
|
264
|
+
let shims = AvailableShims.get(store);
|
|
265
|
+
if (!shims) {
|
|
266
|
+
shims = Object.create(null);
|
|
267
|
+
AvailableShims.set(store, shims);
|
|
268
|
+
}
|
|
269
|
+
let shim = shims[modelName];
|
|
270
|
+
if (shim === void 0) shim = shims[modelName] = new ShimModelClass(store, modelName);
|
|
271
|
+
return shim;
|
|
272
|
+
}
|
|
273
|
+
const AttributeKinds = [
|
|
274
|
+
"field",
|
|
275
|
+
"attribute",
|
|
276
|
+
"object",
|
|
277
|
+
"array",
|
|
278
|
+
"schema-object",
|
|
279
|
+
"schema-array"
|
|
280
|
+
];
|
|
281
|
+
var ShimModelClass = class {
|
|
282
|
+
constructor(store, modelName) {
|
|
283
|
+
this.__store = store;
|
|
284
|
+
this.modelName = modelName;
|
|
285
|
+
}
|
|
286
|
+
get fields() {
|
|
287
|
+
const fields = /* @__PURE__ */ new Map();
|
|
288
|
+
this.__store.schema.fields({ type: this.modelName }).forEach((schema, key) => {
|
|
289
|
+
if (AttributeKinds.includes(schema.kind)) fields.set(key, "attribute");
|
|
290
|
+
else if (schema.kind === "belongsTo" || schema.kind === "hasMany") fields.set(key, schema.kind);
|
|
291
|
+
});
|
|
292
|
+
return fields;
|
|
293
|
+
}
|
|
294
|
+
get attributes() {
|
|
295
|
+
const attrs = /* @__PURE__ */ new Map();
|
|
296
|
+
this.__store.schema.fields({ type: this.modelName }).forEach((schema, key) => {
|
|
297
|
+
if (schema.kind === "attribute") attrs.set(key, schema);
|
|
298
|
+
else if (AttributeKinds.includes(schema.kind)) attrs.set(key, {
|
|
299
|
+
kind: "attribute",
|
|
300
|
+
name: key,
|
|
301
|
+
type: null,
|
|
302
|
+
options: schema.options ?? {}
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
return attrs;
|
|
306
|
+
}
|
|
307
|
+
get relationshipsByName() {
|
|
308
|
+
const rels = /* @__PURE__ */ new Map();
|
|
309
|
+
this.__store.schema.fields({ type: this.modelName }).forEach((schema, key) => {
|
|
310
|
+
if (schema.kind === "belongsTo" || schema.kind === "hasMany") rels.set(key, schema);
|
|
311
|
+
});
|
|
312
|
+
return rels;
|
|
313
|
+
}
|
|
314
|
+
eachAttribute(callback, binding) {
|
|
315
|
+
this.attributes.forEach((schema, key) => {
|
|
316
|
+
callback.call(binding, key, schema);
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
eachRelationship(callback, binding) {
|
|
320
|
+
this.__store.schema.fields({ type: this.modelName }).forEach((schema, key) => {
|
|
321
|
+
if (schema.kind === "belongsTo" || schema.kind === "hasMany") callback.call(binding, key, schema);
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
eachTransformedAttribute(callback, binding) {
|
|
325
|
+
this.__store.schema.fields({ type: this.modelName }).forEach((schema, key) => {
|
|
326
|
+
if (schema.kind === "attribute") {
|
|
327
|
+
const type = schema.type;
|
|
328
|
+
if (type) callback.call(binding, key, type);
|
|
329
|
+
}
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
function fragmentsModelFor(modelName) {
|
|
334
|
+
return getShimClass(this, modelName);
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* The `modelFor` fallback used to construct a `ShimModelClass` schema for a
|
|
338
|
+
* type when ModelFragments support is enabled and no real `Model` subclass
|
|
339
|
+
* is registered for that type.
|
|
340
|
+
*
|
|
341
|
+
* @summary Legacy `store.modelFor` fallback that returns a schema-backed `ShimModelClass` for a type when
|
|
342
|
+
* ModelFragments support is on and no `Model` class exists.
|
|
343
|
+
* @public
|
|
344
|
+
*/
|
|
345
|
+
const modelFor = fragmentsModelFor;
|
|
346
|
+
|
|
347
|
+
export { FragmentExtension as a, withLegacy as c, withArrayDefaults as d, Fragment as i, withFragmentArrayDefaults as l, modelFor as n, FragmentArray as o, registerFragmentExtensions as r, FragmentArrayExtension as s, fragmentsModelFor as t, withFragmentDefaults as u };
|
|
348
|
+
//# sourceMappingURL=model-fragments-bM9vucwt.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model-fragments-DCHMDr5f.js","names":["cached","tracked","Context","g","i","n","FragmentArray","prototype","isDestroying","isDestroyed","hasDirtyAttributes","array","context","path","resourceKey","store","record","peekRecord","root","at","changedAttributes","addFragment","fragment","addObject","createFragment","pushObject","removeFragment","index","indexOf","splice","rollbackAttributes","FragmentArrayExtension","kind","name","features","i","cached","tracked","Context","g","i","n","Fragment","prototype","isDestroying","isDestroyed","hasDirtyAttributes","path","resourceKey","store","record","peekRecord","root","at","changedAttributes","isFragment","$type","field","type","rollbackAttributes","oldValue","cache","getRemoteAttr","setAttr","FragmentExtension","kind","name","features","i","FragmentArrayExtension","FragmentExtension","registerFragmentExtensions","schema","CAUTION_MEGA_DANGER_ZONE_registerExtension","initialize","application","store","lookup","console","warn","name"],"sources":["../../../src/model-fragments/utilities/with-array-defaults.ts","../../../src/model-fragments/utilities/with-fragment-defaults.ts","../../../src/model-fragments/utilities/with-fragment-array-defaults.ts","../../../src/model-fragments/utilities/with-legacy.ts","../../../src/model-fragments/extensions/fragment-array.ts","../../../src/model-fragments/extensions/fragment.ts","../../../src/model-fragments/instance-initializers/fragment-extensions.ts","../../../src/model-fragments/hooks/model-for.ts"],"sourcesContent":["/**\n * Used as a helper to setup the relevant parts of an array\n * schema and add extensions etc.\n *\n * @summary Legacy ModelFragments migration helper that builds an `array` schema field preconfigured with the\n * `ember-object`, `ember-array-like`, and `fragment-array` extensions.\n * @param arrayName The name of the array\n * @param primitiveType The primitive type of items in the array (optional)\n * @returns The schema for an array\n */\nexport function withArrayDefaults<ArrayName extends string, PrimitiveType extends string>(\n arrayName: ArrayName,\n primitiveType?: PrimitiveType\n): PrimitiveType extends undefined\n ? {\n /**\n * This field is an `'array'` schema field.\n */\n kind: 'array';\n /**\n * The name of the array field.\n */\n name: ArrayName;\n /**\n * The array's item type, `'array'` since no `primitiveType` was given.\n */\n type: 'array';\n /**\n * The schema options for this array field.\n */\n options: {\n /**\n * The registered array-schema extensions to apply to this array.\n */\n arrayExtensions: string[];\n };\n }\n : {\n /**\n * This field is an `'array'` schema field.\n */\n kind: 'array';\n /**\n * The name of the array field.\n */\n name: ArrayName;\n /**\n * The array's item type, derived from `primitiveType`.\n */\n type: `array:${PrimitiveType}`;\n /**\n * The schema options for this array field.\n */\n options: {\n /**\n * The registered array-schema extensions to apply to this array.\n */\n arrayExtensions: string[];\n };\n };\n\nexport function withArrayDefaults<ArrayName extends string, PrimitiveType extends string>(\n arrayName: ArrayName,\n primitiveType?: PrimitiveType\n) {\n const type = primitiveType ? (`array:${primitiveType}` as const) : ('array' as const);\n return {\n kind: 'array' as const,\n name: arrayName,\n type,\n options: {\n arrayExtensions: ['ember-object', 'ember-array-like', 'fragment-array'],\n },\n };\n}\n","/**\n * Used as a helper to setup the relevant parts of a fragment schema\n * and add extensions etc.\n *\n * @summary Legacy ModelFragments migration helper that builds a `schema-object` field for a single `fragment:`\n * type with the `ember-object` and `fragment` extensions.\n * @param fragmentType The type of the fragment\n * @param fragmentName The optional name of the fragment. If not provided, it will default to the fragmentType.\n * @returns The schema for a fragment\n */\nexport function withFragmentDefaults<FragmentType extends string, FragmentName extends string>(\n fragmentType: FragmentType,\n fragmentName?: FragmentName\n): {\n /**\n * This field is a `'schema-object'` field, a single fragment.\n */\n kind: 'schema-object';\n /**\n * The resource type of the fragment.\n */\n type: `fragment:${FragmentType}`;\n /**\n * The name of the fragment field.\n */\n name: FragmentType | FragmentName;\n /**\n * The schema options for this fragment field.\n */\n options: {\n /**\n * The registered object-schema extensions to apply to this fragment.\n */\n objectExtensions: string[];\n };\n} {\n return {\n kind: 'schema-object' as const,\n type: `fragment:${fragmentType}` as const,\n name: fragmentName ?? fragmentType,\n options: {\n objectExtensions: ['ember-object', 'fragment'],\n },\n };\n}\n","import { pluralize, singularize } from '@warp-drive/utilities/string';\n\n/**\n * Used as a helper to setup the relevant parts of a fragment-array\n * schema and add extensions etc.\n *\n * @summary Legacy ModelFragments migration helper that builds a `schema-array` field of `fragment:` items,\n * defaulting to an empty array, with Ember and fragment-array extensions.\n * @param fragmentArrayType The type of the fragment-array\n * @param fragmentArrayName The name of the fragment-array\n * @returns The schema for a fragment-array\n */\nexport function withFragmentArrayDefaults<FragmentArrayType extends string, FragmentArrayName extends string>(\n fragmentArrayType: FragmentArrayType,\n fragmentArrayName?: FragmentArrayName\n): {\n /**\n * This field is a `'schema-array'` field, an array of fragments.\n */\n kind: 'schema-array';\n /**\n * The resource type of the fragments contained in this array.\n */\n type: `fragment:${string}`;\n /**\n * The name of the fragment-array field.\n */\n name: string;\n /**\n * The schema options for this fragment-array field.\n */\n options: {\n /**\n * The registered array-schema extensions to apply to this array.\n */\n arrayExtensions: string[];\n /**\n * Whether the array defaults to a non-null empty array when unset.\n */\n defaultValue: boolean;\n };\n} {\n return {\n kind: 'schema-array' as const,\n type: `fragment:${singularize(fragmentArrayType)}` as const,\n name: fragmentArrayName ?? pluralize(fragmentArrayType),\n options: {\n arrayExtensions: ['ember-object', 'ember-array-like', 'fragment-array'],\n defaultValue: true,\n },\n };\n}\n","import type { LegacyResourceSchema, ResourceSchema } from '@warp-drive/core/types/schema/fields';\nimport type { WithPartial } from '@warp-drive/core/types/utils';\n\nimport { withDefaults } from '../../model/migration-support';\n\n/**\n * Used as a helper to setup the relevant parts of a legacy resource schema\n * migrated from `Model`, applying the `ember-object` and `fragment` object\n * extensions and a default `@id` identity field.\n *\n * @summary Legacy ModelFragments migration helper that completes a legacy resource schema with an `id` identity,\n * `Model` defaults, and the `ember-object` and `fragment` extensions.\n * @param schema the partial legacy resource schema to complete\n * @returns the completed resource schema\n */\nexport function withLegacy(schema: WithPartial<LegacyResourceSchema, 'legacy' | 'identity'>): ResourceSchema {\n return withDefaults({\n ...schema,\n identity: { kind: '@id', name: 'id' },\n objectExtensions: ['ember-object', 'fragment'],\n });\n}\n","import { cached, tracked } from '@glimmer/tracking';\n\nimport type { CAUTION_MEGA_DANGER_ZONE_Extension } from '@warp-drive/core/reactive';\nimport { Context, type ManagedArray } from '@warp-drive/core/reactive/-private';\n\nimport type Model from '../../model.ts';\nimport type { WithFragmentArray } from '../index.ts';\nimport type { Fragment } from './fragment.ts';\n\n/**\n * The features added to an array resource by {@link FragmentArrayExtension}, providing\n * a subset of the legacy `ModelFragments` fragment-array API for migrated resources.\n *\n * @summary Legacy `ModelFragments`-compatible array API, such as `addFragment`, `removeFragment`, and\n * `rollbackAttributes`, for reactive arrays migrated off fragments.\n */\nexport class FragmentArray<T extends Fragment> {\n // We might want to check the parent values once we move this code to warp-drive.\n /**\n * Whether this fragment array is in the process of being destroyed.\n */\n @tracked isDestroying = false;\n /**\n * Whether this fragment array has been destroyed.\n */\n @tracked isDestroyed = false;\n\n /**\n * Whether this fragment array (or any of its members) has uncommitted changes.\n */\n @cached\n get hasDirtyAttributes(): boolean {\n const array = this as unknown as ManagedArray;\n const context = array[Context];\n const { path, resourceKey, store } = context;\n const record = store.peekRecord(resourceKey) as Model;\n\n if (record.hasDirtyAttributes && path) {\n const root = path.at(0) as string;\n return root in record.changedAttributes();\n }\n\n return false;\n }\n\n /**\n * Adds an existing fragment to this array, if one was given.\n */\n addFragment(fragment?: T): Fragment[] | undefined {\n if (!fragment) {\n return;\n }\n\n return (this as unknown as WithFragmentArray<T>).addObject(fragment);\n }\n\n /**\n * Appends a new fragment to the end of this array, if one was given.\n */\n createFragment(fragment?: T): Fragment | undefined {\n if (!fragment) {\n return;\n }\n\n return (this as unknown as WithFragmentArray<T>).pushObject(fragment);\n }\n\n /**\n * Removes the given fragment from this array, if present.\n */\n removeFragment(fragment?: T): void {\n if (!fragment) {\n return;\n }\n\n const index = (this as unknown as WithFragmentArray<T>).indexOf(fragment);\n\n if (index !== -1) {\n (this as unknown as WithFragmentArray<T>).splice(index, 1);\n }\n }\n\n /**\n * Reverts each member fragment's attribute back to its last known remote value.\n */\n rollbackAttributes(): void {\n for (const fragment of this as unknown as WithFragmentArray<T>) {\n // @ts-expect-error TODO: fix these types\n fragment?.rollbackAttributes?.();\n }\n }\n}\n\n/**\n * A schema extension that adds the {@link FragmentArray} API to migrated\n * `ModelFragments` array resources.\n *\n * @summary Legacy schema extension named `fragment-array` that adds the `FragmentArray` API to reactive arrays\n * migrated from `ModelFragments`.\n */\nexport const FragmentArrayExtension: {\n /**\n * This extension applies to `'array'` schemas.\n */\n kind: 'array';\n /**\n * The registered name of this extension.\n */\n name: 'fragment-array';\n /**\n * The features ({@link FragmentArray}) added by this extension.\n */\n features: typeof FragmentArray;\n} = {\n kind: 'array' as const,\n name: 'fragment-array' as const,\n features: FragmentArray,\n} satisfies CAUTION_MEGA_DANGER_ZONE_Extension;\n\nexport default FragmentArrayExtension;\n","import { cached, tracked } from '@glimmer/tracking';\n\nimport type { CAUTION_MEGA_DANGER_ZONE_Extension } from '@warp-drive/core/reactive';\nimport type { PrivateReactiveResource } from '@warp-drive/core/reactive/-private';\nimport { Context } from '@warp-drive/core/reactive/-private';\nimport type { Value } from '@warp-drive/core/types/json/raw';\n\nimport type Model from '../../model.ts';\n\n/**\n * The features added to an object resource by {@link FragmentExtension}, providing\n * a subset of the legacy `ModelFragments` fragment API for migrated resources.\n *\n * @summary Legacy `ModelFragments`-compatible object API, such as `hasDirtyAttributes`, `isFragment`, and\n * `rollbackAttributes`, for reactive objects migrated off fragments.\n */\nexport class Fragment {\n // We might want to check the parent values once we move this code to warp-drive.\n /**\n * Whether this fragment is in the process of being destroyed.\n */\n @tracked isDestroying = false;\n /**\n * Whether this fragment has been destroyed.\n */\n @tracked isDestroyed = false;\n\n /**\n * Whether this fragment (or the attribute it is rooted at) has uncommitted changes.\n */\n @cached\n get hasDirtyAttributes(): boolean {\n const { path, resourceKey, store } = (this as unknown as PrivateReactiveResource)[Context];\n const record = store.peekRecord(resourceKey) as Model;\n\n if (record.hasDirtyAttributes && path) {\n const root = path.at(0) as string;\n return root in record.changedAttributes();\n }\n\n return false;\n }\n\n /**\n * Always `true`. Used to distinguish fragments from other resources.\n */\n get isFragment() {\n return true;\n }\n\n /**\n * The resource type of this fragment, if known.\n */\n get $type(): string | null | undefined {\n const { field } = (this as unknown as PrivateReactiveResource)[Context];\n return field?.type;\n }\n\n /**\n * Reverts this fragment's attribute back to its last known remote value.\n */\n rollbackAttributes(this: PrivateReactiveResource): void {\n const { path, resourceKey, store } = this[Context];\n\n if (path) {\n const oldValue = store.cache.getRemoteAttr(resourceKey, path) as Value;\n store.cache.setAttr(resourceKey, path, oldValue);\n }\n }\n}\n\n/**\n * A schema extension that adds the {@link Fragment} API to migrated\n * `ModelFragments` object resources.\n *\n * @summary Legacy schema extension named `fragment` that adds the `Fragment` API to reactive objects migrated from\n * `ModelFragments`.\n */\nexport const FragmentExtension: {\n /**\n * This extension applies to `'object'` schemas.\n */\n kind: 'object';\n /**\n * The registered name of this extension.\n */\n name: 'fragment';\n /**\n * The features ({@link Fragment}) added by this extension.\n */\n features: typeof Fragment;\n} = {\n kind: 'object' as const,\n name: 'fragment' as const,\n features: Fragment,\n} satisfies CAUTION_MEGA_DANGER_ZONE_Extension;\n\nexport default FragmentExtension;\n","import type ApplicationInstance from '@ember/application/instance';\n\nimport type { Store } from '@warp-drive/core';\nimport type { SchemaService } from '@warp-drive/core/types';\n\nimport FragmentArrayExtension from '../extensions/fragment-array.ts';\nimport FragmentExtension from '../extensions/fragment.ts';\n\n/**\n * Registers the {@link FragmentExtension}/{@link FragmentArrayExtension} schema\n * extensions on the given `SchemaService`, enabling ModelFragments migration support.\n *\n * @summary Legacy setup function that registers the fragment and fragment-array schema extensions on a\n * `SchemaService` to support migrating off ModelFragments.\n * @public\n */\nexport function registerFragmentExtensions(schema: SchemaService): void {\n schema.CAUTION_MEGA_DANGER_ZONE_registerExtension?.(FragmentExtension);\n schema.CAUTION_MEGA_DANGER_ZONE_registerExtension?.(FragmentArrayExtension);\n}\n\nexport function initialize(application: ApplicationInstance): void {\n const store = application.lookup('service:store') as Store | undefined;\n\n if (store) {\n registerFragmentExtensions(store.schema);\n } else {\n // oxlint-disable-next-line no-console\n console.warn(\n 'No store service was found, you will need to call `registerFragmentExtensions` manually in your app.'\n );\n }\n}\n\nexport default {\n name: 'fragment-extensions',\n // the cast is required for isolatedDeclarations to determine this default\n // export's type without inference, even though it's a no-op for `tsc`'s checker\n // oxlint-disable-next-line typescript/no-unnecessary-type-assertion\n initialize: initialize as (application: ApplicationInstance) => void,\n};\n","import type { Store } from '@warp-drive/core';\nimport type { ModelSchema } from '@warp-drive/core/types';\nimport type { Value } from '@warp-drive/core/types/json/raw';\nimport type { TypedRecordInstance, TypeFromInstance } from '@warp-drive/core/types/record';\nimport type { LegacyAttributeField, LegacyRelationshipField } from '@warp-drive/core/types/schema/fields';\n\ntype KeyOrString<T> = keyof T & string extends never ? string : keyof T & string;\n\n// if modelFor turns out to be a bottleneck we should replace with a Map\n// and clear it during store teardown.\nconst AvailableShims = new WeakMap<Store, Record<string, ShimModelClass>>();\n\nexport function getShimClass<T>(\n store: Store,\n modelName: T extends TypedRecordInstance ? TypeFromInstance<T> : string\n): ShimModelClass<T> {\n let shims = AvailableShims.get(store);\n\n if (!shims) {\n shims = Object.create(null) as Record<string, ShimModelClass>;\n AvailableShims.set(store, shims);\n }\n\n let shim = shims[modelName];\n if (shim === undefined) {\n shim = shims[modelName] = new ShimModelClass<unknown>(store, modelName);\n }\n\n return shim;\n}\n\nconst AttributeKinds = ['field', 'attribute', 'object', 'array', 'schema-object', 'schema-array'] as const;\n\n// Mimics the static apis of @warp-drive/legacy/model\nexport class ShimModelClass<T = unknown> implements ModelSchema<T> {\n declare __store: Store;\n declare modelName: T extends TypedRecordInstance ? TypeFromInstance<T> : string;\n constructor(store: Store, modelName: T extends TypedRecordInstance ? TypeFromInstance<T> : string) {\n this.__store = store;\n this.modelName = modelName;\n }\n\n get fields(): Map<KeyOrString<T>, 'attribute' | 'belongsTo' | 'hasMany'> {\n const fields = new Map<KeyOrString<T>, 'attribute' | 'belongsTo' | 'hasMany'>();\n const fieldSchemas = this.__store.schema.fields({ type: this.modelName });\n\n fieldSchemas.forEach((schema, key) => {\n // @ts-expect-error checking if a string is a valid string\n if (AttributeKinds.includes(schema.kind)) {\n fields.set(key as KeyOrString<T>, 'attribute');\n } else if (schema.kind === 'belongsTo' || schema.kind === 'hasMany') {\n fields.set(key as KeyOrString<T>, schema.kind);\n }\n });\n\n return fields;\n }\n\n get attributes(): Map<KeyOrString<T>, LegacyAttributeField> {\n const attrs = new Map<KeyOrString<T>, LegacyAttributeField>();\n const fields = this.__store.schema.fields({ type: this.modelName });\n\n fields.forEach((schema, key) => {\n if (schema.kind === 'attribute') {\n attrs.set(key as KeyOrString<T>, schema);\n // @ts-expect-error checking if a string is a valid string\n } else if (AttributeKinds.includes(schema.kind)) {\n attrs.set(key as KeyOrString<T>, {\n kind: 'attribute',\n name: key,\n type: null,\n options: (schema.options ?? {}) as Record<string, Value>,\n });\n }\n });\n\n return attrs;\n }\n\n get relationshipsByName(): Map<KeyOrString<T>, LegacyRelationshipField> {\n const rels = new Map<KeyOrString<T>, LegacyRelationshipField>();\n const fields = this.__store.schema.fields({ type: this.modelName });\n\n fields.forEach((schema, key) => {\n if (schema.kind === 'belongsTo' || schema.kind === 'hasMany') {\n rels.set(key as KeyOrString<T>, schema);\n }\n });\n\n return rels;\n }\n\n eachAttribute<K extends KeyOrString<T>>(\n callback: (key: K, attribute: LegacyAttributeField) => void,\n binding?: T\n ): void {\n this.attributes.forEach((schema, key) => {\n callback.call(binding, key as K, schema);\n });\n }\n\n eachRelationship<K extends KeyOrString<T>>(\n callback: (key: K, relationship: LegacyRelationshipField) => void,\n binding?: T\n ): void {\n this.__store.schema.fields({ type: this.modelName }).forEach((schema, key) => {\n if (schema.kind === 'belongsTo' || schema.kind === 'hasMany') {\n callback.call(binding, key as K, schema);\n }\n });\n }\n\n eachTransformedAttribute<K extends KeyOrString<T>>(\n callback: (key: K, type: string | null) => void,\n binding?: T\n ): void {\n this.__store.schema.fields({ type: this.modelName }).forEach((schema, key) => {\n if (schema.kind === 'attribute') {\n const type = schema.type;\n if (type) callback.call(binding, key as K, type);\n }\n });\n }\n}\n\nexport function fragmentsModelFor<T extends TypedRecordInstance>(\n this: Store,\n modelName: T extends TypedRecordInstance ? TypeFromInstance<T> : string\n): ShimModelClass<T> {\n return getShimClass(this, modelName);\n}\n\n/**\n * The `modelFor` fallback used to construct a `ShimModelClass` schema for a\n * type when ModelFragments support is enabled and no real `Model` subclass\n * is registered for that type.\n *\n * @summary Legacy `store.modelFor` fallback that returns a schema-backed `ShimModelClass` for a type when\n * ModelFragments support is on and no `Model` class exists.\n * @public\n */\nexport const modelFor: typeof fragmentsModelFor = fragmentsModelFor;\n"],"mappings":";;;;;;;AA6DA,SAAgB,kBACd,WACA,eACA;CAEA,OAAO;EACL,MAAM;EACN,MAAM;EACN,MAJW,gBAAiB,SAAS,kBAA6B;EAKlE,SAAS,EACP,iBAAiB;GAAC;GAAgB;GAAoB;EAAgB,EACxE;CACF;AACF;;;;;;;;;;;;;AChEA,SAAgB,qBACd,cACA,cAuBA;CACA,OAAO;EACL,MAAM;EACN,MAAM,YAAY;EAClB,MAAM,gBAAgB;EACtB,SAAS,EACP,kBAAkB,CAAC,gBAAgB,UAAU,EAC/C;CACF;AACF;;;;;;;;;;;;;AChCA,SAAgB,0B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IACN,MAAM;IACN,SAAU,OAAO,WAAW,CAAC;GAC/B,CAAC;EAEL,CAAC;EAED,OAAO;CACT;CAEA,IAAI,sBAAoE;EACtE,MAAM,uBAAO,IAAI,IAA6C;EAG9D,AAFe,KAAK,QAAQ,OAAO,OAAO,EAAE,MAAM,KAAK,UAAU,CAE5D,CAAC,CAAC,SAAS,QAAQ,QAAQ;GAC9B,IAAI,OAAO,SAAS,eAAe,OAAO,SAAS,WACjD,KAAK,IAAI,KAAuB,MAAM;EAE1C,CAAC;EAED,OAAO;CACT;CAEA,cACE,UACA,SACM;EACN,KAAK,WAAW,SAAS,QAAQ,QAAQ;GACvC,SAAS,KAAK,SAAS,KAAU,MAAM;EACzC,CAAC;CACH;CAEA,iBACE,UACA,SACM;EACN,KAAK,QAAQ,OAAO,OAAO,EAAE,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,SAAS,QAAQ,QAAQ;GAC5E,IAAI,OAAO,SAAS,eAAe,OAAO,SAAS,WACjD,SAAS,KAAK,SAAS,KAAU,MAAM;EAE3C,CAAC;CACH;CAEA,yBACE,UACA,SACM;EACN,KAAK,QAAQ,OAAO,OAAO,EAAE,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,SAAS,QAAQ,QAAQ;GAC5E,IAAI,OAAO,SAAS,aAAa;IAC/B,MAAM,OAAO,OAAO;IACpB,IAAI,MAAM,SAAS,KAAK,SAAS,KAAU,IAAI;GACjD;EACF,CAAC;CACH;AACF;AAEA,SAAgB,kBAEd,WACmB;CACnB,OAAO,aAAa,MAAM,SAAS;AACrC;;;;;;;;;;AAWA,MAAa,WAAqC"}
|
|
1
|
+
{"version":3,"file":"model-fragments-bM9vucwt.js","names":["cached","tracked","Context","g","i","n","FragmentArray","prototype","isDestroying","isDestroyed","hasDirtyAttributes","array","context","path","resourceKey","store","record","peekRecord","root","at","changedAttributes","addFragment","fragment","addObject","createFragment","pushObject","removeFragment","index","indexOf","splice","rollbackAttributes","FragmentArrayExtension","kind","name","features","i","cached","tracked","Context","g","i","n","Fragment","prototype","isDestroying","isDestroyed","hasDirtyAttributes","path","resourceKey","store","record","peekRecord","root","at","changedAttributes","isFragment","$type","field","type","rollbackAttributes","oldValue","cache","getRemoteAttr","setAttr","FragmentExtension","kind","name","features","i","FragmentArrayExtension","FragmentExtension","registerFragmentExtensions","schema","CAUTION_MEGA_DANGER_ZONE_registerExtension","initialize","application","store","lookup","console","warn","name"],"sources":["../../../src/model-fragments/utilities/with-array-defaults.ts","../../../src/model-fragments/utilities/with-fragment-defaults.ts","../../../src/model-fragments/utilities/with-fragment-array-defaults.ts","../../../src/model-fragments/utilities/with-legacy.ts","../../../src/model-fragments/extensions/fragment-array.ts","../../../src/model-fragments/extensions/fragment.ts","../../../src/model-fragments/instance-initializers/fragment-extensions.ts","../../../src/model-fragments/hooks/model-for.ts"],"sourcesContent":["/**\n * Used as a helper to setup the relevant parts of an array\n * schema and add extensions etc.\n *\n * @summary Legacy ModelFragments migration helper that builds an `array` schema field preconfigured with the\n * `ember-object`, `ember-array-like`, and `fragment-array` extensions.\n * @param arrayName The name of the array\n * @param primitiveType The primitive type of items in the array (optional)\n * @returns The schema for an array\n */\nexport function withArrayDefaults<ArrayName extends string, PrimitiveType extends string>(\n arrayName: ArrayName,\n primitiveType?: PrimitiveType\n): PrimitiveType extends undefined\n ? {\n /**\n * This field is an `'array'` schema field.\n */\n kind: 'array';\n /**\n * The name of the array field.\n */\n name: ArrayName;\n /**\n * The array's item type, `'array'` since no `primitiveType` was given.\n */\n type: 'array';\n /**\n * The schema options for this array field.\n */\n options: {\n /**\n * The registered array-schema extensions to apply to this array.\n */\n arrayExtensions: string[];\n };\n }\n : {\n /**\n * This field is an `'array'` schema field.\n */\n kind: 'array';\n /**\n * The name of the array field.\n */\n name: ArrayName;\n /**\n * The array's item type, derived from `primitiveType`.\n */\n type: `array:${PrimitiveType}`;\n /**\n * The schema options for this array field.\n */\n options: {\n /**\n * The registered array-schema extensions to apply to this array.\n */\n arrayExtensions: string[];\n };\n };\n\nexport function withArrayDefaults<ArrayName extends string, PrimitiveType extends string>(\n arrayName: ArrayName,\n primitiveType?: PrimitiveType\n) {\n const type = primitiveType ? (`array:${primitiveType}` as const) : ('array' as const);\n return {\n kind: 'array' as const,\n name: arrayName,\n type,\n options: {\n arrayExtensions: ['ember-object', 'ember-array-like', 'fragment-array'],\n },\n };\n}\n","/**\n * Used as a helper to setup the relevant parts of a fragment schema\n * and add extensions etc.\n *\n * @summary Legacy ModelFragments migration helper that builds a `schema-object` field for a single `fragment:`\n * type with the `ember-object` and `fragment` extensions.\n * @param fragmentType The type of the fragment\n * @param fragmentName The optional name of the fragment. If not provided, it will default to the fragmentType.\n * @returns The schema for a fragment\n */\nexport function withFragmentDefaults<FragmentType extends string, FragmentName extends string>(\n fragmentType: FragmentType,\n fragmentName?: FragmentName\n): {\n /**\n * This field is a `'schema-object'` field, a single fragment.\n */\n kind: 'schema-object';\n /**\n * The resource type of the fragment.\n */\n type: `fragment:${FragmentType}`;\n /**\n * The name of the fragment field.\n */\n name: FragmentType | FragmentName;\n /**\n * The schema options for this fragment field.\n */\n options: {\n /**\n * The registered object-schema extensions to apply to this fragment.\n */\n objectExtensions: string[];\n };\n} {\n return {\n kind: 'schema-object' as const,\n type: `fragment:${fragmentType}` as const,\n name: fragmentName ?? fragmentType,\n options: {\n objectExtensions: ['ember-object', 'fragment'],\n },\n };\n}\n","import { pluralize, singularize } from '@warp-drive/utilities/string';\n\n/**\n * Used as a helper to setup the relevant parts of a fragment-array\n * schema and add extensions etc.\n *\n * @summary Legacy ModelFragments migration helper that builds a `schema-array` field of `fragment:` items,\n * defaulting to an empty array, with Ember and fragment-array extensions.\n * @param fragmentArrayType The type of the fragment-array\n * @param fragmentArrayName The name of the fragment-array\n * @returns The schema for a fragment-array\n */\nexport function withFragmentArrayDefaults<FragmentArrayType extends string, FragmentArrayName extends string>(\n fragmentArrayType: FragmentArrayType,\n fragmentArrayName?: FragmentArrayName\n): {\n /**\n * This field is a `'schema-array'` field, an array of fragments.\n */\n kind: 'schema-array';\n /**\n * The resource type of the fragments contained in this array.\n */\n type: `fragment:${string}`;\n /**\n * The name of the fragment-array field.\n */\n name: string;\n /**\n * The schema options for this fragment-array field.\n */\n options: {\n /**\n * The registered array-schema extensions to apply to this array.\n */\n arrayExtensions: string[];\n /**\n * Whether the array defaults to a non-null empty array when unset.\n */\n defaultValue: boolean;\n };\n} {\n return {\n kind: 'schema-array' as const,\n type: `fragment:${singularize(fragmentArrayType)}` as const,\n name: fragmentArrayName ?? pluralize(fragmentArrayType),\n options: {\n arrayExtensions: ['ember-object', 'ember-array-like', 'fragment-array'],\n defaultValue: true,\n },\n };\n}\n","import type { LegacyResourceSchema, ResourceSchema } from '@warp-drive/core/types/schema/fields';\nimport type { WithPartial } from '@warp-drive/core/types/utils';\n\nimport { withDefaults } from '../../model/migration-support';\n\n/**\n * Used as a helper to setup the relevant parts of a legacy resource schema\n * migrated from `Model`, applying the `ember-object` and `fragment` object\n * extensions and a default `@id` identity field.\n *\n * @summary Legacy ModelFragments migration helper that completes a legacy resource schema with an `id` identity,\n * `Model` defaults, and the `ember-object` and `fragment` extensions.\n * @param schema the partial legacy resource schema to complete\n * @returns the completed resource schema\n */\nexport function withLegacy(schema: WithPartial<LegacyResourceSchema, 'legacy' | 'identity'>): ResourceSchema {\n return withDefaults({\n ...schema,\n identity: { kind: '@id', name: 'id' },\n objectExtensions: ['ember-object', 'fragment'],\n });\n}\n","import { cached, tracked } from '@glimmer/tracking';\n\nimport type { CAUTION_MEGA_DANGER_ZONE_Extension } from '@warp-drive/core/reactive';\nimport { Context, type ManagedArray } from '@warp-drive/core/reactive/-private';\n\nimport type Model from '../../model.ts';\nimport type { WithFragmentArray } from '../index.ts';\nimport type { Fragment } from './fragment.ts';\n\n/**\n * The features added to an array resource by {@link FragmentArrayExtension}, providing\n * a subset of the legacy `ModelFragments` fragment-array API for migrated resources.\n *\n * @summary Legacy `ModelFragments`-compatible array API, such as `addFragment`, `removeFragment`, and\n * `rollbackAttributes`, for reactive arrays migrated off fragments.\n */\nexport class FragmentArray<T extends Fragment> {\n // We might want to check the parent values once we move this code to warp-drive.\n /**\n * Whether this fragment array is in the process of being destroyed.\n */\n @tracked isDestroying = false;\n /**\n * Whether this fragment array has been destroyed.\n */\n @tracked isDestroyed = false;\n\n /**\n * Whether this fragment array (or any of its members) has uncommitted changes.\n */\n @cached\n get hasDirtyAttributes(): boolean {\n const array = this as unknown as ManagedArray;\n const context = array[Context];\n const { path, resourceKey, store } = context;\n const record = store.peekRecord(resourceKey) as Model;\n\n if (record.hasDirtyAttributes && path) {\n const root = path.at(0) as string;\n return root in record.changedAttributes();\n }\n\n return false;\n }\n\n /**\n * Adds an existing fragment to this array, if one was given.\n */\n addFragment(fragment?: T): Fragment[] | undefined {\n if (!fragment) {\n return;\n }\n\n return (this as unknown as WithFragmentArray<T>).addObject(fragment);\n }\n\n /**\n * Appends a new fragment to the end of this array, if one was given.\n */\n createFragment(fragment?: T): Fragment | undefined {\n if (!fragment) {\n return;\n }\n\n return (this as unknown as WithFragmentArray<T>).pushObject(fragment);\n }\n\n /**\n * Removes the given fragment from this array, if present.\n */\n removeFragment(fragment?: T): void {\n if (!fragment) {\n return;\n }\n\n const index = (this as unknown as WithFragmentArray<T>).indexOf(fragment);\n\n if (index !== -1) {\n (this as unknown as WithFragmentArray<T>).splice(index, 1);\n }\n }\n\n /**\n * Reverts each member fragment's attribute back to its last known remote value.\n */\n rollbackAttributes(): void {\n for (const fragment of this as unknown as WithFragmentArray<T>) {\n // @ts-expect-error TODO: fix these types\n fragment?.rollbackAttributes?.();\n }\n }\n}\n\n/**\n * A schema extension that adds the {@link FragmentArray} API to migrated\n * `ModelFragments` array resources.\n *\n * @summary Legacy schema extension named `fragment-array` that adds the `FragmentArray` API to reactive arrays\n * migrated from `ModelFragments`.\n */\nexport const FragmentArrayExtension: {\n /**\n * This extension applies to `'array'` schemas.\n */\n kind: 'array';\n /**\n * The registered name of this extension.\n */\n name: 'fragment-array';\n /**\n * The features ({@link FragmentArray}) added by this extension.\n */\n features: typeof FragmentArray;\n} = {\n kind: 'array' as const,\n name: 'fragment-array' as const,\n features: FragmentArray,\n} satisfies CAUTION_MEGA_DANGER_ZONE_Extension;\n\nexport default FragmentArrayExtension;\n","import { cached, tracked } from '@glimmer/tracking';\n\nimport type { CAUTION_MEGA_DANGER_ZONE_Extension } from '@warp-drive/core/reactive';\nimport type { PrivateReactiveResource } from '@warp-drive/core/reactive/-private';\nimport { Context } from '@warp-drive/core/reactive/-private';\nimport type { Value } from '@warp-drive/core/types/json/raw';\n\nimport type Model from '../../model.ts';\n\n/**\n * The features added to an object resource by {@link FragmentExtension}, providing\n * a subset of the legacy `ModelFragments` fragment API for migrated resources.\n *\n * @summary Legacy `ModelFragments`-compatible object API, such as `hasDirtyAttributes`, `isFragment`, and\n * `rollbackAttributes`, for reactive objects migrated off fragments.\n */\nexport class Fragment {\n // We might want to check the parent values once we move this code to warp-drive.\n /**\n * Whether this fragment is in the process of being destroyed.\n */\n @tracked isDestroying = false;\n /**\n * Whether this fragment has been destroyed.\n */\n @tracked isDestroyed = false;\n\n /**\n * Whether this fragment (or the attribute it is rooted at) has uncommitted changes.\n */\n @cached\n get hasDirtyAttributes(): boolean {\n const { path, resourceKey, store } = (this as unknown as PrivateReactiveResource)[Context];\n const record = store.peekRecord(resourceKey) as Model;\n\n if (record.hasDirtyAttributes && path) {\n const root = path.at(0) as string;\n return root in record.changedAttributes();\n }\n\n return false;\n }\n\n /**\n * Always `true`. Used to distinguish fragments from other resources.\n */\n get isFragment() {\n return true;\n }\n\n /**\n * The resource type of this fragment, if known.\n */\n get $type(): string | null | undefined {\n const { field } = (this as unknown as PrivateReactiveResource)[Context];\n return field?.type;\n }\n\n /**\n * Reverts this fragment's attribute back to its last known remote value.\n */\n rollbackAttributes(this: PrivateReactiveResource): void {\n const { path, resourceKey, store } = this[Context];\n\n if (path) {\n const oldValue = store.cache.getRemoteAttr(resourceKey, path) as Value;\n store.cache.setAttr(resourceKey, path, oldValue);\n }\n }\n}\n\n/**\n * A schema extension that adds the {@link Fragment} API to migrated\n * `ModelFragments` object resources.\n *\n * @summary Legacy schema extension named `fragment` that adds the `Fragment` API to reactive objects migrated from\n * `ModelFragments`.\n */\nexport const FragmentExtension: {\n /**\n * This extension applies to `'object'` schemas.\n */\n kind: 'object';\n /**\n * The registered name of this extension.\n */\n name: 'fragment';\n /**\n * The features ({@link Fragment}) added by this extension.\n */\n features: typeof Fragment;\n} = {\n kind: 'object' as const,\n name: 'fragment' as const,\n features: Fragment,\n} satisfies CAUTION_MEGA_DANGER_ZONE_Extension;\n\nexport default FragmentExtension;\n","import type ApplicationInstance from '@ember/application/instance';\n\nimport type { Store } from '@warp-drive/core';\nimport type { SchemaService } from '@warp-drive/core/types';\n\nimport FragmentArrayExtension from '../extensions/fragment-array.ts';\nimport FragmentExtension from '../extensions/fragment.ts';\n\n/**\n * Registers the {@link FragmentExtension}/{@link FragmentArrayExtension} schema\n * extensions on the given `SchemaService`, enabling ModelFragments migration support.\n *\n * @summary Legacy setup function that registers the fragment and fragment-array schema extensions on a\n * `SchemaService` to support migrating off ModelFragments.\n * @public\n */\nexport function registerFragmentExtensions(schema: SchemaService): void {\n schema.CAUTION_MEGA_DANGER_ZONE_registerExtension?.(FragmentExtension);\n schema.CAUTION_MEGA_DANGER_ZONE_registerExtension?.(FragmentArrayExtension);\n}\n\nexport function initialize(application: ApplicationInstance): void {\n const store = application.lookup('service:store') as Store | undefined;\n\n if (store) {\n registerFragmentExtensions(store.schema);\n } else {\n // oxlint-disable-next-line no-console\n console.warn(\n 'No store service was found, you will need to call `registerFragmentExtensions` manually in your app.'\n );\n }\n}\n\nexport default {\n name: 'fragment-extensions',\n // the cast is required for isolatedDeclarations to determine this default\n // export's type without inference, even though it's a no-op for `tsc`'s checker\n // oxlint-disable-next-line typescript/no-unnecessary-type-assertion\n initialize: initialize as (application: ApplicationInstance) => void,\n};\n","import type { Store } from '@warp-drive/core';\nimport type { ModelSchema } from '@warp-drive/core/types';\nimport type { Value } from '@warp-drive/core/types/json/raw';\nimport type { TypedRecordInstance, TypeFromInstance } from '@warp-drive/core/types/record';\nimport type { LegacyAttributeField, LegacyRelationshipField } from '@warp-drive/core/types/schema/fields';\n\ntype KeyOrString<T> = keyof T & string extends never ? string : keyof T & string;\n\n// if modelFor turns out to be a bottleneck we should replace with a Map\n// and clear it during store teardown.\nconst AvailableShims = new WeakMap<Store, Record<string, ShimModelClass>>();\n\nexport function getShimClass<T>(\n store: Store,\n modelName: T extends TypedRecordInstance ? TypeFromInstance<T> : string\n): ShimModelClass<T> {\n let shims = AvailableShims.get(store);\n\n if (!shims) {\n shims = Object.create(null) as Record<string, ShimModelClass>;\n AvailableShims.set(store, shims);\n }\n\n let shim = shims[modelName];\n if (shim === undefined) {\n shim = shims[modelName] = new ShimModelClass<unknown>(store, modelName);\n }\n\n return shim;\n}\n\nconst AttributeKinds = ['field', 'attribute', 'object', 'array', 'schema-object', 'schema-array'] as const;\n\n// Mimics the static apis of @warp-drive/legacy/model\nexport class ShimModelClass<T = unknown> implements ModelSchema<T> {\n declare __store: Store;\n declare modelName: T extends TypedRecordInstance ? TypeFromInstance<T> : string;\n constructor(store: Store, modelName: T extends TypedRecordInstance ? TypeFromInstance<T> : string) {\n this.__store = store;\n this.modelName = modelName;\n }\n\n get fields(): Map<KeyOrString<T>, 'attribute' | 'belongsTo' | 'hasMany'> {\n const fields = new Map<KeyOrString<T>, 'attribute' | 'belongsTo' | 'hasMany'>();\n const fieldSchemas = this.__store.schema.fields({ type: this.modelName });\n\n fieldSchemas.forEach((schema, key) => {\n // @ts-expect-error checking if a string is a valid string\n if (AttributeKinds.includes(schema.kind)) {\n fields.set(key as KeyOrString<T>, 'attribute');\n } else if (schema.kind === 'belongsTo' || schema.kind === 'hasMany') {\n fields.set(key as KeyOrString<T>, schema.kind);\n }\n });\n\n return fields;\n }\n\n get attributes(): Map<KeyOrString<T>, LegacyAttributeField> {\n const attrs = new Map<KeyOrString<T>, LegacyAttributeField>();\n const fields = this.__store.schema.fields({ type: this.modelName });\n\n fields.forEach((schema, key) => {\n if (schema.kind === 'attribute') {\n attrs.set(key as KeyOrString<T>, schema);\n // @ts-expect-error checking if a string is a valid string\n } else if (AttributeKinds.includes(schema.kind)) {\n attrs.set(key as KeyOrString<T>, {\n kind: 'attribute',\n name: key,\n type: null,\n options: (schema.options ?? {}) as Record<string, Value>,\n });\n }\n });\n\n return attrs;\n }\n\n get relationshipsByName(): Map<KeyOrString<T>, LegacyRelationshipField> {\n const rels = new Map<KeyOrString<T>, LegacyRelationshipField>();\n const fields = this.__store.schema.fields({ type: this.modelName });\n\n fields.forEach((schema, key) => {\n if (schema.kind === 'belongsTo' || schema.kind === 'hasMany') {\n rels.set(key as KeyOrString<T>, schema);\n }\n });\n\n return rels;\n }\n\n eachAttribute<K extends KeyOrString<T>>(\n callback: (key: K, attribute: LegacyAttributeField) => void,\n binding?: T\n ): void {\n this.attributes.forEach((schema, key) => {\n callback.call(binding, key as K, schema);\n });\n }\n\n eachRelationship<K extends KeyOrString<T>>(\n callback: (key: K, relationship: LegacyRelationshipField) => void,\n binding?: T\n ): void {\n this.__store.schema.fields({ type: this.modelName }).forEach((schema, key) => {\n if (schema.kind === 'belongsTo' || schema.kind === 'hasMany') {\n callback.call(binding, key as K, schema);\n }\n });\n }\n\n eachTransformedAttribute<K extends KeyOrString<T>>(\n callback: (key: K, type: string | null) => void,\n binding?: T\n ): void {\n this.__store.schema.fields({ type: this.modelName }).forEach((schema, key) => {\n if (schema.kind === 'attribute') {\n const type = schema.type;\n if (type) callback.call(binding, key as K, type);\n }\n });\n }\n}\n\nexport function fragmentsModelFor<T extends TypedRecordInstance>(\n this: Store,\n modelName: T extends TypedRecordInstance ? TypeFromInstance<T> : string\n): ShimModelClass<T> {\n return getShimClass(this, modelName);\n}\n\n/**\n * The `modelFor` fallback used to construct a `ShimModelClass` schema for a\n * type when ModelFragments support is enabled and no real `Model` subclass\n * is registered for that type.\n *\n * @summary Legacy `store.modelFor` fallback that returns a schema-backed `ShimModelClass` for a type when\n * ModelFragments support is on and no `Model` class exists.\n * @public\n */\nexport const modelFor: typeof fragmentsModelFor = fragmentsModelFor;\n"],"mappings":";;;;;;;AA6DA,SAAgB,kBACd,WACA,eACA;CAEA,OAAO;EACL,MAAM;EACN,MAAM;EACN,MAJW,gBAAiB,SAAS,kBAA6B;EAKlE,SAAS,EACP,iBAAiB;GAAC;GAAgB;GAAoB;EAAgB,EACxE;CACF;AACF;;;;;;;;;;;;;AChEA,SAAgB,qBACd,cACA,cAuBA;CACA,OAAO;EACL,MAAM;EACN,MAAM,YAAY;EAClB,MAAM,gBAAgB;EACtB,SAAS,EACP,kBAAkB,CAAC,gBAAgB,UAAU,EAC/C;CACF;AACF;;;;;;;;;;;;;AChCA,SAAgB,0BACd,mBACA,mBA2BA;CACA,OAAO;EACL,MAAM;EACN,MAAM,YAAY,YAAY,iBAAiB;EAC/C,MAAM,qBAAqB,UAAU,iBAAiB;EACtD,SAAS;GACP,iBAAiB;IAAC;IAAgB;IAAoB;GAAgB;GACtE,cAAc;EAChB;CACF;AACF;;;;;;;;;;;;;ACpCA,SAAgB,WAAW,QAAkF;CAC3G,OAAO,aAAa;EAClB,GAAG;EACH,UAAU;GAAE,MAAM;GAAO,MAAM;EAAK;EACpC,kBAAkB,CAAC,gBAAgB,UAAU;CAC/C,CAAC;AACH;;;;;;;;;;ACLA,IAAaM,gBAAb,MAA+C;CAAA;EAAAH,gBAAA,KAAAI,WAAA,gBAAA,CAK5CN,OAAO,GAAA,WAAA;GAAA,OAAgB;EAAK,CAAA;CAAA;CAAA,iBAAAG,4BAAA,MAAA,cAAA,GAAA,KAAA;;;;CAH7B;EAAAD,gBAAA,KAAAI,WAAA,eAAA,CAOCN,OAAO,GAAA,WAAA;GAAA,OAAe;EAAK,CAAA;CAAA;CAAA,gBAAAG,4BAAA,MAAA,aAAA,GAAA,KAAA;;;;;;;CAK5B,IACIM,qBAA8B;EAGhC,MAA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M,KAAK,UAAU,CAAC,CAAC,CAAC,SAAS,QAAQ,QAAQ;GAC5E,IAAI,OAAO,SAAS,aAAa;IAC/B,MAAM,OAAO,OAAO;IACpB,IAAI,MAAM,SAAS,KAAK,SAAS,KAAU,IAAI;GACjD;EACF,CAAC;CACH;AACF;AAEA,SAAgB,kBAEd,WACmB;CACnB,OAAO,aAAa,MAAM,SAAS;AACrC;;;;;;;;;;AAWA,MAAa,WAAqC"}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { a as FragmentExtension, c as withLegacy, d as withArrayDefaults, i as Fragment, l as withFragmentArrayDefaults, n as modelFor, o as FragmentArray, r as registerFragmentExtensions, s as FragmentArrayExtension, u as withFragmentDefaults } from "./model-fragments-
|
|
1
|
+
import { a as FragmentExtension, c as withLegacy, d as withArrayDefaults, i as Fragment, l as withFragmentArrayDefaults, n as modelFor, o as FragmentArray, r as registerFragmentExtensions, s as FragmentArrayExtension, u as withFragmentDefaults } from "./model-fragments-bM9vucwt.js";
|
|
2
2
|
|
|
3
3
|
export { Fragment, FragmentArray, FragmentArrayExtension, FragmentExtension, modelFor, registerFragmentExtensions, withArrayDefaults, withFragmentArrayDefaults, withFragmentDefaults, withLegacy };
|
package/dist/unpkg/prod/model.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as belongsTo, i as hasMany, n as modelFor, o as attr, r as teardownRecord, t as instantiateRecord } from "./model-
|
|
2
|
-
import { i as restoreDeprecatedModelRequestBehaviors, r as Model, t as buildSchema } from "./schema-provider-
|
|
1
|
+
import { a as belongsTo, i as hasMany, n as modelFor, o as attr, r as teardownRecord, t as instantiateRecord } from "./model-QIzJrhcV.js";
|
|
2
|
+
import { i as restoreDeprecatedModelRequestBehaviors, r as Model, t as buildSchema } from "./schema-provider-D_H0kna2.js";
|
|
3
3
|
|
|
4
4
|
export { Model, Model as default, attr, belongsTo, buildSchema, hasMany, instantiateRecord, modelFor, restoreDeprecatedModelRequestBehaviors, teardownRecord };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { n as FetchManager, t as upgradeStore } from "./-private-DuRzYAyW.js";
|
|
2
|
-
import { n as normalizeModelName } from "./util-CI7b4vAE.js";
|
|
3
1
|
import { i as releaseLegacySupport, n as LEGACY_SUPPORT, r as lookupLegacySupport, t as Errors } from "./errors-Bw8niCo8.js";
|
|
2
|
+
import { n as FetchManager, t as upgradeStore } from "./-private-DuRzYAyW.js";
|
|
4
3
|
import { n as decorateMethodV2 } from "./runtime--fcdnjmJ-5yaSpa-d.js";
|
|
4
|
+
import { n as normalizeModelName } from "./util-CI7b4vAE.js";
|
|
5
5
|
import { recordIdentifierFor, storeFor } from "@warp-drive/core";
|
|
6
6
|
import { getOwner } from "@ember/application";
|
|
7
7
|
import { assertPrivateStore, coerceId, recordIdentifierFor as recordIdentifierFor$1 } from "@warp-drive/core/store/-private";
|
|
@@ -1616,4 +1616,4 @@ function getModelFactory(store, type) {
|
|
|
1616
1616
|
}
|
|
1617
1617
|
|
|
1618
1618
|
export { save as _, RecordState as a, _save as c, createSnapshot as d, deleteRecord as f, rollbackAttributes as g, reload as h, restoreDeprecatedModelRequestBehaviors as i, belongsTo as l, hasMany as m, getModelFactory as n, _destroyRecord as o, destroyRecord as p, Model as r, _reload as s, buildSchema as t, changedAttributes as u, serialize as v, unloadRecord as y };
|
|
1619
|
-
//# sourceMappingURL=schema-provider-
|
|
1619
|
+
//# sourceMappingURL=schema-provider-D_H0kna2.js.map
|