@warp-drive/schema-record 0.0.0-beta.15 → 0.0.0-beta.16

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 (54) hide show
  1. package/LICENSE.md +19 -8
  2. package/README.md +211 -123
  3. package/dist/-private.js +1 -0
  4. package/dist/-private.js.map +1 -0
  5. package/dist/{record.js → index.js} +498 -35
  6. package/dist/index.js.map +1 -0
  7. package/dist/symbols-DqoS4ybV.js.map +1 -1
  8. package/logos/NCC-1701-a-gold.svg +4 -0
  9. package/logos/NCC-1701-a-gold_100.svg +1 -0
  10. package/logos/NCC-1701-a-gold_base-64.txt +1 -0
  11. package/logos/README.md +4 -0
  12. package/logos/docs-badge.svg +2 -0
  13. package/logos/ember-data-logo-dark.svg +12 -0
  14. package/logos/ember-data-logo-light.svg +12 -0
  15. package/logos/github-header.svg +444 -0
  16. package/logos/social1.png +0 -0
  17. package/logos/social2.png +0 -0
  18. package/logos/warp-drive-logo-dark.svg +4 -0
  19. package/logos/warp-drive-logo-gold.svg +4 -0
  20. package/package.json +29 -55
  21. package/unstable-preview-types/-private/{compute.d.ts → fields/compute.d.ts} +17 -15
  22. package/unstable-preview-types/-private/fields/compute.d.ts.map +1 -0
  23. package/unstable-preview-types/-private/{managed-array.d.ts → fields/managed-array.d.ts} +4 -4
  24. package/unstable-preview-types/-private/fields/managed-array.d.ts.map +1 -0
  25. package/unstable-preview-types/-private/{managed-object.d.ts → fields/managed-object.d.ts} +4 -4
  26. package/unstable-preview-types/-private/fields/managed-object.d.ts.map +1 -0
  27. package/unstable-preview-types/-private/fields/many-array-manager.d.ts +23 -0
  28. package/unstable-preview-types/-private/fields/many-array-manager.d.ts.map +1 -0
  29. package/unstable-preview-types/{hooks.d.ts → -private/hooks.d.ts} +3 -3
  30. package/unstable-preview-types/-private/hooks.d.ts.map +1 -0
  31. package/unstable-preview-types/{record.d.ts → -private/record.d.ts} +3 -3
  32. package/unstable-preview-types/-private/record.d.ts.map +1 -0
  33. package/unstable-preview-types/{schema.d.ts → -private/schema.d.ts} +9 -9
  34. package/unstable-preview-types/-private/schema.d.ts.map +1 -0
  35. package/unstable-preview-types/{symbols.d.ts → -private/symbols.d.ts} +1 -1
  36. package/unstable-preview-types/-private/symbols.d.ts.map +1 -0
  37. package/unstable-preview-types/-private.d.ts +4 -0
  38. package/unstable-preview-types/-private.d.ts.map +1 -0
  39. package/unstable-preview-types/index.d.ts +16 -7
  40. package/unstable-preview-types/index.d.ts.map +1 -0
  41. package/dist/hooks.js +0 -19
  42. package/dist/hooks.js.map +0 -1
  43. package/dist/record.js.map +0 -1
  44. package/dist/schema.js +0 -278
  45. package/dist/schema.js.map +0 -1
  46. package/unstable-preview-types/-private/compute.d.ts.map +0 -1
  47. package/unstable-preview-types/-private/managed-array.d.ts.map +0 -1
  48. package/unstable-preview-types/-private/managed-object.d.ts.map +0 -1
  49. package/unstable-preview-types/hooks.d.ts.map +0 -1
  50. package/unstable-preview-types/record.d.ts.map +0 -1
  51. package/unstable-preview-types/schema.d.ts.map +0 -1
  52. package/unstable-preview-types/symbols.d.ts.map +0 -1
  53. /package/{NCC-1701-a-blue.svg → logos/NCC-1701-a-blue.svg} +0 -0
  54. /package/{NCC-1701-a.svg → logos/NCC-1701-a.svg} +0 -0
@@ -1,10 +1,14 @@
1
+ import { isResourceSchema } from '@warp-drive/core-types/schema/fields';
1
2
  import { macroCondition, getGlobalConfig, dependencySatisfies, importSync } from '@embroider/macros';
2
- import { setRecordIdentifier, recordIdentifierFor } from '@ember-data/store/-private';
3
- import { createSignal, subscribe, defineSignal, peekSignal, getSignal, Signals, entangleSignal, addToTransaction } from '@ember-data/tracking/-private';
4
- import { STRUCTURED } from '@warp-drive/core-types/request';
5
- import { RecordStore } from '@warp-drive/core-types/symbols';
3
+ import { SOURCE as SOURCE$1, fastPush, RelatedCollection, setRecordIdentifier, recordIdentifierFor } from '@ember-data/store/-private';
4
+ import { createSignal, subscribe, defineSignal, peekSignal, getSignal, Signals, addToTransaction, entangleSignal } from '@ember-data/tracking/-private';
5
+ import { EnableHydration, STRUCTURED } from '@warp-drive/core-types/request';
6
+ import { RecordStore, Type } from '@warp-drive/core-types/symbols';
6
7
  import { getOrSetGlobal } from '@warp-drive/core-types/-private';
7
8
  import { S as SOURCE, A as ARRAY_SIGNAL, E as Editable, L as Legacy, I as Identifier, P as Parent, O as OBJECT_SIGNAL, a as EmbeddedPath, D as Destroy, C as Checkout, b as EmbeddedType } from "./symbols-DqoS4ybV.js";
9
+ import { deprecate } from '@ember/debug';
10
+ import { recordIdentifierFor as recordIdentifierFor$1 } from '@ember-data/store';
11
+ import { createCache, getValue } from '@ember-data/tracking';
8
12
  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']);
9
13
  // const ARRAY_SETTER_METHODS = new Set<KeyType>(['push', 'pop', 'unshift', 'shift', 'splice', 'sort']);
10
14
  const SYNC_PROPS = new Set(['[]', 'length']);
@@ -257,6 +261,12 @@ class ManagedArray {
257
261
  _SIGNAL.shouldReset = true;
258
262
  }
259
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);
260
270
  }
261
271
  });
262
272
  return proxy;
@@ -360,6 +370,75 @@ class ManagedObject {
360
370
  return proxy;
361
371
  }
362
372
  }
373
+ class ManyArrayManager {
374
+ constructor(record) {
375
+ this.record = record;
376
+ this.store = record[RecordStore];
377
+ this.identifier = record[Identifier];
378
+ }
379
+ _syncArray(array) {
380
+ const rawValue = this.store.cache.getRelationship(this.identifier, array.key);
381
+ if (rawValue.meta) {
382
+ array.meta = rawValue.meta;
383
+ }
384
+ if (rawValue.links) {
385
+ array.links = rawValue.links;
386
+ }
387
+ const currentState = array[SOURCE$1];
388
+ currentState.length = 0;
389
+ fastPush(currentState, rawValue.data);
390
+ }
391
+ reloadHasMany(key, options) {
392
+ const field = this.store.schema.fields(this.identifier).get(key);
393
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
394
+ if (!test) {
395
+ throw new Error(`Expected a hasMany field for ${key}`);
396
+ }
397
+ })(field?.kind === 'hasMany') : {};
398
+ const cacheOptions = options ? extractCacheOptions(options) : {
399
+ reload: true
400
+ };
401
+ cacheOptions.types = [field.type];
402
+ const rawValue = this.store.cache.getRelationship(this.identifier, key);
403
+ const req = {
404
+ url: getRelatedLink(rawValue),
405
+ op: 'findHasMany',
406
+ method: 'GET',
407
+ records: rawValue.data,
408
+ cacheOptions,
409
+ options: {
410
+ field,
411
+ identifier: this.identifier,
412
+ links: rawValue.links,
413
+ meta: rawValue.meta
414
+ },
415
+ [EnableHydration]: false
416
+ };
417
+ return this.store.request(req);
418
+ }
419
+ mutate(mutation) {
420
+ this.cache.mutate(mutation);
421
+ }
422
+ }
423
+ function getRelatedLink(resource) {
424
+ const related = resource.links?.related;
425
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
426
+ if (!test) {
427
+ throw new Error(`Expected a related link`);
428
+ }
429
+ })(related) : {};
430
+ return typeof related === 'object' ? related.href : related;
431
+ }
432
+ function extractCacheOptions(options) {
433
+ const cacheOptions = {};
434
+ if ('reload' in options) {
435
+ cacheOptions.reload = options.reload;
436
+ }
437
+ if ('backgroundReload' in options) {
438
+ cacheOptions.backgroundReload = options.backgroundReload;
439
+ }
440
+ return cacheOptions;
441
+ }
363
442
  const ManagedArrayMap = getOrSetGlobal('ManagedArrayMap', new Map());
364
443
  const ManagedObjectMap = getOrSetGlobal('ManagedObjectMap', new Map());
365
444
  function computeLocal(record, field, prop) {
@@ -373,24 +452,25 @@ function computeLocal(record, field, prop) {
373
452
  function peekManagedArray(record, field) {
374
453
  const managedArrayMapForRecord = ManagedArrayMap.get(record);
375
454
  if (managedArrayMapForRecord) {
376
- return managedArrayMapForRecord.get(field);
455
+ return managedArrayMapForRecord.get(field.name);
377
456
  }
378
457
  }
379
458
  function peekManagedObject(record, field) {
380
459
  const managedObjectMapForRecord = ManagedObjectMap.get(record);
381
460
  if (managedObjectMapForRecord) {
382
- return managedObjectMapForRecord.get(field);
461
+ return managedObjectMapForRecord.get(field.name);
383
462
  }
384
463
  }
385
- function computeField(schema, cache, record, identifier, field, prop) {
386
- const rawValue = cache.getAttr(identifier, prop);
464
+ function computeField(schema, cache, record, identifier, field, prop, editable) {
465
+ const rawValue = editable ? cache.getAttr(identifier, prop) : cache.getRemoteAttr(identifier, prop);
387
466
  if (!field.type) {
388
467
  return rawValue;
389
468
  }
390
469
  const transform = schema.transformation(field);
391
470
  return transform.hydrate(rawValue, field.options ?? null, record);
392
471
  }
393
- function computeArray(store, schema, cache, record, identifier, field, path, isSchemaArray, editable, legacy) {
472
+ function computeArray(store, schema, cache, record, identifier, field, path, editable, legacy) {
473
+ const isSchemaArray = field.kind === 'schema-array';
394
474
  // the thing we hand out needs to know its owner and path in a private manner
395
475
  // its "address" is the parent identifier (identifier) + field name (field.name)
396
476
  // in the nested object case field name here is the full dot path from root resource to this value
@@ -400,20 +480,20 @@ function computeArray(store, schema, cache, record, identifier, field, path, isS
400
480
  const managedArrayMapForRecord = ManagedArrayMap.get(record);
401
481
  let managedArray;
402
482
  if (managedArrayMapForRecord) {
403
- managedArray = managedArrayMapForRecord.get(field);
483
+ managedArray = managedArrayMapForRecord.get(field.name);
404
484
  }
405
485
  if (managedArray) {
406
486
  return managedArray;
407
487
  } else {
408
- const rawValue = cache.getAttr(identifier, path);
488
+ const rawValue = editable ? cache.getAttr(identifier, path) : cache.getRemoteAttr(identifier, path);
409
489
  if (!rawValue) {
410
490
  return null;
411
491
  }
412
492
  managedArray = new ManagedArray(store, schema, cache, field, rawValue, identifier, path, record, isSchemaArray, editable, legacy);
413
493
  if (!managedArrayMapForRecord) {
414
- ManagedArrayMap.set(record, new Map([[field, managedArray]]));
494
+ ManagedArrayMap.set(record, new Map([[field.name, managedArray]]));
415
495
  } else {
416
- managedArrayMapForRecord.set(field, managedArray);
496
+ managedArrayMapForRecord.set(field.name, managedArray);
417
497
  }
418
498
  }
419
499
  return managedArray;
@@ -422,12 +502,12 @@ function computeObject(schema, cache, record, identifier, field, path, editable,
422
502
  const managedObjectMapForRecord = ManagedObjectMap.get(record);
423
503
  let managedObject;
424
504
  if (managedObjectMapForRecord) {
425
- managedObject = managedObjectMapForRecord.get(field);
505
+ managedObject = managedObjectMapForRecord.get(field.name);
426
506
  }
427
507
  if (managedObject) {
428
508
  return managedObject;
429
509
  } else {
430
- let rawValue = cache.getAttr(identifier, path);
510
+ let rawValue = editable ? cache.getAttr(identifier, path) : cache.getRemoteAttr(identifier, path);
431
511
  if (!rawValue) {
432
512
  return null;
433
513
  }
@@ -437,9 +517,9 @@ function computeObject(schema, cache, record, identifier, field, path, editable,
437
517
  }
438
518
  managedObject = new ManagedObject(schema, cache, field, rawValue, identifier, path, record, editable, legacy);
439
519
  if (!managedObjectMapForRecord) {
440
- ManagedObjectMap.set(record, new Map([[field, managedObject]]));
520
+ ManagedObjectMap.set(record, new Map([[field.name, managedObject]]));
441
521
  } else {
442
- managedObjectMapForRecord.set(field, managedObject);
522
+ managedObjectMapForRecord.set(field.name, managedObject);
443
523
  }
444
524
  }
445
525
  return managedObject;
@@ -448,12 +528,12 @@ function computeSchemaObject(store, cache, record, identifier, field, path, lega
448
528
  const schemaObjectMapForRecord = ManagedObjectMap.get(record);
449
529
  let schemaObject;
450
530
  if (schemaObjectMapForRecord) {
451
- schemaObject = schemaObjectMapForRecord.get(field);
531
+ schemaObject = schemaObjectMapForRecord.get(field.name);
452
532
  }
453
533
  if (schemaObject) {
454
534
  return schemaObject;
455
535
  } else {
456
- const rawValue = cache.getAttr(identifier, path);
536
+ const rawValue = editable ? cache.getAttr(identifier, path) : cache.getRemoteAttr(identifier, path);
457
537
  if (!rawValue) {
458
538
  return null;
459
539
  }
@@ -464,14 +544,14 @@ function computeSchemaObject(store, cache, record, identifier, field, path, lega
464
544
  }, true, field.type, embeddedPath);
465
545
  }
466
546
  if (!schemaObjectMapForRecord) {
467
- ManagedObjectMap.set(record, new Map([[field, schemaObject]]));
547
+ ManagedObjectMap.set(record, new Map([[field.name, schemaObject]]));
468
548
  } else {
469
- schemaObjectMapForRecord.set(field, schemaObject);
549
+ schemaObjectMapForRecord.set(field.name, schemaObject);
470
550
  }
471
551
  return schemaObject;
472
552
  }
473
- function computeAttribute(cache, identifier, prop) {
474
- return cache.getAttr(identifier, prop);
553
+ function computeAttribute(cache, identifier, prop, editable) {
554
+ return editable ? cache.getAttr(identifier, prop) : cache.getRemoteAttr(identifier, prop);
475
555
  }
476
556
  function computeDerivation(schema, record, identifier, field, prop) {
477
557
  return schema.derivation(field)(record, field.options ?? null, prop);
@@ -480,8 +560,8 @@ function computeDerivation(schema, record, identifier, field, prop) {
480
560
  // TODO probably this should just be a Document
481
561
  // but its separate until we work out the lid situation
482
562
  class ResourceRelationship {
483
- constructor(store, cache, parent, identifier, field, name) {
484
- const rawValue = cache.getRelationship(identifier, name);
563
+ constructor(store, cache, parent, identifier, field, name, editable) {
564
+ const rawValue = editable ? cache.getRelationship(identifier, name) : cache.getRemoteRelationship(identifier, name);
485
565
 
486
566
  // TODO setup true lids for relationship documents
487
567
  // @ts-expect-error we need to give relationship documents a lid
@@ -523,11 +603,56 @@ function getHref(link) {
523
603
  }
524
604
  return link.href;
525
605
  }
526
- function computeResource(store, cache, parent, identifier, field, prop) {
606
+ function computeResource(store, cache, parent, identifier, field, prop, editable) {
527
607
  if (field.kind !== 'resource') {
528
608
  throw new Error(`The schema for ${identifier.type}.${String(prop)} is not a resource relationship`);
529
609
  }
530
- return new ResourceRelationship(store, cache, parent, identifier, field, prop);
610
+ return new ResourceRelationship(store, cache, parent, identifier, field, prop, editable);
611
+ }
612
+ function computeHasMany(store, schema, cache, record, identifier, field, path, editable, legacy) {
613
+ // the thing we hand out needs to know its owner and path in a private manner
614
+ // its "address" is the parent identifier (identifier) + field name (field.name)
615
+ // in the nested object case field name here is the full dot path from root resource to this value
616
+ // its "key" is the field on the parent record
617
+ // its "owner" is the parent record
618
+
619
+ const managedArrayMapForRecord = ManagedArrayMap.get(record);
620
+ let managedArray;
621
+ if (managedArrayMapForRecord) {
622
+ managedArray = managedArrayMapForRecord.get(field.name);
623
+ }
624
+ if (managedArray) {
625
+ return managedArray;
626
+ } else {
627
+ const rawValue = cache.getRelationship(identifier, field.name);
628
+ if (!rawValue) {
629
+ return null;
630
+ }
631
+ managedArray = new RelatedCollection({
632
+ store,
633
+ type: field.type,
634
+ identifier,
635
+ cache,
636
+ identifiers: rawValue.data,
637
+ key: field.name,
638
+ meta: rawValue.meta || null,
639
+ links: rawValue.links || null,
640
+ isPolymorphic: field.options.polymorphic ?? false,
641
+ isAsync: field.options.async ?? false,
642
+ // TODO: Grab the proper value
643
+ _inverseIsAsync: false,
644
+ // @ts-expect-error Typescript doesn't have a way for us to thread the generic backwards so it infers unknown instead of T
645
+ manager: new ManyArrayManager(record),
646
+ isLoaded: true,
647
+ allowMutation: editable
648
+ });
649
+ if (!managedArrayMapForRecord) {
650
+ ManagedArrayMap.set(record, new Map([[field.name, managedArray]]));
651
+ } else {
652
+ managedArrayMapForRecord.set(field.name, managedArray);
653
+ }
654
+ }
655
+ return managedArray;
531
656
  }
532
657
  const HAS_MODEL_PACKAGE = dependencySatisfies('@ember-data/model', '*');
533
658
  const getLegacySupport = macroCondition(dependencySatisfies('@ember-data/model', '*')) ? importSync('@ember-data/model/-private').lookupLegacySupport : null;
@@ -570,6 +695,9 @@ class SchemaRecord {
570
695
  return Array.from(fields.keys());
571
696
  },
572
697
  has(target, prop) {
698
+ if (prop === Destroy || prop === Checkout) {
699
+ return true;
700
+ }
573
701
  return fields.has(prop);
574
702
  },
575
703
  getOwnPropertyDescriptor(target, prop) {
@@ -683,10 +811,10 @@ class SchemaRecord {
683
811
  }
684
812
  })(!target[Legacy]) : {};
685
813
  entangleSignal(signals, receiver, field.name);
686
- return computeField(schema, cache, target, identifier, field, propArray);
814
+ return computeField(schema, cache, target, identifier, field, propArray, IS_EDITABLE);
687
815
  case 'attribute':
688
816
  entangleSignal(signals, receiver, field.name);
689
- return computeAttribute(cache, identifier, prop);
817
+ return computeAttribute(cache, identifier, prop, IS_EDITABLE);
690
818
  case 'resource':
691
819
  macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
692
820
  if (!test) {
@@ -694,12 +822,10 @@ class SchemaRecord {
694
822
  }
695
823
  })(!target[Legacy]) : {};
696
824
  entangleSignal(signals, receiver, field.name);
697
- return computeResource(store, cache, target, identifier, field, prop);
825
+ return computeResource(store, cache, target, identifier, field, prop, IS_EDITABLE);
698
826
  case 'derived':
699
827
  return computeDerivation(schema, receiver, identifier, field, prop);
700
828
  case 'schema-array':
701
- entangleSignal(signals, receiver, field.name);
702
- return computeArray(store, schema, cache, target, identifier, field, propArray, true, Mode[Editable], Mode[Legacy]);
703
829
  case 'array':
704
830
  macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
705
831
  if (!test) {
@@ -707,7 +833,7 @@ class SchemaRecord {
707
833
  }
708
834
  })(!target[Legacy]) : {};
709
835
  entangleSignal(signals, receiver, field.name);
710
- return computeArray(store, schema, cache, target, identifier, field, propArray, false, Mode[Editable], Mode[Legacy]);
836
+ return computeArray(store, schema, cache, target, identifier, field, propArray, Mode[Editable], Mode[Legacy]);
711
837
  case 'object':
712
838
  macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
713
839
  if (!test) {
@@ -728,7 +854,7 @@ class SchemaRecord {
728
854
  case 'belongsTo':
729
855
  if (field.options.linksMode) {
730
856
  entangleSignal(signals, receiver, field.name);
731
- const rawValue = cache.getRelationship(identifier, field.name);
857
+ const rawValue = IS_EDITABLE ? cache.getRelationship(identifier, field.name) : cache.getRemoteRelationship(identifier, field.name);
732
858
 
733
859
  // eslint-disable-next-line @typescript-eslint/no-unsafe-return
734
860
  return rawValue.data ? store.peekRecord(rawValue.data) : null;
@@ -753,6 +879,10 @@ class SchemaRecord {
753
879
  entangleSignal(signals, receiver, field.name);
754
880
  return getLegacySupport(receiver).getBelongsTo(field.name);
755
881
  case 'hasMany':
882
+ if (field.options.linksMode) {
883
+ entangleSignal(signals, receiver, field.name);
884
+ return computeHasMany(store, schema, cache, target, identifier, field, propArray, Mode[Editable], Mode[Legacy]);
885
+ }
756
886
  if (!HAS_MODEL_PACKAGE) {
757
887
  macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
758
888
  {
@@ -850,6 +980,11 @@ class SchemaRecord {
850
980
  cache.setAttr(identifier, propArray, value?.slice());
851
981
  const peeked = peekManagedArray(self, field);
852
982
  if (peeked) {
983
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
984
+ if (!test) {
985
+ throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
986
+ }
987
+ })(ARRAY_SIGNAL in peeked) : {};
853
988
  const arrSignal = peeked[ARRAY_SIGNAL];
854
989
  arrSignal.shouldReset = true;
855
990
  }
@@ -863,6 +998,11 @@ class SchemaRecord {
863
998
  cache.setAttr(identifier, propArray, rawValue);
864
999
  const peeked = peekManagedArray(self, field);
865
1000
  if (peeked) {
1001
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1002
+ if (!test) {
1003
+ throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
1004
+ }
1005
+ })(ARRAY_SIGNAL in peeked) : {};
866
1006
  const arrSignal = peeked[ARRAY_SIGNAL];
867
1007
  arrSignal.shouldReset = true;
868
1008
  }
@@ -877,6 +1017,11 @@ class SchemaRecord {
877
1017
  cache.setAttr(identifier, propArray, arrayValue);
878
1018
  const peeked = peekManagedArray(self, field);
879
1019
  if (peeked) {
1020
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1021
+ if (!test) {
1022
+ throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
1023
+ }
1024
+ })(ARRAY_SIGNAL in peeked) : {};
880
1025
  const arrSignal = peeked[ARRAY_SIGNAL];
881
1026
  arrSignal.shouldReset = true;
882
1027
  }
@@ -1054,6 +1199,11 @@ class SchemaRecord {
1054
1199
  if (field?.kind === 'array' || field?.kind === 'schema-array') {
1055
1200
  const peeked = peekManagedArray(self, field);
1056
1201
  if (peeked) {
1202
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1203
+ if (!test) {
1204
+ throw new Error(`Expected the peekManagedArray for ${field.kind} to return a ManagedArray`);
1205
+ }
1206
+ })(ARRAY_SIGNAL in peeked) : {};
1057
1207
  const arrSignal = peeked[ARRAY_SIGNAL];
1058
1208
  arrSignal.shouldReset = true;
1059
1209
  addToTransaction(arrSignal);
@@ -1090,6 +1240,16 @@ class SchemaRecord {
1090
1240
  }
1091
1241
  // FIXME
1092
1242
  } else if (field.kind === 'resource') ;else if (field.kind === 'hasMany') {
1243
+ if (field.options.linksMode) {
1244
+ const peeked = peekManagedArray(self, field);
1245
+ if (peeked) {
1246
+ // const arrSignal = peeked[ARRAY_SIGNAL];
1247
+ // arrSignal.shouldReset = true;
1248
+ // addToTransaction(arrSignal);
1249
+ peeked.notify();
1250
+ }
1251
+ return;
1252
+ }
1093
1253
  macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1094
1254
  if (!test) {
1095
1255
  throw new Error(`Expected to have a getLegacySupport function`);
@@ -1145,6 +1305,10 @@ class SchemaRecord {
1145
1305
  this[RecordStore].notifications.unsubscribe(this.___notifications);
1146
1306
  }
1147
1307
  [Checkout]() {
1308
+ // IF we are already the editable record, throw an error
1309
+ if (this[Editable]) {
1310
+ throw new Error(`Cannot checkout an already editable record`);
1311
+ }
1148
1312
  const editable = Editables.get(this);
1149
1313
  if (editable) {
1150
1314
  return Promise.resolve(editable);
@@ -1163,4 +1327,303 @@ class SchemaRecord {
1163
1327
  return Promise.resolve(editableRecord);
1164
1328
  }
1165
1329
  }
1166
- export { Checkout, Editable, Legacy, SchemaRecord };
1330
+ function instantiateRecord(store, identifier, createArgs) {
1331
+ const schema = store.schema;
1332
+ const resourceSchema = schema.resource(identifier);
1333
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1334
+ if (!test) {
1335
+ throw new Error(`Expected a resource schema`);
1336
+ }
1337
+ })(isResourceSchema(resourceSchema)) : {};
1338
+ const isLegacy = resourceSchema?.legacy ?? false;
1339
+ const isEditable = isLegacy || store.cache.isNew(identifier);
1340
+ const record = new SchemaRecord(store, identifier, {
1341
+ [Editable]: isEditable,
1342
+ [Legacy]: isLegacy
1343
+ });
1344
+ if (createArgs) {
1345
+ Object.assign(record, createArgs);
1346
+ }
1347
+ return record;
1348
+ }
1349
+ function assertSchemaRecord(record) {
1350
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1351
+ if (!test) {
1352
+ throw new Error('Expected a SchemaRecord');
1353
+ }
1354
+ })(record && typeof record === 'object' && Destroy in record) : {};
1355
+ }
1356
+ function teardownRecord(record) {
1357
+ assertSchemaRecord(record);
1358
+ record[Destroy]();
1359
+ }
1360
+ const Support = getOrSetGlobal('Support', new WeakMap());
1361
+ const SchemaRecordFields = [{
1362
+ type: '@constructor',
1363
+ name: 'constructor',
1364
+ kind: 'derived'
1365
+ }, {
1366
+ type: '@identity',
1367
+ name: '$type',
1368
+ kind: 'derived',
1369
+ options: {
1370
+ key: 'type'
1371
+ }
1372
+ }];
1373
+ function _constructor(record) {
1374
+ let state = Support.get(record);
1375
+ if (!state) {
1376
+ state = {};
1377
+ Support.set(record, state);
1378
+ }
1379
+ return state._constructor = state._constructor || {
1380
+ name: `SchemaRecord<${recordIdentifierFor$1(record).type}>`,
1381
+ get modelName() {
1382
+ throw new Error('Cannot access record.constructor.modelName on non-Legacy Schema Records.');
1383
+ }
1384
+ };
1385
+ }
1386
+ _constructor[Type] = '@constructor';
1387
+ function withDefaults(schema) {
1388
+ schema.identity = schema.identity || {
1389
+ name: 'id',
1390
+ kind: '@id'
1391
+ };
1392
+ schema.fields.push(...SchemaRecordFields);
1393
+ return schema;
1394
+ }
1395
+ function fromIdentity(record, options, key) {
1396
+ const identifier = record[Identifier];
1397
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1398
+ if (!test) {
1399
+ throw new Error(`Cannot compute @identity for a record without an identifier`);
1400
+ }
1401
+ })(identifier) : {};
1402
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1403
+ if (!test) {
1404
+ throw new Error(`Expected to receive a key to compute @identity, but got ${String(options)}`);
1405
+ }
1406
+ })(options?.key && ['lid', 'id', 'type', '^'].includes(options.key)) : {};
1407
+ return options.key === '^' ? identifier : identifier[options.key];
1408
+ }
1409
+ fromIdentity[Type] = '@identity';
1410
+ function registerDerivations(schema) {
1411
+ schema.registerDerivation(fromIdentity);
1412
+ schema.registerDerivation(_constructor);
1413
+ }
1414
+ /**
1415
+ * Wraps a derivation in a new function with Derivation signature but that looks
1416
+ * up the value in the cache before recomputing.
1417
+ *
1418
+ * @param record
1419
+ * @param options
1420
+ * @param prop
1421
+ */
1422
+ function makeCachedDerivation(derivation) {
1423
+ const memoizedDerivation = (record, options, prop) => {
1424
+ const signals = record[Signals];
1425
+ let signal = signals.get(prop);
1426
+ if (!signal) {
1427
+ signal = createCache(() => {
1428
+ return derivation(record, options, prop);
1429
+ }); // a total lie, for convenience of reusing the storage
1430
+ signals.set(prop, signal);
1431
+ }
1432
+ return getValue(signal);
1433
+ };
1434
+ memoizedDerivation[Type] = derivation[Type];
1435
+ return memoizedDerivation;
1436
+ }
1437
+ class SchemaService {
1438
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1439
+
1440
+ constructor() {
1441
+ this._schemas = new Map();
1442
+ this._transforms = new Map();
1443
+ this._hashFns = new Map();
1444
+ this._derivations = new Map();
1445
+ }
1446
+ hasTrait(type) {
1447
+ return this._traits.has(type);
1448
+ }
1449
+ resourceHasTrait(resource, trait) {
1450
+ return this._schemas.get(resource.type).traits.has(trait);
1451
+ }
1452
+ transformation(field) {
1453
+ const kind = 'kind' in field ? field.kind : '<unknown kind>';
1454
+ const name = 'name' in field ? field.name : '<unknown name>';
1455
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1456
+ if (!test) {
1457
+ 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}'.`);
1458
+ }
1459
+ })(!('kind' in field) || ['field', 'object', 'array'].includes(kind)) : {};
1460
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1461
+ if (!test) {
1462
+ throw new Error(`Expected the '${kind}' field '${name}' to specify a transformation via 'field.type', but none was present`);
1463
+ }
1464
+ })(field.type) : {};
1465
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1466
+ if (!test) {
1467
+ throw new Error(`No transformation registered with name '${field.type}' for '${kind}' field '${name}'`);
1468
+ }
1469
+ })(this._transforms.has(field.type)) : {};
1470
+ return this._transforms.get(field.type);
1471
+ }
1472
+ derivation(field) {
1473
+ const kind = 'kind' in field ? field.kind : '<unknown kind>';
1474
+ const name = 'name' in field ? field.name : '<unknown name>';
1475
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1476
+ if (!test) {
1477
+ throw new Error(`The '${kind}' field '${name}' is not derived and so cannot be used to lookup a derivation`);
1478
+ }
1479
+ })(!('kind' in field) || kind === 'derived') : {};
1480
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1481
+ if (!test) {
1482
+ throw new Error(`Expected the '${kind}' field '${name}' to specify a derivation via 'field.type', but no value was present`);
1483
+ }
1484
+ })(field.type) : {};
1485
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1486
+ if (!test) {
1487
+ throw new Error(`No '${field.type}' derivation registered for use by the '${kind}' field '${name}'`);
1488
+ }
1489
+ })(this._derivations.has(field.type)) : {};
1490
+ return this._derivations.get(field.type);
1491
+ }
1492
+ hashFn(field) {
1493
+ const kind = 'kind' in field ? field.kind : '<unknown kind>';
1494
+ const name = 'name' in field ? field.name : '<unknown name>';
1495
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1496
+ if (!test) {
1497
+ throw new Error(`The '${kind}' field '${name}' is not a HashField and so cannot be used to lookup a hash function`);
1498
+ }
1499
+ })(!('kind' in field) || kind === '@hash') : {};
1500
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1501
+ if (!test) {
1502
+ throw new Error(`Expected the '${kind}' field '${name}' to specify a hash function via 'field.type', but no value was present`);
1503
+ }
1504
+ })(field.type) : {};
1505
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1506
+ if (!test) {
1507
+ throw new Error(`No '${field.type}' hash function is registered for use by the '${kind}' field '${name}'`);
1508
+ }
1509
+ })(this._hashFns.has(field.type)) : {};
1510
+ return this._hashFns.get(field.type);
1511
+ }
1512
+ resource(resource) {
1513
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1514
+ if (!test) {
1515
+ throw new Error(`No resource registered with name '${resource.type}'`);
1516
+ }
1517
+ })(this._schemas.has(resource.type)) : {};
1518
+ return this._schemas.get(resource.type).original;
1519
+ }
1520
+ registerResources(schemas) {
1521
+ schemas.forEach(schema => {
1522
+ this.registerResource(schema);
1523
+ });
1524
+ }
1525
+ registerResource(schema) {
1526
+ const fields = new Map();
1527
+ const relationships = {};
1528
+ const attributes = {};
1529
+ schema.fields.forEach(field => {
1530
+ macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
1531
+ if (!test) {
1532
+ throw new Error(`${field.kind} is not valid inside a ResourceSchema's fields.`);
1533
+ }
1534
+ })(
1535
+ // @ts-expect-error we are checking for mistakes at runtime
1536
+ field.kind !== '@id' && field.kind !== '@hash') : {};
1537
+ fields.set(field.name, field);
1538
+ if (field.kind === 'attribute') {
1539
+ attributes[field.name] = field;
1540
+ } else if (field.kind === 'belongsTo' || field.kind === 'hasMany') {
1541
+ relationships[field.name] = field;
1542
+ }
1543
+ });
1544
+ const traits = new Set(isResourceSchema(schema) ? schema.traits : []);
1545
+ traits.forEach(trait => {
1546
+ this._traits.add(trait);
1547
+ });
1548
+ const internalSchema = {
1549
+ original: schema,
1550
+ fields,
1551
+ relationships,
1552
+ attributes,
1553
+ traits
1554
+ };
1555
+ this._schemas.set(schema.type, internalSchema);
1556
+ }
1557
+ registerTransformation(transformation) {
1558
+ this._transforms.set(transformation[Type], transformation);
1559
+ }
1560
+ registerDerivation(derivation) {
1561
+ this._derivations.set(derivation[Type], makeCachedDerivation(derivation));
1562
+ }
1563
+ registerHashFn(hashFn) {
1564
+ this._hashFns.set(hashFn[Type], hashFn);
1565
+ }
1566
+ fields({
1567
+ type
1568
+ }) {
1569
+ const schema = this._schemas.get(type);
1570
+ if (!schema) {
1571
+ throw new Error(`No schema defined for ${type}`);
1572
+ }
1573
+ return schema.fields;
1574
+ }
1575
+ hasResource(resource) {
1576
+ return this._schemas.has(resource.type);
1577
+ }
1578
+ }
1579
+ if (macroCondition(getGlobalConfig().WarpDrive.deprecations.ENABLE_LEGACY_SCHEMA_SERVICE)) {
1580
+ SchemaService.prototype.attributesDefinitionFor = function ({
1581
+ type
1582
+ }) {
1583
+ deprecate(`Use \`schema.fields({ type })\` instead of \`schema.attributesDefinitionFor({ type })\``, false, {
1584
+ id: 'ember-data:schema-service-updates',
1585
+ until: '6.0',
1586
+ for: 'ember-data',
1587
+ since: {
1588
+ available: '4.13',
1589
+ enabled: '5.4'
1590
+ }
1591
+ });
1592
+ const schema = this._schemas.get(type);
1593
+ if (!schema) {
1594
+ throw new Error(`No schema defined for ${type}`);
1595
+ }
1596
+ return schema.attributes;
1597
+ };
1598
+ SchemaService.prototype.relationshipsDefinitionFor = function ({
1599
+ type
1600
+ }) {
1601
+ deprecate(`Use \`schema.fields({ type })\` instead of \`schema.relationshipsDefinitionFor({ type })\``, false, {
1602
+ id: 'ember-data:schema-service-updates',
1603
+ until: '6.0',
1604
+ for: 'ember-data',
1605
+ since: {
1606
+ available: '4.13',
1607
+ enabled: '5.4'
1608
+ }
1609
+ });
1610
+ const schema = this._schemas.get(type);
1611
+ if (!schema) {
1612
+ throw new Error(`No schema defined for ${type}`);
1613
+ }
1614
+ return schema.relationships;
1615
+ };
1616
+ SchemaService.prototype.doesTypeExist = function (type) {
1617
+ deprecate(`Use \`schema.hasResource({ type })\` instead of \`schema.doesTypeExist(type)\``, false, {
1618
+ id: 'ember-data:schema-service-updates',
1619
+ until: '6.0',
1620
+ for: 'ember-data',
1621
+ since: {
1622
+ available: '4.13',
1623
+ enabled: '5.4'
1624
+ }
1625
+ });
1626
+ return this._schemas.has(type);
1627
+ };
1628
+ }
1629
+ export { Checkout, SchemaService, fromIdentity, instantiateRecord, registerDerivations, teardownRecord, withDefaults };