@warp-drive/schema-record 5.4.0-alpha.149 → 5.4.0-alpha.151
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 +30 -16
- package/dist/index.js +54 -28
- package/dist/index.js.map +1 -1
- package/package.json +15 -15
- package/unstable-preview-types/-private/record.d.ts.map +1 -1
- package/unstable-preview-types/-private/schema.d.ts +48 -2
- package/unstable-preview-types/-private/schema.d.ts.map +1 -1
- package/unstable-preview-types/index.d.ts +337 -0
- package/unstable-preview-types/index.d.ts.map +1 -1
package/README.md
CHANGED
|
@@ -1,23 +1,21 @@
|
|
|
1
1
|
<p align="center">
|
|
2
2
|
<img
|
|
3
3
|
class="project-logo"
|
|
4
|
-
src="./logos/
|
|
5
|
-
alt="WarpDrive"
|
|
6
|
-
|
|
7
|
-
|
|
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/
|
|
11
|
-
alt="WarpDrive"
|
|
12
|
-
|
|
13
|
-
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
|
|
@@ -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
|
]
|
|
@@ -286,6 +290,16 @@ store.registerSchemas([
|
|
|
286
290
|
]);
|
|
287
291
|
```
|
|
288
292
|
|
|
293
|
+
Additionally, `@warp-drive/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
|
@@ -878,49 +878,24 @@ class SchemaRecord {
|
|
|
878
878
|
return lastValue;
|
|
879
879
|
}
|
|
880
880
|
case 'field':
|
|
881
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
882
|
-
if (!test) {
|
|
883
|
-
throw new Error(`SchemaRecord.${field.name} is not available in legacy mode because it has type '${field.kind}'`);
|
|
884
|
-
}
|
|
885
|
-
})(!target[Legacy]) : {};
|
|
886
881
|
entangleSignal(signals, receiver, field.name);
|
|
887
882
|
return computeField(schema, cache, target, identifier, field, propArray, IS_EDITABLE);
|
|
888
883
|
case 'attribute':
|
|
889
884
|
entangleSignal(signals, receiver, field.name);
|
|
890
885
|
return computeAttribute(cache, identifier, prop, IS_EDITABLE);
|
|
891
886
|
case 'resource':
|
|
892
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
893
|
-
if (!test) {
|
|
894
|
-
throw new Error(`SchemaRecord.${field.name} is not available in legacy mode because it has type '${field.kind}'`);
|
|
895
|
-
}
|
|
896
|
-
})(!target[Legacy]) : {};
|
|
897
887
|
entangleSignal(signals, receiver, field.name);
|
|
898
888
|
return computeResource(store, cache, target, identifier, field, prop, IS_EDITABLE);
|
|
899
889
|
case 'derived':
|
|
900
890
|
return computeDerivation(schema, receiver, identifier, field, prop);
|
|
901
891
|
case 'schema-array':
|
|
902
892
|
case 'array':
|
|
903
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
904
|
-
if (!test) {
|
|
905
|
-
throw new Error(`SchemaRecord.${field.name} is not available in legacy mode because it has type '${field.kind}'`);
|
|
906
|
-
}
|
|
907
|
-
})(!target[Legacy]) : {};
|
|
908
893
|
entangleSignal(signals, receiver, field.name);
|
|
909
894
|
return computeArray(store, schema, cache, target, identifier, field, propArray, Mode[Editable], Mode[Legacy]);
|
|
910
895
|
case 'object':
|
|
911
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
912
|
-
if (!test) {
|
|
913
|
-
throw new Error(`SchemaRecord.${field.name} is not available in legacy mode because it has type '${field.kind}'`);
|
|
914
|
-
}
|
|
915
|
-
})(!target[Legacy]) : {};
|
|
916
896
|
entangleSignal(signals, receiver, field.name);
|
|
917
897
|
return computeObject(schema, cache, target, identifier, field, propArray, Mode[Editable], Mode[Legacy]);
|
|
918
898
|
case 'schema-object':
|
|
919
|
-
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
920
|
-
if (!test) {
|
|
921
|
-
throw new Error(`SchemaRecord.${field.name} is not available in legacy mode because it has type '${field.kind}'`);
|
|
922
|
-
}
|
|
923
|
-
})(!target[Legacy]) : {};
|
|
924
899
|
entangleSignal(signals, receiver, field.name);
|
|
925
900
|
// run transform, then use that value as the object to manage
|
|
926
901
|
return computeSchemaObject(store, cache, target, identifier, field, propArray, Mode[Legacy], Mode[Editable]);
|
|
@@ -1443,6 +1418,10 @@ function teardownRecord(record) {
|
|
|
1443
1418
|
assertSchemaRecord(record);
|
|
1444
1419
|
record[Destroy]();
|
|
1445
1420
|
}
|
|
1421
|
+
|
|
1422
|
+
/**
|
|
1423
|
+
* @module @warp-drive/schema-record
|
|
1424
|
+
*/
|
|
1446
1425
|
const Support = getOrSetGlobal('Support', new WeakMap());
|
|
1447
1426
|
const ConstructorField = {
|
|
1448
1427
|
type: '@constructor',
|
|
@@ -1484,6 +1463,13 @@ _constructor[Type] = '@constructor';
|
|
|
1484
1463
|
/**
|
|
1485
1464
|
* Utility for constructing a ResourceSchema with the recommended fields
|
|
1486
1465
|
* for the Polaris experience.
|
|
1466
|
+
*
|
|
1467
|
+
* @method withDefaults
|
|
1468
|
+
* @for @warp-drive/schema-record
|
|
1469
|
+
* @static
|
|
1470
|
+
* @public
|
|
1471
|
+
* @param schema
|
|
1472
|
+
* @return {ResourceSchema}
|
|
1487
1473
|
*/
|
|
1488
1474
|
function withDefaults(schema) {
|
|
1489
1475
|
schema.identity = schema.identity || DefaultIdentityField;
|
|
@@ -1495,6 +1481,32 @@ function withDefaults(schema) {
|
|
|
1495
1481
|
schema.fields.push(ConstructorField);
|
|
1496
1482
|
return schema;
|
|
1497
1483
|
}
|
|
1484
|
+
|
|
1485
|
+
/**
|
|
1486
|
+
* A derivation that computes its value from the
|
|
1487
|
+
* record's identity.
|
|
1488
|
+
*
|
|
1489
|
+
* It can be used via a derived field definition like:
|
|
1490
|
+
*
|
|
1491
|
+
* ```ts
|
|
1492
|
+
* {
|
|
1493
|
+
* kind: 'derived',
|
|
1494
|
+
* name: 'id',
|
|
1495
|
+
* type: '@identity',
|
|
1496
|
+
* options: { key: 'id' }
|
|
1497
|
+
* }
|
|
1498
|
+
* ```
|
|
1499
|
+
*
|
|
1500
|
+
* Valid keys are `'id'`, `'lid'`, `'type'`, and `'^'`.
|
|
1501
|
+
*
|
|
1502
|
+
* `^` returns the entire identifier object.
|
|
1503
|
+
*
|
|
1504
|
+
* @method fromIdentity
|
|
1505
|
+
* @for @warp-drive/schema-record
|
|
1506
|
+
* @static
|
|
1507
|
+
* @public
|
|
1508
|
+
*/
|
|
1509
|
+
|
|
1498
1510
|
function fromIdentity(record, options, key) {
|
|
1499
1511
|
const identifier = record[Identifier];
|
|
1500
1512
|
macroCondition(getGlobalConfig().WarpDrive.env.DEBUG) ? (test => {
|
|
@@ -1510,6 +1522,16 @@ function fromIdentity(record, options, key) {
|
|
|
1510
1522
|
return options.key === '^' ? identifier : identifier[options.key];
|
|
1511
1523
|
}
|
|
1512
1524
|
fromIdentity[Type] = '@identity';
|
|
1525
|
+
|
|
1526
|
+
/**
|
|
1527
|
+
* Registers the default derivations for the SchemaRecord
|
|
1528
|
+
*
|
|
1529
|
+
* @method registerDerivations
|
|
1530
|
+
* @for @warp-drive/schema-record
|
|
1531
|
+
* @static
|
|
1532
|
+
* @public
|
|
1533
|
+
* @param {SchemaService} schema
|
|
1534
|
+
*/
|
|
1513
1535
|
function registerDerivations(schema) {
|
|
1514
1536
|
schema.registerDerivation(fromIdentity);
|
|
1515
1537
|
schema.registerDerivation(_constructor);
|
|
@@ -1518,9 +1540,7 @@ function registerDerivations(schema) {
|
|
|
1518
1540
|
* Wraps a derivation in a new function with Derivation signature but that looks
|
|
1519
1541
|
* up the value in the cache before recomputing.
|
|
1520
1542
|
*
|
|
1521
|
-
* @
|
|
1522
|
-
* @param options
|
|
1523
|
-
* @param prop
|
|
1543
|
+
* @internal
|
|
1524
1544
|
*/
|
|
1525
1545
|
function makeCachedDerivation(derivation) {
|
|
1526
1546
|
const memoizedDerivation = (record, options, prop) => {
|
|
@@ -1537,6 +1557,12 @@ function makeCachedDerivation(derivation) {
|
|
|
1537
1557
|
memoizedDerivation[Type] = derivation[Type];
|
|
1538
1558
|
return memoizedDerivation;
|
|
1539
1559
|
}
|
|
1560
|
+
/**
|
|
1561
|
+
* A SchemaService designed to work with dynamically registered schemas.
|
|
1562
|
+
*
|
|
1563
|
+
* @class SchemaService
|
|
1564
|
+
* @public
|
|
1565
|
+
*/
|
|
1540
1566
|
class SchemaService {
|
|
1541
1567
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1542
1568
|
|