@warp-drive-mirror/schema-record 0.0.0-beta.18 → 0.0.0-beta.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,23 +1,21 @@
1
1
  <p align="center">
2
2
  <img
3
3
  class="project-logo"
4
- src="./logos/NCC-1701-a-blue.svg#gh-light-mode-only"
5
- alt="WarpDrive"
6
- width="120px"
7
- title="WarpDrive" />
4
+ src="./logos/github-header.svg#gh-light-mode-only"
5
+ alt="WarpDrive | Boldly go where no app has gone before"
6
+ title="WarpDrive | Boldly go where no app has gone before"
7
+ />
8
8
  <img
9
9
  class="project-logo"
10
- src="./logos/NCC-1701-a.svg#gh-dark-mode-only"
11
- alt="WarpDrive"
12
- width="120px"
13
- title="WarpDrive" />
10
+ src="./logos/github-header.svg#gh-dark-mode-only"
11
+ alt="WarpDrive | Boldly go where no app has gone before"
12
+ title="WarpDrive | Boldly go where no app has gone before"
13
+ />
14
14
  </p>
15
15
 
16
16
  <h3 align="center">Your Data, Managed.</h3>
17
17
  <p align="center">🌲 Get back to Nature 🐿️ Or shipping 💚</p>
18
18
 
19
- SchemaRecord is:
20
-
21
19
  - ⚡️ Fast
22
20
  - 📦 Tiny
23
21
  - ✨ Optimized
@@ -25,11 +23,15 @@ SchemaRecord is:
25
23
  - ⚛️ Universal
26
24
  - ☢️ Reactive
27
25
 
28
- This package provides reactive capabilities for your resource data.
29
- It works together with a [*Warp***Drive**](https://github.com/emberjs/data/)
30
- [Cache](https://github.com/emberjs/data/blob/main/packages/core-types/src/cache.ts)
31
- and associated Schemas to simplify the most complex parts of your
32
- app's state management.
26
+ SchemaRecord is a reactive object that transforms raw data from an [associated cache](https://github.com/emberjs/data/blob/main/packages/core-types/src/cache.ts) into reactive data backed by Signals.
27
+
28
+ The shape of the object and the transformation of raw cache data into its
29
+ reactive form is controlled by a resource schema.
30
+
31
+ Resource schemas are simple JSON, allowing them to be defined and delivered from anywhere.
32
+
33
+ The capabilities that SchemaRecord brings to [*Warp***Drive**](https://github.com/emberjs/data/)
34
+ will simplify even the most complex parts of your app's state management.
33
35
 
34
36
  ## Installation
35
37
 
@@ -124,7 +126,7 @@ We could describe the `'user'` and `'dog'` resources in the above payload
124
126
  with the following schemas:
125
127
 
126
128
  ```ts
127
- store.registerSchemas([
129
+ store.schema.registerResources([
128
130
  {
129
131
  type: 'user',
130
132
  identity: { type: '@id', name: 'id' },
@@ -150,7 +152,8 @@ store.registerSchemas([
150
152
  options: {
151
153
  async: false,
152
154
  inverse: 'owner',
153
- polymorphic: true
155
+ polymorphic: true,
156
+ linksMode: true,
154
157
  }
155
158
  }
156
159
  ]
@@ -174,6 +177,7 @@ store.registerSchemas([
174
177
  async: false,
175
178
  inverse: 'pets',
176
179
  as: 'pet',
180
+ linksMode: true,
177
181
  }
178
182
  }
179
183
  ]
@@ -243,7 +247,7 @@ definition above using this utility like so:
243
247
  ```ts
244
248
  import { withDefaults } from '@warp-drive-mirror/schema-record';
245
249
 
246
- store.registerSchemas([
250
+ store.schema.registerResources([
247
251
  withDefaults({
248
252
  type: 'user',
249
253
  fields: [
@@ -286,6 +290,16 @@ store.registerSchemas([
286
290
  ]);
287
291
  ```
288
292
 
293
+ Additionally, `@warp-drive-mirror/core-types` provides several utilities for type-checking and narrowing schemas.
294
+
295
+ - (type) [PolarisResourceSchema]()
296
+ - (type) [LegacyResourceSchema]()
297
+ - (type) [ObjectSchema]()
298
+ - [resourceSchema]()
299
+ - [objectSchema]()
300
+ - [isResourceSchema]()
301
+ - [isLegacyResourceSchema]()
302
+
289
303
 
290
304
  ### Field Schemas
291
305
 
package/dist/index.js CHANGED
@@ -390,8 +390,14 @@ class ManyArrayManager {
390
390
  array.links = rawValue.links;
391
391
  }
392
392
  const currentState = array[SOURCE$1];
393
- currentState.length = 0;
394
- fastPush(currentState, rawValue.data);
393
+
394
+ // unlike in the normal RecordArray case, we don't need to divorce the reference
395
+ // because we don't need to worry about associate/disassociate since the graph
396
+ // takes care of that for us
397
+ if (currentState !== rawValue.data) {
398
+ currentState.length = 0;
399
+ fastPush(currentState, rawValue.data);
400
+ }
395
401
  }
396
402
  reloadHasMany(key, options) {
397
403
  const field = this.store.schema.fields(this.identifier).get(key);
@@ -1418,6 +1424,10 @@ function teardownRecord(record) {
1418
1424
  assertSchemaRecord(record);
1419
1425
  record[Destroy]();
1420
1426
  }
1427
+
1428
+ /**
1429
+ * @module @warp-drive-mirror/schema-record
1430
+ */
1421
1431
  const Support = getOrSetGlobal('Support', new WeakMap());
1422
1432
  const ConstructorField = {
1423
1433
  type: '@constructor',
@@ -1459,6 +1469,13 @@ _constructor[Type] = '@constructor';
1459
1469
  /**
1460
1470
  * Utility for constructing a ResourceSchema with the recommended fields
1461
1471
  * for the Polaris experience.
1472
+ *
1473
+ * @method withDefaults
1474
+ * @for @warp-drive-mirror/schema-record
1475
+ * @static
1476
+ * @public
1477
+ * @param schema
1478
+ * @return {ResourceSchema}
1462
1479
  */
1463
1480
  function withDefaults(schema) {
1464
1481
  schema.identity = schema.identity || DefaultIdentityField;
@@ -1470,6 +1487,32 @@ function withDefaults(schema) {
1470
1487
  schema.fields.push(ConstructorField);
1471
1488
  return schema;
1472
1489
  }
1490
+
1491
+ /**
1492
+ * A derivation that computes its value from the
1493
+ * record's identity.
1494
+ *
1495
+ * It can be used via a derived field definition like:
1496
+ *
1497
+ * ```ts
1498
+ * {
1499
+ * kind: 'derived',
1500
+ * name: 'id',
1501
+ * type: '@identity',
1502
+ * options: { key: 'id' }
1503
+ * }
1504
+ * ```
1505
+ *
1506
+ * Valid keys are `'id'`, `'lid'`, `'type'`, and `'^'`.
1507
+ *
1508
+ * `^` returns the entire identifier object.
1509
+ *
1510
+ * @method fromIdentity
1511
+ * @for @warp-drive-mirror/schema-record
1512
+ * @static
1513
+ * @public
1514
+ */
1515
+
1473
1516
  function fromIdentity(record, options, key) {
1474
1517
  const identifier = record[Identifier];
1475
1518
  macroCondition(getGlobalConfig().WarpDriveMirror.env.DEBUG) ? (test => {
@@ -1485,6 +1528,16 @@ function fromIdentity(record, options, key) {
1485
1528
  return options.key === '^' ? identifier : identifier[options.key];
1486
1529
  }
1487
1530
  fromIdentity[Type] = '@identity';
1531
+
1532
+ /**
1533
+ * Registers the default derivations for the SchemaRecord
1534
+ *
1535
+ * @method registerDerivations
1536
+ * @for @warp-drive-mirror/schema-record
1537
+ * @static
1538
+ * @public
1539
+ * @param {SchemaService} schema
1540
+ */
1488
1541
  function registerDerivations(schema) {
1489
1542
  schema.registerDerivation(fromIdentity);
1490
1543
  schema.registerDerivation(_constructor);
@@ -1493,9 +1546,7 @@ function registerDerivations(schema) {
1493
1546
  * Wraps a derivation in a new function with Derivation signature but that looks
1494
1547
  * up the value in the cache before recomputing.
1495
1548
  *
1496
- * @param record
1497
- * @param options
1498
- * @param prop
1549
+ * @internal
1499
1550
  */
1500
1551
  function makeCachedDerivation(derivation) {
1501
1552
  const memoizedDerivation = (record, options, prop) => {
@@ -1512,6 +1563,12 @@ function makeCachedDerivation(derivation) {
1512
1563
  memoizedDerivation[Type] = derivation[Type];
1513
1564
  return memoizedDerivation;
1514
1565
  }
1566
+ /**
1567
+ * A SchemaService designed to work with dynamically registered schemas.
1568
+ *
1569
+ * @class SchemaService
1570
+ * @public
1571
+ */
1515
1572
  class SchemaService {
1516
1573
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1517
1574
 
@@ -1521,6 +1578,9 @@ class SchemaService {
1521
1578
  this._hashFns = new Map();
1522
1579
  this._derivations = new Map();
1523
1580
  }
1581
+ resourceTypes() {
1582
+ return Array.from(this._schemas.keys());
1583
+ }
1524
1584
  hasTrait(type) {
1525
1585
  return this._traits.has(type);
1526
1586
  }