@warp-drive/legacy 5.8.0-alpha.3 → 5.8.0-alpha.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/README.md +14 -27
  2. package/declarations/adapter/json-api.d.ts +4 -6
  3. package/declarations/adapter/rest.d.ts +5 -8
  4. package/declarations/adapter.d.ts +4 -6
  5. package/declarations/compat/-private.d.ts +1 -1
  6. package/declarations/compat/extensions.d.ts +1 -1
  7. package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts +7 -9
  8. package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +20 -30
  9. package/declarations/compat/utils.d.ts +17 -17
  10. package/declarations/compat.d.ts +32 -8
  11. package/declarations/index.d.ts +70 -0
  12. package/declarations/model/-private/attr.d.ts +5 -6
  13. package/declarations/model/-private/belongs-to.d.ts +4 -5
  14. package/declarations/model/-private/has-many.d.ts +4 -5
  15. package/declarations/model/-private/hooks.d.ts +1 -1
  16. package/declarations/model/-private/legacy-relationships-support.d.ts +2 -2
  17. package/declarations/model/-private/model.d.ts +8 -8
  18. package/declarations/model/-private/record-state.d.ts +1 -1
  19. package/declarations/model/-private/references/belongs-to.d.ts +1 -1
  20. package/declarations/model/-private/references/has-many.d.ts +1 -1
  21. package/declarations/model/migration-support.d.ts +12 -3
  22. package/declarations/model-fragments/extensions/fragment-array.d.ts +16 -0
  23. package/declarations/model-fragments/extensions/fragment.d.ts +15 -0
  24. package/declarations/model-fragments/hooks/model-for.d.ts +20 -0
  25. package/declarations/model-fragments/index.d.ts +5 -0
  26. package/declarations/model-fragments/instance-initializers/fragment-extensions.d.ts +9 -0
  27. package/declarations/model-fragments/utilities/with-array-defaults.d.ts +15 -0
  28. package/declarations/model-fragments/utilities/with-fragment-array-defaults.d.ts +20 -0
  29. package/declarations/model-fragments/utilities/with-fragment-defaults.d.ts +19 -0
  30. package/declarations/model-fragments/utilities/with-legacy.d.ts +3 -0
  31. package/declarations/model-fragments.d.ts +9 -0
  32. package/declarations/serializer/-private/embedded-records-mixin.d.ts +0 -5
  33. package/declarations/serializer/-private/transforms/boolean.d.ts +2 -2
  34. package/declarations/serializer/-private/transforms/date.d.ts +2 -2
  35. package/declarations/serializer/-private/transforms/number.d.ts +1 -1
  36. package/declarations/serializer/-private/transforms/string.d.ts +1 -1
  37. package/declarations/serializer/json-api.d.ts +4 -6
  38. package/declarations/serializer/json.d.ts +6 -8
  39. package/declarations/serializer/rest.d.ts +4 -6
  40. package/declarations/serializer.d.ts +8 -11
  41. package/dist/{-private-8UmnAf9J.js → -private-B1pSSN52.js} +1 -1
  42. package/dist/adapter/-private.js +1 -1
  43. package/dist/adapter/error.js +5 -6
  44. package/dist/adapter/json-api.js +4 -1
  45. package/dist/adapter/rest.js +6 -9
  46. package/dist/adapter.js +4 -6
  47. package/dist/compat/-private.js +1 -1
  48. package/dist/compat/utils.js +17 -17
  49. package/dist/compat.js +55 -38
  50. package/dist/{errors-8kD2mSe_.js → errors-B9CDPh3R.js} +75 -53
  51. package/dist/hooks-CQXyievu.js +74 -0
  52. package/dist/index.js +195 -0
  53. package/dist/{json-DziiodPf.js → json-BHxlccxF.js} +8 -12
  54. package/dist/model/-private.js +1 -1
  55. package/dist/model/migration-support.js +15 -2
  56. package/dist/model-for-B0TSd9HU.js +221 -0
  57. package/dist/model-fragments.js +76 -0
  58. package/dist/model.js +18 -90
  59. package/dist/{schema-provider-DQu4Rjco.js → schema-provider-BnVr_CnJ.js} +8 -10
  60. package/dist/{serialize-into-hash-CS0MIv4F.js → serialize-into-hash-BnYvPex3.js} +1 -1
  61. package/dist/serializer/json-api.js +11 -38
  62. package/dist/serializer/json.js +1 -1
  63. package/dist/serializer/rest.js +5 -12
  64. package/dist/serializer/transform.js +15 -6
  65. package/dist/serializer.js +8 -12
  66. package/dist/store.js +3 -0
  67. package/logos/README.md +2 -2
  68. package/logos/logo-yellow-slab.svg +1 -0
  69. package/logos/word-mark-black.svg +1 -0
  70. package/logos/word-mark-white.svg +1 -0
  71. package/package.json +6 -6
  72. package/logos/NCC-1701-a-blue.svg +0 -4
  73. package/logos/NCC-1701-a-gold.svg +0 -4
  74. package/logos/NCC-1701-a-gold_100.svg +0 -1
  75. package/logos/NCC-1701-a-gold_base-64.txt +0 -1
  76. package/logos/NCC-1701-a.svg +0 -4
  77. package/logos/docs-badge.svg +0 -2
  78. package/logos/ember-data-logo-dark.svg +0 -12
  79. package/logos/ember-data-logo-light.svg +0 -12
  80. package/logos/social1.png +0 -0
  81. package/logos/social2.png +0 -0
  82. package/logos/warp-drive-logo-dark.svg +0 -4
  83. package/logos/warp-drive-logo-gold.svg +0 -4
package/dist/index.js ADDED
@@ -0,0 +1,195 @@
1
+ import { Store, RequestManager, Fetch, CacheHandler, recordIdentifierFor } from '@warp-drive/core';
2
+ import { SchemaService, registerDerivations, instantiateRecord as instantiateRecord$1, teardownRecord as teardownRecord$1 } from '@warp-drive/core/reactive';
3
+ import { DefaultCachePolicy } from '@warp-drive/core/store';
4
+ import { LegacyNetworkHandler, adapterFor, serializerFor, pushPayload, normalize, serializeRecord, cleanup } from './compat.js';
5
+ import { EmberArrayLikeExtension, EmberObjectArrayExtension, EmberObjectExtension } from './compat/extensions.js';
6
+ import '@ember/object';
7
+ import '@warp-drive/core/types/symbols';
8
+ import '@ember/debug';
9
+ import '@warp-drive/utilities/string';
10
+ import { macroCondition, getGlobalConfig } from '@embroider/macros';
11
+ import '@warp-drive/core/store/-private';
12
+ import "./errors-B9CDPh3R.js";
13
+ import "./schema-provider-BnVr_CnJ.js";
14
+ import { i as instantiateRecord, t as teardownRecord, m as modelFor } from "./hooks-CQXyievu.js";
15
+ import { registerDerivations as registerDerivations$1, DelegatingSchemaService } from './model/migration-support.js';
16
+ import { F as FragmentExtension, a as FragmentArrayExtension, f as fragmentsModelFor } from "./model-for-B0TSd9HU.js";
17
+ import { restoreDeprecatedStoreBehaviors } from './store.js';
18
+
19
+ /**
20
+ * @module
21
+ * @mergeModuleWith <project>
22
+ */
23
+
24
+ /**
25
+ * Use the legacy store with the given options.
26
+ */
27
+ function useLegacyStore(options, StoreKlass = Store) {
28
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
29
+ if (!test) {
30
+ throw new Error(`If legacyRequests is true, linksMode must be false`);
31
+ }
32
+ })(!(options.linksMode && options.legacyRequests)) : {};
33
+ // we extend the store to ensure we don't leak our prototype overrides to other stores below.
34
+ class BaseKlass extends StoreKlass {}
35
+ class LegacyConfiguredStore extends BaseKlass {
36
+ requestManager = new RequestManager().use([options.linksMode ? null : LegacyNetworkHandler, ...(options.handlers ?? []), Fetch].filter(Boolean)).useCache(CacheHandler);
37
+ lifetimes = options.policy ?? new DefaultCachePolicy({
38
+ apiCacheHardExpires: 15 * 60 * 1000,
39
+ // 15 minutes
40
+ apiCacheSoftExpires: 1 * 30 * 1000,
41
+ // 30 seconds
42
+ constraints: {
43
+ headers: {
44
+ 'X-WarpDrive-Expires': true,
45
+ 'Cache-Control': true,
46
+ Expires: true
47
+ }
48
+ }
49
+ });
50
+ createSchemaService() {
51
+ // prepare for PolarisMode
52
+ const schema = new SchemaService();
53
+ registerDerivations(schema);
54
+ if (options.schemas) schema.registerResources(options.schemas);
55
+ if (options.traits) {
56
+ for (const trait of options.traits) {
57
+ schema.registerTrait(trait);
58
+ }
59
+ }
60
+ if (options.derivations) {
61
+ for (const derivation of options.derivations) {
62
+ schema.registerDerivation(derivation);
63
+ }
64
+ }
65
+ if (options.transformations) {
66
+ for (const transformation of options.transformations) {
67
+ schema.registerTransformation(transformation);
68
+ }
69
+ }
70
+ if (options.hashFns) {
71
+ for (const hashFn of options.hashFns) {
72
+ schema.registerHashFn(hashFn);
73
+ }
74
+ }
75
+ if (options.CAUTION_MEGA_DANGER_ZONE_extensions) {
76
+ for (const extension of options.CAUTION_MEGA_DANGER_ZONE_extensions) {
77
+ schema.CAUTION_MEGA_DANGER_ZONE_registerExtension(extension);
78
+ }
79
+ }
80
+
81
+ // Add support for LegacyMode ReactiveResource with Maximal coverage
82
+ // for upgrading from 4.x
83
+ registerDerivations$1(schema);
84
+ schema.CAUTION_MEGA_DANGER_ZONE_registerExtension(EmberArrayLikeExtension);
85
+ schema.CAUTION_MEGA_DANGER_ZONE_registerExtension(EmberObjectArrayExtension);
86
+ schema.CAUTION_MEGA_DANGER_ZONE_registerExtension(EmberObjectExtension);
87
+
88
+ // add support for fragments
89
+ if (options.modelFragments) {
90
+ schema.CAUTION_MEGA_DANGER_ZONE_registerExtension?.(FragmentExtension);
91
+ schema.CAUTION_MEGA_DANGER_ZONE_registerExtension?.(FragmentArrayExtension);
92
+ }
93
+
94
+ // Add fallback for Models
95
+ return new DelegatingSchemaService(this, schema);
96
+ }
97
+ createCache(capabilities) {
98
+ // eslint-disable-next-line new-cap
99
+ return new options.cache(capabilities);
100
+ }
101
+ instantiateRecord(key, createArgs) {
102
+ if (this.schema.isDelegated(key)) {
103
+ return instantiateRecord.call(this, key, createArgs);
104
+ }
105
+ return instantiateRecord$1(this, key, createArgs);
106
+ }
107
+ teardownRecord(record) {
108
+ const key = recordIdentifierFor(record);
109
+ if (this.schema.isDelegated(key)) {
110
+ return teardownRecord.call(this, record);
111
+ }
112
+ return teardownRecord$1(record);
113
+ }
114
+ modelFor(type) {
115
+ assertType(this.schema, type);
116
+ // TODO I'm not sure this is right
117
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
118
+ if (!test) {
119
+ throw new Error(`modelFor should only be used to lookup legacy models when in linksMode: false`);
120
+ }
121
+ })(!options.linksMode || !this.schema.isDelegated({
122
+ type
123
+ })) : {};
124
+ const klass =
125
+ // prefer real models if present
126
+ modelFor.call(this, type) || (
127
+ // fallback to ShimModelClass specific to fragments if fragments support in use
128
+ options.modelFragments ? fragmentsModelFor.call(this, type) : false) ||
129
+ // fallback to ShimModelClass
130
+ super.modelFor(type);
131
+ return klass;
132
+ }
133
+ adapterFor(modelName, _allowMissing) {
134
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
135
+ if (!test) {
136
+ throw new Error(`useLegacyStore was setup in linksMode. linksMode assumes that all requests have been migrated away from adapters and serializers.`);
137
+ }
138
+ })(!options.linksMode) : {};
139
+ // @ts-expect-error
140
+ return adapterFor.call(this, modelName, _allowMissing);
141
+ }
142
+ serializerFor(...args) {
143
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
144
+ if (!test) {
145
+ throw new Error(`useLegacyStore was setup in linksMode. linksMode assumes that all requests have been migrated away from adapters and serializers.`);
146
+ }
147
+ })(!options.linksMode) : {};
148
+ return serializerFor.call(this, ...args);
149
+ }
150
+ pushPayload(...args) {
151
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
152
+ if (!test) {
153
+ throw new Error(`useLegacyStore was setup in linksMode. linksMode assumes that all requests have been migrated away from adapters and serializers.`);
154
+ }
155
+ })(!options.linksMode) : {};
156
+ return pushPayload.call(this, ...args);
157
+ }
158
+ normalize(...args) {
159
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
160
+ if (!test) {
161
+ throw new Error(`useLegacyStore was setup in linksMode. linksMode assumes that all requests have been migrated away from adapters and serializers.`);
162
+ }
163
+ })(!options.linksMode) : {};
164
+ return normalize.call(this, ...args);
165
+ }
166
+ serializeRecord(...args) {
167
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
168
+ if (!test) {
169
+ throw new Error(`useLegacyStore was setup in linksMode. linksMode assumes that all requests have been migrated away from adapters and serializers.`);
170
+ }
171
+ })(!options.linksMode) : {};
172
+ return serializeRecord.call(this, ...args);
173
+ }
174
+ destroy() {
175
+ if (!options.linksMode) {
176
+ cleanup.call(this);
177
+ }
178
+ super.destroy();
179
+ }
180
+ }
181
+ if (options.legacyRequests) {
182
+ restoreDeprecatedStoreBehaviors(BaseKlass);
183
+ }
184
+ return LegacyConfiguredStore;
185
+ }
186
+ function assertType(schema, type) {
187
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
188
+ if (!test) {
189
+ throw new Error(`Expected type ${type} to be a valid ResourceType`);
190
+ }
191
+ })(schema.hasResource({
192
+ type
193
+ })) : {};
194
+ }
195
+ export { useLegacyStore };
@@ -25,20 +25,18 @@ const SOURCE_POINTER_PRIMARY_REGEXP = /^\/?data/;
25
25
  const PRIMARY_ATTRIBUTE_KEY = 'base';
26
26
 
27
27
  /**
28
- * <blockquote style="margin: 1em; padding: .1em 1em .1em 1em; border-left: solid 1em #E34C32; background: #e0e0e0;">
29
- <p>
30
- ⚠️ <strong>This is LEGACY documentation</strong> for a feature that is no longer encouraged to be used.
28
+ * :::danger
29
+ ⚠️ **This is LEGACY documentation** for a feature that is no longer encouraged to be used.
31
30
  If starting a new app or thinking of implementing a new adapter, consider writing a
32
- <a href="/ember-data/release/classes/%3CInterface%3E%20Handler">Handler</a> instead to be used with the <a href="https://github.com/warp-drive-data/warp-drive/tree/main/packages/request#readme">RequestManager</a>
33
- </p>
34
- </blockquote>
31
+ {@link Handler} instead to be used with the {@link RequestManager}
32
+ :::
35
33
 
36
- In EmberData a Serializer is used to serialize and deserialize
34
+ In WarpDrive a Serializer is used to serialize and deserialize
37
35
  records when they are transferred in and out of an external source.
38
36
  This process involves normalizing property names, transforming
39
37
  attribute values and serializing relationships.
40
38
 
41
- By default, EmberData uses and recommends the `JSONAPISerializer`.
39
+ By default, WarpDrive uses and recommends the `JSONAPISerializer`.
42
40
 
43
41
  `JSONSerializer` is useful for simpler or legacy backends that may
44
42
  not support the http://jsonapi.org/ spec.
@@ -159,7 +157,7 @@ const JSONSerializer = Serializer.extend({
159
157
  }
160
158
  ```
161
159
  When serialized:
162
- ```javascript
160
+ ```js
163
161
  {
164
162
  "firstName": "Harry",
165
163
  "lastName": "Houdini",
@@ -170,9 +168,7 @@ const JSONSerializer = Serializer.extend({
170
168
  Setting `serialize` to `true` enforces serialization for hasMany
171
169
  relationships even if it's neither a many-to-many nor many-to-none
172
170
  relationship.
173
- @property attrs
174
- @public
175
- @type {Object}
171
+ @public
176
172
  */
177
173
  mergedProperties: ['attrs'],
178
174
  /**
@@ -1 +1 @@
1
- export { E as Errors, L as LEGACY_SUPPORT, P as PromiseBelongsTo, a as PromiseManyArray, l as lookupLegacySupport } from "../errors-8kD2mSe_.js";
1
+ export { E as Errors, L as LEGACY_SUPPORT, P as PromiseBelongsTo, a as PromiseManyArray, l as lookupLegacySupport } from "../errors-B9CDPh3R.js";
@@ -4,8 +4,8 @@ import { Context } from '@warp-drive/core/reactive/-private';
4
4
  import { notifyInternalSignal, assertPrivateStore } from '@warp-drive/core/store/-private';
5
5
  import { getOrSetGlobal } from '@warp-drive/core/types/-private';
6
6
  import { Type } from '@warp-drive/core/types/symbols';
7
- import { l as lookupLegacySupport, E as Errors } from "../errors-8kD2mSe_.js";
8
- import { b as buildSchema, u as unloadRecord, s as serialize, _ as _save, a as save, r as rollbackAttributes, c as _reload, d as reload, h as hasMany, e as _destroyRecord, f as destroyRecord, g as deleteRecord, R as RecordState, i as changedAttributes, j as belongsTo, k as createSnapshot } from "../schema-provider-DQu4Rjco.js";
7
+ import { l as lookupLegacySupport, E as Errors } from "../errors-B9CDPh3R.js";
8
+ import { b as buildSchema, u as unloadRecord, s as serialize, _ as _save, a as save, r as rollbackAttributes, c as _reload, d as reload, h as hasMany, e as _destroyRecord, f as destroyRecord, i as deleteRecord, R as RecordState, j as changedAttributes, k as belongsTo, l as createSnapshot } from "../schema-provider-BnVr_CnJ.js";
9
9
  import { macroCondition, getGlobalConfig } from '@embroider/macros';
10
10
 
11
11
  /**
@@ -417,7 +417,9 @@ function registerDerivations(schema) {
417
417
  */
418
418
 
419
419
  class DelegatingSchemaService {
420
+ /** @internal */
420
421
  _preferred;
422
+ /** @internal */
421
423
  _secondary;
422
424
  constructor(store, schema) {
423
425
  this._preferred = schema;
@@ -452,6 +454,14 @@ class DelegatingSchemaService {
452
454
  }
453
455
  return this._secondary.fields(resource);
454
456
  }
457
+ cacheFields(resource) {
458
+ if (this._preferred.cacheFields?.(resource)) {
459
+ return this._preferred.cacheFields(resource);
460
+ }
461
+
462
+ // @ts-expect-error
463
+ return this._secondary.cacheFields?.(resource);
464
+ }
455
465
  transformation(field) {
456
466
  return this._preferred.transformation(field);
457
467
  }
@@ -482,6 +492,9 @@ class DelegatingSchemaService {
482
492
  registerHashFn(hashFn) {
483
493
  this._preferred.registerHashFn(hashFn);
484
494
  }
495
+ CAUTION_MEGA_DANGER_ZONE_hasExtension(ext) {
496
+ return this._preferred.CAUTION_MEGA_DANGER_ZONE_hasExtension(ext);
497
+ }
485
498
  CAUTION_MEGA_DANGER_ZONE_registerExtension(extension) {
486
499
  this._preferred.CAUTION_MEGA_DANGER_ZONE_registerExtension(extension);
487
500
  }
@@ -0,0 +1,221 @@
1
+ import { tracked, cached } from '@glimmer/tracking';
2
+ import { Context } from '@warp-drive/core/reactive/-private';
3
+ import { a as decorateFieldV2, i as initializeDeferredDecorator, d as decorateMethodV2 } from "./runtime-BPCpkOf1-BKOwiRJp.js";
4
+ class Fragment {
5
+ static {
6
+ decorateFieldV2(this.prototype, "isDestroying", [tracked], function () {
7
+ return false;
8
+ });
9
+ }
10
+ #isDestroying = (initializeDeferredDecorator(this, "isDestroying"), void 0); // We might want to check the parent values once we move this code to warp-drive.
11
+ static {
12
+ decorateFieldV2(this.prototype, "isDestroyed", [tracked], function () {
13
+ return false;
14
+ });
15
+ }
16
+ #isDestroyed = (initializeDeferredDecorator(this, "isDestroyed"), void 0);
17
+ get hasDirtyAttributes() {
18
+ const {
19
+ path,
20
+ resourceKey,
21
+ store
22
+ } = this[Context];
23
+ const record = store.peekRecord(resourceKey);
24
+ if (record.hasDirtyAttributes && path) {
25
+ const root = path.at(0);
26
+ return root in record.changedAttributes();
27
+ }
28
+ return false;
29
+ }
30
+ static {
31
+ decorateMethodV2(this.prototype, "hasDirtyAttributes", [cached]);
32
+ }
33
+ get isFragment() {
34
+ return true;
35
+ }
36
+ get $type() {
37
+ const {
38
+ field
39
+ } = this[Context];
40
+ return field?.type;
41
+ }
42
+ rollbackAttributes() {
43
+ const {
44
+ path,
45
+ resourceKey,
46
+ store
47
+ } = this[Context];
48
+ if (path) {
49
+ const oldValue = store.cache.getRemoteAttr(resourceKey, path);
50
+ store.cache.setAttr(resourceKey, path, oldValue);
51
+ }
52
+ }
53
+ }
54
+ const FragmentExtension = {
55
+ kind: 'object',
56
+ name: 'fragment',
57
+ features: Fragment
58
+ };
59
+ class FragmentArray {
60
+ static {
61
+ decorateFieldV2(this.prototype, "isDestroying", [tracked], function () {
62
+ return false;
63
+ });
64
+ }
65
+ #isDestroying = (initializeDeferredDecorator(this, "isDestroying"), void 0); // We might want to check the parent values once we move this code to warp-drive.
66
+ static {
67
+ decorateFieldV2(this.prototype, "isDestroyed", [tracked], function () {
68
+ return false;
69
+ });
70
+ }
71
+ #isDestroyed = (initializeDeferredDecorator(this, "isDestroyed"), void 0);
72
+ get hasDirtyAttributes() {
73
+ const {
74
+ path,
75
+ resourceKey,
76
+ store
77
+ } = this[Context];
78
+ const record = store.peekRecord(resourceKey);
79
+ if (record.hasDirtyAttributes && path) {
80
+ const root = path.at(0);
81
+ return root in record.changedAttributes();
82
+ }
83
+ return false;
84
+ }
85
+ static {
86
+ decorateMethodV2(this.prototype, "hasDirtyAttributes", [cached]);
87
+ }
88
+ addFragment(fragment) {
89
+ if (!fragment) {
90
+ return;
91
+ }
92
+ return this.addObject(fragment);
93
+ }
94
+ createFragment(fragment) {
95
+ if (!fragment) {
96
+ return;
97
+ }
98
+ return this.pushObject(fragment);
99
+ }
100
+ removeFragment(fragment) {
101
+ if (!fragment) {
102
+ return;
103
+ }
104
+ const index = this.indexOf(fragment);
105
+ if (index !== -1) {
106
+ this.splice(index, 1);
107
+ }
108
+ }
109
+ rollbackAttributes() {
110
+ for (const fragment of this) {
111
+ // @ts-expect-error TODO: fix these types
112
+ fragment?.rollbackAttributes?.();
113
+ }
114
+ }
115
+ }
116
+ const FragmentArrayExtension = {
117
+ kind: 'array',
118
+ name: 'fragment-array',
119
+ features: FragmentArray
120
+ };
121
+
122
+ // if modelFor turns out to be a bottleneck we should replace with a Map
123
+ // and clear it during store teardown.
124
+ const AvailableShims = new WeakMap();
125
+ function getShimClass(store, modelName) {
126
+ let shims = AvailableShims.get(store);
127
+ if (!shims) {
128
+ shims = Object.create(null);
129
+ AvailableShims.set(store, shims);
130
+ }
131
+ let shim = shims[modelName];
132
+ if (shim === undefined) {
133
+ shim = shims[modelName] = new ShimModelClass(store, modelName);
134
+ }
135
+ return shim;
136
+ }
137
+ const AttributeKinds = ['field', 'attribute', 'object', 'array', 'schema-object', 'schema-array'];
138
+
139
+ // Mimics the static apis of @ember-data/model
140
+ class ShimModelClass {
141
+ constructor(store, modelName) {
142
+ this.__store = store;
143
+ this.modelName = modelName;
144
+ }
145
+ get fields() {
146
+ const fields = new Map();
147
+ const fieldSchemas = this.__store.schema.fields({
148
+ type: this.modelName
149
+ });
150
+ fieldSchemas.forEach((schema, key) => {
151
+ // @ts-expect-error checking if a string is a valid string
152
+ if (AttributeKinds.includes(schema.kind)) {
153
+ fields.set(key, 'attribute');
154
+ } else if (schema.kind === 'belongsTo' || schema.kind === 'hasMany') {
155
+ fields.set(key, schema.kind);
156
+ }
157
+ });
158
+ return fields;
159
+ }
160
+ get attributes() {
161
+ const attrs = new Map();
162
+ const fields = this.__store.schema.fields({
163
+ type: this.modelName
164
+ });
165
+ fields.forEach((schema, key) => {
166
+ if (schema.kind === 'attribute') {
167
+ attrs.set(key, schema);
168
+ // @ts-expect-error checking if a string is a valid string
169
+ } else if (AttributeKinds.includes(schema.kind)) {
170
+ attrs.set(key, {
171
+ kind: 'attribute',
172
+ name: key,
173
+ type: null,
174
+ options: schema.options ?? {}
175
+ });
176
+ }
177
+ });
178
+ return attrs;
179
+ }
180
+ get relationshipsByName() {
181
+ const rels = new Map();
182
+ const fields = this.__store.schema.fields({
183
+ type: this.modelName
184
+ });
185
+ fields.forEach((schema, key) => {
186
+ if (schema.kind === 'belongsTo' || schema.kind === 'hasMany') {
187
+ rels.set(key, schema);
188
+ }
189
+ });
190
+ return rels;
191
+ }
192
+ eachAttribute(callback, binding) {
193
+ this.attributes.forEach((schema, key) => {
194
+ callback.call(binding, key, schema);
195
+ });
196
+ }
197
+ eachRelationship(callback, binding) {
198
+ this.__store.schema.fields({
199
+ type: this.modelName
200
+ }).forEach((schema, key) => {
201
+ if (schema.kind === 'belongsTo' || schema.kind === 'hasMany') {
202
+ callback.call(binding, key, schema);
203
+ }
204
+ });
205
+ }
206
+ eachTransformedAttribute(callback, binding) {
207
+ this.__store.schema.fields({
208
+ type: this.modelName
209
+ }).forEach((schema, key) => {
210
+ if (schema.kind === 'attribute') {
211
+ const type = schema.type;
212
+ if (type) callback.call(binding, key, type);
213
+ }
214
+ });
215
+ }
216
+ }
217
+ function fragmentsModelFor(modelName) {
218
+ return getShimClass(this, modelName);
219
+ }
220
+ const modelFor = fragmentsModelFor;
221
+ export { FragmentExtension as F, FragmentArrayExtension as a, FragmentArray as b, Fragment as c, fragmentsModelFor as f, modelFor as m };
@@ -0,0 +1,76 @@
1
+ import { singularize, pluralize } from '@warp-drive/utilities/string';
2
+ import { withDefaults } from './model/migration-support.js';
3
+ import { F as FragmentExtension, a as FragmentArrayExtension } from "./model-for-B0TSd9HU.js";
4
+ export { c as Fragment, b as FragmentArray, m as modelFor } from "./model-for-B0TSd9HU.js";
5
+
6
+ /**
7
+ * Used as a helper to setup the relevant parts of an array
8
+ * schema and add extensions etc.
9
+ *
10
+ * @param arrayName The name of the array
11
+ * @returns The schema for an array
12
+ */
13
+ function withArrayDefaults(arrayName) {
14
+ return {
15
+ kind: 'array',
16
+ name: arrayName,
17
+ type: `array:${singularize(arrayName)}`,
18
+ options: {
19
+ arrayExtensions: ['ember-object', 'ember-array-like', 'fragment-array']
20
+ }
21
+ };
22
+ }
23
+
24
+ /**
25
+ * Used as a helper to setup the relevant parts of a fragment schema
26
+ * and add extensions etc.
27
+ *
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: {
38
+ objectExtensions: ['ember-object', 'fragment']
39
+ }
40
+ };
41
+ }
42
+
43
+ /**
44
+ * Used as a helper to setup the relevant parts of a fragment-array
45
+ * schema and add extensions etc.
46
+ *
47
+ * @param fragmentArrayType The type of the fragment-array
48
+ * @param fragmentArrayName The name of the fragment-array
49
+ * @returns The schema for a fragment-array
50
+ */
51
+ function withFragmentArrayDefaults(fragmentArrayType, fragmentArrayName) {
52
+ return {
53
+ kind: 'schema-array',
54
+ type: `fragment:${singularize(fragmentArrayType)}`,
55
+ name: fragmentArrayName ?? pluralize(fragmentArrayType),
56
+ options: {
57
+ arrayExtensions: ['ember-object', 'ember-array-like', 'fragment-array'],
58
+ defaultValue: true
59
+ }
60
+ };
61
+ }
62
+ function withLegacy(schema) {
63
+ return withDefaults({
64
+ ...schema,
65
+ identity: {
66
+ kind: '@id',
67
+ name: 'id'
68
+ },
69
+ objectExtensions: ['ember-object', 'fragment']
70
+ });
71
+ }
72
+ function registerFragmentExtensions(schema) {
73
+ schema.CAUTION_MEGA_DANGER_ZONE_registerExtension?.(FragmentExtension);
74
+ schema.CAUTION_MEGA_DANGER_ZONE_registerExtension?.(FragmentArrayExtension);
75
+ }
76
+ export { FragmentArrayExtension, FragmentExtension, registerFragmentExtensions, withArrayDefaults, withFragmentArrayDefaults, withFragmentDefaults, withLegacy };