@warp-drive/legacy 5.8.0-alpha.4 → 5.8.0-alpha.40

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 (213) hide show
  1. package/README.md +14 -27
  2. package/declarations/adapter/error.d.ts +5 -5
  3. package/declarations/adapter/json-api.d.ts +6 -8
  4. package/declarations/adapter/rest.d.ts +5 -8
  5. package/declarations/adapter.d.ts +4 -6
  6. package/declarations/compat/-private.d.ts +1 -1
  7. package/declarations/compat/builders/find-all.d.ts +6 -6
  8. package/declarations/compat/builders/find-record.d.ts +8 -8
  9. package/declarations/compat/builders/query.d.ts +12 -12
  10. package/declarations/compat/extensions.d.ts +1 -1
  11. package/declarations/compat/legacy-network-handler/minimum-adapter-interface.d.ts +7 -9
  12. package/declarations/compat/legacy-network-handler/minimum-serializer-interface.d.ts +20 -30
  13. package/declarations/compat/utils.d.ts +17 -17
  14. package/declarations/compat.d.ts +35 -11
  15. package/declarations/index.d.ts +70 -0
  16. package/declarations/model/-private/attr.d.ts +5 -6
  17. package/declarations/model/-private/belongs-to.d.ts +4 -5
  18. package/declarations/model/-private/has-many.d.ts +4 -5
  19. package/declarations/model/-private/hooks.d.ts +1 -1
  20. package/declarations/model/-private/legacy-relationships-support.d.ts +2 -2
  21. package/declarations/model/-private/model.d.ts +8 -8
  22. package/declarations/model/-private/record-state.d.ts +1 -1
  23. package/declarations/model/-private/references/belongs-to.d.ts +5 -5
  24. package/declarations/model/-private/references/has-many.d.ts +3 -3
  25. package/declarations/model/migration-support.d.ts +46 -21
  26. package/declarations/model-fragments/extensions/fragment-array.d.ts +16 -0
  27. package/declarations/model-fragments/extensions/fragment.d.ts +15 -0
  28. package/declarations/model-fragments/hooks/model-for.d.ts +20 -0
  29. package/declarations/model-fragments/index.d.ts +5 -0
  30. package/declarations/model-fragments/instance-initializers/fragment-extensions.d.ts +9 -0
  31. package/declarations/model-fragments/utilities/with-array-defaults.d.ts +15 -0
  32. package/declarations/model-fragments/utilities/with-fragment-array-defaults.d.ts +20 -0
  33. package/declarations/model-fragments/utilities/with-fragment-defaults.d.ts +19 -0
  34. package/declarations/model-fragments/utilities/with-legacy.d.ts +3 -0
  35. package/declarations/model-fragments.d.ts +9 -0
  36. package/declarations/model.d.ts +2 -2
  37. package/declarations/serializer/-private/embedded-records-mixin.d.ts +1 -6
  38. package/declarations/serializer/-private/transforms/boolean.d.ts +2 -2
  39. package/declarations/serializer/-private/transforms/date.d.ts +2 -2
  40. package/declarations/serializer/-private/transforms/number.d.ts +1 -1
  41. package/declarations/serializer/-private/transforms/string.d.ts +1 -1
  42. package/declarations/serializer/json-api.d.ts +7 -9
  43. package/declarations/serializer/json.d.ts +7 -9
  44. package/declarations/serializer/rest.d.ts +4 -6
  45. package/declarations/serializer.d.ts +9 -12
  46. package/dist/{-private-8UmnAf9J.js → -private-B1pSSN52.js} +1 -1
  47. package/dist/adapter/-private.js +1 -1
  48. package/dist/adapter/error.js +12 -13
  49. package/dist/adapter/json-api.js +4 -1
  50. package/dist/adapter/rest.js +6 -9
  51. package/dist/adapter.js +4 -6
  52. package/dist/compat/-private.js +1 -1
  53. package/dist/compat/builders.js +26 -26
  54. package/dist/compat/utils.js +17 -18
  55. package/dist/compat.js +58 -41
  56. package/dist/{errors-8kD2mSe_.js → errors-CIGPcDvd.js} +87 -65
  57. package/dist/hooks-QqRnX108.js +74 -0
  58. package/dist/index.js +195 -0
  59. package/dist/{json-DziiodPf.js → json-BNrV8EYG.js} +12 -16
  60. package/dist/model/-private.js +1 -1
  61. package/dist/model/migration-support.js +54 -24
  62. package/dist/model-for-CqXsIKws.js +221 -0
  63. package/dist/model-fragments.js +76 -0
  64. package/dist/model.js +18 -90
  65. package/dist/{schema-provider-DQu4Rjco.js → schema-provider-g5MfTj8n.js} +18 -20
  66. package/dist/{serialize-into-hash-CS0MIv4F.js → serialize-into-hash-BnYvPex3.js} +1 -1
  67. package/dist/serializer/json-api.js +17 -44
  68. package/dist/serializer/json.js +1 -1
  69. package/dist/serializer/rest.js +14 -21
  70. package/dist/serializer/transform.js +15 -6
  71. package/dist/serializer.js +9 -13
  72. package/dist/store.js +3 -0
  73. package/dist/unpkg/dev/-private-DbaSCSym.js +1205 -0
  74. package/dist/unpkg/dev/adapter/-private.js +1 -0
  75. package/dist/unpkg/dev/adapter/error.js +335 -0
  76. package/dist/unpkg/dev/adapter/json-api.js +271 -0
  77. package/dist/unpkg/dev/adapter/rest.js +1255 -0
  78. package/dist/unpkg/dev/adapter.js +1252 -0
  79. package/dist/unpkg/dev/compat/-private.js +1 -0
  80. package/dist/unpkg/dev/compat/builders.js +275 -0
  81. package/dist/unpkg/dev/compat/extensions.js +242 -0
  82. package/dist/unpkg/dev/compat/utils.js +223 -0
  83. package/dist/unpkg/dev/compat.js +1146 -0
  84. package/dist/unpkg/dev/errors-DD96TBEs.js +2591 -0
  85. package/dist/unpkg/dev/hooks-CqWjNWeL.js +73 -0
  86. package/dist/unpkg/dev/index.js +197 -0
  87. package/dist/unpkg/dev/json-CCU-ZQ4b.js +1269 -0
  88. package/dist/unpkg/dev/model/-private.js +1 -0
  89. package/dist/unpkg/dev/model/migration-support.js +551 -0
  90. package/dist/unpkg/dev/model-for-CqXsIKws.js +221 -0
  91. package/dist/unpkg/dev/model-fragments.js +76 -0
  92. package/dist/unpkg/dev/model.js +678 -0
  93. package/dist/unpkg/dev/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  94. package/dist/unpkg/dev/schema-provider-B8jiJOYC.js +2229 -0
  95. package/dist/unpkg/dev/serialize-into-hash-CPAZXrQU.js +259 -0
  96. package/dist/unpkg/dev/serializer/json-api.js +648 -0
  97. package/dist/unpkg/dev/serializer/json.js +4 -0
  98. package/dist/unpkg/dev/serializer/rest.js +1242 -0
  99. package/dist/unpkg/dev/serializer/transform.js +278 -0
  100. package/dist/unpkg/dev/serializer.js +248 -0
  101. package/dist/unpkg/dev/store.js +636 -0
  102. package/dist/unpkg/dev/util-DvanW33H.js +20 -0
  103. package/dist/unpkg/dev/utils-BhvS1iTS.js +8 -0
  104. package/dist/unpkg/dev-deprecated/-private-DbaSCSym.js +1205 -0
  105. package/dist/unpkg/dev-deprecated/adapter/-private.js +1 -0
  106. package/dist/unpkg/dev-deprecated/adapter/error.js +335 -0
  107. package/dist/unpkg/dev-deprecated/adapter/json-api.js +271 -0
  108. package/dist/unpkg/dev-deprecated/adapter/rest.js +1255 -0
  109. package/dist/unpkg/dev-deprecated/adapter.js +1252 -0
  110. package/dist/unpkg/dev-deprecated/compat/-private.js +1 -0
  111. package/dist/unpkg/dev-deprecated/compat/builders.js +275 -0
  112. package/dist/unpkg/dev-deprecated/compat/extensions.js +242 -0
  113. package/dist/unpkg/dev-deprecated/compat/utils.js +223 -0
  114. package/dist/unpkg/dev-deprecated/compat.js +1146 -0
  115. package/dist/unpkg/dev-deprecated/errors-DEnabIZj.js +2594 -0
  116. package/dist/unpkg/dev-deprecated/hooks-CAll-Ets.js +73 -0
  117. package/dist/unpkg/dev-deprecated/index.js +196 -0
  118. package/dist/unpkg/dev-deprecated/json-CCU-ZQ4b.js +1269 -0
  119. package/dist/unpkg/dev-deprecated/model/-private.js +1 -0
  120. package/dist/unpkg/dev-deprecated/model/migration-support.js +568 -0
  121. package/dist/unpkg/dev-deprecated/model-for-CqXsIKws.js +221 -0
  122. package/dist/unpkg/dev-deprecated/model-fragments.js +76 -0
  123. package/dist/unpkg/dev-deprecated/model.js +682 -0
  124. package/dist/unpkg/dev-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  125. package/dist/unpkg/dev-deprecated/schema-provider-k2qXQTBg.js +2254 -0
  126. package/dist/unpkg/dev-deprecated/serialize-into-hash-CPAZXrQU.js +259 -0
  127. package/dist/unpkg/dev-deprecated/serializer/json-api.js +648 -0
  128. package/dist/unpkg/dev-deprecated/serializer/json.js +4 -0
  129. package/dist/unpkg/dev-deprecated/serializer/rest.js +1242 -0
  130. package/dist/unpkg/dev-deprecated/serializer/transform.js +278 -0
  131. package/dist/unpkg/dev-deprecated/serializer.js +248 -0
  132. package/dist/unpkg/dev-deprecated/store.js +636 -0
  133. package/dist/unpkg/dev-deprecated/util-CWr5WQOT.js +24 -0
  134. package/dist/unpkg/dev-deprecated/utils-C9PJehtL.js +12 -0
  135. package/dist/unpkg/prod/-private-Cvf_97EG.js +970 -0
  136. package/dist/unpkg/prod/adapter/-private.js +1 -0
  137. package/dist/unpkg/prod/adapter/error.js +330 -0
  138. package/dist/unpkg/prod/adapter/json-api.js +266 -0
  139. package/dist/unpkg/prod/adapter/rest.js +1218 -0
  140. package/dist/unpkg/prod/adapter.js +1219 -0
  141. package/dist/unpkg/prod/compat/-private.js +1 -0
  142. package/dist/unpkg/prod/compat/builders.js +210 -0
  143. package/dist/unpkg/prod/compat/extensions.js +232 -0
  144. package/dist/unpkg/prod/compat/utils.js +218 -0
  145. package/dist/unpkg/prod/compat.js +726 -0
  146. package/dist/unpkg/prod/errors-CXnfnBfQ.js +2343 -0
  147. package/dist/unpkg/prod/hooks-DvyWhLNg.js +41 -0
  148. package/dist/unpkg/prod/index.js +151 -0
  149. package/dist/unpkg/prod/json-BYrUP8ao.js +1256 -0
  150. package/dist/unpkg/prod/model/-private.js +1 -0
  151. package/dist/unpkg/prod/model/migration-support.js +544 -0
  152. package/dist/unpkg/prod/model-for-CqXsIKws.js +221 -0
  153. package/dist/unpkg/prod/model-fragments.js +76 -0
  154. package/dist/unpkg/prod/model.js +593 -0
  155. package/dist/unpkg/prod/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  156. package/dist/unpkg/prod/schema-provider-CHujJvA9.js +1904 -0
  157. package/dist/unpkg/prod/serialize-into-hash-DYU2egXl.js +215 -0
  158. package/dist/unpkg/prod/serializer/json-api.js +591 -0
  159. package/dist/unpkg/prod/serializer/json.js +4 -0
  160. package/dist/unpkg/prod/serializer/rest.js +1210 -0
  161. package/dist/unpkg/prod/serializer/transform.js +278 -0
  162. package/dist/unpkg/prod/serializer.js +248 -0
  163. package/dist/unpkg/prod/store.js +504 -0
  164. package/dist/unpkg/prod/util-DvanW33H.js +20 -0
  165. package/dist/unpkg/prod/utils-BhvS1iTS.js +8 -0
  166. package/dist/unpkg/prod-deprecated/-private-Cvf_97EG.js +970 -0
  167. package/dist/unpkg/prod-deprecated/adapter/-private.js +1 -0
  168. package/dist/unpkg/prod-deprecated/adapter/error.js +330 -0
  169. package/dist/unpkg/prod-deprecated/adapter/json-api.js +266 -0
  170. package/dist/unpkg/prod-deprecated/adapter/rest.js +1218 -0
  171. package/dist/unpkg/prod-deprecated/adapter.js +1219 -0
  172. package/dist/unpkg/prod-deprecated/compat/-private.js +1 -0
  173. package/dist/unpkg/prod-deprecated/compat/builders.js +210 -0
  174. package/dist/unpkg/prod-deprecated/compat/extensions.js +232 -0
  175. package/dist/unpkg/prod-deprecated/compat/utils.js +218 -0
  176. package/dist/unpkg/prod-deprecated/compat.js +726 -0
  177. package/dist/unpkg/prod-deprecated/errors-CG1SPYVg.js +2346 -0
  178. package/dist/unpkg/prod-deprecated/hooks-BIUBiNGR.js +41 -0
  179. package/dist/unpkg/prod-deprecated/index.js +150 -0
  180. package/dist/unpkg/prod-deprecated/json-BYrUP8ao.js +1256 -0
  181. package/dist/unpkg/prod-deprecated/model/-private.js +1 -0
  182. package/dist/unpkg/prod-deprecated/model/migration-support.js +561 -0
  183. package/dist/unpkg/prod-deprecated/model-for-CqXsIKws.js +221 -0
  184. package/dist/unpkg/prod-deprecated/model-fragments.js +76 -0
  185. package/dist/unpkg/prod-deprecated/model.js +596 -0
  186. package/dist/unpkg/prod-deprecated/runtime-BPCpkOf1-BKOwiRJp.js +65 -0
  187. package/dist/unpkg/prod-deprecated/schema-provider-BJ4TWnZf.js +1947 -0
  188. package/dist/unpkg/prod-deprecated/serialize-into-hash-DYU2egXl.js +215 -0
  189. package/dist/unpkg/prod-deprecated/serializer/json-api.js +591 -0
  190. package/dist/unpkg/prod-deprecated/serializer/json.js +4 -0
  191. package/dist/unpkg/prod-deprecated/serializer/rest.js +1210 -0
  192. package/dist/unpkg/prod-deprecated/serializer/transform.js +278 -0
  193. package/dist/unpkg/prod-deprecated/serializer.js +248 -0
  194. package/dist/unpkg/prod-deprecated/store.js +504 -0
  195. package/dist/unpkg/prod-deprecated/util-B6cn-i93.js +23 -0
  196. package/dist/unpkg/prod-deprecated/utils-BUWwQwCh.js +11 -0
  197. package/logos/README.md +2 -2
  198. package/logos/logo-yellow-slab.svg +1 -0
  199. package/logos/word-mark-black.svg +1 -0
  200. package/logos/word-mark-white.svg +1 -0
  201. package/package.json +14 -6
  202. package/logos/NCC-1701-a-blue.svg +0 -4
  203. package/logos/NCC-1701-a-gold.svg +0 -4
  204. package/logos/NCC-1701-a-gold_100.svg +0 -1
  205. package/logos/NCC-1701-a-gold_base-64.txt +0 -1
  206. package/logos/NCC-1701-a.svg +0 -4
  207. package/logos/docs-badge.svg +0 -2
  208. package/logos/ember-data-logo-dark.svg +0 -12
  209. package/logos/ember-data-logo-light.svg +0 -12
  210. package/logos/social1.png +0 -0
  211. package/logos/social2.png +0 -0
  212. package/logos/warp-drive-logo-dark.svg +0 -4
  213. package/logos/warp-drive-logo-gold.svg +0 -4
@@ -0,0 +1 @@
1
+ export { E as Errors, L as LEGACY_SUPPORT, P as PromiseBelongsTo, a as PromiseManyArray, l as lookupLegacySupport } from "../errors-DD96TBEs.js";
@@ -0,0 +1,551 @@
1
+ import { recordIdentifierFor } from '@warp-drive/core';
2
+ import { Context } from '@warp-drive/core/reactive/-private';
3
+ import { notifyInternalSignal, assertPrivateStore } from '@warp-drive/core/store/-private';
4
+ import { getOrSetGlobal } from '@warp-drive/core/types/-private';
5
+ import { Type } from '@warp-drive/core/types/symbols';
6
+ import { l as lookupLegacySupport, E as Errors } from "../errors-DD96TBEs.js";
7
+ 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-B8jiJOYC.js";
8
+
9
+ /**
10
+ * This module provides support for migrating away from @warp-drive/legacy/model
11
+ * to ReactiveResource from @warp-drive/core/reactive.
12
+ *
13
+ * It includes:
14
+ *
15
+ * - A `withDefaults` function to assist in creating a schema in LegacyMode
16
+ * - A `registerDerivations` function to register the derivations necessary to support LegacyMode
17
+ * - A `DelegatingSchemaService` that can be used to provide a schema service that works with both
18
+ * @warp-drive/legacy/model and @warp-drive/core/reactive simultaneously for migration purposes.
19
+ * - A `WithLegacy` type util that can be used to create a type that includes the legacy
20
+ * properties and methods of a record.
21
+ *
22
+ * Using LegacyMode features on a ReactiveResource *requires* the use of these derivations and schema
23
+ * additions. LegacyMode is not intended to be a long-term solution, but rather a stepping stone
24
+ * to assist in more rapidly adopting modern WarpDrive features.
25
+ *
26
+ * @module
27
+ */
28
+
29
+ // 'isDestroying', 'isDestroyed'
30
+ const LegacyFields = ['_createSnapshot', 'adapterError', 'belongsTo', 'changedAttributes', 'constructor', 'currentState', 'deleteRecord', 'destroyRecord', 'dirtyType', 'errors', 'hasDirtyAttributes', 'hasMany', 'isDeleted', 'isEmpty', 'isError', 'isLoaded', 'isLoading', 'isNew', 'isSaving', 'isValid', 'reload', 'rollbackAttributes', 'save', 'serialize', 'unloadRecord'];
31
+
32
+ /**
33
+ * A Type utility that enables quickly adding type information for the fields
34
+ * defined by `import { withDefaults } from '@warp-drive/legacy/model/migration-support'`.
35
+ *
36
+ * Example:
37
+ *
38
+ * ```ts
39
+ * import { withDefaults, WithLegacy } from '@warp-drive/legacy/model/migration-support';
40
+ * import { Type } from '@warp-drive/core/types/symbols';
41
+ * import type { HasMany } from '@@warp-drive/legacy/model';
42
+ *
43
+ * export const UserSchema = withDefaults({
44
+ * type: 'user',
45
+ * fields: [
46
+ * { name: 'firstName', kind: 'attribute' },
47
+ * { name: 'lastName', kind: 'attribute' },
48
+ * { name: 'age', kind: 'attribute' },
49
+ * { name: 'friends',
50
+ * kind: 'hasMany',
51
+ * type: 'user',
52
+ * options: { inverse: 'friends', async: false }
53
+ * },
54
+ * { name: 'bestFriend',
55
+ * kind: 'belongsTo',
56
+ * type: 'user',
57
+ * options: { inverse: null, async: false }
58
+ * },
59
+ * ],
60
+ * });
61
+ *
62
+ * export type User = WithLegacy<{
63
+ * firstName: string;
64
+ * lastName: string;
65
+ * age: number;
66
+ * friends: HasMany<User>;
67
+ * bestFriend: User | null;
68
+ * [Type]: 'user';
69
+ * }>
70
+ * ```
71
+ *
72
+ */
73
+
74
+ const LegacySupport = getOrSetGlobal('LegacySupport', new WeakMap());
75
+ function legacySupport(record, options, prop) {
76
+ let state = LegacySupport.get(record);
77
+ if (!state) {
78
+ state = {};
79
+ LegacySupport.set(record, state);
80
+ }
81
+ const suppressDeprecation = Boolean(options && options.suppressDeprecation);
82
+ switch (prop) {
83
+ case '_createSnapshot':
84
+ // FIXME should be deprecated too?
85
+ return createSnapshot;
86
+ case 'adapterError':
87
+ // FIXME should be deprecated too?
88
+ return record.currentState.adapterError;
89
+ case 'belongsTo':
90
+ return belongsTo;
91
+ case 'changedAttributes':
92
+ return changedAttributes;
93
+ case 'constructor':
94
+ return state._constructor = state._constructor || {
95
+ isModel: true,
96
+ name: `Record<${recordIdentifierFor(record).type}>`,
97
+ modelName: recordIdentifierFor(record).type
98
+ };
99
+ case 'currentState':
100
+ return state.recordState = state.recordState || new RecordState(record);
101
+ case 'deleteRecord':
102
+ return deleteRecord;
103
+ case 'destroyRecord':
104
+ return suppressDeprecation ? _destroyRecord : destroyRecord;
105
+ case 'dirtyType':
106
+ return record.currentState.dirtyType;
107
+ case 'errors':
108
+ // FIXME should be deprecated too?
109
+ // @ts-expect-error
110
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
111
+ return state.errors = state.errors || Errors.create({
112
+ __record: record
113
+ });
114
+ case 'hasDirtyAttributes':
115
+ return record.currentState.isDirty;
116
+ case 'hasMany':
117
+ return hasMany;
118
+ case 'isDeleted':
119
+ return record.currentState.isDeleted;
120
+ case 'isEmpty':
121
+ return record.currentState.isEmpty;
122
+ case 'isError':
123
+ return record.currentState.isError;
124
+ case 'isLoaded':
125
+ return record.currentState.isLoaded;
126
+ case 'isLoading':
127
+ return record.currentState.isLoading;
128
+ case 'isNew':
129
+ return record.currentState.isNew;
130
+ case 'isSaving':
131
+ return record.currentState.isSaving;
132
+ case 'isValid':
133
+ return record.currentState.isValid;
134
+ case 'reload':
135
+ return suppressDeprecation ? _reload : reload;
136
+ case 'rollbackAttributes':
137
+ return rollbackAttributes;
138
+ case 'save':
139
+ return suppressDeprecation ? _save : save;
140
+ case 'serialize':
141
+ // FIXME should be deprecated too? (is somewhat deprecated via store.serializeRecord)
142
+ return serialize;
143
+ case 'unloadRecord':
144
+ return unloadRecord;
145
+ default:
146
+ (test => {
147
+ {
148
+ throw new Error(`${prop} is not a supported legacy field`);
149
+ }
150
+ })();
151
+ }
152
+ }
153
+ legacySupport[Type] = '@legacy';
154
+
155
+ /**
156
+ * A function which adds the necessary fields to a schema and marks it as
157
+ * being in LegacyMode. This is used to support the legacy features of
158
+ * @warp-drive/legacy/model while migrating to WarpDrive.
159
+ *
160
+ * Example:
161
+ *
162
+ * ```ts
163
+ * import { withDefaults, WithLegacy } from '@warp-drive/legacy/model/migration-support';
164
+ * import { Type } from '@warp-drive/core/types/symbols';
165
+ * import type { HasMany } from '@warp-drive/legacy/model';
166
+ *
167
+ * export const UserSchema = withDefaults({
168
+ * type: 'user',
169
+ * fields: [
170
+ * { name: 'firstName', kind: 'attribute' },
171
+ * { name: 'lastName', kind: 'attribute' },
172
+ * { name: 'age', kind: 'attribute' },
173
+ * { name: 'friends',
174
+ * kind: 'hasMany',
175
+ * type: 'user',
176
+ * options: { inverse: 'friends', async: false }
177
+ * },
178
+ * { name: 'bestFriend',
179
+ * kind: 'belongsTo',
180
+ * type: 'user',
181
+ * options: { inverse: null, async: false }
182
+ * },
183
+ * ],
184
+ * });
185
+ *
186
+ * export type User = WithLegacy<{
187
+ * firstName: string;
188
+ * lastName: string;
189
+ * age: number;
190
+ * friends: HasMany<User>;
191
+ * bestFriend: User | null;
192
+ * [Type]: 'user';
193
+ * }>
194
+ * ```
195
+ *
196
+ * Using this function require registering the derivations
197
+ * it requires with the schema service.
198
+ *
199
+ * ```ts
200
+ * import { registerDerivations } from '@warp-drive/legacy/model/migration-support';
201
+ *
202
+ * registerDerivations(schema);
203
+ * ```
204
+ *
205
+ * @param schema The schema to add legacy support to.
206
+ * @return The schema with legacy support added.
207
+ * @public
208
+ */
209
+ function withDefaults(schema) {
210
+ schema.legacy = true;
211
+ schema.identity = {
212
+ kind: '@id',
213
+ name: 'id'
214
+ };
215
+ LegacyFields.forEach(field => {
216
+ schema.fields.push({
217
+ type: '@legacy',
218
+ name: field,
219
+ kind: 'derived'
220
+ });
221
+ });
222
+ schema.fields.push({
223
+ name: '_isReloading',
224
+ kind: '@local',
225
+ type: 'boolean',
226
+ options: {
227
+ defaultValue: false
228
+ }
229
+ });
230
+ schema.fields.push({
231
+ name: 'isDestroying',
232
+ kind: '@local',
233
+ type: 'boolean',
234
+ options: {
235
+ defaultValue: false
236
+ }
237
+ });
238
+ schema.fields.push({
239
+ name: 'isDestroyed',
240
+ kind: '@local',
241
+ type: 'boolean',
242
+ options: {
243
+ defaultValue: false
244
+ }
245
+ });
246
+ schema.objectExtensions = schema.objectExtensions || [];
247
+ schema.objectExtensions.push('deprecated-model-behaviors');
248
+ return schema;
249
+ }
250
+
251
+ /**
252
+ * Adds the necessasary fields to the schema for supporting
253
+ * the deprecated request methods on LegacyMode schemas.
254
+ *
255
+ * Use this instead of `withDefaults` to add the fields
256
+ * and behaviors necessary to support Model-Like capabilities.
257
+ *
258
+ * ```ts
259
+ * import { withRestoredDeprecatedModelRequestBehaviors } from '@warp-drive/legacy/model/migration-support';
260
+ *
261
+ * export const UserSchema = withRestoredDeprecatedModelRequestBehaviors({
262
+ * type: 'user',
263
+ * fields: [
264
+ * { name: 'firstName', kind: 'attribute' },
265
+ * { name: 'lastName', kind: 'attribute' },
266
+ * ]
267
+ * });
268
+ * ```
269
+ */
270
+ function withRestoredDeprecatedModelRequestBehaviors(schema) {
271
+ schema.legacy = true;
272
+ schema.identity = {
273
+ kind: '@id',
274
+ name: 'id'
275
+ };
276
+ LegacyFields.forEach(field => {
277
+ schema.fields.push({
278
+ type: '@legacy',
279
+ name: field,
280
+ kind: 'derived',
281
+ options: {
282
+ suppressDeprecation: true
283
+ }
284
+ });
285
+ });
286
+ schema.fields.push({
287
+ name: 'isReloading',
288
+ kind: '@local',
289
+ type: 'boolean',
290
+ options: {
291
+ defaultValue: false
292
+ }
293
+ });
294
+ schema.fields.push({
295
+ name: 'isDestroying',
296
+ kind: '@local',
297
+ type: 'boolean',
298
+ options: {
299
+ defaultValue: false
300
+ }
301
+ });
302
+ schema.fields.push({
303
+ name: 'isDestroyed',
304
+ kind: '@local',
305
+ type: 'boolean',
306
+ options: {
307
+ defaultValue: false
308
+ }
309
+ });
310
+ return schema;
311
+ }
312
+
313
+ /**
314
+ * A function which registers the necessary derivations to support
315
+ * the LegacyMode features of @warp-drive/legacy/model while migrating to WarpDrive.
316
+ *
317
+ * This must be called in order to use the fields added by {@link withDefaults} or
318
+ * {@link withRestoredDeprecatedModelRequestBehaviors}.
319
+ *
320
+ * @param schema The schema service to register the derivations with.
321
+ * @public
322
+ */
323
+ function registerDerivations(schema) {
324
+ schema.registerDerivation(legacySupport);
325
+ // @ts-expect-error
326
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
327
+ schema._registerMode('@legacy', {
328
+ belongsTo: {
329
+ get(store, record, cacheKey, field) {
330
+ // FIXME field.name here should likely be field.sourceKey || field.name
331
+ return lookupLegacySupport(record).getBelongsTo(field.name);
332
+ },
333
+ set(store, record, cacheKey, field, value) {
334
+ assertPrivateStore(store);
335
+ store._join(() => {
336
+ // FIXME field.name here should likely be field.sourceKey || field.name
337
+ lookupLegacySupport(record).setDirtyBelongsTo(field.name, value);
338
+ });
339
+ }
340
+ },
341
+ hasMany: {
342
+ get(store, record, cacheKey, field) {
343
+ // FIXME field.name here should likely be field.sourceKey || field.name
344
+ return lookupLegacySupport(record).getHasMany(field.name);
345
+ },
346
+ set(store, record, cacheKey, field, value) {
347
+ assertPrivateStore(store);
348
+ store._join(() => {
349
+ const support = lookupLegacySupport(record);
350
+ // FIXME field.name here should likely be field.sourceKey || field.name
351
+ const manyArray = support.getManyArray(field.name);
352
+ manyArray.splice(0, manyArray.length, ...value);
353
+ });
354
+ },
355
+ notify(store, record, cacheKey, field) {
356
+ const support = lookupLegacySupport(record);
357
+ // FIXME field.name here should likely be field.sourceKey || field.name
358
+ const manyArray = support && support._manyArrayCache[field.name];
359
+ const hasPromise = support && support._relationshipPromisesCache[field.name];
360
+ if (manyArray && hasPromise) {
361
+ // do nothing, we will notify the ManyArray directly
362
+ // once the fetch has completed.
363
+ return false;
364
+ }
365
+ if (manyArray) {
366
+ notifyInternalSignal(manyArray[Context].signal);
367
+ return true;
368
+ }
369
+ return false;
370
+ }
371
+ }
372
+ });
373
+ schema.CAUTION_MEGA_DANGER_ZONE_registerExtension({
374
+ name: 'deprecated-model-behaviors',
375
+ kind: 'object',
376
+ features: {
377
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
378
+ get isReloading() {
379
+ console.warn(`record.isReloading is deprecated, please use store.request and either <Request> or getRequuestState to keep track of the request state instead.`); // @ts-expect-error
380
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
381
+ return this._isReloading;
382
+ },
383
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
384
+ set isReloading(v) {
385
+ // @ts-expect-error
386
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
387
+ this._isReloading = v;
388
+ }
389
+ }
390
+ });
391
+ }
392
+
393
+ /**
394
+ * A class which provides a schema service that delegates between
395
+ * a primary schema service and one that supports legacy model
396
+ * classes as its schema source.
397
+ *
398
+ * When the primary schema service has a schema for the given
399
+ * resource, it will be used. Otherwise, the fallback schema
400
+ * service will be used.
401
+ *
402
+ * This can be used when incrementally migrating from Models to
403
+ * ReactiveResources by enabling unmigrated Models to continue to
404
+ * provide their own schema information to the application.
405
+ *
406
+ * ```ts
407
+ * import { DelegatingSchemaService } from '@warp-drive/legacy/model/migration-support';
408
+ * import { SchemaService } from '@warp-drive/core/reactive';
409
+ *
410
+ * class AppStore extends Store {
411
+ * createSchemaService() {
412
+ * const schema = new SchemaService();
413
+ * return new DelegatingSchemaService(this, schema);
414
+ * }
415
+ * }
416
+ * ```
417
+ *
418
+ * All calls to register resources, derivations, transformations, hash functions
419
+ * etc. will be delegated to the primary schema service.
420
+ *
421
+ * @class DelegatingSchemaService
422
+ * @public
423
+ */
424
+
425
+ class DelegatingSchemaService {
426
+ /** @internal */
427
+ _preferred;
428
+ /** @internal */
429
+ _secondary;
430
+ constructor(store, schema) {
431
+ this._preferred = schema;
432
+ this._secondary = buildSchema(store);
433
+ }
434
+ isDelegated(resource) {
435
+ return !this._preferred.hasResource(resource) && this._secondary.hasResource(resource);
436
+ }
437
+ resourceTypes() {
438
+ return Array.from(new Set(this._preferred.resourceTypes().concat(this._secondary.resourceTypes())));
439
+ }
440
+ hasResource(resource) {
441
+ return this._preferred.hasResource(resource) || this._secondary.hasResource(resource);
442
+ }
443
+ hasTrait(type) {
444
+ if (this._preferred.hasResource({
445
+ type
446
+ })) {
447
+ return this._preferred.hasTrait(type);
448
+ }
449
+ return this._secondary.hasTrait(type);
450
+ }
451
+ resourceHasTrait(resource, trait) {
452
+ if (this._preferred.hasResource(resource)) {
453
+ return this._preferred.resourceHasTrait(resource, trait);
454
+ }
455
+ return this._secondary.resourceHasTrait(resource, trait);
456
+ }
457
+ fields(resource) {
458
+ if (this._preferred.hasResource(resource)) {
459
+ return this._preferred.fields(resource);
460
+ }
461
+ return this._secondary.fields(resource);
462
+ }
463
+ cacheFields(resource) {
464
+ if (this._preferred.cacheFields?.(resource)) {
465
+ return this._preferred.cacheFields(resource);
466
+ }
467
+
468
+ // @ts-expect-error
469
+ return this._secondary.cacheFields?.(resource);
470
+ }
471
+ transformation(field) {
472
+ return this._preferred.transformation(field);
473
+ }
474
+ hashFn(field) {
475
+ return this._preferred.hashFn(field);
476
+ }
477
+ derivation(field) {
478
+ return this._preferred.derivation(field);
479
+ }
480
+ resource(resource) {
481
+ if (this._preferred.hasResource(resource)) {
482
+ return this._preferred.resource(resource);
483
+ }
484
+ return this._secondary.resource(resource);
485
+ }
486
+ registerResources(schemas) {
487
+ this._preferred.registerResources(schemas);
488
+ }
489
+ registerResource(schema) {
490
+ this._preferred.registerResource(schema);
491
+ }
492
+ registerTransformation(transform) {
493
+ this._preferred.registerTransformation(transform);
494
+ }
495
+ registerDerivation(derivation) {
496
+ this._preferred.registerDerivation(derivation);
497
+ }
498
+ registerHashFn(hashFn) {
499
+ this._preferred.registerHashFn(hashFn);
500
+ }
501
+ CAUTION_MEGA_DANGER_ZONE_hasExtension(ext) {
502
+ return this._preferred.CAUTION_MEGA_DANGER_ZONE_hasExtension(ext);
503
+ }
504
+ CAUTION_MEGA_DANGER_ZONE_registerExtension(extension) {
505
+ this._preferred.CAUTION_MEGA_DANGER_ZONE_registerExtension(extension);
506
+ }
507
+ CAUTION_MEGA_DANGER_ZONE_resourceExtensions(resource) {
508
+ return this._preferred.CAUTION_MEGA_DANGER_ZONE_resourceExtensions(resource);
509
+ }
510
+ CAUTION_MEGA_DANGER_ZONE_objectExtensions(field, resolvedType) {
511
+ return this._preferred.CAUTION_MEGA_DANGER_ZONE_objectExtensions(field, resolvedType);
512
+ }
513
+ CAUTION_MEGA_DANGER_ZONE_arrayExtensions(field) {
514
+ return this._preferred.CAUTION_MEGA_DANGER_ZONE_arrayExtensions(field);
515
+ }
516
+
517
+ /**
518
+ * This is an internal method used to register behaviors for legacy mode.
519
+ * It is not intended for public use.
520
+ *
521
+ * We do think a generalized `kind` registration system would be useful,
522
+ * but we have not yet designed it.
523
+ *
524
+ * See https://github.com/warp-drive-data/warp-drive/issues/9534
525
+ *
526
+ * @internal
527
+ */
528
+ _registerMode(mode, kinds) {
529
+ // @ts-expect-error
530
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
531
+ this._preferred._registerMode(mode, kinds);
532
+ }
533
+
534
+ /**
535
+ * This is an internal method used to enable legacy behaviors for legacy mode.
536
+ * It is not intended for public use.
537
+ *
538
+ * We do think a generalized `kind` registration system would be useful,
539
+ * but we have not yet designed it.
540
+ *
541
+ * See https://github.com/warp-drive-data/warp-drive/issues/9534
542
+ *
543
+ * @internal
544
+ */
545
+ _kind(mode, kind) {
546
+ // @ts-expect-error
547
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-call
548
+ return this._preferred._kind(mode, kind);
549
+ }
550
+ }
551
+ export { DelegatingSchemaService, registerDerivations, withDefaults, withRestoredDeprecatedModelRequestBehaviors };