@warp-drive-mirror/schema-record 5.6.0-beta.0 → 5.6.0-beta.2

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 (29) hide show
  1. package/README.md +2 -2
  2. package/addon-main.cjs +1 -1
  3. package/dist/-private.js +1 -1
  4. package/dist/index.js +1 -1777
  5. package/package.json +5 -27
  6. package/unstable-preview-types/-private.d.ts +3 -3
  7. package/unstable-preview-types/index.d.ts +340 -352
  8. package/dist/-private.js.map +0 -1
  9. package/dist/index.js.map +0 -1
  10. package/dist/symbols-B_60yPO2.js +0 -44
  11. package/dist/symbols-B_60yPO2.js.map +0 -1
  12. package/unstable-preview-types/-private/fields/compute.d.ts +0 -43
  13. package/unstable-preview-types/-private/fields/compute.d.ts.map +0 -1
  14. package/unstable-preview-types/-private/fields/managed-array.d.ts +0 -25
  15. package/unstable-preview-types/-private/fields/managed-array.d.ts.map +0 -1
  16. package/unstable-preview-types/-private/fields/managed-object.d.ts +0 -23
  17. package/unstable-preview-types/-private/fields/managed-object.d.ts.map +0 -1
  18. package/unstable-preview-types/-private/fields/many-array-manager.d.ts +0 -22
  19. package/unstable-preview-types/-private/fields/many-array-manager.d.ts.map +0 -1
  20. package/unstable-preview-types/-private/hooks.d.ts +0 -8
  21. package/unstable-preview-types/-private/hooks.d.ts.map +0 -1
  22. package/unstable-preview-types/-private/record.d.ts +0 -25
  23. package/unstable-preview-types/-private/record.d.ts.map +0 -1
  24. package/unstable-preview-types/-private/schema.d.ts +0 -148
  25. package/unstable-preview-types/-private/schema.d.ts.map +0 -1
  26. package/unstable-preview-types/-private/symbols.d.ts +0 -13
  27. package/unstable-preview-types/-private/symbols.d.ts.map +0 -1
  28. package/unstable-preview-types/-private.d.ts.map +0 -1
  29. package/unstable-preview-types/index.d.ts.map +0 -1
package/dist/index.js CHANGED
@@ -1,1777 +1 @@
1
- import { isResourceSchema } from '@warp-drive-mirror/core-types/schema/fields';
2
- import { macroCondition, getGlobalConfig, dependencySatisfies, importSync } from '@embroider/macros';
3
- import { withSignalStore, entangleSignal, ARRAY_SIGNAL, consumeInternalSignal, OBJECT_SIGNAL, SOURCE as SOURCE$1, fastPush, defineSignal, RelatedCollection, getOrCreateInternalSignal, notifyInternalSignal, Signals, setRecordIdentifier, recordIdentifierFor, createMemo } from '@ember-data-mirror/store/-private';
4
- import { EnableHydration, STRUCTURED } from '@warp-drive-mirror/core-types/request';
5
- import { RecordStore, Type } from '@warp-drive-mirror/core-types/symbols';
6
- import { getOrSetGlobal } from '@warp-drive-mirror/core-types/-private';
7
- import { S as SOURCE, E as Editable, L as Legacy, I as Identifier, P as Parent, a as EmbeddedPath, D as Destroy, C as Checkout, b as EmbeddedType } from "./symbols-B_60yPO2.js";
8
- import { deprecate } from '@ember/debug';
9
- import { recordIdentifierFor as recordIdentifierFor$1 } from '@ember-data-mirror/store';
10
- const ARRAY_GETTER_METHODS = new Set([Symbol.iterator, 'concat', 'entries', 'every', 'fill', 'filter', 'find', 'findIndex', 'flat', 'flatMap', 'forEach', 'includes', 'indexOf', 'join', 'keys', 'lastIndexOf', 'map', 'reduce', 'reduceRight', 'slice', 'some', 'values']);
11
- // const ARRAY_SETTER_METHODS = new Set<KeyType>(['push', 'pop', 'unshift', 'shift', 'splice', 'sort']);
12
- const SYNC_PROPS = new Set(['[]', 'length']);
13
- function isArrayGetter(prop) {
14
- return ARRAY_GETTER_METHODS.has(prop);
15
- }
16
- const ARRAY_SETTER_METHODS = new Set(['push', 'pop', 'unshift', 'shift', 'splice', 'sort']);
17
- function isArraySetter(prop) {
18
- return ARRAY_SETTER_METHODS.has(prop);
19
- }
20
-
21
- // function isSelfProp<T extends object>(self: T, prop: KeyType): prop is keyof T {
22
- // return prop in self;
23
- // }
24
-
25
- function convertToInt(prop) {
26
- if (typeof prop === 'symbol') return null;
27
- const num = Number(prop);
28
- if (isNaN(num)) return null;
29
- return num % 1 === 0 ? num : null;
30
- }
31
- function safeForEach(instance, arr, store, callback, target) {
32
- if (target === undefined) {
33
- target = null;
34
- }
35
- // clone to prevent mutation
36
- arr = arr.slice();
37
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
38
- if (!test) {
39
- throw new Error('`forEach` expects a function as first argument.');
40
- }
41
- })(typeof callback === 'function') : {};
42
-
43
- // because we retrieveLatest above we need not worry if array is mutated during iteration
44
- // by unloadRecord/rollbackAttributes
45
- // push/add/removeObject may still be problematic
46
- // but this is a more traditionally expected forEach bug.
47
- const length = arr.length; // we need to access length to ensure we are consumed
48
-
49
- for (let index = 0; index < length; index++) {
50
- callback.call(target, arr[index], index, instance);
51
- }
52
- return instance;
53
- }
54
- class ManagedArray {
55
- [SOURCE];
56
- constructor(store, schema, cache, field, data, identifier, path, owner, isSchemaArray, editable, legacy) {
57
- // eslint-disable-next-line @typescript-eslint/no-this-alias
58
- const self = this;
59
- this[SOURCE] = data?.slice();
60
- const IS_EDITABLE = this[Editable] = editable ?? false;
61
- this[Legacy] = legacy;
62
-
63
- // FIXME probably can get rid of the manual ARRAY_SIGNAL storage
64
- // FIXME probably the storage should be on the proxy/receiver not this class
65
- const signals = withSignalStore(this);
66
- const _SIGNAL = this[ARRAY_SIGNAL] = entangleSignal(signals, this, ARRAY_SIGNAL, undefined);
67
- const boundFns = new Map();
68
- this.identifier = identifier;
69
- this.path = path;
70
- this.owner = owner;
71
- let transaction = false;
72
- const mode = field.options?.key ?? '@identity';
73
- const RefStorage = mode === '@identity' ? WeakMap :
74
- // CAUTION CAUTION CAUTION
75
- // this is a pile of lies
76
- // the Map is Map<string, WeakRef<SchemaRecord>>
77
- // but TS does not understand how to juggle modes like this
78
- // internal to a method like ours without us duplicating the code
79
- // into two separate methods.
80
- Map;
81
- const ManagedRecordRefs = isSchemaArray ? new RefStorage() : null;
82
- const proxy = new Proxy(this[SOURCE], {
83
- get(target, prop, receiver) {
84
- if (prop === ARRAY_SIGNAL) {
85
- return _SIGNAL;
86
- }
87
- if (prop === 'identifier') {
88
- return self.identifier;
89
- }
90
- if (prop === 'owner') {
91
- return self.owner;
92
- }
93
- const index = convertToInt(prop);
94
- if (_SIGNAL.isStale && (index !== null || SYNC_PROPS.has(prop) || isArrayGetter(prop))) {
95
- _SIGNAL.isStale = false;
96
- const newData = cache.getAttr(identifier, path);
97
- if (newData && newData !== self[SOURCE]) {
98
- self[SOURCE].length = 0;
99
- self[SOURCE].push(...newData);
100
- }
101
- }
102
- if (index !== null) {
103
- let val;
104
- if (mode === '@hash') {
105
- val = target[index];
106
- const hashField = schema.resource({
107
- type: field.type
108
- }).identity;
109
- const hashFn = schema.hashFn(hashField);
110
- val = hashFn(val, null, null);
111
- } else {
112
- // if mode is not @identity or @index, then access the key path.
113
- // we should assert that `mode` is a string
114
- // it should read directly from the cache value for that field (e.g. no derivation, no transformation)
115
- // and, we likely should lookup the associated field and throw an error IF
116
- // the given field does not exist OR
117
- // the field is anything other than a GenericField or LegacyAttributeField.
118
- if (mode !== '@identity' && mode !== '@index') {
119
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
120
- if (!test) {
121
- throw new Error('mode must be a string');
122
- }
123
- })(typeof mode === 'string') : {};
124
- const modeField = schema.resource({
125
- type: field.type
126
- }).fields.find(f => f.name === mode);
127
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
128
- if (!test) {
129
- throw new Error('field must exist in schema');
130
- }
131
- })(modeField) : {};
132
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
133
- if (!test) {
134
- throw new Error('field must be a GenericField or LegacyAttributeField');
135
- }
136
- })(modeField.kind === 'field' || modeField.kind === 'attribute') : {};
137
- }
138
- val = mode === '@identity' ? target[index] : mode === '@index' ? '@index' : target[index][mode];
139
- }
140
- if (isSchemaArray) {
141
- if (!transaction) {
142
- consumeInternalSignal(_SIGNAL);
143
- }
144
- if (val) {
145
- const recordRef = ManagedRecordRefs.get(val);
146
- let record = recordRef?.deref();
147
- if (!record) {
148
- const recordPath = path.slice();
149
- // this is a dirty lie since path is string[] but really we
150
- // should change the types for paths to `Array<string | number>`
151
- // TODO we should allow the schema for the field to define a "key"
152
- // for stability. Default should be `@identity` which means that
153
- // same object reference from cache should result in same SchemaRecord
154
- // embedded object.
155
- recordPath.push(index);
156
- const recordIdentifier = self.owner[Identifier] || self.owner[Parent];
157
- record = new SchemaRecord(store, recordIdentifier, {
158
- [Editable]: self.owner[Editable],
159
- [Legacy]: self.owner[Legacy]
160
- }, true, field.type, recordPath);
161
- // if mode is not @identity or @index, then access the key path now
162
- // to determine the key value.
163
- // chris says we can implement this as a special kind `@hash` which
164
- // would be a function that only has access to the cache value and not
165
- // the record itself, so derivation is possible but intentionally limited
166
- // and non-reactive?
167
- ManagedRecordRefs.set(val, new WeakRef(record));
168
- }
169
- return record;
170
- }
171
- return val;
172
- }
173
- if (!transaction) {
174
- consumeInternalSignal(_SIGNAL);
175
- }
176
- if (field.type) {
177
- const transform = schema.transformation(field);
178
- return transform.hydrate(val, field.options ?? null, self.owner);
179
- }
180
- return val;
181
- }
182
- if (isArrayGetter(prop)) {
183
- let fn = boundFns.get(prop);
184
- if (fn === undefined) {
185
- if (prop === 'forEach') {
186
- fn = function () {
187
- consumeInternalSignal(_SIGNAL);
188
- transaction = true;
189
- const result = safeForEach(receiver, target, store, arguments[0], arguments[1]);
190
- transaction = false;
191
- return result;
192
- };
193
- } else {
194
- fn = function () {
195
- consumeInternalSignal(_SIGNAL);
196
- // array functions must run through Reflect to work properly
197
- // binding via other means will not work.
198
- transaction = true;
199
- const result = Reflect.apply(target[prop], receiver, arguments);
200
- transaction = false;
201
- return result;
202
- };
203
- }
204
- boundFns.set(prop, fn);
205
- }
206
- return fn;
207
- }
208
- if (isArraySetter(prop)) {
209
- let fn = boundFns.get(prop);
210
- if (fn === undefined) {
211
- fn = function () {
212
- if (!IS_EDITABLE) {
213
- throw new Error(`Mutating this array via ${String(prop)} is not allowed because the record is not editable`);
214
- }
215
- consumeInternalSignal(_SIGNAL);
216
- transaction = true;
217
- const result = Reflect.apply(target[prop], receiver, arguments);
218
- transaction = false;
219
- return result;
220
- };
221
- boundFns.set(prop, fn);
222
- }
223
- return fn;
224
- }
225
- return Reflect.get(target, prop, receiver);
226
- },
227
- set(target, prop, value, receiver) {
228
- if (!IS_EDITABLE) {
229
- let errorPath = identifier.type;
230
- if (path) {
231
- errorPath = path[path.length - 1];
232
- }
233
- throw new Error(`Cannot set ${String(prop)} on ${errorPath} because the record is not editable`);
234
- }
235
- if (prop === 'identifier') {
236
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
237
- self.identifier = value;
238
- return true;
239
- }
240
- if (prop === 'owner') {
241
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
242
- self.owner = value;
243
- return true;
244
- }
245
- const reflect = Reflect.set(target, prop, value, receiver);
246
- if (reflect) {
247
- if (!field.type) {
248
- cache.setAttr(identifier, path, self[SOURCE]);
249
- _SIGNAL.isStale = true;
250
- return true;
251
- }
252
- let rawValue = self[SOURCE];
253
- if (!isSchemaArray) {
254
- const transform = schema.transformation(field);
255
- if (!transform) {
256
- throw new Error(`No '${field.type}' transform defined for use by ${identifier.type}.${String(prop)}`);
257
- }
258
- rawValue = self[SOURCE].map(item => transform.serialize(item, field.options ?? null, self.owner));
259
- }
260
- cache.setAttr(identifier, path, rawValue);
261
- _SIGNAL.isStale = true;
262
- }
263
- return reflect;
264
- },
265
- has(target, prop) {
266
- if (prop === 'identifier' || prop === 'owner' || prop === ARRAY_SIGNAL) {
267
- return true;
268
- }
269
- return Reflect.has(target, prop);
270
- }
271
- });
272
- return proxy;
273
- }
274
- }
275
- const ObjectSymbols = new Set([OBJECT_SIGNAL, Parent, SOURCE, Editable, EmbeddedPath]);
276
-
277
- // const ignoredGlobalFields = new Set<string>(['setInterval', 'nodeType', 'nodeName', 'length', 'document', STRUCTURED]);
278
-
279
- class ManagedObject {
280
- constructor(schema, cache, field, data, identifier, path, owner, editable, legacy) {
281
- // eslint-disable-next-line @typescript-eslint/no-this-alias
282
- const self = this;
283
- this[SOURCE] = {
284
- ...data
285
- };
286
- const signals = withSignalStore(this);
287
- const _SIGNAL = this[OBJECT_SIGNAL] = entangleSignal(signals, this, OBJECT_SIGNAL, undefined);
288
- this[Editable] = editable;
289
- this[Legacy] = legacy;
290
- this[Parent] = identifier;
291
- this[EmbeddedPath] = path;
292
- const proxy = new Proxy(this[SOURCE], {
293
- ownKeys() {
294
- return Object.keys(self[SOURCE]);
295
- },
296
- has(target, prop) {
297
- return prop in self[SOURCE];
298
- },
299
- getOwnPropertyDescriptor(target, prop) {
300
- return {
301
- writable: editable,
302
- enumerable: true,
303
- configurable: true
304
- };
305
- },
306
- get(target, prop, receiver) {
307
- if (ObjectSymbols.has(prop)) {
308
- return self[prop];
309
- }
310
- if (prop === Symbol.toPrimitive) {
311
- return () => null;
312
- }
313
- if (prop === Symbol.toStringTag) {
314
- return `ManagedObject<${identifier.type}:${identifier.id} (${identifier.lid})>`;
315
- }
316
- if (prop === 'constructor') {
317
- return Object;
318
- }
319
- if (prop === 'toString') {
320
- return function () {
321
- return `ManagedObject<${identifier.type}:${identifier.id} (${identifier.lid})>`;
322
- };
323
- }
324
- if (prop === 'toHTML') {
325
- return function () {
326
- return '<span>ManagedObject</span>';
327
- };
328
- }
329
- if (prop === 'toJSON') {
330
- return function () {
331
- return structuredClone(self[SOURCE]);
332
- };
333
- }
334
- if (_SIGNAL.isStale) {
335
- _SIGNAL.isStale = false;
336
- let newData = cache.getAttr(identifier, path);
337
- if (newData && newData !== self[SOURCE]) {
338
- if (field.type) {
339
- const transform = schema.transformation(field);
340
- newData = transform.hydrate(newData, field.options ?? null, owner);
341
- }
342
- self[SOURCE] = {
343
- ...newData
344
- }; // Add type assertion for newData
345
- }
346
- }
347
- if (prop in self[SOURCE]) {
348
- consumeInternalSignal(_SIGNAL);
349
- return self[SOURCE][prop];
350
- }
351
- return Reflect.get(target, prop, receiver);
352
- },
353
- set(target, prop, value, receiver) {
354
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
355
- if (!test) {
356
- throw new Error(`Cannot set read-only property '${String(prop)}' on ManagedObject`);
357
- }
358
- })(editable) : {};
359
- const reflect = Reflect.set(target, prop, value, receiver);
360
- if (!reflect) {
361
- return false;
362
- }
363
- if (!field.type) {
364
- cache.setAttr(identifier, path, self[SOURCE]);
365
- } else {
366
- const transform = schema.transformation(field);
367
- const val = transform.serialize(self[SOURCE], field.options ?? null, owner);
368
- cache.setAttr(identifier, path, val);
369
- }
370
- _SIGNAL.isStale = true;
371
- return true;
372
- }
373
- });
374
- return proxy;
375
- }
376
- }
377
- class ManyArrayManager {
378
- constructor(record, editable) {
379
- this.record = record;
380
- this.store = record[RecordStore];
381
- this.identifier = record[Identifier];
382
- this.editable = editable;
383
- }
384
- _syncArray(array) {
385
- const method = this.editable ? 'getRelationship' : 'getRemoteRelationship';
386
- const rawValue = this.store.cache[method](this.identifier, array.key);
387
- if (rawValue.meta) {
388
- array.meta = rawValue.meta;
389
- }
390
- if (rawValue.links) {
391
- array.links = rawValue.links;
392
- }
393
- const currentState = array[SOURCE$1];
394
-
395
- // unlike in the normal RecordArray case, we don't need to divorce the reference
396
- // because we don't need to worry about associate/disassociate since the graph
397
- // takes care of that for us
398
- if (currentState !== rawValue.data) {
399
- currentState.length = 0;
400
- fastPush(currentState, rawValue.data);
401
- }
402
- }
403
- reloadHasMany(key, options) {
404
- const field = this.store.schema.fields(this.identifier).get(key);
405
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
406
- if (!test) {
407
- throw new Error(`Expected a hasMany field for ${key}`);
408
- }
409
- })(field?.kind === 'hasMany') : {};
410
- const cacheOptions = options ? extractCacheOptions(options) : {
411
- reload: true
412
- };
413
- cacheOptions.types = [field.type];
414
- const rawValue = this.store.cache.getRelationship(this.identifier, key);
415
- const req = {
416
- url: getRelatedLink(rawValue),
417
- op: 'findHasMany',
418
- method: 'GET',
419
- records: rawValue.data,
420
- cacheOptions,
421
- options: {
422
- field,
423
- identifier: this.identifier,
424
- links: rawValue.links,
425
- meta: rawValue.meta
426
- },
427
- [EnableHydration]: false
428
- };
429
- return this.store.request(req);
430
- }
431
- mutate(mutation) {
432
- this.store.cache.mutate(mutation);
433
- }
434
- }
435
- function getRelatedLink(resource) {
436
- const related = resource.links?.related;
437
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
438
- if (!test) {
439
- throw new Error(`Expected a related link`);
440
- }
441
- })(related) : {};
442
- return typeof related === 'object' ? related.href : related;
443
- }
444
- function extractCacheOptions(options) {
445
- const cacheOptions = {};
446
- if ('reload' in options) {
447
- cacheOptions.reload = options.reload;
448
- }
449
- if ('backgroundReload' in options) {
450
- cacheOptions.backgroundReload = options.backgroundReload;
451
- }
452
- return cacheOptions;
453
- }
454
- const ManagedArrayMap = getOrSetGlobal('ManagedArrayMap', new Map());
455
- const ManagedObjectMap = getOrSetGlobal('ManagedObjectMap', new Map());
456
- function computeLocal(record, field, prop) {
457
- const signals = withSignalStore(record);
458
- const signal = getOrCreateInternalSignal(signals, record, prop, field.options?.defaultValue ?? null);
459
- consumeInternalSignal(signal);
460
- return signal.value;
461
- }
462
- function peekManagedArray(record, field) {
463
- const managedArrayMapForRecord = ManagedArrayMap.get(record);
464
- if (managedArrayMapForRecord) {
465
- return managedArrayMapForRecord.get(field.name);
466
- }
467
- }
468
- function peekManagedObject(record, field) {
469
- const managedObjectMapForRecord = ManagedObjectMap.get(record);
470
- if (managedObjectMapForRecord) {
471
- return managedObjectMapForRecord.get(field.name);
472
- }
473
- }
474
- function computeField(schema, cache, record, identifier, field, prop, editable) {
475
- const rawValue = editable ? cache.getAttr(identifier, prop) : cache.getRemoteAttr(identifier, prop);
476
- if (!field.type) {
477
- return rawValue;
478
- }
479
- const transform = schema.transformation(field);
480
- return transform.hydrate(rawValue, field.options ?? null, record);
481
- }
482
- function computeArray(store, schema, cache, record, identifier, field, path, editable, legacy) {
483
- const isSchemaArray = field.kind === 'schema-array';
484
- // the thing we hand out needs to know its owner and path in a private manner
485
- // its "address" is the parent identifier (identifier) + field name (field.name)
486
- // in the nested object case field name here is the full dot path from root resource to this value
487
- // its "key" is the field on the parent record
488
- // its "owner" is the parent record
489
-
490
- const managedArrayMapForRecord = ManagedArrayMap.get(record);
491
- let managedArray;
492
- if (managedArrayMapForRecord) {
493
- managedArray = managedArrayMapForRecord.get(field.name);
494
- }
495
- if (managedArray) {
496
- return managedArray;
497
- } else {
498
- const rawValue = editable ? cache.getAttr(identifier, path) : cache.getRemoteAttr(identifier, path);
499
- if (!rawValue) {
500
- return null;
501
- }
502
- managedArray = new ManagedArray(store, schema, cache, field, rawValue, identifier, path, record, isSchemaArray, editable, legacy);
503
- if (!managedArrayMapForRecord) {
504
- ManagedArrayMap.set(record, new Map([[field.name, managedArray]]));
505
- } else {
506
- managedArrayMapForRecord.set(field.name, managedArray);
507
- }
508
- }
509
- return managedArray;
510
- }
511
- function computeObject(schema, cache, record, identifier, field, path, editable, legacy) {
512
- const managedObjectMapForRecord = ManagedObjectMap.get(record);
513
- let managedObject;
514
- if (managedObjectMapForRecord) {
515
- managedObject = managedObjectMapForRecord.get(field.name);
516
- }
517
- if (managedObject) {
518
- return managedObject;
519
- } else {
520
- let rawValue = editable ? cache.getAttr(identifier, path) : cache.getRemoteAttr(identifier, path);
521
- if (!rawValue) {
522
- return null;
523
- }
524
- if (field.type) {
525
- const transform = schema.transformation(field);
526
- rawValue = transform.hydrate(rawValue, field.options ?? null, record);
527
- }
528
- managedObject = new ManagedObject(schema, cache, field, rawValue, identifier, path, record, editable, legacy);
529
- if (!managedObjectMapForRecord) {
530
- ManagedObjectMap.set(record, new Map([[field.name, managedObject]]));
531
- } else {
532
- managedObjectMapForRecord.set(field.name, managedObject);
533
- }
534
- }
535
- return managedObject;
536
- }
537
- function computeSchemaObject(store, cache, record, identifier, field, path, legacy, editable) {
538
- const schemaObjectMapForRecord = ManagedObjectMap.get(record);
539
- let schemaObject;
540
- if (schemaObjectMapForRecord) {
541
- schemaObject = schemaObjectMapForRecord.get(field.name);
542
- }
543
- if (schemaObject) {
544
- return schemaObject;
545
- } else {
546
- const rawValue = editable ? cache.getAttr(identifier, path) : cache.getRemoteAttr(identifier, path);
547
- if (!rawValue) {
548
- return null;
549
- }
550
- const embeddedPath = path.slice();
551
- schemaObject = new SchemaRecord(store, identifier, {
552
- [Editable]: editable,
553
- [Legacy]: legacy
554
- }, true, field.type, embeddedPath);
555
- }
556
- if (!schemaObjectMapForRecord) {
557
- ManagedObjectMap.set(record, new Map([[field.name, schemaObject]]));
558
- } else {
559
- schemaObjectMapForRecord.set(field.name, schemaObject);
560
- }
561
- return schemaObject;
562
- }
563
- function computeAttribute(cache, identifier, prop, editable) {
564
- return editable ? cache.getAttr(identifier, prop) : cache.getRemoteAttr(identifier, prop);
565
- }
566
- function computeDerivation(schema, record, identifier, field, prop) {
567
- return schema.derivation(field)(record, field.options ?? null, prop);
568
- }
569
-
570
- // TODO probably this should just be a Document
571
- // but its separate until we work out the lid situation
572
- class ResourceRelationship {
573
- constructor(store, cache, parent, identifier, field, name, editable) {
574
- const rawValue = editable ? cache.getRelationship(identifier, name) : cache.getRemoteRelationship(identifier, name);
575
-
576
- // TODO setup true lids for relationship documents
577
- // @ts-expect-error we need to give relationship documents a lid
578
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
579
- this.lid = rawValue.lid ?? rawValue.links?.self ?? `relationship:${identifier.lid}.${name}`;
580
- this.data = rawValue.data ? store.peekRecord(rawValue.data) : null;
581
- this.name = name;
582
- if (macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG)) {
583
- this.links = Object.freeze(Object.assign({}, rawValue.links));
584
- this.meta = Object.freeze(Object.assign({}, rawValue.meta));
585
- } else {
586
- this.links = rawValue.links ?? {};
587
- this.meta = rawValue.meta ?? {};
588
- }
589
- this[RecordStore] = store;
590
- this[Parent] = parent;
591
- }
592
- fetch(options) {
593
- const url = options?.url ?? getHref(this.links.related) ?? getHref(this.links.self) ?? null;
594
- if (!url) {
595
- throw new Error(`Cannot ${options?.method ?? 'fetch'} ${this[Parent][Identifier].type}.${String(this.name)} because it has no related link`);
596
- }
597
- const request = Object.assign({
598
- url,
599
- method: 'GET'
600
- }, options);
601
- return this[RecordStore].request(request);
602
- }
603
- }
604
- defineSignal(ResourceRelationship.prototype, 'data', null);
605
- defineSignal(ResourceRelationship.prototype, 'links', null);
606
- defineSignal(ResourceRelationship.prototype, 'meta', null);
607
- function getHref(link) {
608
- if (!link) {
609
- return null;
610
- }
611
- if (typeof link === 'string') {
612
- return link;
613
- }
614
- return link.href;
615
- }
616
- function computeResource(store, cache, parent, identifier, field, prop, editable) {
617
- if (field.kind !== 'resource') {
618
- throw new Error(`The schema for ${identifier.type}.${String(prop)} is not a resource relationship`);
619
- }
620
- return new ResourceRelationship(store, cache, parent, identifier, field, prop, editable);
621
- }
622
- function computeHasMany(store, schema, cache, record, identifier, field, path, editable, legacy) {
623
- // the thing we hand out needs to know its owner and path in a private manner
624
- // its "address" is the parent identifier (identifier) + field name (field.name)
625
- // in the nested object case field name here is the full dot path from root resource to this value
626
- // its "key" is the field on the parent record
627
- // its "owner" is the parent record
628
-
629
- const managedArrayMapForRecord = ManagedArrayMap.get(record);
630
- let managedArray;
631
- if (managedArrayMapForRecord) {
632
- managedArray = managedArrayMapForRecord.get(field.name);
633
- }
634
- if (managedArray) {
635
- return managedArray;
636
- } else {
637
- const rawValue = cache.getRelationship(identifier, field.name);
638
- if (!rawValue) {
639
- return null;
640
- }
641
- managedArray = new RelatedCollection({
642
- store,
643
- type: field.type,
644
- identifier,
645
- cache,
646
- // we divorce the reference here because ManyArray mutates the target directly
647
- // before sending the mutation op to the cache. We may be able to avoid this in the future
648
- identifiers: rawValue.data?.slice(),
649
- key: field.name,
650
- meta: rawValue.meta || null,
651
- links: rawValue.links || null,
652
- isPolymorphic: field.options.polymorphic ?? false,
653
- isAsync: field.options.async ?? false,
654
- // TODO: Grab the proper value
655
- _inverseIsAsync: false,
656
- // @ts-expect-error Typescript doesn't have a way for us to thread the generic backwards so it infers unknown instead of T
657
- manager: new ManyArrayManager(record, editable),
658
- isLoaded: true,
659
- allowMutation: editable
660
- });
661
- if (!managedArrayMapForRecord) {
662
- ManagedArrayMap.set(record, new Map([[field.name, managedArray]]));
663
- } else {
664
- managedArrayMapForRecord.set(field.name, managedArray);
665
- }
666
- }
667
- return managedArray;
668
- }
669
- const HAS_MODEL_PACKAGE = dependencySatisfies('@ember-data-mirror/model', '*');
670
- const getLegacySupport = macroCondition(dependencySatisfies('@ember-data-mirror/model', '*')) ? importSync('@ember-data-mirror/model/-private').lookupLegacySupport : null;
671
- const IgnoredGlobalFields = new Set(['length', 'nodeType', 'then', 'setInterval', 'document', STRUCTURED]);
672
- const symbolList = [Destroy, RecordStore, Identifier, Editable, Parent, Checkout, Legacy, EmbeddedPath, EmbeddedType];
673
- const RecordSymbols = new Set(symbolList);
674
- function isPathMatch(a, b) {
675
- return a.length === b.length && a.every((v, i) => v === b[i]);
676
- }
677
- function isNonEnumerableProp(prop) {
678
- return prop === 'constructor' || prop === 'prototype' || prop === '__proto__' || prop === 'toString' || prop === 'toJSON' || prop === 'toHTML' || typeof prop === 'symbol';
679
- }
680
- const Editables = new WeakMap();
681
- class SchemaRecord {
682
- constructor(store, identifier, Mode, isEmbedded = false, embeddedType = null, embeddedPath = null) {
683
- // eslint-disable-next-line @typescript-eslint/no-this-alias
684
- const self = this;
685
- this[RecordStore] = store;
686
- if (isEmbedded) {
687
- this[Parent] = identifier;
688
- } else {
689
- this[Identifier] = identifier;
690
- }
691
- const IS_EDITABLE = this[Editable] = Mode[Editable] ?? false;
692
- this[Legacy] = Mode[Legacy] ?? false;
693
- const schema = store.schema;
694
- const cache = store.cache;
695
- const identityField = schema.resource(isEmbedded ? {
696
- type: embeddedType
697
- } : identifier).identity;
698
- const BoundFns = new Map();
699
- this[EmbeddedType] = embeddedType;
700
- this[EmbeddedPath] = embeddedPath;
701
- const fields = isEmbedded ? schema.fields({
702
- type: embeddedType
703
- }) : schema.fields(identifier);
704
- const signals = withSignalStore(this);
705
- const proxy = new Proxy(this, {
706
- ownKeys() {
707
- const identityKey = identityField?.name;
708
- const keys = Array.from(fields.keys());
709
- if (identityKey) {
710
- keys.unshift(identityKey);
711
- }
712
- return keys;
713
- },
714
- has(target, prop) {
715
- if (prop === Destroy || prop === Checkout) {
716
- return true;
717
- }
718
- return fields.has(prop);
719
- },
720
- getOwnPropertyDescriptor(target, prop) {
721
- const schemaForField = prop === identityField?.name ? identityField : fields.get(prop);
722
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
723
- if (!test) {
724
- throw new Error(`No field named ${String(prop)} on ${identifier.type}`);
725
- }
726
- })(schemaForField) : {};
727
- if (isNonEnumerableProp(prop)) {
728
- return {
729
- writable: false,
730
- enumerable: false,
731
- configurable: true
732
- };
733
- }
734
- switch (schemaForField.kind) {
735
- case 'derived':
736
- return {
737
- writable: false,
738
- enumerable: true,
739
- configurable: true
740
- };
741
- case '@id':
742
- return {
743
- writable: identifier.id === null,
744
- enumerable: true,
745
- configurable: true
746
- };
747
- case '@local':
748
- case 'field':
749
- case 'attribute':
750
- case 'resource':
751
- case 'alias':
752
- case 'belongsTo':
753
- case 'hasMany':
754
- case 'collection':
755
- case 'schema-array':
756
- case 'array':
757
- case 'schema-object':
758
- case 'object':
759
- return {
760
- writable: IS_EDITABLE,
761
- enumerable: true,
762
- configurable: true
763
- };
764
- default:
765
- return {
766
- writable: false,
767
- enumerable: false,
768
- configurable: false
769
- };
770
- }
771
- },
772
- get(target, prop, receiver) {
773
- if (RecordSymbols.has(prop)) {
774
- return target[prop];
775
- }
776
- if (prop === Signals) {
777
- return signals;
778
- }
779
-
780
- // TODO make this a symbol
781
- if (prop === '___notifications') {
782
- return target.___notifications;
783
- }
784
-
785
- // SchemaRecord reserves use of keys that begin with these characters
786
- // for its own usage.
787
- // _, @, $, *
788
-
789
- const maybeField = prop === identityField?.name ? identityField : fields.get(prop);
790
- if (!maybeField) {
791
- if (IgnoredGlobalFields.has(prop)) {
792
- return undefined;
793
- }
794
-
795
- /////////////////////////////////////////////////////////////
796
- //// Note these bound function behaviors are essentially ////
797
- //// built-in but overrideable derivations. ////
798
- //// ////
799
- //// The bar for this has to be "basic expectations of ////
800
- /// an object" – very, very high ////
801
- /////////////////////////////////////////////////////////////
802
-
803
- if (prop === Symbol.toStringTag || prop === 'toString') {
804
- let fn = BoundFns.get('toString');
805
- if (!fn) {
806
- fn = function () {
807
- entangleSignal(signals, receiver, '@identity', null);
808
- return `Record<${identifier.type}:${identifier.id} (${identifier.lid})>`;
809
- };
810
- BoundFns.set(prop, fn);
811
- }
812
- return fn;
813
- }
814
- if (prop === 'toHTML') {
815
- let fn = BoundFns.get('toHTML');
816
- if (!fn) {
817
- fn = function () {
818
- entangleSignal(signals, receiver, '@identity', null);
819
- return `<span>Record<${identifier.type}:${identifier.id} (${identifier.lid})></span>`;
820
- };
821
- BoundFns.set(prop, fn);
822
- }
823
- return fn;
824
- }
825
- if (prop === 'toJSON') {
826
- let fn = BoundFns.get('toJSON');
827
- if (!fn) {
828
- fn = function () {
829
- const json = {};
830
- for (const key in receiver) {
831
- json[key] = receiver[key];
832
- }
833
- return json;
834
- };
835
- BoundFns.set(prop, fn);
836
- }
837
- return fn;
838
- }
839
- if (prop === Symbol.toPrimitive) return () => null;
840
- if (prop === Symbol.iterator) {
841
- let fn = BoundFns.get(Symbol.iterator);
842
- if (!fn) {
843
- fn = function* () {
844
- for (const key in receiver) {
845
- yield [key, receiver[key]];
846
- }
847
- };
848
- BoundFns.set(Symbol.iterator, fn);
849
- }
850
- return fn;
851
- }
852
- if (prop === 'constructor') {
853
- return SchemaRecord;
854
- }
855
- // too many things check for random symbols
856
- if (typeof prop === 'symbol') return undefined;
857
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
858
- {
859
- throw new Error(`No field named ${String(prop)} on ${isEmbedded ? embeddedType : identifier.type}`);
860
- }
861
- })() : {};
862
- return undefined;
863
- }
864
- const field = maybeField.kind === 'alias' ? maybeField.options : maybeField;
865
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
866
- if (!test) {
867
- throw new Error(`Alias fields cannot alias '@id' '@local' '@hash' or 'derived' fields`);
868
- }
869
- })(maybeField.kind !== 'alias' || !['@id', '@local', '@hash', 'derived'].includes(maybeField.options.kind)) : {};
870
- const propArray = isEmbedded ? embeddedPath.slice() : [];
871
- // we use the field.name instead of prop here because we want to use the cache-path not
872
- // the record path.
873
- propArray.push(field.name);
874
- // propArray.push(prop as string);
875
-
876
- switch (field.kind) {
877
- case '@id':
878
- entangleSignal(signals, receiver, '@identity', null);
879
- return identifier.id;
880
- case '@hash':
881
- // TODO pass actual cache value not {}
882
- return schema.hashFn(field)({}, field.options ?? null, field.name ?? null);
883
- case '@local':
884
- {
885
- return computeLocal(receiver, field, prop);
886
- }
887
- case 'field':
888
- entangleSignal(signals, receiver, field.name, null);
889
- return computeField(schema, cache, target, identifier, field, propArray, IS_EDITABLE);
890
- case 'attribute':
891
- entangleSignal(signals, receiver, field.name, null);
892
- return computeAttribute(cache, identifier, prop, IS_EDITABLE);
893
- case 'resource':
894
- entangleSignal(signals, receiver, field.name, null);
895
- return computeResource(store, cache, target, identifier, field, prop, IS_EDITABLE);
896
- case 'derived':
897
- return computeDerivation(schema, receiver, identifier, field, prop);
898
- case 'schema-array':
899
- case 'array':
900
- entangleSignal(signals, receiver, field.name, null);
901
- return computeArray(store, schema, cache, target, identifier, field, propArray, Mode[Editable], Mode[Legacy]);
902
- case 'object':
903
- entangleSignal(signals, receiver, field.name, null);
904
- return computeObject(schema, cache, target, identifier, field, propArray, Mode[Editable], Mode[Legacy]);
905
- case 'schema-object':
906
- entangleSignal(signals, receiver, field.name, null);
907
- // run transform, then use that value as the object to manage
908
- return computeSchemaObject(store, cache, target, identifier, field, propArray, Mode[Legacy], Mode[Editable]);
909
- case 'belongsTo':
910
- if (field.options.linksMode) {
911
- entangleSignal(signals, receiver, field.name, null);
912
- const rawValue = IS_EDITABLE ? cache.getRelationship(identifier, field.name) : cache.getRemoteRelationship(identifier, field.name);
913
-
914
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
915
- return rawValue.data ? store.peekRecord(rawValue.data) : null;
916
- }
917
- if (!HAS_MODEL_PACKAGE) {
918
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
919
- {
920
- throw new Error(`Cannot use belongsTo fields in your schema unless @ember-data-mirror/model is installed to provide legacy model support. ${field.name} should likely be migrated to be a resource field.`);
921
- }
922
- })() : {};
923
- }
924
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
925
- if (!test) {
926
- throw new Error(`Expected to have a getLegacySupport function`);
927
- }
928
- })(getLegacySupport) : {};
929
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
930
- if (!test) {
931
- throw new Error(`Can only use belongsTo fields when the resource is in legacy mode`);
932
- }
933
- })(Mode[Legacy]) : {};
934
- entangleSignal(signals, receiver, field.name, null);
935
- return getLegacySupport(receiver).getBelongsTo(field.name);
936
- case 'hasMany':
937
- if (field.options.linksMode) {
938
- entangleSignal(signals, receiver, field.name, null);
939
- return computeHasMany(store, schema, cache, target, identifier, field, propArray, Mode[Editable], Mode[Legacy]);
940
- }
941
- if (!HAS_MODEL_PACKAGE) {
942
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
943
- {
944
- throw new Error(`Cannot use hasMany fields in your schema unless @ember-data-mirror/model is installed to provide legacy model support. ${field.name} should likely be migrated to be a collection field.`);
945
- }
946
- })() : {};
947
- }
948
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
949
- if (!test) {
950
- throw new Error(`Expected to have a getLegacySupport function`);
951
- }
952
- })(getLegacySupport) : {};
953
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
954
- if (!test) {
955
- throw new Error(`Can only use hasMany fields when the resource is in legacy mode`);
956
- }
957
- })(Mode[Legacy]) : {};
958
- entangleSignal(signals, receiver, field.name, null);
959
- return getLegacySupport(receiver).getHasMany(field.name);
960
- default:
961
- throw new Error(`Field '${String(prop)}' on '${identifier.type}' has the unknown kind '${field.kind}'`);
962
- }
963
- },
964
- set(target, prop, value, receiver) {
965
- if (!IS_EDITABLE) {
966
- const type = isEmbedded ? embeddedType : identifier.type;
967
- throw new Error(`Cannot set ${String(prop)} on ${type} because the record is not editable`);
968
- }
969
- const maybeField = prop === identityField?.name ? identityField : fields.get(prop);
970
- if (!maybeField) {
971
- const type = isEmbedded ? embeddedType : identifier.type;
972
- throw new Error(`There is no field named ${String(prop)} on ${type}`);
973
- }
974
- const field = maybeField.kind === 'alias' ? maybeField.options : maybeField;
975
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
976
- if (!test) {
977
- throw new Error(`Alias fields cannot alias '@id' '@local' '@hash' or 'derived' fields`);
978
- }
979
- })(maybeField.kind !== 'alias' || !['@id', '@local', '@hash', 'derived'].includes(maybeField.options.kind)) : {};
980
- const propArray = isEmbedded ? embeddedPath.slice() : [];
981
- // we use the field.name instead of prop here because we want to use the cache-path not
982
- // the record path.
983
- propArray.push(field.name);
984
- // propArray.push(prop as string);
985
-
986
- switch (field.kind) {
987
- case '@id':
988
- {
989
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
990
- if (!test) {
991
- throw new Error(`Expected to receive a string id`);
992
- }
993
- })(typeof value === 'string' && value.length) : {};
994
- const normalizedId = String(value);
995
- const didChange = normalizedId !== identifier.id;
996
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
997
- if (!test) {
998
- throw new Error(`Cannot set ${identifier.type} record's id to ${normalizedId}, because id is already ${identifier.id}`);
999
- }
1000
- })(!didChange || identifier.id === null) : {};
1001
- if (normalizedId !== null && didChange) {
1002
- store._instanceCache.setRecordId(identifier, normalizedId);
1003
- store.notifications.notify(identifier, 'identity');
1004
- }
1005
- return true;
1006
- }
1007
- case '@local':
1008
- {
1009
- const signal = getOrCreateInternalSignal(signals, receiver, prop, field.options?.defaultValue ?? null);
1010
- if (signal.value !== value) {
1011
- signal.value = value;
1012
- notifyInternalSignal(signal);
1013
- }
1014
- return true;
1015
- }
1016
- case 'field':
1017
- {
1018
- if (!field.type) {
1019
- cache.setAttr(identifier, propArray, value);
1020
- return true;
1021
- }
1022
- const transform = schema.transformation(field);
1023
- const rawValue = transform.serialize(value, field.options ?? null, target);
1024
- cache.setAttr(identifier, propArray, rawValue);
1025
- return true;
1026
- }
1027
- case 'attribute':
1028
- {
1029
- cache.setAttr(identifier, propArray, value);
1030
- return true;
1031
- }
1032
- case 'array':
1033
- {
1034
- if (!field.type) {
1035
- cache.setAttr(identifier, propArray, value?.slice());
1036
- const peeked = peekManagedArray(self, field);
1037
- if (peeked) {
1038
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1039
- if (!test) {
1040
- throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
1041
- }
1042
- })(ARRAY_SIGNAL in peeked) : {};
1043
- const arrSignal = peeked[ARRAY_SIGNAL];
1044
- arrSignal.isStale = true;
1045
- }
1046
- if (!Array.isArray(value)) {
1047
- ManagedArrayMap.delete(target);
1048
- }
1049
- return true;
1050
- }
1051
- const transform = schema.transformation(field);
1052
- const rawValue = value.map(item => transform.serialize(item, field.options ?? null, target));
1053
- cache.setAttr(identifier, propArray, rawValue);
1054
- const peeked = peekManagedArray(self, field);
1055
- if (peeked) {
1056
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1057
- if (!test) {
1058
- throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
1059
- }
1060
- })(ARRAY_SIGNAL in peeked) : {};
1061
- const arrSignal = peeked[ARRAY_SIGNAL];
1062
- arrSignal.isStale = true;
1063
- }
1064
- return true;
1065
- }
1066
- case 'schema-array':
1067
- {
1068
- const arrayValue = value?.slice();
1069
- if (!Array.isArray(arrayValue)) {
1070
- ManagedArrayMap.delete(target);
1071
- }
1072
- cache.setAttr(identifier, propArray, arrayValue);
1073
- const peeked = peekManagedArray(self, field);
1074
- if (peeked) {
1075
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1076
- if (!test) {
1077
- throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
1078
- }
1079
- })(ARRAY_SIGNAL in peeked) : {};
1080
- const arrSignal = peeked[ARRAY_SIGNAL];
1081
- arrSignal.isStale = true;
1082
- }
1083
- if (!Array.isArray(value)) {
1084
- ManagedArrayMap.delete(target);
1085
- }
1086
- return true;
1087
- }
1088
- case 'object':
1089
- {
1090
- if (!field.type) {
1091
- let newValue = value;
1092
- if (value !== null) {
1093
- newValue = {
1094
- ...value
1095
- };
1096
- } else {
1097
- ManagedObjectMap.delete(target);
1098
- }
1099
- cache.setAttr(identifier, propArray, newValue);
1100
- const peeked = peekManagedObject(self, field);
1101
- if (peeked) {
1102
- const objSignal = peeked[OBJECT_SIGNAL];
1103
- objSignal.isStale = true;
1104
- }
1105
- return true;
1106
- }
1107
- const transform = schema.transformation(field);
1108
- const rawValue = transform.serialize({
1109
- ...value
1110
- }, field.options ?? null, target);
1111
- cache.setAttr(identifier, propArray, rawValue);
1112
- const peeked = peekManagedObject(self, field);
1113
- if (peeked) {
1114
- const objSignal = peeked[OBJECT_SIGNAL];
1115
- objSignal.isStale = true;
1116
- }
1117
- return true;
1118
- }
1119
- case 'schema-object':
1120
- {
1121
- let newValue = value;
1122
- if (value !== null) {
1123
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1124
- if (!test) {
1125
- throw new Error(`Expected value to be an object`);
1126
- }
1127
- })(typeof value === 'object') : {};
1128
- newValue = {
1129
- ...value
1130
- };
1131
- const schemaFields = schema.fields({
1132
- type: field.type
1133
- });
1134
- for (const key of Object.keys(newValue)) {
1135
- if (!schemaFields.has(key)) {
1136
- throw new Error(`Field ${key} does not exist on schema object ${field.type}`);
1137
- }
1138
- }
1139
- } else {
1140
- ManagedObjectMap.delete(target);
1141
- }
1142
- cache.setAttr(identifier, propArray, newValue);
1143
- // const peeked = peekManagedObject(self, field);
1144
- // if (peeked) {
1145
- // const objSignal = peeked[OBJECT_SIGNAL];
1146
- // objSignal.isStale = true;
1147
- // }
1148
- return true;
1149
- }
1150
- case 'derived':
1151
- {
1152
- throw new Error(`Cannot set ${String(prop)} on ${identifier.type} because it is derived`);
1153
- }
1154
- case 'belongsTo':
1155
- if (!HAS_MODEL_PACKAGE) {
1156
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1157
- {
1158
- throw new Error(`Cannot use belongsTo fields in your schema unless @ember-data-mirror/model is installed to provide legacy model support. ${field.name} should likely be migrated to be a resource field.`);
1159
- }
1160
- })() : {};
1161
- }
1162
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1163
- if (!test) {
1164
- throw new Error(`Expected to have a getLegacySupport function`);
1165
- }
1166
- })(getLegacySupport) : {};
1167
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1168
- if (!test) {
1169
- throw new Error(`Can only use belongsTo fields when the resource is in legacy mode`);
1170
- }
1171
- })(Mode[Legacy]) : {};
1172
- store._join(() => {
1173
- getLegacySupport(receiver).setDirtyBelongsTo(field.name, value);
1174
- });
1175
- return true;
1176
- case 'hasMany':
1177
- if (!HAS_MODEL_PACKAGE) {
1178
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1179
- {
1180
- throw new Error(`Cannot use hasMany fields in your schema unless @ember-data-mirror/model is installed to provide legacy model support. ${field.name} should likely be migrated to be a collection field.`);
1181
- }
1182
- })() : {};
1183
- }
1184
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1185
- if (!test) {
1186
- throw new Error(`Expected to have a getLegacySupport function`);
1187
- }
1188
- })(getLegacySupport) : {};
1189
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1190
- if (!test) {
1191
- throw new Error(`Can only use hasMany fields when the resource is in legacy mode`);
1192
- }
1193
- })(Mode[Legacy]) : {};
1194
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1195
- if (!test) {
1196
- throw new Error(`You must pass an array of records to set a hasMany relationship`);
1197
- }
1198
- })(Array.isArray(value)) : {};
1199
- store._join(() => {
1200
- const support = getLegacySupport(receiver);
1201
- const manyArray = support.getManyArray(field.name);
1202
- manyArray.splice(0, manyArray.length, ...value);
1203
- });
1204
- return true;
1205
- default:
1206
- throw new Error(`Unknown field kind ${field.kind}`);
1207
- }
1208
- }
1209
- });
1210
-
1211
- // what signal do we need for embedded record?
1212
- this.___notifications = store.notifications.subscribe(identifier, (_, type, key) => {
1213
- switch (type) {
1214
- case 'identity':
1215
- {
1216
- if (isEmbedded || !identityField) return; // base paths never apply to embedded records
1217
-
1218
- if (identityField.name && identityField.kind === '@id') {
1219
- const signal = signals.get('@identity');
1220
- if (signal) {
1221
- notifyInternalSignal(signal);
1222
- }
1223
- }
1224
- break;
1225
- }
1226
- case 'attributes':
1227
- if (key) {
1228
- if (Array.isArray(key)) {
1229
- if (!isEmbedded) return; // deep paths will be handled by embedded records
1230
- // TODO we should have the notification manager
1231
- // ensure it is safe for each callback to mutate this array
1232
- if (isPathMatch(embeddedPath, key)) {
1233
- // handle the notification
1234
- // TODO we should likely handle this notification here
1235
- // also we should add a LOGGING flag
1236
- // eslint-disable-next-line no-console
1237
- console.warn(`Notification unhandled for ${key.join(',')} on ${identifier.type}`, self);
1238
- return;
1239
- }
1240
-
1241
- // TODO we should add a LOGGING flag
1242
- // console.log(`Deep notification skipped for ${key.join('.')} on ${identifier.type}`, self);
1243
- // deep notify the key path
1244
- } else {
1245
- if (isEmbedded) return; // base paths never apply to embedded records
1246
-
1247
- // TODO determine what LOGGING flag to wrap this in if any
1248
- // console.log(`Notification for ${key} on ${identifier.type}`, self);
1249
- const signal = signals.get(key);
1250
- if (signal) {
1251
- notifyInternalSignal(signal);
1252
- }
1253
- const field = fields.get(key);
1254
- if (field?.kind === 'array' || field?.kind === 'schema-array') {
1255
- const peeked = peekManagedArray(self, field);
1256
- if (peeked) {
1257
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1258
- if (!test) {
1259
- throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
1260
- }
1261
- })(ARRAY_SIGNAL in peeked) : {};
1262
- const arrSignal = peeked[ARRAY_SIGNAL];
1263
- notifyInternalSignal(arrSignal);
1264
- }
1265
- }
1266
- if (field?.kind === 'object') {
1267
- const peeked = peekManagedObject(self, field);
1268
- if (peeked) {
1269
- const objSignal = peeked[OBJECT_SIGNAL];
1270
- notifyInternalSignal(objSignal);
1271
- }
1272
- }
1273
- }
1274
- }
1275
- break;
1276
- case 'relationships':
1277
- if (key) {
1278
- if (Array.isArray(key)) ;else {
1279
- if (isEmbedded) return; // base paths never apply to embedded records
1280
-
1281
- const field = fields.get(key);
1282
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1283
- if (!test) {
1284
- throw new Error(`Expected relationshp ${key} to be the name of a field`);
1285
- }
1286
- })(field) : {};
1287
- if (field.kind === 'belongsTo') {
1288
- // TODO determine what LOGGING flag to wrap this in if any
1289
- // console.log(`Notification for ${key} on ${identifier.type}`, self);
1290
- const signal = signals.get(key);
1291
- if (signal) {
1292
- notifyInternalSignal(signal);
1293
- }
1294
- // FIXME
1295
- } else if (field.kind === 'resource') ;else if (field.kind === 'hasMany') {
1296
- if (field.options.linksMode) {
1297
- const peeked = peekManagedArray(self, field);
1298
- if (peeked) {
1299
- notifyInternalSignal(peeked[ARRAY_SIGNAL]);
1300
- }
1301
- return;
1302
- }
1303
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1304
- if (!test) {
1305
- throw new Error(`Expected to have a getLegacySupport function`);
1306
- }
1307
- })(getLegacySupport) : {};
1308
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1309
- if (!test) {
1310
- throw new Error(`Can only use hasMany fields when the resource is in legacy mode`);
1311
- }
1312
- })(Mode[Legacy]) : {};
1313
- const support = getLegacySupport(proxy);
1314
- const manyArray = support && support._manyArrayCache[key];
1315
- const hasPromise = support && support._relationshipPromisesCache[key];
1316
- if (manyArray && hasPromise) {
1317
- // do nothing, we will notify the ManyArray directly
1318
- // once the fetch has completed.
1319
- return;
1320
- }
1321
- if (manyArray) {
1322
- notifyInternalSignal(manyArray[ARRAY_SIGNAL]);
1323
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1324
- if (!test) {
1325
- throw new Error(`Expected options to exist on relationship meta`);
1326
- }
1327
- })(field.options) : {};
1328
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1329
- if (!test) {
1330
- throw new Error(`Expected async to exist on relationship meta options`);
1331
- }
1332
- })('async' in field.options) : {};
1333
- if (field.options.async) {
1334
- const signal = signals.get(key);
1335
- if (signal) {
1336
- notifyInternalSignal(signal);
1337
- }
1338
- }
1339
- }
1340
- } else if (field.kind === 'collection') ;
1341
- }
1342
- }
1343
- break;
1344
- }
1345
- });
1346
- if (macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG)) {
1347
- Object.defineProperty(this, '__SHOW_ME_THE_DATA_(debug mode only)__', {
1348
- enumerable: false,
1349
- configurable: true,
1350
- get() {
1351
- const data = {};
1352
- for (const key of fields.keys()) {
1353
- data[key] = proxy[key];
1354
- }
1355
- return data;
1356
- }
1357
- });
1358
- }
1359
- return proxy;
1360
- }
1361
- [Destroy]() {
1362
- if (this[Legacy]) {
1363
- // @ts-expect-error
1364
- this.isDestroying = true;
1365
- // @ts-expect-error
1366
- this.isDestroyed = true;
1367
- }
1368
- this[RecordStore].notifications.unsubscribe(this.___notifications);
1369
- }
1370
- [Checkout]() {
1371
- // IF we are already the editable record, throw an error
1372
- if (this[Editable]) {
1373
- throw new Error(`Cannot checkout an already editable record`);
1374
- }
1375
- const editable = Editables.get(this);
1376
- if (editable) {
1377
- return Promise.resolve(editable);
1378
- }
1379
- const embeddedType = this[EmbeddedType];
1380
- const embeddedPath = this[EmbeddedPath];
1381
- const isEmbedded = embeddedType !== null && embeddedPath !== null;
1382
- if (isEmbedded) {
1383
- throw new Error(`Cannot checkout an embedded record (yet)`);
1384
- }
1385
- const editableRecord = new SchemaRecord(this[RecordStore], this[Identifier], {
1386
- [Editable]: true,
1387
- [Legacy]: this[Legacy]
1388
- }, isEmbedded, embeddedType, embeddedPath);
1389
- setRecordIdentifier(editableRecord, recordIdentifierFor(this));
1390
- return Promise.resolve(editableRecord);
1391
- }
1392
- }
1393
- function instantiateRecord(store, identifier, createArgs) {
1394
- const schema = store.schema;
1395
- const resourceSchema = schema.resource(identifier);
1396
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1397
- if (!test) {
1398
- throw new Error(`Expected a resource schema`);
1399
- }
1400
- })(isResourceSchema(resourceSchema)) : {};
1401
- const isLegacy = resourceSchema?.legacy ?? false;
1402
- const isEditable = isLegacy || store.cache.isNew(identifier);
1403
- const record = new SchemaRecord(store, identifier, {
1404
- [Editable]: isEditable,
1405
- [Legacy]: isLegacy
1406
- });
1407
- if (createArgs) {
1408
- Object.assign(record, createArgs);
1409
- }
1410
- return record;
1411
- }
1412
- function assertSchemaRecord(record) {
1413
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1414
- if (!test) {
1415
- throw new Error('Expected a SchemaRecord');
1416
- }
1417
- })(record && typeof record === 'object' && Destroy in record) : {};
1418
- }
1419
- function teardownRecord(record) {
1420
- assertSchemaRecord(record);
1421
- record[Destroy]();
1422
- }
1423
-
1424
- /**
1425
- * @module @warp-drive-mirror/schema-record
1426
- */
1427
- const Support = getOrSetGlobal('Support', new WeakMap());
1428
- const ConstructorField = {
1429
- type: '@constructor',
1430
- name: 'constructor',
1431
- kind: 'derived'
1432
- };
1433
- const TypeField = {
1434
- type: '@identity',
1435
- name: '$type',
1436
- kind: 'derived',
1437
- options: {
1438
- key: 'type'
1439
- }
1440
- };
1441
- const DefaultIdentityField = {
1442
- name: 'id',
1443
- kind: '@id'
1444
- };
1445
- function _constructor(record) {
1446
- let state = Support.get(record);
1447
- if (!state) {
1448
- state = {};
1449
- Support.set(record, state);
1450
- }
1451
- return state._constructor = state._constructor || {
1452
- name: `SchemaRecord<${recordIdentifierFor$1(record).type}>`,
1453
- get modelName() {
1454
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1455
- {
1456
- throw new Error(`record.constructor.modelName is not available outside of legacy mode`);
1457
- }
1458
- })() : {};
1459
- return undefined;
1460
- }
1461
- };
1462
- }
1463
- _constructor[Type] = '@constructor';
1464
-
1465
- /**
1466
- * Utility for constructing a ResourceSchema with the recommended
1467
- * fields for the PolarisMode experience.
1468
- *
1469
- * Using this requires registering the PolarisMode derivations
1470
- *
1471
- * ```ts
1472
- * import { registerDerivations } from '@warp-drive-mirror/schema-record';
1473
- *
1474
- * registerDerivations(schema);
1475
- * ```
1476
- *
1477
- * @method withDefaults
1478
- * @for @warp-drive-mirror/schema-record
1479
- * @static
1480
- * @public
1481
- * @param schema
1482
- * @return {PolarisResourceSchema}
1483
- */
1484
- function withDefaults(schema) {
1485
- schema.identity = schema.identity || DefaultIdentityField;
1486
-
1487
- // because fields gets iterated in definition order,
1488
- // we add TypeField to the beginning so that it will
1489
- // appear right next to the identity field
1490
- schema.fields.unshift(TypeField);
1491
- schema.fields.push(ConstructorField);
1492
- return schema;
1493
- }
1494
-
1495
- /**
1496
- * A derivation that computes its value from the
1497
- * record's identity.
1498
- *
1499
- * It can be used via a derived field definition like:
1500
- *
1501
- * ```ts
1502
- * {
1503
- * kind: 'derived',
1504
- * name: 'id',
1505
- * type: '@identity',
1506
- * options: { key: 'id' }
1507
- * }
1508
- * ```
1509
- *
1510
- * Valid keys are `'id'`, `'lid'`, `'type'`, and `'^'`.
1511
- *
1512
- * `^` returns the entire identifier object.
1513
- *
1514
- * @method fromIdentity
1515
- * @for @warp-drive-mirror/schema-record
1516
- * @static
1517
- * @public
1518
- */
1519
-
1520
- function fromIdentity(record, options, key) {
1521
- const identifier = record[Identifier];
1522
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1523
- if (!test) {
1524
- throw new Error(`Cannot compute @identity for a record without an identifier`);
1525
- }
1526
- })(identifier) : {};
1527
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1528
- if (!test) {
1529
- throw new Error(`Expected to receive a key to compute @identity, but got ${String(options)}`);
1530
- }
1531
- })(options?.key && ['lid', 'id', 'type', '^'].includes(options.key)) : {};
1532
- return options.key === '^' ? identifier : identifier[options.key];
1533
- }
1534
- fromIdentity[Type] = '@identity';
1535
-
1536
- /**
1537
- * Registers the default derivations for records that want
1538
- * to use the PolarisMode defaults provided by
1539
- *
1540
- * ```ts
1541
- * import { withDefaults } from '@warp-drive-mirror/schema-record';
1542
- * ```
1543
- *
1544
- * @method registerDerivations
1545
- * @for @warp-drive-mirror/schema-record
1546
- * @static
1547
- * @public
1548
- * @param {SchemaService} schema
1549
- */
1550
- function registerDerivations(schema) {
1551
- schema.registerDerivation(fromIdentity);
1552
- schema.registerDerivation(_constructor);
1553
- }
1554
- /**
1555
- * Wraps a derivation in a new function with Derivation signature but that looks
1556
- * up the value in the cache before recomputing.
1557
- *
1558
- * @internal
1559
- */
1560
- function makeCachedDerivation(derivation) {
1561
- const memoizedDerivation = (record, options, prop) => {
1562
- const signals = withSignalStore(record);
1563
- let signal = signals.get(prop);
1564
- if (!signal) {
1565
- signal = createMemo(record, prop, () => {
1566
- return derivation(record, options, prop);
1567
- }); // a total lie, for convenience of reusing the storage
1568
- signals.set(prop, signal);
1569
- }
1570
- return signal();
1571
- };
1572
- memoizedDerivation[Type] = derivation[Type];
1573
- return memoizedDerivation;
1574
- }
1575
- /**
1576
- * A SchemaService designed to work with dynamically registered schemas.
1577
- *
1578
- * @class SchemaService
1579
- * @public
1580
- */
1581
- class SchemaService {
1582
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
1583
-
1584
- constructor() {
1585
- this._schemas = new Map();
1586
- this._transforms = new Map();
1587
- this._hashFns = new Map();
1588
- this._derivations = new Map();
1589
- this._traits = new Set();
1590
- }
1591
- resourceTypes() {
1592
- return Array.from(this._schemas.keys());
1593
- }
1594
- hasTrait(type) {
1595
- return this._traits.has(type);
1596
- }
1597
- resourceHasTrait(resource, trait) {
1598
- return this._schemas.get(resource.type).traits.has(trait);
1599
- }
1600
- transformation(field) {
1601
- const kind = 'kind' in field ? field.kind : '<unknown kind>';
1602
- const name = 'name' in field ? field.name : '<unknown name>';
1603
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1604
- if (!test) {
1605
- throw new Error(`'${kind}' fields cannot be transformed. Only fields of kind 'field' 'object' or 'array' can specify a transformation. Attempted to find '${field.type ?? '<unknown type>'}' on field '${name}'.`);
1606
- }
1607
- })(!('kind' in field) || ['field', 'object', 'array'].includes(kind)) : {};
1608
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1609
- if (!test) {
1610
- throw new Error(`Expected the '${kind}' field '${name}' to specify a transformation via 'field.type', but none was present`);
1611
- }
1612
- })(field.type) : {};
1613
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1614
- if (!test) {
1615
- throw new Error(`No transformation registered with name '${field.type}' for '${kind}' field '${name}'`);
1616
- }
1617
- })(this._transforms.has(field.type)) : {};
1618
- return this._transforms.get(field.type);
1619
- }
1620
- derivation(field) {
1621
- const kind = 'kind' in field ? field.kind : '<unknown kind>';
1622
- const name = 'name' in field ? field.name : '<unknown name>';
1623
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1624
- if (!test) {
1625
- throw new Error(`The '${kind}' field '${name}' is not derived and so cannot be used to lookup a derivation`);
1626
- }
1627
- })(!('kind' in field) || kind === 'derived') : {};
1628
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1629
- if (!test) {
1630
- throw new Error(`Expected the '${kind}' field '${name}' to specify a derivation via 'field.type', but no value was present`);
1631
- }
1632
- })(field.type) : {};
1633
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1634
- if (!test) {
1635
- throw new Error(`No '${field.type}' derivation registered for use by the '${kind}' field '${name}'`);
1636
- }
1637
- })(this._derivations.has(field.type)) : {};
1638
- return this._derivations.get(field.type);
1639
- }
1640
- hashFn(field) {
1641
- const kind = 'kind' in field ? field.kind : '<unknown kind>';
1642
- const name = 'name' in field ? field.name : '<unknown name>';
1643
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1644
- if (!test) {
1645
- throw new Error(`The '${kind}' field '${name}' is not a HashField and so cannot be used to lookup a hash function`);
1646
- }
1647
- })(!('kind' in field) || kind === '@hash') : {};
1648
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1649
- if (!test) {
1650
- throw new Error(`Expected the '${kind}' field '${name}' to specify a hash function via 'field.type', but no value was present`);
1651
- }
1652
- })(field.type) : {};
1653
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1654
- if (!test) {
1655
- throw new Error(`No '${field.type}' hash function is registered for use by the '${kind}' field '${name}'`);
1656
- }
1657
- })(this._hashFns.has(field.type)) : {};
1658
- return this._hashFns.get(field.type);
1659
- }
1660
- resource(resource) {
1661
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1662
- if (!test) {
1663
- throw new Error(`No resource registered with name '${resource.type}'`);
1664
- }
1665
- })(this._schemas.has(resource.type)) : {};
1666
- return this._schemas.get(resource.type).original;
1667
- }
1668
- registerResources(schemas) {
1669
- schemas.forEach(schema => {
1670
- this.registerResource(schema);
1671
- });
1672
- }
1673
- registerResource(schema) {
1674
- const fields = new Map();
1675
- const relationships = {};
1676
- const attributes = {};
1677
- schema.fields.forEach(field => {
1678
- macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
1679
- if (!test) {
1680
- throw new Error(`${field.kind} is not valid inside a ResourceSchema's fields.`);
1681
- }
1682
- })(
1683
- // @ts-expect-error we are checking for mistakes at runtime
1684
- field.kind !== '@id' && field.kind !== '@hash') : {};
1685
- fields.set(field.name, field);
1686
- if (field.kind === 'attribute') {
1687
- attributes[field.name] = field;
1688
- } else if (field.kind === 'belongsTo' || field.kind === 'hasMany') {
1689
- relationships[field.name] = field;
1690
- }
1691
- });
1692
- const traits = new Set(isResourceSchema(schema) ? schema.traits : []);
1693
- traits.forEach(trait => {
1694
- this._traits.add(trait);
1695
- });
1696
- const internalSchema = {
1697
- original: schema,
1698
- fields,
1699
- relationships,
1700
- attributes,
1701
- traits
1702
- };
1703
- this._schemas.set(schema.type, internalSchema);
1704
- }
1705
- registerTransformation(transformation) {
1706
- this._transforms.set(transformation[Type], transformation);
1707
- }
1708
- registerDerivation(derivation) {
1709
- this._derivations.set(derivation[Type], makeCachedDerivation(derivation));
1710
- }
1711
- registerHashFn(hashFn) {
1712
- this._hashFns.set(hashFn[Type], hashFn);
1713
- }
1714
- fields({
1715
- type
1716
- }) {
1717
- const schema = this._schemas.get(type);
1718
- if (!schema) {
1719
- throw new Error(`No schema defined for ${type}`);
1720
- }
1721
- return schema.fields;
1722
- }
1723
- hasResource(resource) {
1724
- return this._schemas.has(resource.type);
1725
- }
1726
- }
1727
- if (macroCondition(getGlobalConfig().WarpDriveMirror.deprecations.ENABLE_LEGACY_SCHEMA_SERVICE)) {
1728
- SchemaService.prototype.attributesDefinitionFor = function ({
1729
- type
1730
- }) {
1731
- deprecate(`Use \`schema.fields({ type })\` instead of \`schema.attributesDefinitionFor({ type })\``, false, {
1732
- id: 'ember-data-mirror:schema-service-updates',
1733
- until: '6.0',
1734
- for: 'ember-data-mirror',
1735
- since: {
1736
- available: '4.13',
1737
- enabled: '5.4'
1738
- }
1739
- });
1740
- const schema = this._schemas.get(type);
1741
- if (!schema) {
1742
- throw new Error(`No schema defined for ${type}`);
1743
- }
1744
- return schema.attributes;
1745
- };
1746
- SchemaService.prototype.relationshipsDefinitionFor = function ({
1747
- type
1748
- }) {
1749
- deprecate(`Use \`schema.fields({ type })\` instead of \`schema.relationshipsDefinitionFor({ type })\``, false, {
1750
- id: 'ember-data-mirror:schema-service-updates',
1751
- until: '6.0',
1752
- for: 'ember-data-mirror',
1753
- since: {
1754
- available: '4.13',
1755
- enabled: '5.4'
1756
- }
1757
- });
1758
- const schema = this._schemas.get(type);
1759
- if (!schema) {
1760
- throw new Error(`No schema defined for ${type}`);
1761
- }
1762
- return schema.relationships;
1763
- };
1764
- SchemaService.prototype.doesTypeExist = function (type) {
1765
- deprecate(`Use \`schema.hasResource({ type })\` instead of \`schema.doesTypeExist(type)\``, false, {
1766
- id: 'ember-data-mirror:schema-service-updates',
1767
- until: '6.0',
1768
- for: 'ember-data-mirror',
1769
- since: {
1770
- available: '4.13',
1771
- enabled: '5.4'
1772
- }
1773
- });
1774
- return this._schemas.has(type);
1775
- };
1776
- }
1777
- export { Checkout, SchemaService, fromIdentity, instantiateRecord, registerDerivations, teardownRecord, withDefaults };
1
+ export { Checkout, SchemaService, fromIdentity, instantiateRecord, registerDerivations, teardownRecord, withDefaults } from '@warp-drive-mirror/core/reactive';